Skip to content

Commit

Permalink
RecommendedCutoffMixin: improve error messages (#86)
Browse files Browse the repository at this point in the history
Improve the error message in two cases:

* When deleting the final stringency with the `delete_cutoffs` method.
* An element requested with the `get_recommended_cutoffs` method does
  not have a pseudo in the pseudo family.
  • Loading branch information
mbercx authored May 6, 2021
1 parent f73c7b8 commit bc7e5ea
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions aiida_pseudo/groups/mixins/cutoffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,25 @@ def delete_cutoffs(self, stringency) -> None:
try:
if stringency == self.get_default_stringency():
self.delete_extra('_default_stringency')
warning += f'`{stringency}` was the default stringency of this family. '
warning += f'`{stringency}` was the default stringency of this family.'
assign_new_default = True
else:
assign_new_default = False
except ValueError:
assign_new_default = True

if assign_new_default:
if len(cutoffs_dict) == 1:
if len(cutoffs_dict) == 0:
warning += (
' Since no other stringencies are defined for this family, no new default can be specified.'
)
elif len(cutoffs_dict) == 1:
final_stringency = list(cutoffs_dict.keys())[0]
self.set_default_stringency(final_stringency)
warning += f'Setting `{final_stringency}` as the default since it is now the only defined stringency.'
warning += f' Setting `{final_stringency}` as the default since it is now the only defined stringency.'
else:
warning += (
f'Please set one of {self.get_cutoff_stringencies()} as the new default stringency with the '
f' Please set one of {self.get_cutoff_stringencies()} as the new default stringency with the '
'`set_default_stringency` method.'
)

Expand Down Expand Up @@ -276,6 +280,7 @@ def get_recommended_cutoffs(self, *, elements=None, structure=None, stringency=N
:return: tuple of recommended wavefunction and density cutoff.
:raises ValueError: if the requested stringency is not defined for this family.
:raises ValueError: if optional unit specified is invalid.
:raises ValueError: if the family does not have a pseudo for one of the elements (of the structure).
"""
if (elements is None and structure is None) or (elements is not None and structure is not None):
raise ValueError('at least one and only one of `elements` or `structure` should be defined')
Expand All @@ -299,6 +304,9 @@ def get_recommended_cutoffs(self, *, elements=None, structure=None, stringency=N

for element in symbols:

if element not in cutoffs:
raise ValueError(f'family does not contain a pseudo for element `{element}`.')

if unit is not None:
current_unit = self.get_cutoffs_unit(stringency)
values = {k: U.Quantity(v, current_unit).to(unit).to_tuple()[0] for k, v in cutoffs[element].items()}
Expand Down

0 comments on commit bc7e5ea

Please sign in to comment.