Coverage for ase/gui/quickinfo.py: 72.15%
79 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1"Module for displaying information about the system."
3import warnings
5import numpy as np
7from ase.gui.i18n import _
9ucellformat = """\
10 {:8.3f} {:8.3f} {:8.3f}
11 {:8.3f} {:8.3f} {:8.3f}
12 {:8.3f} {:8.3f} {:8.3f}
13"""
16def info(gui):
17 images = gui.images
18 nimg = len(images)
19 atoms = gui.atoms
21 tokens = []
23 def add(token=''):
24 tokens.append(token)
26 if len(atoms) < 1:
27 add(_('This frame has no atoms.'))
28 else:
29 img = gui.frame
31 if nimg == 1:
32 add(_('Single image loaded.'))
33 else:
34 add(_('Image {} loaded (0–{}).').format(img, nimg - 1))
35 add()
36 add(_('Number of atoms: {}').format(len(atoms)))
38 # We need to write ų further down, so we have no choice but to
39 # use proper subscripts in the chemical formula:
40 formula = atoms.get_chemical_formula()
41 subscripts = dict(zip('0123456789', '₀₁₂₃₄₅₆₇₈₉'))
42 pretty_formula = ''.join(subscripts.get(c, c) for c in formula)
43 add(pretty_formula)
45 add()
46 add(_('Unit cell [Å]:'))
47 add(ucellformat.format(*atoms.cell.ravel()))
48 periodic = [
49 [_('no'), _('yes')][int(periodic)] for periodic in atoms.pbc
50 ]
51 # TRANSLATORS: This has the form Periodic: no, no, yes
52 add(_('Periodic: {}, {}, {}').format(*periodic))
53 add()
55 cellpar = atoms.cell.cellpar()
56 add()
57 add(_('Lengths [Å]: {:.3f}, {:.3f}, {:.3f}').format(*cellpar[:3]))
58 add(_('Angles: {:.1f}°, {:.1f}°, {:.1f}°').format(*cellpar[3:]))
60 if atoms.cell.rank == 3:
61 add(_('Volume: {:.3f} ų').format(atoms.cell.volume))
63 add()
65 if nimg > 1:
66 if all((atoms.cell == img.cell).all() for img in images):
67 add(_('Unit cell is fixed.'))
68 else:
69 add(_('Unit cell varies.'))
71 if atoms.pbc[:2].all() and atoms.cell.rank >= 1:
72 try:
73 lat = atoms.cell.get_bravais_lattice()
74 except RuntimeError:
75 add(_('Could not recognize the lattice type'))
76 except Exception:
77 add(_('Unexpected error determining lattice type'))
78 else:
79 add(_('Reduced Bravais lattice:\n{}').format(lat))
81 # Print electronic structure information if we have a calculator
82 if atoms.calc:
83 calc = atoms.calc
85 def getresult(name, get_quantity):
86 # ase/io/trajectory.py line 170 does this by using
87 # the get_property(prop, atoms, allow_calculation=False)
88 # so that is an alternative option.
89 try:
90 if calc.calculation_required(atoms, [name]):
91 quantity = None
92 else:
93 quantity = get_quantity()
94 except Exception as err:
95 quantity = None
96 errmsg = (
97 'An error occurred while retrieving {} '
98 'from the calculator: {}'.format(name, err)
99 )
100 warnings.warn(errmsg)
101 return quantity
103 # SinglePointCalculators are named after the code which
104 # produced the result, so this will typically list the
105 # name of a code even if they are just cached results.
106 add()
107 from ase.calculators.singlepoint import SinglePointCalculator
109 if isinstance(calc, SinglePointCalculator):
110 add(_('Calculator: {} (cached)').format(calc.name))
111 else:
112 add(_('Calculator: {} (attached)').format(calc.name))
114 energy = getresult('energy', atoms.get_potential_energy)
115 forces = getresult('forces', atoms.get_forces)
116 magmom = getresult('magmom', atoms.get_magnetic_moment)
118 if energy is not None:
119 energy_str = _('Energy: {:.3f} eV').format(energy)
120 add(energy_str)
122 if forces is not None:
123 maxf = np.linalg.norm(forces, axis=1).max()
124 forces_str = _('Max force: {:.3f} eV/Å').format(maxf)
125 add(forces_str)
127 if magmom is not None:
128 mag_str = _('Magmom: {:.3f} µ').format(magmom)
129 add(mag_str)
131 return '\n'.join(tokens)