Coverage for ase/gui/status.py: 82.65%
98 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
1import warnings
2from math import acos, pi, sqrt
4import numpy as np
6from ase.data import atomic_names as names
7from ase.data import chemical_symbols as symbols
8from ase.gui.i18n import _
9from ase.gui.utils import get_magmoms
12def formula(Z):
13 hist = {}
14 for z in Z:
15 if z in hist:
16 hist[z] += 1
17 else:
18 hist[z] = 1
19 Z = sorted(hist.keys())
20 strings = []
21 for z in Z:
22 n = hist[z]
23 s = ('' if n == 1 else str(n)) + symbols[z]
24 strings.append(s)
25 return '+'.join(strings)
28class Status:
29 def __init__(self, gui):
30 self.gui = gui
32 def status(self, atoms):
33 gui = self.gui
34 natoms = len(atoms)
35 indices = np.arange(natoms)[gui.images.selected[:natoms]]
36 ordered_indices = [
37 i for i in gui.images.selected_ordered if i < len(atoms)
38 ]
39 n = len(indices)
41 if n == 0:
42 line = ''
43 if atoms.calc:
44 calc = atoms.calc
46 def getresult(name, get_quantity):
47 # ase/io/trajectory.py line 170 does this by using
48 # the get_property(prop, atoms, allow_calculation=False)
49 # so that is an alternative option.
50 try:
51 if calc.calculation_required(atoms, [name]):
52 quantity = None
53 else:
54 quantity = get_quantity()
55 except Exception as err:
56 quantity = None
57 errmsg = (
58 'An error occurred while retrieving {} '
59 'from the calculator: {}'.format(name, err)
60 )
61 warnings.warn(errmsg)
62 return quantity
64 energy = getresult('energy', atoms.get_potential_energy)
65 forces = getresult('forces', atoms.get_forces)
67 if energy is not None:
68 line += f'Energy = {energy:.3f} eV'
70 if forces is not None:
71 maxf = np.linalg.norm(forces, axis=1).max()
72 line += f' Max force = {maxf:.3f} eV/Å'
73 gui.window.update_status_line(line)
74 return
76 Z = atoms.numbers[indices]
77 R = atoms.positions[indices]
79 if n == 1:
80 tag = atoms.get_tags()[indices[0]]
81 text = ' #%d %s (%s): %.3f Å, %.3f Å, %.3f Å ' % (
82 (indices[0], names[Z[0]], symbols[Z[0]]) + tuple(R[0])
83 )
84 text += _(' tag=%(tag)s') % dict(tag=tag)
85 magmoms = get_magmoms(gui.atoms)
86 if magmoms.any():
87 # TRANSLATORS: mom refers to magnetic moment
88 text += _(' mom={:1.2f}').format(magmoms[indices][0])
89 charges = gui.atoms.get_initial_charges()
90 if charges.any():
91 text += _(' q={:1.2f}').format(charges[indices][0])
92 haveit = {
93 'numbers',
94 'positions',
95 'forces',
96 'momenta',
97 'initial_charges',
98 'initial_magmoms',
99 'tags',
100 }
101 for key in atoms.arrays:
102 if key not in haveit:
103 val = atoms.get_array(key)[indices[0]]
104 if val is not None:
105 if isinstance(val, int):
106 text += f' {key}={val:g}'
107 else:
108 text += f' {key}={val}'
109 elif n == 2:
110 D = R[0] - R[1]
111 d = sqrt(np.dot(D, D))
112 text = f' {symbols[Z[0]]}-{symbols[Z[1]]}: {d:.3f} Å'
113 elif n == 3:
114 d = []
115 for c in range(3):
116 D = R[c] - R[(c + 1) % 3]
117 d.append(np.dot(D, D))
118 a = []
119 for c in range(3):
120 t1 = 0.5 * (d[c] + d[(c + 1) % 3] - d[(c + 2) % 3])
121 t2 = sqrt(d[c] * d[(c + 1) % 3])
122 try:
123 t3 = acos(t1 / t2)
124 except ValueError:
125 if t1 > 0:
126 t3 = 0
127 else:
128 t3 = pi
129 a.append(t3 * 180 / pi)
130 text = ' %s-%s-%s: %.1f°, %.1f°, %.1f°' % tuple(
131 [symbols[z] for z in Z] + a
132 )
133 elif len(ordered_indices) == 4:
134 angle = gui.atoms.get_dihedral(*ordered_indices, mic=True)
135 text = '%s %s → %s → %s → %s: %.1f°' % tuple(
136 [_('dihedral')] + [symbols[z] for z in Z] + [angle]
137 )
138 else:
139 text = ' ' + formula(Z)
141 gui.window.update_status_line(text)