Skip to content

Commit a6258b1

Browse files
authored
Number of supercell is done only on request and for <= 30 sites. (#95)
This is because the app may get stuck in evaluating this for large systems.
1 parent 634dc6e commit a6258b1

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

src/aiidalab_qe_vibroscopy/app/settings.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def change_supercell(_=None):
157157
)
158158
for elem in [self._sc_x, self._sc_y, self._sc_z]:
159159
elem.observe(change_supercell, names="value")
160-
elem.observe(self._estimate_supercells, names="value")
160+
elem.observe(self._activate_estimate_supercells, names="value")
161161

162162
self.supercell_selector = ipw.HBox(
163163
children=[
@@ -179,7 +179,7 @@ def change_supercell(_=None):
179179
self.supercell_hint_button = ipw.Button(
180180
description="Size hint",
181181
disabled=False,
182-
width="100px",
182+
layout=ipw.Layout(width="100px"),
183183
button_style="info",
184184
)
185185
# supercell hint (15A lattice params)
@@ -195,11 +195,23 @@ def change_supercell(_=None):
195195
# supercell reset reaction
196196
self.supercell_reset_button.on_click(self._reset_supercell)
197197

198+
# Estimate supercell button
199+
self.supercell_estimate_button = ipw.Button(
200+
description="Estimate number of supercells ➡",
201+
disabled=False,
202+
layout=ipw.Layout(width="240px", display="none"),
203+
button_style="info",
204+
tooltip="Number of supercells for phonons calculations;\nwarning: for large systems, this may take some time.",
205+
)
206+
# supercell reset reaction
207+
self.supercell_estimate_button.on_click(self._estimate_supercells)
208+
198209
# Estimate the number of supercells for frozen phonons.
199210
self.supercell_number_estimator = ipw.HTML(
200-
description="Number of supercells to be computed:",
201-
value="0",
211+
# description="Number of supercells:",
212+
value="?",
202213
style={"description_width": "initial"},
214+
layout=ipw.Layout(display="none"),
203215
)
204216

205217
## end supercell hint.
@@ -212,6 +224,7 @@ def change_supercell(_=None):
212224
self.supercell_selector,
213225
self.supercell_hint_button,
214226
self.supercell_reset_button,
227+
self.supercell_estimate_button, # I do it on request, as it can take long time.
215228
self.supercell_number_estimator,
216229
],
217230
),
@@ -229,19 +242,17 @@ def change_supercell(_=None):
229242
style={"description_width": "initial"},
230243
layout={"width": "300px"},
231244
)
232-
self.symmetry_symprec.observe(self._estimate_supercells, "value")
245+
self.symmetry_symprec.observe(self._activate_estimate_supercells, "value")
233246

234247
# reset supercell
235248
self.symmetry_symprec_reset_button = ipw.Button(
236249
description="Reset symprec",
237250
disabled=False,
238-
layout=ipw.Layout(width="150px"),
251+
layout=ipw.Layout(width="125px"),
239252
button_style="warning",
240253
)
241254
# supercell reset reaction
242-
self.symmetry_symprec_reset_button.on_click(
243-
lambda _: setattr(self.symmetry_symprec, "value", 1e-5)
244-
)
255+
self.symmetry_symprec_reset_button.on_click(self._reset_symprec)
245256

