-
Notifications
You must be signed in to change notification settings - Fork 41
/
ts.py
292 lines (243 loc) · 8.95 KB
/
ts.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
"""Transition state recipes for the NewtonNet code."""
from __future__ import annotations
from importlib.util import find_spec
from typing import TYPE_CHECKING
from monty.dev import requires
from quacc import SETTINGS, change_settings, job, strip_decorator
from quacc.recipes.newtonnet.core import _add_stdev_and_hess, freq_job, relax_job
from quacc.runners.ase import Runner
from quacc.schemas.ase import summarize_opt_run
from quacc.utils.dicts import recursive_dict_merge
has_sella = bool(find_spec("sella"))
has_newtonnet = bool(find_spec("newtonnet"))
if has_sella:
from sella import IRC, Sella
if has_newtonnet:
from newtonnet.utils.ase_interface import MLAseCalculator as NewtonNet
if TYPE_CHECKING:
from typing import Any, Literal
from ase.atoms import Atoms
from numpy.typing import NDArray
from quacc.recipes.newtonnet.core import FreqSchema
from quacc.runners.ase import OptParams
from quacc.schemas._aliases.ase import OptSchema
class TSSchema(OptSchema):
freq_job: FreqSchema | None
class IRCSchema(OptSchema):
freq_job: FreqSchema | None
class QuasiIRCSchema(OptSchema):
irc_job: IRCSchema
freq_job: FreqSchema | None
@job
@requires(
has_newtonnet, "NewtonNet must be installed. Refer to the quacc documentation."
)
@requires(has_sella, "Sella must be installed. Refer to the quacc documentation.")
def ts_job(
atoms: Atoms,
use_custom_hessian: bool = False,
run_freq: bool = True,
freq_job_kwargs: dict[str, Any] | None = None,
opt_params: OptParams | None = None,
**calc_kwargs,
) -> TSSchema:
"""
Perform a transition state (TS) job using the given atoms object.
Parameters
----------
atoms
The atoms object representing the system.
use_custom_hessian
Whether to use a custom Hessian matrix.
run_freq
Whether to run the frequency job.
freq_job_kwargs
Keyword arguments to use for the [quacc.recipes.newtonnet.ts.freq_job][]
opt_params
Dictionary of custom kwargs for the optimization process. For a list
of available keys, refer to [quacc.runners.ase.Runner.run_opt][].
**calc_kwargs
Dictionary of custom kwargs for the NewtonNet calculator. Set a value to
`quacc.Remove` to remove a pre-existing key entirely. For a list of available
keys, refer to the `newtonnet.utils.ase_interface.MLAseCalculator` calculator.
Returns
-------
TSSchema
Dictionary of results. See the type-hint for the data structure.
"""
freq_job_kwargs = freq_job_kwargs or {}
calc_defaults = {
"model_path": SETTINGS.NEWTONNET_MODEL_PATH,
"settings_path": SETTINGS.NEWTONNET_CONFIG_PATH,
"hess_method": "autograd",
}
opt_defaults = {
"optimizer": Sella,
"optimizer_kwargs": (
{"diag_every_n": 0, "order": 1} if use_custom_hessian else {"order": 1}
),
}
calc_flags = recursive_dict_merge(calc_defaults, calc_kwargs)
opt_flags = recursive_dict_merge(opt_defaults, opt_params)
if use_custom_hessian:
opt_flags["optimizer_kwargs"]["hessian_function"] = _get_hessian
calc = NewtonNet(**calc_flags)
# Run the TS optimization
dyn = Runner(atoms, calc).run_opt(**opt_flags)
opt_ts_summary = _add_stdev_and_hess(
summarize_opt_run(dyn, additional_fields={"name": "NewtonNet TS"})
)
# Run a frequency calculation
freq_summary = (
strip_decorator(freq_job)(opt_ts_summary["atoms"], **freq_job_kwargs)
if run_freq
else None
)
opt_ts_summary["freq_job"] = freq_summary
return opt_ts_summary
@job
@requires(
has_newtonnet, "NewtonNet must be installed. Refer to the quacc documentation."
)
@requires(has_sella, "Sella must be installed. Refer to the quacc documentation.")
def irc_job(
atoms: Atoms,
direction: Literal["forward", "reverse"] = "forward",
run_freq: bool = True,
freq_job_kwargs: dict[str, Any] | None = None,
opt_params: OptParams | None = None,
**calc_kwargs,
) -> IRCSchema:
"""
Perform an intrinsic reaction coordinate (IRC) job using the given atoms object.
Parameters
----------
atoms
The atoms object representing the system.
direction
The direction of the IRC calculation ("forward" or "reverse").
run_freq
Whether to run the frequency analysis.
freq_job_kwargs
Keyword arguments to use for the [quacc.recipes.newtonnet.ts.freq_job][]
opt_params
Dictionary of custom kwargs for the optimization process. For a list
of available keys, refer to [quacc.runners.ase.Runner.run_opt][].
**calc_kwargs
Custom kwargs for the NewtonNet calculator. Set a value to
`quacc.Remove` to remove a pre-existing key entirely. For a list of available
keys, refer to the `newtonnet.utils.ase_interface.MLAseCalculator` calculator.
Returns
-------
IRCSchema
A dictionary containing the IRC summary and thermodynamic summary.
See the type-hint for the data structure.
"""
freq_job_kwargs = freq_job_kwargs or {}
calc_defaults = {
"model_path": SETTINGS.NEWTONNET_MODEL_PATH,
"settings_path": SETTINGS.NEWTONNET_CONFIG_PATH,
}
opt_defaults = {
"optimizer": IRC,
"optimizer_kwargs": {"dx": 0.1, "eta": 1e-4, "gamma": 0.4, "keep_going": True},
"run_kwargs": {"direction": direction},
}
calc_flags = recursive_dict_merge(calc_defaults, calc_kwargs)
opt_flags = recursive_dict_merge(opt_defaults, opt_params)
# Define calculator
calc = NewtonNet(**calc_flags)
# Run IRC
with change_settings({"CHECK_CONVERGENCE": False}):
dyn = Runner(atoms, calc).run_opt(**opt_flags)
opt_irc_summary = _add_stdev_and_hess(
summarize_opt_run(
dyn, additional_fields={"name": f"NewtonNet IRC: {direction}"}
)
)
# Run frequency job
freq_summary = (
strip_decorator(freq_job)(opt_irc_summary["atoms"], **freq_job_kwargs)
if run_freq
else None
)
opt_irc_summary["freq_job"] = freq_summary
return opt_irc_summary
@job
@requires(
has_newtonnet, "NewtonNet must be installed. Refer to the quacc documentation."
)
@requires(has_sella, "Sella must be installed. Refer to the quacc documentation.")
def quasi_irc_job(
atoms: Atoms,
direction: Literal["forward", "reverse"] = "forward",
run_freq: bool = True,
irc_job_kwargs: dict[str, Any] | None = None,
relax_job_kwargs: dict[str, Any] | None = None,
freq_job_kwargs: dict[str, Any] | None = None,
) -> QuasiIRCSchema:
"""
Perform a quasi-IRC job using the given atoms object. The initial IRC job by default
is run with `max_steps: 5`.
Parameters
----------
atoms
The atoms object representing the system
direction
The direction of the IRC calculation
run_freq
Whether to run the frequency analysis
irc_job_kwargs
Keyword arguments to use for the [quacc.recipes.newtonnet.ts.irc_job][]
relax_job_kwargs
Keyword arguments to use for the [quacc.recipes.newtonnet.core.relax_job][]
freq_job_kwargs
Keyword arguments to use for the [quacc.recipes.newtonnet.ts.freq_job][]
Returns
-------
QuasiIRCSchema
A dictionary containing the IRC summary, optimization summary, and
thermodynamic summary.
See the type-hint for the data structure.
"""
relax_job_kwargs = relax_job_kwargs or {}
freq_job_kwargs = freq_job_kwargs or {}
irc_job_defaults = {"max_steps": 5}
irc_job_kwargs = recursive_dict_merge(irc_job_defaults, irc_job_kwargs)
# Run IRC
irc_summary = strip_decorator(irc_job)(
atoms, direction=direction, run_freq=False, **irc_job_kwargs
)
# Run opt
relax_summary = strip_decorator(relax_job)(irc_summary["atoms"], **relax_job_kwargs)
# Run frequency
freq_summary = (
strip_decorator(freq_job)(relax_summary["atoms"], **freq_job_kwargs)
if run_freq
else None
)
relax_summary["freq_job"] = freq_summary
relax_summary["irc_job"] = irc_summary
return relax_summary
def _get_hessian(atoms: Atoms) -> NDArray:
"""
Calculate and retrieve the Hessian matrix for the given molecular configuration.
This function takes an ASE Atoms object representing a molecular
configuration and uses the NewtonNet machine learning calculator to
calculate the Hessian matrix. The calculated Hessian matrix is then
returned.
Parameters
----------
atoms
The ASE Atoms object representing the molecular configuration.
Returns
-------
NDArray
The calculated Hessian matrix, reshaped into a 2D array.
"""
ml_calculator = NewtonNet(
model_path=SETTINGS.NEWTONNET_MODEL_PATH,
settings_path=SETTINGS.NEWTONNET_CONFIG_PATH,
)
ml_calculator.calculate(atoms)
return ml_calculator.results["hessian"].reshape((-1, 3 * len(atoms)))