Skip to content

Commit

Permalink
Fix periodic PySCF calculations (#74)
Browse files Browse the repository at this point in the history
* pass lattice vectors if pyscf object has them
* Add test case based on reported issue
  • Loading branch information
aizvorski authored Jul 27, 2024
1 parent a13813a commit 3d9f9d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 8 additions & 0 deletions python/dftd3/pyscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,17 @@ def kernel(self) -> Tuple[float, np.ndarray]:
"""
mol = self.mol

lattice = None
periodic = None
if hasattr(mol, 'lattice_vectors'):
lattice = mol.lattice_vectors()
periodic = np.array([True, True, True], dtype=bool)

disp = DispersionModel(
np.array([gto.charge(mol.atom_symbol(ia)) for ia in range(mol.natm)]),
mol.atom_coords(),
lattice=lattice,
periodic=periodic,
)

if self.param is not None:
Expand Down
30 changes: 29 additions & 1 deletion python/dftd3/test_pyscf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

try:
import pyscf
from pyscf import lib, gto, scf
from pyscf import gto, scf, pbc
import dftd3.pyscf as disp
except ModuleNotFoundError:
pyscf = None
Expand Down Expand Up @@ -225,3 +225,31 @@ def test_gradient_hf():
)
grad = disp.energy(scf.RHF(mol)).run().nuc_grad_method()
assert grad.kernel() == approx(ref, abs=1.0e-7)


def test_issue_gh73():
mol = gto.M(
atom="""
O -1.6256 -0.0413 0.3705
H -0.7061 -0.0938 0.0934
H -2.0618 -0.7328 -0.1359
""",
basis="def2-tzvp",
)

pmol = pbc.gto.M(
atom="""
O -1.6256 -0.0413 0.3705
H -0.7061 -0.0938 0.0934
H -2.0618 -0.7328 -0.1359
""",
basis="def2-tzvp",
a=[[3, 0, 0], [0, 3, 0], [0, 0, 3]],
)

xc = 'pbe'

e_mol_disp = disp.DFTD3Dispersion(mol, xc=xc, version="d3bj").kernel()[0]
e_pbc_disp = disp.DFTD3Dispersion(pmol, xc=xc, version="d3bj").kernel()[0]

assert e_mol_disp != e_pbc_disp

0 comments on commit 3d9f9d2

Please sign in to comment.