-
Notifications
You must be signed in to change notification settings - Fork 41
/
vasp_custodian.py
226 lines (199 loc) · 7.57 KB
/
vasp_custodian.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
"""Custodian handlers for VASP."""
from __future__ import annotations
import os
import shlex
from typing import TYPE_CHECKING
from custodian import Custodian
from custodian.vasp.handlers import (
FrozenJobErrorHandler,
IncorrectSmearingHandler,
KspacingMetalHandler,
LargeSigmaHandler,
MeshSymmetryErrorHandler,
NonConvergingErrorHandler,
PositiveEnergyErrorHandler,
PotimErrorHandler,
StdErrHandler,
UnconvergedErrorHandler,
VaspErrorHandler,
WalltimeHandler,
)
from custodian.vasp.jobs import VaspJob
from custodian.vasp.validators import VaspFilesValidator, VasprunXMLValidator
if TYPE_CHECKING:
from pathlib import Path
from typing import Callable, TypedDict
class VaspJobKwargs(TypedDict, total=False):
"""
Type hint for `vasp_job_kwargs` in in [quacc.calculators.vasp.vasp_custodian.run_custodian][].
"""
output_file: str # default = "vasp.out"
stderr_file: str # default = "std_err.txt"
suffix: str # default = ""
final: bool # default = True
backup: bool # default = True
auto_npar: bool # default = False
auto_gamma: bool # default = True
settings_override: dict | None # default = None
copy_magmom: bool # default = False
auto_continue: bool # default = False
class CustodianKwargs(TypedDict, total=False):
"""
Type hint for `custodian_kwargs` in [quacc.calculators.vasp.vasp_custodian.run_custodian][].
"""
max_errors_per_job: int | None # default = None
polling_time_step: int # default = 10
monitor_freq: int # default = 10
skip_over_errors: bool # default = False
gzipped_output: bool # default = False
checkpoint: bool # default = False
terminate_func: Callable | None # default = None
terminate_on_nonzero_returncode: bool # default = False
_DEFAULT_SETTING = ()
def run_custodian(
vasp_parallel_cmd: str = _DEFAULT_SETTING,
vasp_cmd: str = _DEFAULT_SETTING,
vasp_gamma_cmd: str = _DEFAULT_SETTING,
vasp_custodian_max_errors: int = _DEFAULT_SETTING,
vasp_custodian_wall_time: float = _DEFAULT_SETTING,
vtst_fixes: bool = _DEFAULT_SETTING,
vasp_custodian_handlers: list[str] | None = _DEFAULT_SETTING,
vasp_custodian_validators: list[str] | None = _DEFAULT_SETTING,
scratch_dir: str | None = None,
directory: str | Path | None = None,
vasp_job_kwargs: VaspJobKwargs | None = None,
custodian_kwargs: CustodianKwargs | None = None,
) -> list[list[dict]]:
"""
Function to run VASP Custodian.
Parameters
----------
vasp_parallel_cmd
VASP parallel command, e.g. "srun -N 2 --ntasks-per-node=24". Defaults
to the $VASP_PARALLEL_CMD environment variable in settings.
vasp_cmd
VASP command. Defaults to "vasp_std" in settings.
vasp_gamma_cmd
VASP gamma command. Defaults to "vasp_gam" in settings.
vasp_custodian_max_errors
Maximum number of errors to allow before stopping the run. Defaults to 5
in settings.
vasp_custodian_wall_time
Maximum wall time to allow before creating a STOPCAR. Defaults to
infinity in settings.
vtst_fixes
Whether to apply VTST input swaps. Defaults to False in settings.
vasp_custodian_handlers
List of handlers to use in Custodian. See settings for list.
vasp_custodian_validators
List of validators to use in Custodian. See settings for list.
scratch_dir
Scratch directory to use. Defaults to None.
directory
Directory to run the calculation in. Defaults to None.
vasp_job_kwargs
Keyword arguments to pass to the Custodian VaspJob. Defaults to None.
custodian_kwargs
Any remaining keyword arguments to pass to Custodian. Defaults to None.
Returns
-------
list[list[dict]]
List of errors from each Custodian job.
"""
# Adapted from atomate2.vasp.run.run_vasp
from quacc import SETTINGS
# Set defaults
vasp_parallel_cmd = os.path.expandvars(
SETTINGS.VASP_PARALLEL_CMD
if vasp_parallel_cmd == _DEFAULT_SETTING
else vasp_parallel_cmd
)
vasp_cmd = SETTINGS.VASP_CMD if vasp_cmd == _DEFAULT_SETTING else vasp_cmd
vasp_gamma_cmd = (
SETTINGS.VASP_GAMMA_CMD
if vasp_gamma_cmd == _DEFAULT_SETTING
else vasp_gamma_cmd
)
vasp_custodian_max_errors = (
SETTINGS.VASP_CUSTODIAN_MAX_ERRORS
if vasp_custodian_max_errors == _DEFAULT_SETTING
else vasp_custodian_max_errors
)
vasp_custodian_wall_time = (
SETTINGS.VASP_CUSTODIAN_WALL_TIME
if vasp_custodian_wall_time == _DEFAULT_SETTING
else vasp_custodian_wall_time
)
vtst_fixes = (
SETTINGS.VASP_CUSTODIAN_VTST if vtst_fixes == _DEFAULT_SETTING else vtst_fixes
)
vasp_custodian_handlers = (
SETTINGS.VASP_CUSTODIAN_HANDLERS
if vasp_custodian_handlers == _DEFAULT_SETTING
else vasp_custodian_handlers
)
vasp_custodian_validators = (
SETTINGS.VASP_CUSTODIAN_VALIDATORS
if vasp_custodian_validators == _DEFAULT_SETTING
else vasp_custodian_validators
)
# Handlers for VASP
handlers = []
handlers_dict = {
"VaspErrorHandler": VaspErrorHandler(vtst_fixes=vtst_fixes),
"FrozenJobErrorHandler": FrozenJobErrorHandler(),
"IncorrectSmearingHandler": IncorrectSmearingHandler(),
"LargeSigmaHandler": LargeSigmaHandler(),
"MeshSymmetryErrorHandler": MeshSymmetryErrorHandler(),
"NonConvergingErrorHandler": NonConvergingErrorHandler(),
"PositiveEnergyErrorHandler": PositiveEnergyErrorHandler(),
"PotimErrorHandler": PotimErrorHandler(),
"StdErrHandler": StdErrHandler(),
"UnconvergedErrorHandler": UnconvergedErrorHandler(),
"WalltimeHandler": WalltimeHandler(),
"KspacingMetalHandler": KspacingMetalHandler(),
}
validators_dict = {
"VaspFilesValidator": VaspFilesValidator(),
"VasprunXMLValidator": VasprunXMLValidator(),
}
handlers = []
for handler_flag in vasp_custodian_handlers:
if handler_flag not in handlers_dict:
msg = f"Unknown VASP error handler: {handler_flag}"
raise ValueError(msg)
handlers.append(handlers_dict[handler_flag])
validators = []
for validator_flag in vasp_custodian_validators:
if validator_flag not in validators_dict:
msg = f"Unknown VASP validator: {validator_flag}"
raise ValueError(msg)
validators.append(validators_dict[validator_flag])
# Populate settings
full_vasp_cmd = f"{vasp_parallel_cmd} {vasp_cmd}"
full_vasp_gamma_cmd = f"{vasp_parallel_cmd} {vasp_gamma_cmd}"
# Run VASP
vasp_job_kwargs = {} if vasp_job_kwargs is None else vasp_job_kwargs
custodian_kwargs = {} if custodian_kwargs is None else custodian_kwargs
split_vasp_cmd = shlex.split(full_vasp_cmd)
split_vasp_gamma_cmd = shlex.split(full_vasp_gamma_cmd)
vasp_job_kwargs["gamma_vasp_cmd"] = split_vasp_gamma_cmd
# Run with Custodian
jobs = [VaspJob(split_vasp_cmd, **vasp_job_kwargs)]
if vasp_custodian_wall_time:
handlers = [
*list(handlers),
WalltimeHandler(wall_time=vasp_custodian_wall_time),
]
c = Custodian(
handlers,
jobs,
validators=validators,
max_errors=vasp_custodian_max_errors,
scratch_dir=scratch_dir,
directory=directory,
**custodian_kwargs,
)
return c.run()
if __name__ == "__main__":
run_custodian()