Coverage for ase/gui/celleditor.py: 82.86%
105 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"""celleditor.py - Window for editing the cell of an atoms object"""
3import numpy as np
5import ase.gui.ui as ui
6from ase.cell import Cell
7from ase.gui.i18n import _
10class CellEditor:
11 """Window for editing the cell of an atoms object."""
13 def __init__(self, gui):
14 self.gui = gui
15 self.gui.obs.set_atoms.register(self.notify_atoms_changed)
17 # Create grid control for cells
18 # xx xy xz ||x|| pbc
19 # yx yy yz ||y|| pbc
20 # zx zy zz ||z|| pbc
21 self.cell_grid = []
22 self.pbc = []
23 self.angles = []
25 atoms = self.gui.atoms
27 cell = atoms.cell
28 mags = cell.lengths()
29 angles = cell.angles()
30 pbc = atoms.pbc
32 for i in [0, 1, 2]: # x_ y_ z_
33 row = []
34 for j in [0, 1, 2]: # _x _y _z
35 row.append(
36 ui.SpinBox(
37 cell[i][j],
38 -30,
39 30,
40 0.1,
41 self.apply_vectors,
42 rounding=7,
43 width=9,
44 )
45 )
46 row.append(
47 ui.SpinBox(
48 mags[i],
49 -30,
50 30,
51 0.1,
52 self.apply_magnitudes,
53 rounding=7,
54 width=9,
55 )
56 )
57 self.cell_grid.append(row)
58 self.pbc.append(ui.CheckButton('', bool(pbc[i]), self.apply_pbc))
59 self.angles.append(
60 ui.SpinBox(
61 angles[i],
62 -360,
63 360,
64 15,
65 self.apply_angles,
66 rounding=7,
67 width=9,
68 )
69 )
71 self.scale_atoms = ui.CheckButton('', False)
72 self.vacuum = ui.SpinBox(5, 0, 15, 0.1, self.apply_vacuum)
74 # TRANSLATORS: This is a title of a window.
75 win = self.win = ui.Window(_('Cell Editor'))
77 x, y, z = self.cell_grid
79 win.add(
80 [
81 _('A:'),
82 x[0],
83 x[1],
84 x[2],
85 _('||A||:'),
86 x[3],
87 _('periodic:'),
88 self.pbc[0],
89 ]
90 )
91 win.add(
92 [
93 _('B:'),
94 y[0],
95 y[1],
96 y[2],
97 _('||B||:'),
98 y[3],
99 _('periodic:'),
100 self.pbc[1],
101 ]
102 )
103 win.add(
104 [
105 _('C:'),
106 z[0],
107 z[1],
108 z[2],
109 _('||C||:'),
110 z[3],
111 _('periodic:'),
112 self.pbc[2],
113 ]
114 )
115 win.add(
116 [
117 _('∠BC:'),
118 self.angles[0],
119 _('∠AC:'),
120 self.angles[1],
121 _('∠AB:'),
122 self.angles[2],
123 ]
124 )
125 win.add([_('Scale atoms with cell:'), self.scale_atoms])
126 win.add(
127 [
128 ui.Button(_('Apply Vectors'), self.apply_vectors),
129 ui.Button(_('Apply Magnitudes'), self.apply_magnitudes),
130 ui.Button(_('Apply Angles'), self.apply_angles),
131 ]
132 )
133 win.add(
134 [
135 _(
136 'Pressing 〈Enter〉 as you enter values will '
137 'automatically apply correctly'
138 )
139 ]
140 )
141 # TRANSLATORS: verb
142 win.add(
143 [
144 ui.Button(_('Center'), self.apply_center),
145 ui.Button(_('Wrap'), self.apply_wrap),
146 _('Vacuum:'),
147 self.vacuum,
148 ui.Button(_('Apply Vacuum'), self.apply_vacuum),
149 ]
150 )
152 def apply_center(self, *args):
153 atoms = self.gui.atoms.copy()
154 atoms.center()
155 self.gui.new_atoms(atoms)
157 def apply_wrap(self, *args):
158 atoms = self.gui.atoms.copy()
159 atoms.wrap()
160 self.gui.new_atoms(atoms)
162 def apply_vacuum(self, *args):
163 atoms = self.gui.atoms.copy()
165 axis = []
166 for index, pbc in enumerate(atoms.pbc):
167 if not pbc:
168 axis.append(index)
170 atoms.center(vacuum=self.vacuum.value, axis=axis)
171 self.gui.new_atoms(atoms)
173 def apply_vectors(self, *args):
174 atoms = self.gui.atoms.copy()
176 atoms.set_cell(
177 self.get_vectors(), scale_atoms=self.scale_atoms.var.get()
178 )
179 self.gui.new_atoms(atoms)
181 def get_vectors(self):
182 x, y, z = self.cell_grid
183 cell = np.array(
184 [
185 [x[0].value, x[1].value, x[2].value],
186 [y[0].value, y[1].value, y[2].value],
187 [z[0].value, z[1].value, z[2].value],
188 ]
189 )
190 return Cell(cell)
192 def get_magnitudes(self):
193 x, y, z = self.cell_grid
194 return np.array([x[3].value, y[3].value, z[3].value])
196 def apply_magnitudes(self, *args):
197 atoms = self.gui.atoms.copy()
199 old_mags = atoms.cell.lengths()
200 new_mags = self.get_magnitudes()
202 newcell = atoms.cell.copy()
203 for i in range(3):
204 if old_mags[i]:
205 newcell[i] *= new_mags[i] / old_mags[i]
206 else:
207 from ase.gui.ui import showwarning
209 showwarning(
210 _('Null vector'),
211 _("Can't change the magnitude of a null vector"),
212 )
214 atoms.set_cell(newcell, scale_atoms=self.scale_atoms.var.get())
216 self.gui.new_atoms(atoms)
218 def apply_angles(self, *args):
219 atoms = self.gui.atoms.copy()
221 cell_data = atoms.cell.cellpar()
222 cell_data[3:7] = [
223 self.angles[0].value,
224 self.angles[1].value,
225 self.angles[2].value,
226 ]
228 atoms.set_cell(cell_data, scale_atoms=self.scale_atoms.var.get())
230 self.gui.new_atoms(atoms)
232 def apply_pbc(self, *args):
233 atoms = self.gui.atoms.copy()
235 pbc = [pbc.var.get() for pbc in self.pbc]
236 atoms.set_pbc(pbc)
238 self.gui.new_atoms(atoms)
240 def notify_atoms_changed(self):
241 atoms = self.gui.atoms
242 self.update(atoms.cell, atoms.pbc)
244 def update(self, cell, pbc):
245 cell = Cell(cell)
246 mags = cell.lengths()
247 angles = cell.angles()
249 for i in range(3):
250 for j in range(3):
251 if np.isnan(cell[i][j]):
252 cell[i][j] = 0
253 self.cell_grid[i][j].value = cell[i][j]
255 if np.isnan(mags[i]):
256 mags[i] = 0
257 self.cell_grid[i][3].value = mags[i]
259 if np.isnan(angles[i]):
260 angles[i] = 0
261 self.angles[i].value = angles[i]
263 self.pbc[i].var.set(bool(pbc[i]))