aiidateam / aiida-quantumespresso

The official AiiDA plugin for Quantum ESPRESSO
https://aiida-quantumespresso.readthedocs.io
Other
54 stars 79 forks source link

Input parameters about PwRelaxWorkChain #636

Open hezhengda opened 3 years ago

hezhengda commented 3 years ago

Dear AIida developers,

I tried to use the PwRelaxWorkChain in aiida-quantumespresso, and I found that if in input dictionary when I define base_final_scf, relaxation_scheme and relax_type, then use submit(PwRelaxWorkChain, **inputs), it will give me an error:

ValueError: Error occurred validating port 'inputs': Unexpected ports {'base_final_scf': {'pw': {'code': <Code: Remote code 'pw.x-6.5-rwth-claix-mac' on rwth-claix-mac, pk: 2959, uuid: 6362159c-a662-4822-ab55-4185919685b1>, 'pseudos': {'Pt': <UpfData: uuid: c03d5f79-e275-49d2-a480-53fb683bf2d2 (pk: 5585)>}, 'parameters': <Dict: uuid: 0f3a210e-8fe1-4563-b3b7-890fb6ebfbf3 (pk: 5586)>, 'settings': <Dict: uuid: 36bed618-c426-4671-9af7-cb26404d448b (pk: 5587)>, 'metadata': {'label': 'RelaxWorkChain_Trial_1', 'description': 'Trial on the PwRelaxWorkChain', 'options': {'resources': {'num_machines': 4}, 'max_wallclock_seconds': 86400, 'account': 'jara0037', 'scheduler_stderr': 'stderr', 'scheduler_stdout': 'stdout', 'queue_name': 'c18m'}}}, 'kpoints': <KpointsData: uuid: 1b7991c0-cd19-4f80-8ca9-c000011152eb (pk: 5588)>}, 'relax_scheme': <Str: uuid: b9f2b91a-f55e-44c1-aa44-592f7560748b (unstored) value: vc-relax>, 'relax_type': <Str: uuid: d48995e5-ab37-4120-9774-378ce5f5a243 (unstored) value: atoms_cell>}, for a non dynamic namespace

After get rid of all three values in the input dictionary, it works.

Is this a bug? Or I might do something wrong?

My input dictionary is:

{'base': {'pw': {'code': <Code: Remote code 'xxxxxx' on xxxxxx, pk: 2959, uuid: 6362159c-a662-4822-ab55-4185919685b1>,
   'pseudos': {'Pt': <UpfData: uuid: c03d5f79-e275-49d2-a480-53fb683bf2d2 (pk: 5585)>},
   'parameters': <Dict: uuid: 0f3a210e-8fe1-4563-b3b7-890fb6ebfbf3 (pk: 5586)>,
   'settings': <Dict: uuid: 36bed618-c426-4671-9af7-cb26404d448b (pk: 5587)>,
   'metadata': {'label': 'RelaxWorkChain_Trial_1',
    'description': 'Trial on the PwRelaxWorkChain',
    'options': {'resources': {'num_machines': 4},
     'max_wallclock_seconds': 86400,
     'account': 'xxxxxx',
     'scheduler_stderr': 'stderr',
     'scheduler_stdout': 'stdout',
     'queue_name': 'xxxxxx'}}},
  'kpoints': <KpointsData: uuid: 1b7991c0-cd19-4f80-8ca9-c000011152eb (pk: 5588)>},
 'base_final_scf': {'pw': {'code': <Code: Remote code 'xxxxxxx', pk: 2959, uuid: 6362159c-a662-4822-ab55-4185919685b1>,
   'pseudos': {'Pt': <UpfData: uuid: c03d5f79-e275-49d2-a480-53fb683bf2d2 (pk: 5585)>},
   'parameters': <Dict: uuid: 0f3a210e-8fe1-4563-b3b7-890fb6ebfbf3 (pk: 5586)>,
   'settings': <Dict: uuid: 36bed618-c426-4671-9af7-cb26404d448b (pk: 5587)>,
   'metadata': {'label': 'RelaxWorkChain_Trial_1',
    'description': 'Trial on the PwRelaxWorkChain',
    'options': {'resources': {'num_machines': 4},
     'max_wallclock_seconds': 86400,
     'account': 'xxxxxxxx',
     'scheduler_stderr': 'stderr',
     'scheduler_stdout': 'stdout',
     'queue_name': 'xxxxxxx'}}},
  'kpoints': <KpointsData: uuid: 1b7991c0-cd19-4f80-8ca9-c000011152eb (pk: 5588)>},
 'structure': <StructureData: uuid: 5d0c380f-9548-4298-a876-8dc291da4972 (pk: 5590)>,
 'final_scf': <Bool: uuid: f98c267a-b6c7-4094-9f79-3488cd6cda6f (pk: 5591) value: True>,
 'relax_scheme': <Str: uuid: b9f2b91a-f55e-44c1-aa44-592f7560748b (unstored) value: vc-relax>,
 'relax_type': <Str: uuid: d48995e5-ab37-4120-9774-378ce5f5a243 (unstored) value: atoms_cell>,
 'meta_convergence': <Bool: uuid: b3cf6ba3-f80f-40c8-9317-e5c297686165 (pk: 5592) value: True>,
 'max_meta_convergence_iterations': <Int: uuid: 6b789eb8-2469-44c8-b46d-aacc452dae0f (pk: 5593) value: 5>,
 'volume_convergence': <Float: uuid: 23eb7685-4dca-4296-a46f-fc97b7cdaced (pk: 5594) value: 0.01>,
 'clean_workdir': <Bool: uuid: 948187ab-a309-400c-ad80-0d555223d167 (pk: 5595) value: True>}
