-
Notifications
You must be signed in to change notification settings - Fork 41
/
settings.py
571 lines (503 loc) · 17.5 KB
/
settings.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""Settings for quacc."""
from __future__ import annotations
import os
from contextlib import contextmanager
from pathlib import Path
from shutil import which
from typing import TYPE_CHECKING, Literal, Optional, Union
import psutil
from maggma.core import Store
from pydantic import Field, field_validator, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
if TYPE_CHECKING:
from typing import Any
_DEFAULT_CONFIG_FILE_PATH = Path("~", ".quacc.yaml").expanduser().resolve()
class QuaccSettings(BaseSettings):
"""
Settings for quacc.
The default way to modify these is to make a ~/.quacc.yaml file. Alternatively, the
environment variable QUACC_CONFIG_FILE can be set to point to a custom yaml file
with quacc settings. The quacc CLI offers a `quacc set <setting> <value>` option to
do this as well.
The variables can also be modified individually though environment variables by
using the "QUACC" prefix. e.g. `export QUACC_SCRATCH_DIR=/path/to/scratch`.
"""
model_config = SettingsConfigDict(
env_prefix="quacc_",
env_nested_delimiter="__",
env_parse_none_str="None",
extra="forbid",
validate_assignment=True,
)
CONFIG_FILE: Path = Field(
_DEFAULT_CONFIG_FILE_PATH,
description=(
"""
Path to the YAML file to load alternative quacc configuration
defaults from.
"""
),
)
# --8<-- [start:settings]
# ---------------------------
# Workflow Engine
# ---------------------------
WORKFLOW_ENGINE: Optional[
Literal["covalent", "dask", "parsl", "prefect", "redun", "jobflow"]
] = Field(None, description=("The workflow manager to use, if any."))
# ---------------------------
# General Settings
# ---------------------------
RESULTS_DIR: Path = Field(
Path.cwd(),
description=(
"""
Directory to permanently store I/O-based calculation results in.
Note that this behavior may be modified by the chosen workflow engine.
"""
),
)
SCRATCH_DIR: Optional[Path] = Field(
None,
description=(
"""
The base directory where calculations are run. If set to None, calculations will be run in a
temporary directory within `RESULTS_DIR`. If a `Path` is supplied, calculations will
be run in a temporary directory within `SCRATCH_DIR`. Files are always moved back
to `RESULTS_DIR` after the calculation is complete, and the temporary directory
in `SCRATCH_DIR` is removed.
"""
),
)
CREATE_UNIQUE_DIR: bool = Field(
True,
description=(
"""
Whether to have a unique directory in RESULTS_DIR for each job.
Some workflow engines have an option to do this for you already,
in which case you should set this to False.
"""
),
)
GZIP_FILES: bool = Field(
True, description="Whether generated files should be gzip'd."
)
CHECK_CONVERGENCE: bool = Field(
True,
description="Whether to check for convergence, when implemented by a given recipe.",
)
# ---------------------------
# Data Store Settings
# ---------------------------
STORE: Optional[Union[dict[str, dict], Store]] = Field(
None,
description=(
"""
The desired Maggma data store where calculation results will be stored. All data stores listed in
`maggma.stores.__init__.py` are supported. If a dictionary is provided, the first key must be set
to the desired store type. The sub-parameters are the keyword arguments accepted by the Store.
An example is shown below:
```yaml
STORE:
MongoStore:
database: my_db
collection_name: my_collection
username: my_username
password: my_password
host: localhost
port: 27017
```
"""
),
)
# ---------------------------
# Prefect Settings
# ---------------------------
PREFECT_AUTO_SUBMIT: bool = Field(
True, description="Whether to auto-submit tasks to the task runner."
)
# ---------------------------
# ORCA Settings
# ---------------------------
ORCA_CMD: str = Field(
which("orca") or "orca",
description=(
"""
Path to the ORCA executable. This must be the full, absolute path
for parallel calculations to work.
"""
),
)
# ---------------------------
# ESPRESSO Settings
# ---------------------------
ESPRESSO_BIN_DIR: Path = Field(
Path(), description="Base path to the espresso binaries."
)
ESPRESSO_BINARIES: dict[str, str] = Field(
{
"pw": "pw.x",
"ph": "ph.x",
"neb": "neb.x",
"q2r": "q2r.x",
"dos": "dos.x",
"matdyn": "matdyn.x",
"dynmat": "dynmat.x",
"bands": "bands.x",
"projwfc": "projwfc.x",
"pp": "pp.x",
"wannier90": "wannier90.x",
"fs": "fs.x",
"postahc": "postahc.x",
"dvscf_q2r": "dvscf_q2r.x",
},
description="Name for each espresso binary.",
)
ESPRESSO_PARALLEL_CMD: Union[str, tuple[str, str]] = Field(
"",
description=(
"""
Parallelization flags to run Espresso. The 0th index of the tuple
contains any commands that come before the binary, and the 1st index
contains any commands that come after the binary.
For example: ["mpirun -np 4", "-nk 2"] would be the same as running
Espresso via `"mpirun -np 4 {binary} -nk 2"` where {binary} is the
binary that is automatically determined based on the job type,
`ESPRESSO_BIN_DIR` and `ESPRESSO_BINARIES`.
If only a `str` is provided, no post-binary commands will be used.
"""
),
)
ESPRESSO_PSEUDO: Optional[Path] = Field(
None, description=("Path to a pseudopotential library for espresso.")
)
ESPRESSO_PRESET_DIR: Path = Field(
Path(__file__).parent / "calculators" / "espresso" / "presets",
description="Path to the espresso preset directory",
)
# ---------------------------
# Gaussian Settings
# ---------------------------
GAUSSIAN_CMD: str = Field("g16", description=("Path to the Gaussian executable."))
# ---------------------------
# ONETEP Settings
# ---------------------------
ONETEP_CMD: str = Field(
"onetep.arch",
description=("Full ONETEP command, including parallelization flags."),
)
ONETEP_PP_PATH: Path = Field(Path(), description=("Path to pseudopotentials."))
# ---------------------------
# GULP Settings
# ---------------------------
GULP_CMD: str = Field("gulp", description=("Path to the GULP executable."))
GULP_LIB: Optional[Path] = Field(
None,
description=(
"Path to the GULP force field library. If not specified, the GULP_LIB environment variable will be used (if present)."
),
)
# ---------------------------
# VASP Settings
# ---------------------------
# VASP Settings: Main
VASP_PARALLEL_CMD: str = Field(
"",
description=(
"""
Parallel command to run VASP with Custodian.
For example: `"srun -N 2 --ntasks-per-node 48"`.
Note that this does not include the executable name.
"""
),
)
VASP_CMD: str = Field(
"vasp_std", description="Command to run the standard version of VASP."
)
VASP_GAMMA_CMD: str = Field(
"vasp_gam", description="Command to run the gamma-point only version of VASP."
)
VASP_PP_PATH: Optional[Path] = Field(
None,
description="Path to the VASP pseudopotential library. Must contain the directories `potpaw_PBE` and `potpaw` for PBE and LDA pseudopotentials, respectively.",
)
VASP_VDW: Optional[Path] = Field(
None, description="Path to the vdw_kernel.bindat file for VASP vdW functionals."
)
# VASP Settings: General
VASP_INCAR_COPILOT: Literal["off", "on", "aggressive"] = Field(
"on",
description=(
"""
Controls VASP co-pilot mode for automated INCAR parameter handling.
off: Do not use co-pilot mode. INCAR parameters will be unmodified.
on: Use co-pilot mode. This will only modify INCAR flags not already set by the user.
aggressive: Use co-pilot mode in aggressive mode. This will modify INCAR flags even if they are already set by the user.
"""
),
)
VASP_BADER: bool = Field(
bool(which("bader")),
description=(
""""
Whether to run a Bader analysis when summarizing VASP results.
Requires bader to be in PATH.
"""
),
)
VASP_CHARGEMOL: bool = Field(
bool(os.environ.get("DDEC6_ATOMIC_DENSITIES_DIR")),
description=(
"""
Whether to run a Chargemol (i.e. DDEC6, CM5) analysis when summarizing VASP results.
Requires the Chargemol executable to be in PATH and the DDEC6_ATOMIC_DENSITIES_DIR environment variable.
"""
),
)
VASP_PRESET_MAG_DEFAULT: float = Field(
1.0,
description=(
"""
Default initial magmom to use for a given element if a preset
with magmoms is provided but an element is missing from the list.
"""
),
)
VASP_MAG_CUTOFF: float = Field(
0.02,
description=(
"""
If the absolute value of all magnetic moments are below this value,
they will be set to 0 such that a spin-unpolarized calculation will be performed.
"""
),
)
VASP_COPY_MAGMOMS: bool = Field(
True,
description=(
"""
If True, any pre-existing atoms.get_magnetic_moments() will be set
in atoms.set_initial_magnetic_moments().
"""
),
)
VASP_PRESET_DIR: Path = Field(
Path(__file__).parent / "calculators" / "vasp" / "presets",
description="Path to the VASP preset directory",
)
# VASP Settings: Custodian
VASP_USE_CUSTODIAN: bool = Field(
True, description="Whether Custodian should be used to run VASP"
)
VASP_CUSTODIAN_VTST: bool = Field(
False,
description=(
"""
If VTST-related input swaps should be used when running Custodian.
Requires VASP to be compiled with VTST
"""
),
)
VASP_CUSTODIAN_MAX_ERRORS: int = Field(
5, description="Maximum errors for Custodian"
)
VASP_CUSTODIAN_HANDLERS: list[str] = Field(
[
"VaspErrorHandler",
"MeshSymmetryErrorHandler",
"UnconvergedErrorHandler",
"NonConvergingErrorHandler",
"PotimErrorHandler",
"PositiveEnergyErrorHandler",
"FrozenJobErrorHandler",
"StdErrHandler",
"LargeSigmaHandler",
"IncorrectSmearingHandler",
],
description="Handlers for Custodian",
)
VASP_CUSTODIAN_VALIDATORS: list[str] = Field(
["VasprunXMLValidator", "VaspFilesValidator"],
description="Validators for Custodian",
)
VASP_CUSTODIAN_WALL_TIME: Optional[int] = Field(
None,
description=(
"""
After this many seconds, Custodian will stop running
and ensure that VASP writes a STOPCAR
"""
),
)
# ---------------------------
# Q-Chem Settings
# ---------------------------
# Q-Chem Settings: Main
QCHEM_CMD: str = Field(
"qchem", description="Command to run the standard version of Q-Chem."
)
QCHEM_LOCAL_SCRATCH: Path = Field(
Path("/tmp") if Path("/tmp").exists() else Path.cwd() / ".qchem_scratch",
description="Compute-node local scratch directory in which Q-Chem should perform IO.",
)
QCHEM_NUM_CORES: int = Field(
psutil.cpu_count(logical=False),
description="Number of cores to use for the Q-Chem calculation.",
)
# Q-Chem Settings: Custodian
QCHEM_USE_ERROR_HANDLERS: bool = Field(
True,
description="Whether Custodian's error handlers should be employed for Q-Chem.",
)
QCHEM_CUSTODIAN_MAX_ERRORS: int = Field(
5, description="Maximum errors for Q-Chem Custodian."
)
# NBO Settings
QCHEM_NBO_EXE: Optional[Path] = Field(
None, description="Full path to the NBO executable."
)
# ---------------------------
# NewtonNet Settings
# ---------------------------
NEWTONNET_MODEL_PATH: Union[Path, list[Path]] = Field(
"best_model_state.tar", description="Path to NewtonNet .tar model"
)
NEWTONNET_CONFIG_PATH: Union[Path, list[Path]] = Field(
"config.yml", description="Path to NewtonNet YAML settings file"
)
# ---------------------------
# Debug Settings
# ---------------------------
DEBUG: bool = Field(
False,
description=(
"""
Whether to run in debug mode. This will set the logging level to DEBUG,
ASE logs (e.g. optimizations, vibrations, thermo) are printed to stdout.
"""
),
)
# --8<-- [end:settings]
@field_validator(
"RESULTS_DIR",
"SCRATCH_DIR",
"ESPRESSO_PRESET_DIR",
"ESPRESSO_PSEUDO",
"GULP_LIB",
"QCHEM_LOCAL_SCRATCH",
"NEWTONNET_MODEL_PATH",
"VASP_PRESET_DIR",
"VASP_PP_PATH",
"VASP_VDW",
)
@classmethod
def expand_paths(cls, v: Optional[Path]) -> Optional[Path]:
"""Expand ~/ and $ENV_VARS in paths."""
if v:
v = Path(os.path.expandvars(v)).expanduser()
return v
@field_validator("RESULTS_DIR", "SCRATCH_DIR")
@classmethod
def make_directories(cls, v: Optional[Path]) -> Optional[Path]:
"""Make directories."""
if v:
v.mkdir(exist_ok=True, parents=True)
return v
@field_validator("STORE")
@classmethod
def generate_store(cls, v: Union[dict[str, dict[str, Any]], Store]) -> Store:
"""Generate the Maggma store."""
from maggma import stores
if isinstance(v, dict):
store_name = next(iter(v.keys()))
store = getattr(stores, store_name)
return store(**v[store_name])
else:
return v
@field_validator("ESPRESSO_PARALLEL_CMD")
@classmethod
def validate_espresso_parallel_cmd(
cls, v: Union[str, tuple[str, str]]
) -> tuple[str, str]:
"""Clean up Espresso parallel command."""
parsl_mpi_prefix = os.environ.get("PARSL_MPI_PREFIX")
if isinstance(v, str):
v = (v, "")
if parsl_mpi_prefix:
v = (parsl_mpi_prefix, v[1])
return v
@model_validator(mode="before")
@classmethod
def load_user_settings(cls, settings: dict[str, Any]) -> dict[str, Any]:
"""
Loads settings from a root file if available and uses that as defaults in place
of built in defaults. Will also convert common strings to their proper types.
Parameters
----------
settings
Settings to load.
Returns
-------
dict
Loaded settings.
"""
return _type_handler(_use_custom_config_settings(settings))
def _use_custom_config_settings(settings: dict[str, Any]) -> dict[str, Any]:
"""Parse user settings from a custom YAML.
Parameters
----------
settings : dict
Initial settings.
Returns
-------
dict
Updated settings based on the custom YAML.
"""
from monty.serialization import loadfn
config_file_path = (
Path(settings.get("CONFIG_FILE", _DEFAULT_CONFIG_FILE_PATH))
.expanduser()
.resolve()
)
new_settings = {} # type: dict
if config_file_path.exists() and config_file_path.stat().st_size > 0:
new_settings |= loadfn(config_file_path)
new_settings.update(settings)
return new_settings
def _type_handler(settings: dict[str, Any]) -> dict[str, Any]:
"""
Convert common strings to their proper types.
Parameters
----------
settings
Initial settings.
Returns
-------
dict
Updated settings.
"""
for key, value in settings.items():
if isinstance(value, str):
if value.lower() in {"null", "none"}:
settings[key] = None
elif value.lower() in {"true", "false"}:
settings[key] = value.lower() == "true"
return settings
@contextmanager
def change_settings(changes: dict[str, Any]):
"""
Temporarily change an attribute of an object.
Parameters
----------
changes
Dictionary of changes to make formatted as attribute: value.
"""
from quacc import SETTINGS
original_values = {attr: getattr(SETTINGS, attr) for attr in changes}
for attr, new_value in changes.items():
setattr(SETTINGS, attr, new_value)
try:
yield
finally:
for attr, original_value in original_values.items():
setattr(SETTINGS, attr, original_value)