Skip to content

Commit

Permalink
Add unit tests of checking consistency of NMR, polarizability, and IR…
Browse files Browse the repository at this point in the history
… with PySCF (#261)

* compare with pyscf on cpu

* ruff
  • Loading branch information
wxj6000 authored Nov 29, 2024
1 parent 9927b4f commit 8a897f3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
python3 -m pip install --upgrade pip
pip3 install flake8 pytest coverage pytest-cov pyscf-dispersion
pip3 install pyscf --upgrade
pip3 install git+https://github.com/pyscf/properties --upgrade
pip3 install numpy --upgrade
pip3 install h5py --upgrade
pip3 install gpu4pyscf-libxc-cuda12x --upgrade
Expand Down
31 changes: 28 additions & 3 deletions gpu4pyscf/properties/tests/test_ir_intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import numpy as np
import pyscf
from pyscf import lib
from pyscf.dft import rks as rks_cpu
from gpu4pyscf.dft import rks, uks
from gpu4pyscf.properties import ir

try:
from pyscf.prop import infrared
except Exception:
infrared = None

lib.num_threads(8)

atom = '''
Expand Down Expand Up @@ -53,6 +59,24 @@ def run_dft_df_if(xc):
freq, intensity = ir.eval_ir_freq_intensity(mf, h)
return e_dft, freq, intensity

def _vs_cpu(xc):
mf = rks.RKS(mol, xc=xc)
mf.grids.level = grids_level
mf.conv_tol = 1e-13
mf.conv_tol_cpscf = 1e-7
e_gpu = mf.kernel()
h = mf.Hessian()
freq, intensity_gpu = ir.eval_ir_freq_intensity(mf, h)

mf_cpu = rks_cpu.RKS(mol, xc=xc)
mf.grids.level = grids_level
mf_cpu.conv_tol = 1e-13
mf_cpu.conv_tol_cpscf = 1e-7
e_cpu = mf_cpu.kernel()
mf_ir = infrared.rks.Infrared(mf_cpu).run()
assert np.abs(e_gpu - e_cpu) < 1e-5
assert np.linalg.norm(mf_ir.ir_inten - intensity_gpu.get()) < 1e-2


class KnownValues(unittest.TestCase):
'''
Expand Down Expand Up @@ -90,12 +114,13 @@ def test_rks_b3lyp_df(self):
assert np.allclose(e_tot, -76.4666819537)
qchem_freq = np.array([1630.86, 3850.08, 3949.35])
qchem_intensity = np.array([69.334, 4.675, 47.943])
print(freq, intensity)
assert np.allclose(freq, qchem_freq, rtol=1e-03)
assert np.allclose(intensity, qchem_intensity, rtol=1e-02)


@unittest.skipIf(infrared is None, "Skipping test if pyscf.properties is not installed")
def test_cpu(self):
_vs_cpu('b3lyp')

if __name__ == "__main__":
print("Full Tests for ir intensity")
unittest.main()
unittest.main()
23 changes: 22 additions & 1 deletion gpu4pyscf/properties/tests/test_polarizability.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import numpy as np
import pyscf
from pyscf import lib
from pyscf.dft import rks as rks_cpu
from gpu4pyscf.dft import rks, uks
from gpu4pyscf.properties import polarizability

try:
from pyscf.prop import polarizability as polar
except Exception:
polar = None

lib.num_threads(8)

atom = '''
Expand Down Expand Up @@ -57,6 +63,19 @@ def run_dft_df_polarizability(xc):
polar = polarizability.eval_polarizability(mf)
return e_dft, polar

def _vs_cpu(xc):
mf = rks.RKS(mol, xc=xc)
mf.grids.level = grids_level
e_gpu = mf.kernel()
polar_gpu = polarizability.eval_polarizability(mf)

mf_cpu = rks_cpu.RKS(mol, xc=xc)
mf_cpu.conv_tol = 1e-12
e_cpu = mf_cpu.kernel()
polar_cpu = polar.rhf.Polarizability(mf_cpu).polarizability()

assert np.abs(e_gpu - e_cpu) < 1e-5
assert np.linalg.norm(polar_cpu - polar_gpu) < 1e-3

class KnownValues(unittest.TestCase):
'''
Expand Down Expand Up @@ -121,7 +140,9 @@ def test_rks_b3lyp_df(self):
[ -0.0000000, -0.0000000, 7.5688173]])
assert np.allclose(polar, qchem_polar)


@unittest.skipIf(polar is None, "Skipping test if pyscf.properties is not installed")
def test_cpu(self):
_vs_cpu('b3lyp')

if __name__ == "__main__":
print("Full Tests for polarizabillity")
Expand Down
25 changes: 25 additions & 0 deletions gpu4pyscf/properties/tests/test_shielding.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@
import numpy as np
import pyscf
from pyscf import lib
from pyscf.dft import rks as rks_cpu
from gpu4pyscf.dft import rks, uks
from gpu4pyscf.properties import shielding

try:
from pyscf.prop import nmr
except Exception:
nmr = None

lib.num_threads(8)

atom = '''
Expand Down Expand Up @@ -57,6 +63,21 @@ def run_dft_df_nmr_shielding(xc):
tensor = shielding.eval_shielding(mf)
return e_dft, tensor

def _vs_cpu(xc):
mf = rks.RKS(mol, xc=xc)
mf.grids.level = grids_level
mf.conv_tol = 1e-12
e_gpu = mf.kernel()
msc_d, msc_p = shielding.eval_shielding(mf)
msc = (msc_d + msc_p).get()

mf_cpu = rks_cpu.RKS(mol, xc=xc)
mf_cpu.conv_tol = 1e-12
e_cpu = mf_cpu.kernel()
msc_cpu = nmr.RKS(mf_cpu).kernel()

assert np.abs(e_gpu - e_cpu) < 1e-5
assert np.linalg.norm(msc - msc_cpu) < 1e-3

class KnownValues(unittest.TestCase):
'''
Expand Down Expand Up @@ -150,6 +171,10 @@ def test_rks_b3lyp_df(self):
assert np.allclose(e_tot, -76.4666819553)
assert np.allclose(isotropic_pyscf, isotropic_qchem, rtol=1.0E-4)

@unittest.skipIf(nmr is None, "Skipping test if pyscf.properties is not installed")
def test_cpu(self):
_vs_cpu('b3lyp')

if __name__ == "__main__":
print("Full Tests for NMR Shielding Constants")
unittest.main()

0 comments on commit 8a897f3

Please sign in to comment.