forked from pyscf/pyscf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
native support dispersion correction (pyscf#1916)
* added solvent models * add example for RHF * cleanup variables * support casci casscf and ccsd * uncomment unittests * change example name * update reset * for flake8 * fixed a bug in soscf/newton_ah.py * updated for recent master changes * remove whitespace * remove whitespace * native support dispersion correction * fixed a bug in pcm * Update hf.py * move dispersion to addons * remove disp in RKS * call get_dispersion in kernels * added unit test for d4 * fixed dispersion correction in testing h2o * updated ci * skip unittest if dftd3 or dftd4 is missing
- Loading branch information
Showing
17 changed files
with
383 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2014-2023 The PySCF Developers. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# Author: Xiaojie Wu <[email protected]> | ||
# | ||
|
||
''' | ||
gradient of dispersion correction for HF and DFT | ||
''' | ||
|
||
import numpy | ||
from pyscf.scf.hf import KohnShamDFT | ||
|
||
def get_dispersion(mf_grad, disp_version=None): | ||
'''gradient of dispersion correction for RHF/RKS''' | ||
if disp_version is None: | ||
disp_version = mf_grad.base.disp | ||
mol = mf_grad.base.mol | ||
disp_version = mf_grad.base.disp | ||
if disp_version is None: | ||
return numpy.zeros([mol.natm,3]) | ||
|
||
if isinstance(mf_grad.base, KohnShamDFT): | ||
method = mf_grad.base.xc | ||
else: | ||
method = 'hf' | ||
|
||
if disp_version[:2].upper() == 'D3': | ||
# raised error in SCF module, assuming dftd3 installed | ||
import dftd3.pyscf as disp | ||
d3 = disp.DFTD3Dispersion(mol, xc=method, version=disp_version) | ||
_, g_d3 = d3.kernel() | ||
return g_d3 | ||
elif disp_version[:2].upper() == 'D4': | ||
from pyscf.data.elements import charge | ||
atoms = numpy.array([ charge(a[0]) for a in mol._atom]) | ||
coords = mol.atom_coords() | ||
from dftd4.interface import DampingParam, DispersionModel | ||
model = DispersionModel(atoms, coords) | ||
res = model.get_dispersion(DampingParam(method=method), grad=True) | ||
return res.get("gradient") | ||
else: | ||
raise RuntimeError(f'dispersion correction: {disp_version} is not supported.') | ||
|
||
# Inject to Gradient | ||
from pyscf import grad | ||
grad.rhf.GradientsBase.get_dispersion = get_dispersion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2014-2023 The PySCF Developers. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# Author: Xiaojie Wu <[email protected]> | ||
# | ||
|
||
''' | ||
Hessian of dispersion correction for HF and DFT | ||
''' | ||
|
||
|
||
import numpy | ||
from pyscf.scf.hf import KohnShamDFT | ||
|
||
def get_dispersion(hessobj, disp_version=None): | ||
if disp_version is None: | ||
disp_version = hessobj.base.disp | ||
mol = hessobj.base.mol | ||
natm = mol.natm | ||
mf = hessobj.base | ||
h_disp = numpy.zeros([natm,natm,3,3]) | ||
if disp_version is None: | ||
return h_disp | ||
if isinstance(hessobj.base, KohnShamDFT): | ||
method = hessobj.base.xc | ||
else: | ||
method = 'hf' | ||
|
||
if mf.disp[:2].upper() == 'D3': | ||
import dftd3.pyscf as disp | ||
coords = hessobj.mol.atom_coords() | ||
mol = mol.copy() | ||
eps = 1e-5 | ||
for i in range(natm): | ||
for j in range(3): | ||
coords[i,j] += eps | ||
mol.set_geom_(coords, unit='Bohr') | ||
d3 = disp.DFTD3Dispersion(mol, xc=method, version=mf.disp) | ||
_, g1 = d3.kernel() | ||
|
||
coords[i,j] -= 2.0*eps | ||
mol.set_geom_(coords, unit='Bohr') | ||
d3 = disp.DFTD3Dispersion(mol, xc=method, version=mf.disp) | ||
_, g2 = d3.kernel() | ||
|
||
coords[i,j] += eps | ||
h_disp[i,:,j,:] = (g1 - g2)/(2.0*eps) | ||
return h_disp | ||
|
||
elif mf.disp[:2].upper() == 'D4': | ||
from pyscf.data.elements import charge | ||
atoms = numpy.array([ charge(a[0]) for a in mol._atom]) | ||
coords = mol.atom_coords() | ||
natm = mol.natm | ||
from dftd4.interface import DampingParam, DispersionModel | ||
params = DampingParam(method=method) | ||
mol = mol.copy() | ||
eps = 1e-5 | ||
for i in range(natm): | ||
for j in range(3): | ||
coords[i,j] += eps | ||
mol.set_geom_(coords, unit='Bohr') | ||
model = DispersionModel(atoms, coords) | ||
res = model.get_dispersion(params, grad=True) | ||
g1 = res.get("gradient") | ||
|
||
coords[i,j] -= 2.0*eps | ||
mol.set_geom_(coords, unit='Bohr') | ||
model = DispersionModel(atoms, coords) | ||
res = model.get_dispersion(params, grad=True) | ||
g2 = res.get("gradient") | ||
|
||
coords[i,j] += eps | ||
h_disp[i,:,j,:] = (g1 - g2)/(2.0*eps) | ||
|
||
return h_disp | ||
else: | ||
raise RuntimeError(f'dispersion correction: {disp_version} is not supported.') | ||
|
||
# Inject to SCF class | ||
from pyscf import hessian | ||
hessian.rhf.HessianBase.get_dispersion = get_dispersion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.