Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from ase.gui.i18n import _
2from math import sqrt, pi, acos
4import numpy as np
6from ase.data import chemical_symbols as symbols
7from ase.data import atomic_names as names
8from ase.gui.utils import get_magmoms
11def formula(Z):
12 hist = {}
13 for z in Z:
14 if z in hist:
15 hist[z] += 1
16 else:
17 hist[z] = 1
18 Z = sorted(hist.keys())
19 strings = []
20 for z in Z:
21 n = hist[z]
22 s = ('' if n == 1 else str(n)) + symbols[z]
23 strings.append(s)
24 return '+'.join(strings)
27class Status: # Status is used as a mixin in GUI
28 def __init__(self):
29 self.ordered_indices = []
31 def status(self, atoms):
32 # use where here: XXX
33 natoms = len(atoms)
34 indices = np.arange(natoms)[self.images.selected[:natoms]]
35 ordered_indices = [i for i in self.images.selected_ordered
36 if i < len(atoms)]
37 n = len(indices)
39 if n == 0:
40 self.window.update_status_line('')
41 return
43 Z = atoms.numbers[indices]
44 R = atoms.positions[indices]
46 if n == 1:
47 tag = atoms.get_tags()[indices[0]]
48 text = (u' #%d %s (%s): %.3f Å, %.3f Å, %.3f Å ' %
49 ((indices[0], names[Z[0]], symbols[Z[0]]) + tuple(R[0])))
50 text += _(' tag=%(tag)s') % dict(tag=tag)
51 magmoms = get_magmoms(self.atoms)
52 if magmoms.any():
53 # TRANSLATORS: mom refers to magnetic moment
54 text += _(' mom={0:1.2f}'.format(
55 magmoms[indices][0]))
56 charges = self.atoms.get_initial_charges()
57 if charges.any():
58 text += _(' q={0:1.2f}'.format(
59 charges[indices][0]))
60 haveit = ['numbers', 'positions', 'forces', 'momenta',
61 'initial_charges', 'initial_magmoms']
62 for key in atoms.arrays:
63 if key not in haveit:
64 val = atoms.get_array(key)[indices[0]]
65 if val is not None:
66 text += ' {0}={1:g}'.format(key, val)
67 elif n == 2:
68 D = R[0] - R[1]
69 d = sqrt(np.dot(D, D))
70 text = u' %s-%s: %.3f Å' % (symbols[Z[0]], symbols[Z[1]], d)
71 elif n == 3:
72 d = []
73 for c in range(3):
74 D = R[c] - R[(c + 1) % 3]
75 d.append(np.dot(D, D))
76 a = []
77 for c in range(3):
78 t1 = 0.5 * (d[c] + d[(c + 1) % 3] - d[(c + 2) % 3])
79 t2 = sqrt(d[c] * d[(c + 1) % 3])
80 try:
81 t3 = acos(t1 / t2)
82 except ValueError:
83 if t1 > 0:
84 t3 = 0
85 else:
86 t3 = pi
87 a.append(t3 * 180 / pi)
88 text = (u' %s-%s-%s: %.1f°, %.1f°, %.1f°' %
89 tuple([symbols[z] for z in Z] + a))
90 elif len(ordered_indices) == 4:
91 angle = self.atoms.get_dihedral(*ordered_indices, mic=True)
92 text = (u'%s %s → %s → %s → %s: %.1f°' %
93 tuple([_('dihedral')] + [symbols[z] for z in Z] + [angle]))
94 else:
95 text = ' ' + formula(Z)
97 self.window.update_status_line(text)