-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathrelax.py
386 lines (307 loc) · 17.7 KB
/
relax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-
"""Workchain to relax a structure using Quantum ESPRESSO pw.x."""
from aiida import orm
from aiida.common import AttributeDict, exceptions
from aiida.common.lang import type_check
from aiida.engine import ToContext, WorkChain, append_, if_, while_
from aiida.plugins import CalculationFactory, WorkflowFactory
from aiida_quantumespresso.common.types import RelaxType
from aiida_quantumespresso.utils.mapping import prepare_process_inputs
from ..protocols.utils import ProtocolMixin
PwCalculation = CalculationFactory('quantumespresso.pw')
PwBaseWorkChain = WorkflowFactory('quantumespresso.pw.base')
def validate_inputs(inputs, _):
"""Validate the top level namespace."""
parameters = inputs['base']['pw']['parameters'].get_dict()
if 'calculation' not in parameters.get('CONTROL', {}):
return 'The parameters in `base.pw.parameters` do not specify the required key `CONTROL.calculation`.'
class PwRelaxWorkChain(ProtocolMixin, WorkChain):
"""Workchain to relax a structure using Quantum ESPRESSO pw.x."""
@classmethod
def define(cls, spec):
"""Define the process specification."""
# yapf: disable
super().define(spec)
spec.expose_inputs(PwBaseWorkChain, namespace='base',
exclude=('clean_workdir', 'pw.structure', 'pw.parent_folder'),
namespace_options={'help': 'Inputs for the `PwBaseWorkChain` for the main relax loop.'})
spec.expose_inputs(PwBaseWorkChain, namespace='base_final_scf',
exclude=('clean_workdir', 'pw.structure', 'pw.parent_folder'),
namespace_options={'required': False, 'populate_defaults': False,
'help': 'Inputs for the `PwBaseWorkChain` for the final scf.'})
spec.input('structure', valid_type=orm.StructureData, help='The inputs structure.')
spec.input('meta_convergence', valid_type=orm.Bool, default=lambda: orm.Bool(True),
help='If `True` the workchain will perform a meta-convergence on the cell volume.')
spec.input('max_meta_convergence_iterations', valid_type=orm.Int, default=lambda: orm.Int(5),
help='The maximum number of variable cell relax iterations in the meta convergence cycle.')
spec.input('volume_convergence', valid_type=orm.Float, default=lambda: orm.Float(0.01),
help='The volume difference threshold between two consecutive meta convergence iterations.')
spec.input('clean_workdir', valid_type=orm.Bool, default=lambda: orm.Bool(False),
help='If `True`, work directories of all called calculation will be cleaned at the end of execution.')
spec.inputs.validator = validate_inputs
spec.outline(
cls.setup,
while_(cls.should_run_relax)(
cls.run_relax,
cls.inspect_relax,
),
if_(cls.should_run_final_scf)(
cls.run_final_scf,
cls.inspect_final_scf,
),
cls.results,
)
spec.exit_code(401, 'ERROR_SUB_PROCESS_FAILED_RELAX',
message='the relax PwBaseWorkChain sub process failed')
spec.exit_code(402, 'ERROR_SUB_PROCESS_FAILED_FINAL_SCF',
message='the final scf PwBaseWorkChain sub process failed')
spec.expose_outputs(PwBaseWorkChain, exclude=('output_structure',))
spec.output('output_structure', valid_type=orm.StructureData, required=False,
help='The successfully relaxed structure.')
# yapf: enable
@classmethod
def get_protocol_filepath(cls):
"""Return ``pathlib.Path`` to the ``.yaml`` file that defines the protocols."""
from importlib_resources import files
from ..protocols import pw as pw_protocols
return files(pw_protocols) / 'relax.yaml'
@classmethod
def get_builder_from_protocol(
cls,
code,
structure,
protocol=None,
overrides=None,
relax_type=RelaxType.POSITIONS_CELL,
options=None,
**kwargs
):
"""Return a builder prepopulated with inputs selected according to the chosen protocol.
:param code: the ``Code`` instance configured for the ``quantumespresso.pw`` plugin.
:param structure: the ``StructureData`` instance to use.
:param protocol: protocol to use, if not specified, the default will be used.
:param overrides: optional dictionary of inputs to override the defaults of the protocol.
:param relax_type: the relax type to use: should be a value of the enum ``common.types.RelaxType``.
:param options: A dictionary of options that will be recursively set for the ``metadata.options`` input of all
the ``CalcJobs`` that are nested in this work chain.
:param kwargs: additional keyword arguments that will be passed to the ``get_builder_from_protocol`` of all the
sub processes that are called by this workchain.
:return: a process builder instance with all inputs defined ready for launch.
"""
type_check(relax_type, RelaxType)
inputs = cls.get_protocol_inputs(protocol, overrides)
args = (code, structure, protocol)
base = PwBaseWorkChain.get_builder_from_protocol(
*args, overrides=inputs.get('base', None), options=options, **kwargs
)
base_final_scf = PwBaseWorkChain.get_builder_from_protocol(
*args, overrides=inputs.get('base_final_scf', None), options=options, **kwargs
)
base['pw'].pop('structure', None)
base.pop('clean_workdir', None)
base_final_scf['pw'].pop('structure', None)
base_final_scf.pop('clean_workdir', None)
# Quantum ESPRESSO currently only supports optimization of the volume for simple cubic systems. It requires
# to set `ibrav=1` or the code will except.
if relax_type in (RelaxType.VOLUME, RelaxType.POSITIONS_VOLUME):
raise ValueError(f'relax type `{relax_type} is not yet supported.')
if relax_type in (RelaxType.VOLUME, RelaxType.SHAPE, RelaxType.CELL):
base.pw.settings = orm.Dict(PwRelaxWorkChain._fix_atomic_positions(structure, base.pw.settings))
if relax_type is RelaxType.NONE:
base.pw.parameters['CONTROL']['calculation'] = 'scf'
base.pw.parameters.base.attributes.delete('CELL')
elif relax_type is RelaxType.POSITIONS:
base.pw.parameters['CONTROL']['calculation'] = 'relax'
base.pw.parameters.base.attributes.delete('CELL')
else:
base.pw.parameters['CONTROL']['calculation'] = 'vc-relax'
if relax_type in (RelaxType.VOLUME, RelaxType.POSITIONS_VOLUME):
base.pw.parameters['CELL']['cell_dofree'] = 'volume'
if relax_type in (RelaxType.SHAPE, RelaxType.POSITIONS_SHAPE):
base.pw.parameters['CELL']['cell_dofree'] = 'shape'
if relax_type in (RelaxType.CELL, RelaxType.POSITIONS_CELL):
pbc_cell_dofree_map = {
(True, True, True): 'all',
(True, False, False): 'x',
(False, True, False): 'y',
(False, False, True): 'z',
(True, True, False): '2Dxy',
}
if structure.pbc in pbc_cell_dofree_map:
base.pw.parameters['CELL']['cell_dofree'] = pbc_cell_dofree_map[structure.pbc]
else:
raise ValueError(f'Structures with periodic boundary conditions `{structure.pbc}` are not supported.')
builder = cls.get_builder()
builder.base = base
builder.base_final_scf = base_final_scf
builder.structure = structure
builder.clean_workdir = orm.Bool(inputs['clean_workdir'])
builder.max_meta_convergence_iterations = orm.Int(inputs['max_meta_convergence_iterations'])
builder.meta_convergence = orm.Bool(inputs['meta_convergence'])
builder.volume_convergence = orm.Float(inputs['volume_convergence'])
return builder
def setup(self):
"""Input validation and context setup."""
self.ctx.current_number_of_bands = None
self.ctx.current_structure = self.inputs.structure
self.ctx.current_cell_volume = None
self.ctx.is_converged = False
self.ctx.iteration = 0
self.ctx.relax_inputs = AttributeDict(self.exposed_inputs(PwBaseWorkChain, namespace='base'))
self.ctx.relax_inputs.pw.parameters = self.ctx.relax_inputs.pw.parameters.get_dict()
self.ctx.relax_inputs.pw.parameters.setdefault('CONTROL', {})
# Set the meta_convergence and add it to the context
self.ctx.meta_convergence = self.inputs.meta_convergence.value
volume_cannot_change = (
self.ctx.relax_inputs.pw.parameters['CONTROL'].get('calculation', 'scf') in ('scf', 'relax') or
self.ctx.relax_inputs.pw.parameters.get('CELL', {}).get('cell_dofree', None) == 'shape'
)
if self.ctx.meta_convergence and volume_cannot_change:
self.report(
'No change in volume possible for the provided base input parameters. Meta convergence is turned off.'
)
self.ctx.meta_convergence = False
# Add the final scf inputs to the context if a final scf should be run
if 'base_final_scf' in self.inputs:
self.ctx.final_scf_inputs = AttributeDict(self.exposed_inputs(PwBaseWorkChain, namespace='base_final_scf'))
if self.ctx.relax_inputs.pw.parameters['CONTROL'].get('calculation', 'scf') == 'scf':
self.report(
'Work chain will not run final SCF when `calculation` is set to `scf` for the relaxation '
'`PwBaseWorkChain`.'
)
self.ctx.pop('final_scf_inputs')
else:
self.ctx.final_scf_inputs.pw.parameters = self.ctx.final_scf_inputs.pw.parameters.get_dict()
self.ctx.final_scf_inputs.pw.parameters.setdefault('CONTROL', {})
self.ctx.final_scf_inputs.metadata.call_link_label = 'final_scf'
def should_run_relax(self):
"""Return whether a relaxation workchain should be run.
This is the case as long as the volume change between two consecutive relaxation runs is larger than the volume
convergence threshold value and the maximum number of meta convergence iterations is not exceeded.
"""
return not self.ctx.is_converged and self.ctx.iteration < self.inputs.max_meta_convergence_iterations.value
def should_run_final_scf(self):
"""Return whether after successful relaxation a final scf calculation should be run.
If the maximum number of meta convergence iterations has been exceeded and convergence has not been reached, the
structure cannot be considered to be relaxed and the final scf should not be run.
"""
return self.ctx.is_converged and 'final_scf_inputs' in self.ctx
def run_relax(self):
"""Run the `PwBaseWorkChain` to run a relax `PwCalculation`."""
self.ctx.iteration += 1
inputs = self.ctx.relax_inputs
inputs.pw.structure = self.ctx.current_structure
# If one of the nested `PwBaseWorkChains` changed the number of bands, apply it here
if self.ctx.current_number_of_bands is not None:
inputs.pw.parameters.setdefault('SYSTEM', {})['nbnd'] = self.ctx.current_number_of_bands
# Set the `CALL` link label
inputs.metadata.call_link_label = f'iteration_{self.ctx.iteration:02d}'
inputs = prepare_process_inputs(PwBaseWorkChain, inputs)
running = self.submit(PwBaseWorkChain, **inputs)
self.report(f'launching PwBaseWorkChain<{running.pk}>')
return ToContext(workchains=append_(running))
def inspect_relax(self):
"""Inspect the results of the last `PwBaseWorkChain`.
Compare the cell volume of the relaxed structure of the last completed workchain with the previous. If the
difference ratio is less than the volume convergence threshold we consider the cell relaxation converged.
"""
workchain = self.ctx.workchains[-1]
acceptable_statuses = ['ERROR_IONIC_CONVERGENCE_REACHED_EXCEPT_IN_FINAL_SCF']
if workchain.is_excepted or workchain.is_killed:
self.report('relax PwBaseWorkChain was excepted or killed')
return self.exit_codes.ERROR_SUB_PROCESS_FAILED_RELAX
if workchain.is_failed and workchain.exit_status not in PwBaseWorkChain.get_exit_statuses(acceptable_statuses):
self.report(f'relax PwBaseWorkChain failed with exit status {workchain.exit_status}')
return self.exit_codes.ERROR_SUB_PROCESS_FAILED_RELAX
try:
structure = workchain.outputs.output_structure
except exceptions.NotExistent:
# If the calculation is set to 'scf', this is expected, so we are done
if self.ctx.relax_inputs.pw.parameters['CONTROL']['calculation'] == 'scf':
self.ctx.is_converged = True
return
self.report('`vc-relax` or `relax` PwBaseWorkChain finished successfully but without output structure')
return self.exit_codes.ERROR_SUB_PROCESS_FAILED_RELAX
prev_cell_volume = self.ctx.current_cell_volume
curr_cell_volume = structure.get_cell_volume()
# Set relaxed structure as input structure for next iteration
self.ctx.current_structure = structure
self.ctx.current_number_of_bands = workchain.outputs.output_parameters.get_dict()['number_of_bands']
self.report(f'after iteration {self.ctx.iteration} cell volume of relaxed structure is {curr_cell_volume}')
# After first iteration, simply set the cell volume and restart the next base workchain
if not prev_cell_volume:
self.ctx.current_cell_volume = curr_cell_volume
# If meta convergence is switched off we are done
if not self.ctx.meta_convergence:
self.ctx.is_converged = True
return
# Check whether the cell volume is converged
volume_threshold = self.inputs.volume_convergence.value
volume_difference = abs(prev_cell_volume - curr_cell_volume) / prev_cell_volume
if volume_difference < volume_threshold:
self.ctx.is_converged = True
self.report(
f'relative cell volume difference {volume_difference} smaller than threshold {volume_threshold}'
)
else:
self.report(
f'current relative cell volume difference {volume_difference} larger than threshold {volume_threshold}'
)
self.ctx.current_cell_volume = curr_cell_volume
return
def run_final_scf(self):
"""Run the `PwBaseWorkChain` to run a final scf `PwCalculation` for the relaxed structure."""
inputs = self.ctx.final_scf_inputs
inputs.pw.structure = self.ctx.current_structure
inputs_nbnd = inputs.pw.parameters.get('SYSTEM', {}).get('nbnd', None)
if self.ctx.current_number_of_bands is not None and inputs_nbnd is None:
inputs.pw.parameters.setdefault('SYSTEM', {})['nbnd'] = self.ctx.current_number_of_bands
inputs = prepare_process_inputs(PwBaseWorkChain, inputs)
running = self.submit(PwBaseWorkChain, **inputs)
self.report(f'launching PwBaseWorkChain<{running.pk}> for final scf')
return ToContext(workchain_scf=running)
def inspect_final_scf(self):
"""Inspect the result of the final scf `PwBaseWorkChain`."""
workchain = self.ctx.workchain_scf
if not workchain.is_finished_ok:
self.report(f'final scf PwBaseWorkChain failed with exit status {workchain.exit_status}')
return self.exit_codes.ERROR_SUB_PROCESS_FAILED_FINAL_SCF
def results(self):
"""Attach the output parameters and structure of the last workchain to the outputs."""
if self.ctx.is_converged and self.ctx.iteration <= self.inputs.max_meta_convergence_iterations.value:
self.report(f'workchain completed after {self.ctx.iteration} iterations')
else:
self.report('maximum number of meta convergence iterations exceeded')
# Get the latest relax workchain and pass the outputs
final_relax_workchain = self.ctx.workchains[-1]
if self.ctx.relax_inputs.pw.parameters['CONTROL']['calculation'] != 'scf':
self.out('output_structure', final_relax_workchain.outputs.output_structure)
try:
self.out_many(self.exposed_outputs(self.ctx.workchain_scf, PwBaseWorkChain))
except AttributeError:
self.out_many(self.exposed_outputs(final_relax_workchain, PwBaseWorkChain))
def on_terminated(self):
"""Clean the working directories of all child calculations if `clean_workdir=True` in the inputs."""
super().on_terminated()
if self.inputs.clean_workdir.value is False:
self.report('remote folders will not be cleaned')
return
cleaned_calcs = []
for called_descendant in self.node.called_descendants:
if isinstance(called_descendant, orm.CalcJobNode):
try:
called_descendant.outputs.remote_folder._clean() # pylint: disable=protected-access
cleaned_calcs.append(called_descendant.pk)
except (IOError, OSError, KeyError):
pass
if cleaned_calcs:
self.report(f"cleaned remote folders of calculations: {' '.join(map(str, cleaned_calcs))}")
@staticmethod
def _fix_atomic_positions(structure, settings):
"""Fix the atomic positions, by setting the `FIXED_COORDS` key in the `settings` input node."""
if settings is not None:
settings = settings.get_dict()
else:
settings = {}
settings['FIXED_COORDS'] = [[True, True, True]] * len(structure.sites)
return settings