Skip to content

Commit

Permalink
Add guards on null active eigenvalues (#1102)
Browse files Browse the repository at this point in the history
There were no guards on the active eigenvalues. So if someone selected to define eigenvalues, but didn't (leaving all as -1), it raised errors regarding reshaping a null-size array.
  • Loading branch information
edan-bainglass authored Jan 17, 2025
1 parent 891d378 commit 2f53644
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ def render(self):
)

self.eigenvalues_help = ipw.HTML("""
<div style="line-height: 1.4; margin-bottom: 5px;">
<div style="line-height: 1.4; margin: 10px 0 5px;">
For transition metals and lanthanoids, the starting eigenvalues can be defined (magnetic calculation).
<br>
It is useful to suggest the desired orbital occupations when the default choice takes another path.
It is useful to suggest the desired orbital occupations when the default choice takes another path.
<br>
To do so, tick the checkbox below and set the desired eigenvalues to a value other than -1 (unset).
</div>
""")
self.define_eigenvalues_checkbox = ipw.Checkbox(
Expand Down
24 changes: 15 additions & 9 deletions src/aiidalab_qe/app/configuration/advanced/hubbard/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,22 @@ def update(self, specific=""): # noqa: ARG002
self.needs_eigenvalues_widget = len(self.applicable_kind_names) > 0

def get_active_eigenvalues(self):
active_eigenvalues = [
orbital_eigenvalue
for element_eigenvalues in self.eigenvalues
for spin_row in element_eigenvalues
for orbital_eigenvalue in spin_row
if orbital_eigenvalue[-1] != -1
]
if not (
active_eigenvalues := [
orbital_eigenvalue
for element_eigenvalues in self.eigenvalues
for spin_row in element_eigenvalues
for orbital_eigenvalue in spin_row
if orbital_eigenvalue[-1] != -1
]
):
return []
eigenvalues_array = np.array(active_eigenvalues, dtype=object)
new_shape = (np.prod(eigenvalues_array.shape[:-1]), 4)
return eigenvalues_array.reshape(new_shape).tolist()
new_shape = (int(np.prod(eigenvalues_array.shape[:-1])), 4)
return [
tuple(eigenvalue)
for eigenvalue in eigenvalues_array.reshape(new_shape).tolist()
]

def set_active_eigenvalues(self, eigenvalues: list):
eigenvalues_array = np.array(eigenvalues, dtype=object)
Expand Down
6 changes: 3 additions & 3 deletions tests/configuration/test_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_advanced_hubbard_settings(generate_structure_data):
Co_spin_down_row.children[5].value = "1"

assert model.get_active_eigenvalues() == [
[1, 1, "Co", 1],
[3, 1, "Co", 1],
[5, 1, "Co", 1],
(1, 1, "Co", 1),
(3, 1, "Co", 1),
(5, 1, "Co", 1),
]

0 comments on commit 2f53644

Please sign in to comment.