-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathbase.py
293 lines (251 loc) · 8.87 KB
/
base.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
"""Definition of base VASP job maker."""
from __future__ import annotations
import warnings
from dataclasses import dataclass, field
from pathlib import Path
from shutil import which
from typing import TYPE_CHECKING
from emmet.core.tasks import TaskDoc
from jobflow import Maker, Response, job
from monty.serialization import dumpfn
from pymatgen.core.trajectory import Trajectory
from pymatgen.electronic_structure.bandstructure import (
BandStructure,
BandStructureSymmLine,
)
from pymatgen.electronic_structure.dos import DOS, CompleteDos, Dos
from pymatgen.io.vasp import Chgcar, Locpot, Wavecar
from atomate2 import SETTINGS
from atomate2.common.files import gzip_output_folder
from atomate2.vasp.files import copy_vasp_outputs, write_vasp_input_set
from atomate2.vasp.run import run_vasp, should_stop_children
from atomate2.vasp.sets.base import VaspInputGenerator
if TYPE_CHECKING:
from collections.abc import Callable
from pymatgen.core import Structure
_BADER_EXE_EXISTS = bool(which("bader") or which("bader.exe"))
_CHARGEMOL_EXE_EXISTS = bool(
which("Chargemol_09_26_2017_linux_parallel")
or which("Chargemol_09_26_2017_linux_serial")
or which("chargemol")
)
_DATA_OBJECTS = [
BandStructure,
BandStructureSymmLine,
DOS,
Dos,
CompleteDos,
Locpot,
Chgcar,
Wavecar,
Trajectory,
"force_constants",
"normalmode_eigenvecs",
"bandstructure", # FIX: BandStructure is not currently MSONable
]
# Input files. Partially from https://www.vasp.at/wiki/index.php/Category:Input_files
# Exclude those that are also outputs
_INPUT_FILES = [
"DYNMATFULL",
"ICONST",
"INCAR",
"KPOINTS",
"KPOINTS OPT",
"ML_AB",
"ML_FF",
"PENALTYPOT",
"POSCAR",
"POTCAR",
"QPOINTS",
]
# Output files. Partially from https://www.vasp.at/wiki/index.php/Category:Output_files
_OUTPUT_FILES = [
"AECCAR0",
"AECCAR1",
"AECCAR2",
"BSEFATBAND",
"CHG",
"CHGCAR",
"CONTCAR",
"DOSCAR",
"EIGENVAL",
"ELFCAR",
"HILLSPOT",
"IBZKPT",
"LOCPOT",
"ML_ABN",
"ML_FFN",
"ML_HIS",
"ML_LOGFILE",
"ML_REG",
"OSZICAR",
"OUTCAR",
"PARCHG",
"PCDAT",
"POT",
"PROCAR",
"PROOUT",
"REPORT",
"TMPCAR",
"vasprun.xml",
"vaspout.h5",
"vaspwave.h5",
"W*.tmp",
"WAVECAR",
"WAVEDER",
"WFULL*.tmp",
"XDATCAR",
]
# Files to zip: inputs, outputs and additionally generated files
_FILES_TO_ZIP = (
_INPUT_FILES
+ _OUTPUT_FILES
+ [f"{name}.orig" for name in _INPUT_FILES]
+ ["vasp.out", "custodian.json"]
)
def vasp_job(method: Callable) -> job:
"""
Decorate the ``make`` method of VASP job makers.
This is a thin wrapper around :obj:`~jobflow.core.job.Job` that configures common
settings for all VASP jobs. For example, it ensures that large data objects
(band structures, density of states, LOCPOT, CHGCAR, etc) are all stored in the
atomate2 data store. It also configures the output schema to be a VASP
:obj:`.TaskDoc`.
Any makers that return VASP jobs (not flows) should decorate the ``make`` method
with @vasp_job. For example:
.. code-block:: python
class MyVaspMaker(BaseVaspMaker):
@vasp_job
def make(structure):
# code to run VASP job.
pass
Parameters
----------
method : callable
A BaseVaspMaker.make method. This should not be specified directly and is
implied by the decorator.
Returns
-------
callable
A decorated version of the make function that will generate VASP jobs.
"""
return job(method, data=_DATA_OBJECTS, output_schema=TaskDoc)
@dataclass
class BaseVaspMaker(Maker):
"""
Base VASP job maker.
Parameters
----------
name : str
The job name.
input_set_generator : .VaspInputGenerator
A generator used to make the input set.
write_input_set_kwargs : dict
Keyword arguments that will get passed to :obj:`.write_vasp_input_set`.
copy_vasp_kwargs : dict
Keyword arguments that will get passed to :obj:`.copy_vasp_outputs`.
run_vasp_kwargs : dict
Keyword arguments that will get passed to :obj:`.run_vasp`.
task_document_kwargs : dict
Keyword arguments that will get passed to :obj:`.TaskDoc.from_directory`.
stop_children_kwargs : dict
Keyword arguments that will get passed to :obj:`.should_stop_children`.
write_additional_data : dict
Additional data to write to the current directory. Given as a dict of
{filename: data}. Note that if using FireWorks, dictionary keys cannot contain
the "." character which is typically used to denote file extensions. To avoid
this, use the ":" character, which will automatically be converted to ".". E.g.
``{"my_file:txt": "contents of the file"}``.
"""
name: str = "base vasp job"
input_set_generator: VaspInputGenerator = field(default_factory=VaspInputGenerator)
write_input_set_kwargs: dict = field(default_factory=dict)
copy_vasp_kwargs: dict = field(default_factory=dict)
run_vasp_kwargs: dict = field(default_factory=dict)
task_document_kwargs: dict = field(default_factory=dict)
stop_children_kwargs: dict = field(default_factory=dict)
write_additional_data: dict = field(default_factory=dict)
@vasp_job
def make(
self, structure: Structure, prev_dir: str | Path | None = None
) -> Response:
"""Run a VASP calculation.
Parameters
----------
structure : Structure
A pymatgen structure object.
prev_dir : str or Path or None
A previous VASP calculation directory to copy output files from.
Returns
-------
Response: A response object containing the output, detours and stop
commands of the VASP run.
"""
# copy previous inputs
from_prev = prev_dir is not None
if prev_dir is not None:
copy_vasp_outputs(prev_dir, **self.copy_vasp_kwargs)
self.write_input_set_kwargs.setdefault("from_prev", from_prev)
# write vasp input files
write_vasp_input_set(
structure, self.input_set_generator, **self.write_input_set_kwargs
)
# write any additional data
for filename, data in self.write_additional_data.items():
dumpfn(data, filename.replace(":", "."))
# run vasp
run_vasp(**self.run_vasp_kwargs)
# parse vasp outputs
task_doc = get_vasp_task_document(Path.cwd(), **self.task_document_kwargs)
task_doc.task_label = self.name
# decide whether child jobs should proceed
stop_children = should_stop_children(task_doc, **self.stop_children_kwargs)
# gzip folder
gzip_output_folder(
directory=Path.cwd(),
setting=SETTINGS.VASP_ZIP_FILES,
files_list=_FILES_TO_ZIP,
)
return Response(
stop_children=stop_children,
stored_data={"custodian": task_doc.custodian},
output=task_doc,
)
def get_vasp_task_document(path: Path | str, **kwargs) -> TaskDoc:
"""Get VASP Task Document using atomate2 settings."""
kwargs.setdefault("store_additional_json", SETTINGS.VASP_STORE_ADDITIONAL_JSON)
kwargs.setdefault(
"volume_change_warning_tol", SETTINGS.VASP_VOLUME_CHANGE_WARNING_TOL
)
if SETTINGS.VASP_RUN_BADER:
kwargs.setdefault("run_bader", _BADER_EXE_EXISTS)
if not _BADER_EXE_EXISTS:
warnings.warn(
f"{SETTINGS.VASP_RUN_BADER=} but bader executable not found on path",
stacklevel=1,
)
if SETTINGS.VASP_RUN_DDEC6:
# if VASP_RUN_DDEC6 is True but _CHARGEMOL_EXE_EXISTS is False, just silently
# skip running DDEC6
run_ddec6: bool | str = _CHARGEMOL_EXE_EXISTS
if run_ddec6 and isinstance(SETTINGS.DDEC6_ATOMIC_DENSITIES_DIR, str):
# if DDEC6_ATOMIC_DENSITIES_DIR is a string and directory at that path
# exists, use as path to the atomic densities
if Path(SETTINGS.DDEC6_ATOMIC_DENSITIES_DIR).is_dir():
run_ddec6 = SETTINGS.DDEC6_ATOMIC_DENSITIES_DIR
else:
# if the directory doesn't exist, warn the user and skip running DDEC6
warnings.warn(
f"{SETTINGS.DDEC6_ATOMIC_DENSITIES_DIR=} does not exist, skipping "
"DDEC6",
stacklevel=1,
)
kwargs.setdefault("run_ddec6", run_ddec6)
if not _CHARGEMOL_EXE_EXISTS:
warnings.warn(
f"{SETTINGS.VASP_RUN_DDEC6=} but chargemol executable not found on "
"path",
stacklevel=1,
)
kwargs.setdefault("store_volumetric_data", SETTINGS.VASP_STORE_VOLUMETRIC_DATA)
return TaskDoc.from_directory(path, **kwargs)