hezhengda commented 3 years ago

This seems to be a version problem (I'm using the release version of 3.2.1).

After git clone path_repository and pip3 install -e ., now it's working fine.

The correct input structure for PwRelaxWorkChain is:

{'base': {'pw': {'code': <Code: Remote code 'xxxx' on xxxx, pk: 2959, uuid: 6362159c-a662-4822-ab55-4185919685b1>,
   'pseudos': {'Pt': <UpfData: uuid: af798a12-434e-4232-b4a6-2f26d94e19a4 (unstored)>},
   'parameters': <Dict: uuid: 214b65e7-6b3f-4070-af25-33ae7f87b7e2 (unstored)>,
   'settings': <Dict: uuid: b46b6f29-2f14-41eb-95da-c1b51bb917e5 (unstored)>,
   'metadata': {'label': 'RelaxWorkChain_Trial_1',
    'description': 'Trial on the PwRelaxWorkChain',
    'options': {'resources': {'num_machines': 4},
     'max_wallclock_seconds': 86400,
     'account': 'xxxxx',
     'scheduler_stderr': 'stderr',
     'scheduler_stdout': 'stdout',
     'queue_name': 'xxxxx'}}},
  'kpoints': <KpointsData: uuid: e9c859de-2b59-495e-8434-4419f1469b61 (unstored)>},
 'base_final_scf': {'pw': {'code': <Code: Remote code 'xxxxx' on xxxxxx, pk: 2959, uuid: 6362159c-a662-4822-ab55-4185919685b1>,
   'pseudos': {'Pt': <UpfData: uuid: af798a12-434e-4232-b4a6-2f26d94e19a4 (unstored)>},
   'parameters': <Dict: uuid: 214b65e7-6b3f-4070-af25-33ae7f87b7e2 (unstored)>,
   'settings': <Dict: uuid: b46b6f29-2f14-41eb-95da-c1b51bb917e5 (unstored)>,
   'metadata': {'label': 'RelaxWorkChain_Trial_1',
    'description': 'Trial on the PwRelaxWorkChain',
    'options': {'resources': {'num_machines': 4},
     'max_wallclock_seconds': 86400,
     'account': 'xxxxxxx',
     'scheduler_stderr': 'stderr',
     'scheduler_stdout': 'stdout',
     'queue_name': 'xxxxxxxx'}}},
  'kpoints': <KpointsData: uuid: e9c859de-2b59-495e-8434-4419f1469b61 (unstored)>},
 'structure': <StructureData: uuid: 7ad21fbf-b365-4e98-84c9-f1ba381cbca1 (unstored)>,
 'relaxation_scheme': <Str: uuid: 304b9879-566a-4722-8896-dff8b3965520 (unstored) value: vc-relax>,
 'relax_type': <Str: uuid: e267c892-8879-429f-ac64-e22f3a2ce59d (unstored) value: atoms_cell>,
 'meta_convergence': <Bool: uuid: 4828f715-7d2a-40c2-9485-76a88542f401 (unstored) value: True>,
 'max_meta_convergence_iterations': <Int: uuid: 60644408-7387-4c8d-8b27-246b21ce0498 (unstored) value: 5>,
 'volume_convergence': <Float: uuid: 1f98d295-1cca-4c3f-b81f-1363fac4a90c (unstored) value: 0.01>,
 'clean_workdir': <Bool: uuid: 712d402d-167f-4137-9e46-23944aad76b5 (unstored) value: True>}

I also wrote an input generator for the PwCalculation, PwBaseWorkChain and PwRelaxWorkChain, if you want to take a look and use it, you can go and have a look: https://github.com/hezhengda/hzdplugins/blob/master/hzdplugins/aiidaplugins/inputgenerator.py

The code for using the inputgenerator is given below (the results is the above dictionary, but the value you can set your own):

from hzdplugins.aiidaplugins.inputgenerator import PwBaseWorkChainInputGenerator
from hzdplugins.aiidaplugins.inputgenerator import PwCalculationInputGenerator
from hzdplugins.aiidaplugins.inputgenerator import PwRelaxWorkChainInputGenerator 

inputPwCalc = PwCalculationInputGenerator(
    code=code, 
    structure=Pt_bulk, 
    pseudos=pseudos, 
    parameters = param, 
    settings=settings_dict,
    metadata=metadata,
    kpoints=kpts
)

inputPwBaseWC = PwBaseWorkChainInputGenerator(
    pw=inputPwCalc, 
    kpoints=kpts,
    clean_workdir=True
)

inputPwBaseWC_finalscf = PwBaseWorkChainInputGenerator(
    pw=inputPwCalc,
    kpoints=kpts,
    clean_workdir=True
)

inputPwRelaxWC = PwRelaxWorkChainInputGenerator(
    base=inputPwBaseWC,
    base_final_scf=inputPwBaseWC_finalscf,
    structure=Pt_bulk,
    relaxation_scheme='vc-relax',
    relax_type='atoms_cell',
    meta_convergence=True,
    max_meta_convergence_iterations=5,
    volume_convergence=0.01,
    clean_workdir=True,
)

inputs = inputPwRelaxWC.outputdict() # outputdict() is a method for all InputGenerator classes.

Also the package is documented in: https://hzdplugins.readthedocs.io/en/latest/aiidaplugins.html#module-hzdplugins.aiidaplugins.inputgenerator if you want more detailed information.

Pros: The auto-completion can help you remember which parameters are needed for each simulation, since the input file can become larger when your workflow becomes more complex, so it's very important to combine different generators together, as shown in the above. Cons: Needs to write it manually, depend on how the WorkChain is established, could be nicer if the InputGenerator can be used as a class method in the WorkChain class (or automatically generated from the self.input dictionary).

hezhengda commented 3 years ago

Now it seems there is a small bug:

Log messages
---------------------------------------------
There are 1 log messages for this calculation
Run 'verdi process report 5836' to see them
(aiidaconda) z.he@iek13006 python % verdi process report 5836
2020-12-20 11:42:30 [4830 | REPORT]: [5836|PwRelaxWorkChain|on_except]: Traceback (most recent call last):
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/utils.py", line 123, in __getattr__
    return self[attr]
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/frozendict/__init__.py", line 29, in __getitem__
    return self._dict[key]
KeyError: 'relaxation_scheme'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/process_states.py", line 225, in execute
    result = self.run_fn(*self.args, **self.kwargs)
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/aiida/engine/processes/workchains/workchain.py", line 205, in _do_step
    finished, stepper_result = self._stepper.step()
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/workchains.py", line 268, in step
    finished, result = self._child_stepper.step()
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/workchains.py", line 500, in step
    finished, result = self._child_stepper.step()
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/workchains.py", line 268, in step
    finished, result = self._child_stepper.step()
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/workchains.py", line 219, in step
    return True, self._fn(self._workchain)
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/aiida_quantumespresso/workflows/pw/relax.py", line 91, in run_relax
    )
  File "/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/plumpy/utils.py", line 126, in __getattr__
    raise AttributeError(errmsg)
