|
| 1 | +# This file is part of s-dftd3. |
| 2 | +# SPDX-Identifier: LGPL-3.0-or-later |
| 3 | +# |
| 4 | +# s-dftd3 is free software: you can redistribute it and/or modify it under |
| 5 | +# the terms of the Lesser GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# s-dftd3 is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# Lesser GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the Lesser GNU General Public License |
| 15 | +# along with s-dftd3. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +"""Integration with the `QCArchive infrastructure <http://docs.qcarchive.molssi.org>`_. |
| 17 | +
|
| 18 | +This module provides a way to translate QCSchema or QCElemental Atomic Input |
| 19 | +into a format understandable by the ``dftd3`` API which in turn provides the |
| 20 | +calculation results in a QCSchema compatible format. |
| 21 | +
|
| 22 | +Supported keywords are |
| 23 | +
|
| 24 | +======================== =========== ============================================ |
| 25 | + Keyword Default Description |
| 26 | +======================== =========== ============================================ |
| 27 | + level_hint None Dispersion correction level (allowed: "d4") |
| 28 | + params_tweaks None Optional dict with the damping parameters |
| 29 | +======================== =========== ============================================ |
| 30 | +
|
| 31 | +The params_tweaks dict contains the damping parameters, at least s8, a1 and a2 |
| 32 | +must be provided |
| 33 | +
|
| 34 | +======================== =========== ============================================ |
| 35 | + Tweakable parameter Default Description |
| 36 | +======================== =========== ============================================ |
| 37 | + s6 1.0 Scaling of the dipole-dipole dispersion |
| 38 | + s8 None Scaling of the dipole-quadrupole dispersion |
| 39 | + s9 1.0 Scaling of the three-body dispersion energy |
| 40 | + a1 None Scaling of the critical radii |
| 41 | + a2 None Offset of the critical radii |
| 42 | + alp 16.0 Exponent of the zero damping (ATM only) |
| 43 | +======================== =========== ============================================ |
| 44 | +
|
| 45 | +Either method or s8, a1 and a2 must be provided, s9 can be used to overwrite |
| 46 | +the ATM scaling if the method is provided in the model. |
| 47 | +Disabling the three-body dispersion (s9=0.0) changes the internal selection rules |
| 48 | +for damping parameters of a given method and prefers special two-body only |
| 49 | +damping parameters if available! |
| 50 | +If input_data.model.method and input_data.keywords["params_tweaks"] are both |
| 51 | +provided, the former "wins" without any consistency checking. Note that with the |
| 52 | +QCEngine QCSchema runner for classic ``dftd3``, the latter "wins" without any |
| 53 | +consistency checking. |
| 54 | +
|
| 55 | +Example |
| 56 | +------- |
| 57 | +
|
| 58 | +>>> from dftd3.qcschema import run_qcschema |
| 59 | +>>> import qcelemental as qcel |
| 60 | +>>> atomic_input = qcel.models.AtomicInput( |
| 61 | +... molecule = qcel.models.Molecule( |
| 62 | +... symbols = ["O", "H", "H"], |
| 63 | +... geometry = [ |
| 64 | +... 0.00000000000000, 0.00000000000000, -0.73578586109551, |
| 65 | +... 1.44183152868459, 0.00000000000000, 0.36789293054775, |
| 66 | +... -1.44183152868459, 0.00000000000000, 0.36789293054775 |
| 67 | +... ], |
| 68 | +... ), |
| 69 | +... driver = "energy", |
| 70 | +... model = { |
| 71 | +... "method": "TPSS-D3(BJ)", |
| 72 | +... }, |
| 73 | +... keywords = {}, |
| 74 | +... ) |
| 75 | +... |
| 76 | +>>> atomic_result = run_qcschema(atomic_input) |
| 77 | +>>> atomic_result.return_result |
| 78 | +-0.0002667885779142513 |
| 79 | +""" |
| 80 | + |
| 81 | +from typing import Union |
| 82 | +from .interface import ( |
| 83 | + DispersionModel, |
| 84 | + RationalDampingParam, |
| 85 | + ZeroDampingParam, |
| 86 | + ModifiedRationalDampingParam, |
| 87 | + ModifiedZeroDampingParam, |
| 88 | +) |
| 89 | +from .libdftd3 import get_api_version |
| 90 | +import numpy as np |
| 91 | +import qcelemental as qcel |
| 92 | + |
| 93 | + |
| 94 | +_supported_drivers = [ |
| 95 | + "energy", |
| 96 | + "gradient", |
| 97 | +] |
| 98 | + |
| 99 | +_available_levels = [ |
| 100 | + "d3bj", |
| 101 | + "d3zero", |
| 102 | + "d3bjm", |
| 103 | + "d3zerom", |
| 104 | +] |
| 105 | + |
| 106 | +_damping_param = { |
| 107 | + "d3bj": RationalDampingParam, |
| 108 | + "d3zero": ZeroDampingParam, |
| 109 | + "d3bjm": ModifiedRationalDampingParam, |
| 110 | + "d3zerom": ModifiedZeroDampingParam, |
| 111 | +} |
| 112 | + |
| 113 | +_clean_dashlevel = str.maketrans("", "", "()") |
| 114 | + |
| 115 | + |
| 116 | +def run_qcschema( |
| 117 | + input_data: Union[dict, qcel.models.AtomicInput] |
| 118 | +) -> qcel.models.AtomicResult: |
| 119 | + """Perform disperson correction based on an atomic inputmodel""" |
| 120 | + |
| 121 | + if not isinstance(input_data, qcel.models.AtomicInput): |
| 122 | + atomic_input = qcel.models.AtomicInput(**input_data) |
| 123 | + else: |
| 124 | + atomic_input = input_data |
| 125 | + ret_data = atomic_input.dict() |
| 126 | + |
| 127 | + provenance = { |
| 128 | + "creator": "s-dftd3", |
| 129 | + "version": get_api_version(), |
| 130 | + "routine": "dftd4.qcschema.run_qcschema", |
| 131 | + } |
| 132 | + success = False |
| 133 | + return_result = 0.0 |
| 134 | + properties = {} |
| 135 | + |
| 136 | + # Since it is a level hint we a forgiving if it is not present, |
| 137 | + # we are much less forgiving if the wrong level is hinted here. |
| 138 | + _level = atomic_input.keywords.get("level_hint", "d3bj") |
| 139 | + if _level.lower() not in _available_levels: |
| 140 | + ret_data.update( |
| 141 | + provenance=provenance, |
| 142 | + success=success, |
| 143 | + properties=properties, |
| 144 | + return_result=return_result, |
| 145 | + error=qcel.models.ComputeError( |
| 146 | + error_type="input error", |
| 147 | + error_message="Level '{}' is invalid for this dispersion correction".format( |
| 148 | + _level |
| 149 | + ), |
| 150 | + ), |
| 151 | + ) |
| 152 | + return qcel.models.AtomicResult(**ret_data) |
| 153 | + |
| 154 | + # Check if the method is provided and strip the “dashlevel” from the method |
| 155 | + _method = atomic_input.model.method |
| 156 | + if len(_method) == 0: |
| 157 | + _method = None |
| 158 | + else: |
| 159 | + _method = _method.split("-") |
| 160 | + if _method[-1].lower().translate(_clean_dashlevel) == _level.lower(): |
| 161 | + _method.pop() |
| 162 | + _method = "-".join(_method) |
| 163 | + |
| 164 | + # Obtain the parameters for the damping function |
| 165 | + _input_param = atomic_input.keywords.get("params_tweaks", {}) |
| 166 | + |
| 167 | + try: |
| 168 | + param = _damping_param[_level]( |
| 169 | + method=_method, |
| 170 | + **_input_param, |
| 171 | + ) |
| 172 | + |
| 173 | + disp = DispersionModel( |
| 174 | + atomic_input.molecule.atomic_numbers[atomic_input.molecule.real], |
| 175 | + atomic_input.molecule.geometry[atomic_input.molecule.real], |
| 176 | + ) |
| 177 | + |
| 178 | + res = disp.get_dispersion( |
| 179 | + param=param, |
| 180 | + grad=atomic_input.driver == "gradient", |
| 181 | + ) |
| 182 | + extras = {"dftd3": res} |
| 183 | + |
| 184 | + if atomic_input.driver == "gradient": |
| 185 | + if all(atomic_input.molecule.real): |
| 186 | + fullgrad = res.get("gradient") |
| 187 | + else: |
| 188 | + ireal = np.argwhere(atomic_input.molecule.real).reshape((-1)) |
| 189 | + fullgrad = np.zeros_like(atomic_input.molecule.geometry) |
| 190 | + fullgrad[ireal, :] = res.get("gradient") |
| 191 | + |
| 192 | + properties.update(return_energy=res.get("energy")) |
| 193 | + |
| 194 | + success = atomic_input.driver in _supported_drivers |
| 195 | + if atomic_input.driver == "energy": |
| 196 | + return_result = properties["return_energy"] |
| 197 | + elif atomic_input.driver == "gradient": |
| 198 | + return_result = fullgrad |
| 199 | + else: |
| 200 | + ret_data.update( |
| 201 | + error=qcel.models.ComputeError( |
| 202 | + error_type="input error", |
| 203 | + error_message="Calculation succeeded but invalid driver request provided", |
| 204 | + ), |
| 205 | + ) |
| 206 | + |
| 207 | + ret_data["extras"].update(extras) |
| 208 | + |
| 209 | + except RuntimeError as e: |
| 210 | + ret_data.update( |
| 211 | + error=qcel.models.ComputeError( |
| 212 | + error_type="input error", error_message=str(e) |
| 213 | + ), |
| 214 | + ), |
| 215 | + |
| 216 | + ret_data.update( |
| 217 | + provenance=provenance, |
| 218 | + success=success, |
| 219 | + properties=properties, |
| 220 | + return_result=return_result, |
| 221 | + ) |
| 222 | + |
| 223 | + return qcel.models.AtomicResult(**ret_data) |
0 commit comments