246257
self.children = [
247258
ipw.VBox(
@@ -285,13 +296,23 @@ def change_supercell(_=None):
285296

286297
@tl.observe("input_structure")
287298
def _update_input_structure(self, change):
288-
if self.input_structure is not None:
299+
if self.input_structure:
289300
for direction, periodic in zip(
290301
[self._sc_x, self._sc_y, self._sc_z], self.input_structure.pbc
291302
):
292303
direction.value = 2 if periodic else 1
293304
direction.disabled = False if periodic else True
294305

306+
self.supercell_number_estimator.layout.display = (
307+
"block" if len(self.input_structure.sites) <= 30 else "none"
308+
)
309+
self.supercell_estimate_button.layout.display = (
310+
"block" if len(self.input_structure.sites) <= 30 else "none"
311+
)
312+
else:
313+
self.supercell_number_estimator.layout.display = "none"
314+
self.supercell_estimate_button.layout.display = "none"
315+
295316
def _display_supercell(self, change):
296317
selected = change["new"]
297318
if selected in [1, 3]:
@@ -308,19 +329,23 @@ def _suggest_supercell(self, _=None):
308329
suggested_3D = 15 // np.array(s.cell.cellpar()[:3]) + 1
309330

310331
# if disabled, it means that it is a non-periodic direction.
311-
# here we manually unobserve the `_estimate_supercells`, so it is faster
332+
# here we manually unobserve the `_activate_estimate_supercells`, so it is faster
312333
# and only compute when all the three directions are updated
313334
self.block = True
314335
for direction, suggested, original in zip(
315336
[self._sc_x, self._sc_y, self._sc_z], suggested_3D, s.cell.cellpar()[:3]
316337
):
317338
direction.value = suggested if not direction.disabled else 1
318339
self.block = False
319-
self._estimate_supercells()
340+
self._activate_estimate_supercells()
320341
else:
321342
return
322343

323-
@tl.observe("input_structure")
344+
def _activate_estimate_supercells(self, _=None):
345+
self.supercell_estimate_button.disabled = False
346+
self.supercell_number_estimator.value = "?"
347+
348+
# @tl.observe("input_structure")
324349
@disable_print
325350
def _estimate_supercells(self, _=None):
326351
"""_summary_
@@ -372,6 +397,7 @@ def _estimate_supercells(self, _=None):
372397
supercells = preprocess_data.get_supercells_with_displacements()
373398
"""
374399
self.supercell_number_estimator.value = f"{len(supercells)}"
400+
self.supercell_estimate_button.disabled = True
375401

376402
return
377403

@@ -385,7 +411,12 @@ def _reset_supercell(self, _=None):
385411
reset_supercell.append(2 if periodic else 1)
386412
(self._sc_x.value, self._sc_y.value, self._sc_z.value) = reset_supercell
387413
self.block = False
388-
self._estimate_supercells()
414+
self._activate_estimate_supercells()
415+
return
416+
417+
def _reset_symprec(self, _=None):
418+
self.symmetry_symprec.value = 1e-5
419+
self._activate_estimate_supercells()
389420
return
390421

391422
def get_panel_value(self):

tests/test_settings.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_x_settings(generate_structure_data):
7979

8080
@pytest.mark.usefixtures("sssp")
8181
def test_supercell_number_estimator(generate_structure_data):
82-
"""Test the settings of the vibroscopy app."""
82+
"""Test the supercell_number_estimator setting of the vibroscopy app."""
8383

8484
from aiidalab_qe.app.configuration import ConfigureQeAppWorkChainStep
8585

@@ -89,25 +89,29 @@ def test_supercell_number_estimator(generate_structure_data):
8989
configure_step.workchain_settings.properties["vibronic"].run.value = True
9090
parameters = configure_step.settings["vibronic"].get_panel_value()
9191
assert parameters["supercell_selector"] == [2, 2, 2]
92-
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "1"
92+
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "?"
9393
configure_step.settings["vibronic"]._suggest_supercell()
9494
parameters = configure_step.settings["vibronic"].get_panel_value()
9595
assert parameters["supercell_selector"] == [4, 4, 4]
96+
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "?"
97+
configure_step.settings["vibronic"]._estimate_supercells()
9698
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "1"
9799
configure_step.settings["vibronic"]._sc_x.value = 3
98100
configure_step.settings["vibronic"]._sc_y.value = 2
99101
configure_step.settings["vibronic"]._sc_z.value = 2
102+
configure_step.settings["vibronic"]._estimate_supercells()
100103
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "4"
101104
configure_step.settings["vibronic"]._reset_supercell()
102105
configure_step.settings["vibronic"]._sc_x.value = 2
103106
configure_step.settings["vibronic"]._sc_y.value = 2
104107
configure_step.settings["vibronic"]._sc_z.value = 2
108+
configure_step.settings["vibronic"]._estimate_supercells()
105109
assert configure_step.settings["vibronic"].supercell_number_estimator.value == "1"
106110

107111

108112
@pytest.mark.usefixtures("sssp")
109113
def test_symprec(generate_structure_data):
110-
"""Test the settings of the vibroscopy app."""
114+
"""Test the symprec setting of the vibroscopy app."""
111115

112116
from aiidalab_qe.app.configuration import ConfigureQeAppWorkChainStep
113117

@@ -123,3 +127,18 @@ def test_symprec(generate_structure_data):
123127
configure_step.settings["vibronic"].reset()
124128
parameters = configure_step.settings["vibronic"].get_panel_value()
125129
assert parameters["symmetry_symprec"] == 1e-5
130+
131+
132+
@pytest.mark.usefixtures("sssp")
133+
def test_supercell_estimator_button(generate_structure_data):
134+
"""Test the settings of the button for the supercell number estimator."""
135+
136+
from aiidalab_qe.app.configuration import ConfigureQeAppWorkChainStep
137+
138+
configure_step = ConfigureQeAppWorkChainStep()
139+
structure = generate_structure_data("silicon")
140+
configure_step.input_structure = structure
141+
configure_step.workchain_settings.properties["vibronic"].run.value = True
142+
assert not configure_step.settings["vibronic"].supercell_estimate_button.disabled
143+
configure_step.settings["vibronic"]._estimate_supercells()
144+
assert configure_step.settings["vibronic"].supercell_estimate_button.disabled

0 commit comments

Comments
 (0)