diff --git a/pyscf/dispersion/dftd3.py b/pyscf/dispersion/dftd3.py index 4526804..b50c73c 100644 --- a/pyscf/dispersion/dftd3.py +++ b/pyscf/dispersion/dftd3.py @@ -64,8 +64,13 @@ 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( @@ -73,8 +78,7 @@ def __init__(self, mol, xc, version='d3bj', atm=False): 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) diff --git a/pyscf/dispersion/dftd4.py b/pyscf/dispersion/dftd4.py index 837fb13..2954841 100644 --- a/pyscf/dispersion/dftd4.py +++ b/pyscf/dispersion/dftd4.py @@ -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( @@ -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) diff --git a/pyscf/dispersion/tests/test_d3.py b/pyscf/dispersion/tests/test_d3.py index 81d2081..8bbb247 100644 --- a/pyscf/dispersion/tests/test_d3.py +++ b/pyscf/dispersion/tests/test_d3.py @@ -1,3 +1,4 @@ +import numpy as np import pytest import pyscf from pyscf.dispersion.dftd3 import DFTD3Dispersion @@ -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 diff --git a/pyscf/dispersion/tests/test_d4.py b/pyscf/dispersion/tests/test_d4.py index 56d49c6..b352739 100644 --- a/pyscf/dispersion/tests/test_d4.py +++ b/pyscf/dispersion/tests/test_d4.py @@ -1,3 +1,4 @@ +import numpy as np import pytest import pyscf from pyscf.dispersion.dftd4 import DFTD4Dispersion @@ -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