-
Notifications
You must be signed in to change notification settings - Fork 19
/
qcschema.py
266 lines (226 loc) · 9.71 KB
/
qcschema.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
# This file is part of s-dftd3.
# SPDX-Identifier: LGPL-3.0-or-later
#
# s-dftd3 is free software: you can redistribute it and/or modify it under
# the terms of the Lesser GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# s-dftd3 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Lesser GNU General Public License for more details.
#
# You should have received a copy of the Lesser GNU General Public License
# along with s-dftd3. If not, see <https://www.gnu.org/licenses/>.
"""
QCSchema Support
================
Integration with the `QCArchive infrastructure <http://docs.qcarchive.molssi.org>`_.
This module provides a way to translate QCSchema or QCElemental Atomic Input
into a format understandable by the ``dftd3`` API which in turn provides the
calculation results in a QCSchema compatible format.
Supported keywords are
======================== =========== ============================================
Keyword Default Description
======================== =========== ============================================
level_hint None Dispersion correction level
params_tweaks None Optional dict with the damping parameters
pair_resolved False Enable pairwise resolved dispersion energy
======================== =========== ============================================
Allowed level hints are ``"d3bj"``, ``"d3zero"``, ``"d3bjm"``/``"d3mbj"``,
``"d3mzero"``/``"d3zerom"``, and ``"d3op"``.
The params_tweaks dict contains the damping parameters, at least s8, a1 and a2
must be provided for rational damping, while s8 and rs6 are required in case
of zero damping.
Parameters for (modified) rational damping are:
======================== =========== ============================================
Tweakable parameter Default Description
======================== =========== ============================================
s6 1.0 Scaling of the dipole-dipole dispersion
s8 None Scaling of the dipole-quadrupole dispersion
s9 1.0 Scaling of the three-body dispersion energy
a1 None Scaling of the critical radii
a2 None Offset of the critical radii
alp 14.0 Exponent of the zero damping (ATM only)
======================== =========== ============================================
Parameters for (modified) zero damping are:
======================== =========== ===================================================
Tweakable parameter Default Description
======================== =========== ===================================================
s6 1.0 Scaling of the dipole-dipole dispersion
s8 None Scaling of the dipole-quadrupole dispersion
s9 1.0 Scaling of the three-body dispersion energy
rs6 None Scaling of the dipole-dipole damping
rs8 1.0 Scaling of the dipole-quadrupole damping
alp 14.0 Exponent of the zero damping
bet None Offset for damping radius (modified zero damping)
======================== =========== ===================================================
Parameters for optimized power damping are:
======================== =========== ============================================
Tweakable parameter Default Description
======================== =========== ============================================
s6 1.0 Scaling of the dipole-dipole dispersion
s8 None Scaling of the dipole-quadrupole dispersion
s9 1.0 Scaling of the three-body dispersion energy
a1 None Scaling of the critical radii
a2 None Offset of the critical radii
alp 14.0 Exponent of the zero damping (ATM only)
bet None Power for the zero-damping component
======================== =========== ============================================
.. note::
input_data.model.method with a full method name and input_data.keywords["params_tweaks"]
cannot be provided at the same time. It is an error to provide both options at the
same time.
Example
-------
>>> from dftd3.qcschema import run_qcschema
>>> import qcelemental as qcel
>>> atomic_input = qcel.models.AtomicInput(
... molecule = qcel.models.Molecule(
... symbols = ["O", "H", "H"],
... geometry = [
... 0.00000000000000, 0.00000000000000, -0.73578586109551,
... 1.44183152868459, 0.00000000000000, 0.36789293054775,
... -1.44183152868459, 0.00000000000000, 0.36789293054775
... ],
... ),
... driver = "energy",
... model = {
... "method": "TPSS-D3(BJ)",
... },
... keywords = {},
... )
...
>>> atomic_result = run_qcschema(atomic_input)
>>> atomic_result.return_result
-0.00042042440936212056
"""
from typing import Union
from .interface import (
DispersionModel,
RationalDampingParam,
ZeroDampingParam,
ModifiedRationalDampingParam,
ModifiedZeroDampingParam,
OptimizedPowerDampingParam,
)
from .library import get_api_version
import numpy as np
import qcelemental as qcel
_supported_drivers = [
"energy",
"gradient",
]
_available_levels = [
"d3bj",
"d3zero",
"d3bjm",
"d3mbj",
"d3zerom",
"d3mzero",
"d3op",
]
_damping_param = {
"d3bj": RationalDampingParam,
"d3zero": ZeroDampingParam,
"d3bjm": ModifiedRationalDampingParam,
"d3mbj": ModifiedRationalDampingParam,
"d3zerom": ModifiedZeroDampingParam,
"d3mzero": ModifiedZeroDampingParam,
"d3op": OptimizedPowerDampingParam,
}
_clean_dashlevel = str.maketrans("", "", "()")
def run_qcschema(
input_data: Union[dict, qcel.models.AtomicInput]
) -> qcel.models.AtomicResult:
"""Perform disperson correction based on an atomic inputmodel"""
if not isinstance(input_data, qcel.models.AtomicInput):
atomic_input = qcel.models.AtomicInput(**input_data)
else:
atomic_input = input_data
ret_data = atomic_input.dict()
provenance = {
"creator": "s-dftd3",
"version": get_api_version(),
"routine": "dftd3.qcschema.run_qcschema",
}
success = False
return_result = 0.0
properties = {}
# Since it is a level hint we a forgiving if it is not present,
# we are much less forgiving if the wrong level is hinted here.
_level = atomic_input.keywords.get("level_hint", "d3bj")
if _level.lower() not in _available_levels:
ret_data.update(
provenance=provenance,
success=success,
properties=properties,
return_result=return_result,
error=qcel.models.ComputeError(
error_type="input error",
error_message="Level '{}' is invalid for this dispersion correction".format(
_level
),
),
)
return qcel.models.AtomicResult(**ret_data)
# Check if the method is provided and strip the “dashlevel” from the method
_method = atomic_input.model.method.split("-")
if _method[-1].lower().translate(_clean_dashlevel) == _level.lower():
_method.pop()
_method = "-".join(_method)
if len(_method) == 0:
_method = None
# Obtain the parameters for the damping function
_input_param = atomic_input.keywords.get("params_tweaks", {"method": _method})
try:
param = _damping_param[_level](
**_input_param,
)
disp = DispersionModel(
atomic_input.molecule.atomic_numbers[atomic_input.molecule.real],
atomic_input.molecule.geometry[atomic_input.molecule.real],
)
res = disp.get_dispersion(
param=param,
grad=atomic_input.driver == "gradient",
)
extras = {"dftd3": res}
if atomic_input.driver == "gradient":
if all(atomic_input.molecule.real):
fullgrad = res.get("gradient")
else:
ireal = np.argwhere(atomic_input.molecule.real).reshape((-1))
fullgrad = np.zeros_like(atomic_input.molecule.geometry)
fullgrad[ireal, :] = res.get("gradient")
properties.update(return_energy=res.get("energy"))
if atomic_input.keywords.get("pair_resolved", False):
res = disp.get_pairwise_dispersion(param=param)
extras["dftd3"].update(res)
success = atomic_input.driver in _supported_drivers
if atomic_input.driver == "energy":
return_result = properties["return_energy"]
elif atomic_input.driver == "gradient":
return_result = fullgrad
else:
ret_data.update(
error=qcel.models.ComputeError(
error_type="input error",
error_message="Calculation succeeded but invalid driver request provided",
),
)
ret_data["extras"].update(extras)
except (RuntimeError, TypeError) as e:
ret_data.update(
error=qcel.models.ComputeError(
error_type="input error", error_message=str(e)
),
),
ret_data.update(
provenance=provenance,
success=success,
properties=properties,
return_result=return_result,
)
return qcel.models.AtomicResult(**ret_data)