Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PwCalculation: Fix restart validation for nscf/bands #906

Merged
merged 2 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/aiida_quantumespresso/calculations/pw.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,29 @@ def validate_inputs_base(value, _):
parameters = value['parameters'].get_dict()
calculation_type = parameters.get('CONTROL', {}).get('calculation', 'scf')

# Check that the restart input parameters are set correctly
if calculation_type in ('nscf', 'bands'):
if parameters.get('ELECTRONS', {}).get('startingpot', 'file') != 'file':
return f'`startingpot` should be set to `file` for a `{calculation_type}` calculation.'
if parameters.get('CONTROL', {}).get('restart_mode', 'from_scratch') != 'from_scratch':
warnings.warn(f'`restart_mode` should be set to `from_scratch` for a `{calculation_type}` calculation.')
elif 'parent_folder' in value:
# If a `parent_folder` input is provided, make sure the inputs are set to restart
if 'parent_folder' in value and calculation_type not in ('nscf', 'bands'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't an scf calculation also not have "some" restart parameters if a parent_folder is specified? Why only nscf and bands? What is the use case for restarting an scf calc from_scratch and without reading the CD from file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got confused myself here again. Note the not in ('nscf', 'bands'), i.e. this check is only done for other calculation types. The reason is that for nscf and bands, the default of ELECTRONS.startingpot changes to 'file', see:

https://www.quantum-espresso.org/Doc/INPUT_PW.html#idm899

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not sure about the logic though. Why would there only have to be validation for anything but nscf and bands? For example, if the user specifies bands but then ELECTRONS.startingpot = 'atomic', surely that should also emit a warning then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that'd be another validation we can do. Much less likely use case though, someone what assigns a parent folder and then specifically sets ELECTRONS.startingpot = 'atomic' might be up to some weird business that actually requires this input.

if not any([
parameters.get('CONTROL', {}).get('restart_mode', None) == 'restart',
parameters.get('ELECTRONS', {}).get('startingpot', None) == 'file',
parameters.get('ELECTRONS', {}).get('startingwfc', None) == 'file'
]):
warnings.warn(
'`parent_folder` input was provided for the `PwCalculation`, but no '
'input parameters are set to restart from these files.'
f'`parent_folder` input was provided for the `{calculation_type}` `PwCalculation`, but no input'
'parameters were provided to restart from this folder.\n\n'
'Please set one of the following in the input parameters:\n'
" parameters['CONTROL']['restart_mode'] = 'restart'\n"
" parameters['ELECTRONS']['startingpot'] = 'file'\n"
" parameters['ELECTRONS']['startingwfc'] = 'file'\n"
)

@classmethod
def validate_inputs(cls, value, port_namespace):
"""Validate the top level namespace.

Check that the restart input parameters are set correctly. In case of 'nscf' and 'bands' calculations, this
means ``parent_folder`` is provided, ``startingpot`` is set to 'file' and ``restart_mode`` is 'from_scratch'.
For other calculations, if the ``parent_folder`` is provided, the restart settings must be set to use some of
the outputs.
means ``parent_folder`` is provided. For other calculations, if the ``parent_folder`` is provided, the restart
settings must be set to use some of the outputs.

Note that the validator is split in two methods: ``validate_inputs`` and ``validate_inputs_base``. This is to
facilitate work chains that wrap this calculation that will provide the ``parent_folder`` themselves and so do
Expand Down
12 changes: 2 additions & 10 deletions tests/calculations/test_pw.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,10 @@ def test_pw_validate_inputs_restart_nscf(
inputs['parent_folder'] = remote_data
generate_calc_job(fixture_sandbox, entry_point_name, inputs)

# Set `startingpot` to `'atomic'` -> raise
parameters['ELECTRONS']['startingpot'] = 'atomic'
inputs['parameters'] = orm.Dict(parameters)
with pytest.raises(ValueError, match='`startingpot` should be set to `file` for a `.*` calculation.'):
generate_calc_job(fixture_sandbox, entry_point_name, inputs)

# Set `restart_mode` to `'restart'` -> warning
parameters['ELECTRONS'].pop('startingpot')
# Set `restart_mode` to `'restart'` -> works
parameters['CONTROL']['restart_mode'] = 'restart'
inputs['parameters'] = orm.Dict(parameters)
with pytest.warns(Warning, match='`restart_mode` should be set to `from_scratch` for a `.*`.'):
generate_calc_job(fixture_sandbox, entry_point_name, inputs)
generate_calc_job(fixture_sandbox, entry_point_name, inputs)


def test_fixed_coords(fixture_sandbox, generate_calc_job, generate_inputs_pw, file_regression):
Expand Down