AttributeError: 'AttributesFrozendict' object has no attribute 'relaxation_scheme'

I try to re-install aiida-quantumespresso and also aiida-core from the reposity, but this exception still remains.

/Users/z.he/opt/miniconda3/envs/aiidaconda/lib/python3.8/site-packages/aiida_quantumespresso/workflows/pw/relax.py:33: AiidaDeprecationWarning: the `relaxation_scheme` input is deprecated and will be removed. Use the ``relax_type`` input instead. Accepted values are values of the ``aiida_quantumespresso.common.types.RelaxType`` enum
  warnings.warn(

I know that in the relax.py the WorkChain will automatically check relaxation_scheme and relax_type:

if 'relaxation_scheme' in self.inputs:
    if self.inputs.relaxation_scheme.value == 'relax':
       relax_type = RelaxType.ATOMS
   elif self.inputs.relaxation_scheme.value == 'vc-relax':
       relax_type = RelaxType.ATOMS_CELL
   else:
       raise ValueError('unsupported value for the `relaxation_scheme` input.')
else:
   relax_type = RelaxType(self.inputs.relax_type)

But why do I get this two different behavior? Something could be inconsistent ...

sphuber commented 3 years ago

Cons: Needs to write it manually, depend on how the WorkChain is established, could be nicer if the InputGenerator can be used as a class method in the WorkChain class (or automatically generated from the self.input dictionary).

We actually just added this a few weeks ago. You can now do PwRelaxWorkChain.get_builder_from_protocol(code, structure) and it will provide a fully built builder ready to be submitted. You can also specify a different protocol from the default with the protocol keyword, e.g., PwRelaxWorkChain.get_builder_from_protocol(code, structure, protocol='precise')

But why do I get this two different behavior? Something could be inconsistent ...

The relaxation_scheme input is deprecated, that is why you get the warning if you define it. It has been replaced with relax_type, so you can simply use only that one.

try to re-install aiida-quantumespresso and also aiida-core from the reposity, but this exception still remains.

The stacktrace doesn't actually give the line in the workchain where it tries to access the relaxation_scheme key, but I have looked at the current code and cannot see where this can happen. The only place we access it is within a conditional that first checks the key is there. You say you installed from repository using pip but are you sure that version is being used? I see you are using conda to install things, which I am not too familiar with. Does it work if you use pip in a conda managed package?