Skip to content

Commit

Permalink
Add PBC interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Aug 9, 2024
1 parent 8f92bb3 commit f9cf5b2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
12 changes: 8 additions & 4 deletions pyscf/dispersion/dftd3.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,21 @@ def __init__(self, mol, xc, version='d3bj', atm=False):
for ia in range(mol.natm)]
nuc_types = np.asarray(nuc_types, dtype=np.int32)
self.natm = mol.natm
self._lattice = lib.c_null_ptr()
self._periodic = lib.c_null_ptr()
if isinstance(mol, gto.Mole):
lattice = lib.c_null_ptr()
periodic = lib.c_null_ptr()
else: # pbc.gto.Cell
a = mol.lattice_vectors()
lattice = a.ctypes
periodic = ctypes.byref(ctypes.c_bool(True))

err = libdftd3.dftd3_new_error()
self._mol = libdftd3.dftd3_new_structure(
err,
ctypes.c_int(mol.natm),
nuc_types.ctypes.data_as(ctypes.c_void_p),
coords.ctypes.data_as(ctypes.c_void_p),
self._lattice,
self._periodic,
lattice, periodic,
)
error_check(err)

Expand Down
12 changes: 8 additions & 4 deletions pyscf/dispersion/dftd4.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ def __init__(self, mol, xc, atm=False):
for ia in range(mol.natm)]
nuc_types = np.asarray(nuc_types, dtype=np.int32)
self.natm = mol.natm
self._lattice = lib.c_null_ptr()
self._periodic = lib.c_null_ptr()
if isinstance(mol, gto.Mole):
lattice = lib.c_null_ptr()
periodic = lib.c_null_ptr()
else: # pbc.gto.Cell
a = mol.lattice_vectors()
lattice = a.ctypes
periodic = ctypes.byref(ctypes.c_bool(True))

err = libdftd4.dftd4_new_error()
self._mol = libdftd4.dftd4_new_structure(
Expand All @@ -60,8 +65,7 @@ def __init__(self, mol, xc, atm=False):
nuc_types.ctypes.data_as(ctypes.c_void_p),
coords.ctypes.data_as(ctypes.c_void_p),
charge.ctypes.data_as(ctypes.c_void_p),
self._lattice,
self._periodic,
lattice, periodic,
)
error_check(err)

Expand Down
7 changes: 7 additions & 0 deletions pyscf/dispersion/tests/test_d3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
import pyscf
from pyscf.dispersion.dftd3 import DFTD3Dispersion
Expand All @@ -20,3 +21,9 @@ def test_d3_gradients():
assert abs(out['energy'] - -0.002682864283734) < 1e-10
assert abs(out['gradient'][0, 2] - 0.0004553384490128) < 1e-10
assert abs(out['virial'][2, 2] - -0.000860464962618) < 1e-10

def test_d3_with_pbc():
mol = pyscf.M(atom='H 0 0 0; H 0 0 1', a=np.eye(3)*2)
model = DFTD3Dispersion(mol, xc='WB97X')
out = model.get_dispersion()
assert abs(out['energy'] - -0.0014506963767424985) < 1e-10
7 changes: 7 additions & 0 deletions pyscf/dispersion/tests/test_d4.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
import pyscf
from pyscf.dispersion.dftd4 import DFTD4Dispersion
Expand All @@ -20,3 +21,9 @@ def test_d4_gradients():
assert abs(out['energy'] - -0.000967454204722) < 1e-10
assert abs(out['gradient'][0,2] - 9.31972590827e-06) < 1e-10
assert abs(out['virial'][2,2] - -1.76117295226e-05) < 1e-10

def test_d4_with_pbc():
mol = pyscf.M(atom='H 0 0 0; H 0 0 1', a=np.eye(3)*2)
model = DFTD4Dispersion(mol, xc='WB97X')
out = model.get_dispersion()
assert abs(out['energy'] - -0.002715970438476524) < 1e-10

0 comments on commit f9cf5b2

Please sign in to comment.