Skip to content

Commit 0fe391f

Browse files
committed
Allow modifying cutoff values from ASE calculator
1 parent 442fbec commit 0fe391f

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

python/dftd3/ase.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
method None Method to calculate dispersion for
4141
damping None Damping function to use
4242
params_tweaks None Optional dict with the damping parameters
43+
realspace_cutoff None Optional dict to override cutoff values
4344
cache_api True Reuse generate API objects (recommended)
4445
======================== ============ ============================================
4546
@@ -63,6 +64,20 @@
6364
for damping parameters of a given method and prefers special two-body only
6465
damping parameters if available!
6566
67+
The realspace cutoff parameters allow adjusting the distance values for which
68+
interactions are considered
69+
70+
================== =========== ==========================================
71+
Realspace cutoff Default Description
72+
================== =========== ==========================================
73+
disp2 60 * Bohr Pairwise dispersion interactions
74+
disp3 40 * Bohr Triple dispersion interactions
75+
cn 40 * Bohr Coordination number counting
76+
================== =========== ==========================================
77+
78+
Values provided in the dict are expected to be in Angstrom. When providing values
79+
in Bohr divide the inputs by the `ase.units.Bohr` constant.
80+
6681
Example
6782
-------
6883
>>> from ase.build import molecule
@@ -144,6 +159,7 @@ class DFTD3(Calculator):
144159
"method": None,
145160
"damping": None,
146161
"params_tweaks": {},
162+
"realspace_cutoff": {},
147163
"cache_api": True,
148164
}
149165

@@ -235,8 +251,18 @@ def _create_api_calculator(self) -> DispersionModel:
235251
_periodic,
236252
)
237253

238-
except RuntimeError:
239-
raise InputError("Cannot construct dispersion model for dftd3")
254+
except RuntimeError as e:
255+
raise InputError("Cannot construct dispersion model for dftd3") from e
256+
257+
try:
258+
if self.parameters.realspace_cutoff:
259+
disp2 = self.parameters.realspace_cutoff.get("disp2", 60.0 * Bohr) / Bohr
260+
disp3 = self.parameters.realspace_cutoff.get("disp3", 40.0 * Bohr ) / Bohr
261+
cn = self.parameters.realspace_cutoff.get("cn", 40.0 * Bohr ) / Bohr
262+
263+
disp.set_realspace_cutoff(disp2=disp2, disp3=disp3, cn=cn)
264+
except RuntimeError as e:
265+
raise InputError("Cannot update realspace cutoff for dftd3") from e
240266

241267
return disp
242268

@@ -247,8 +273,8 @@ def _create_damping_param(self) -> DampingParam:
247273
params_tweaks = self.parameters.params_tweaks if self.parameters.params_tweaks else {"method": self.parameters.get("method")}
248274
dpar = _damping_param[self.parameters.get("damping")](**params_tweaks)
249275

250-
except RuntimeError:
251-
raise InputError("Cannot construct damping parameter for dftd3")
276+
except RuntimeError as e:
277+
raise InputError("Cannot construct damping parameter for dftd3") from e
252278

253279
return dpar
254280

@@ -273,8 +299,8 @@ def calculate(
273299

274300
try:
275301
_res = self._disp.get_dispersion(param=_dpar, grad=True)
276-
except RuntimeError:
277-
raise CalculationFailed("dftd3 could not evaluate input")
302+
except RuntimeError as e:
303+
raise CalculationFailed("dftd3 could not evaluate input") from e
278304

279305
# These properties are garanteed to exist for all implemented calculators
280306
self.results["energy"] = _res.get("energy") * Hartree

0 commit comments

Comments
 (0)