From 692ab04cc3093b6c2feaf1d5d3ad78ff58b03ccc Mon Sep 17 00:00:00 2001 From: Xiaojie Wu Date: Mon, 20 Nov 2023 17:59:59 -0800 Subject: [PATCH] Bugfix (#58) * Add chelpg charges in qmmm folder. (#1) * Add chelpg charges in qmmm folder. * Update chelpg.py * Update chelpg.py * Add unit test for chelpg, and compare with Qchem * Add an example to calculate chelpg * refactor cutensor logics * ignore * fixed warning * fixed test unit test in geometric --------- Co-authored-by: puzhichen <147788878+puzhichen@users.noreply.github.com> --- examples/03-h2o_dzvp.py | 6 ++-- examples/09-mbis.py | 5 ++-- gpu4pyscf/df/tests/test_df_grad.py | 2 +- gpu4pyscf/df/tests/test_df_hessian.py | 1 - gpu4pyscf/df/tests/test_geomopt.py | 25 ++++++++--------- gpu4pyscf/hessian/rks.py | 2 +- gpu4pyscf/lib/cutensor.py | 35 +++++++++++------------- gpu4pyscf/solvent/tests/test_pcm_grad.py | 20 +++++++------- 8 files changed, 45 insertions(+), 51 deletions(-) diff --git a/examples/03-h2o_dzvp.py b/examples/03-h2o_dzvp.py index e95f167c..044d65b5 100644 --- a/examples/03-h2o_dzvp.py +++ b/examples/03-h2o_dzvp.py @@ -19,7 +19,7 @@ from gpu4pyscf.dft import rks lib.num_threads(8) -atom =''' +atom =''' O 0.0000000000 -0.0000000000 0.1174000000 H -0.7570000000 -0.0000000000 -0.4696000000 H 0.7570000000 0.0000000000 -0.4696000000 @@ -35,7 +35,7 @@ mol = pyscf.M(atom=atom, basis=bas, max_memory=32000) auxbasis = {} -from pyscf import df, gto +from pyscf import df etb_basis = df.aug_etb(mol, beta=2.0) for i in range(mol.natm): sym = mol.atom_pure_symbol(i) @@ -57,7 +57,7 @@ # Compute Energy print('------------------- Energy -----------------------------') -e_dft = mf_GPU.kernel() +e_dft = mf_GPU.kernel() print('DFT energy by GPU4PySCF') print(e_dft) diff --git a/examples/09-mbis.py b/examples/09-mbis.py index 73aaf637..95512d50 100644 --- a/examples/09-mbis.py +++ b/examples/09-mbis.py @@ -19,7 +19,7 @@ from gpu4pyscf.dft import rks lib.num_threads(8) -atom =''' +atom =''' O 0.0000000000 -0.0000000000 0.1174000000 H -0.7570000000 -0.0000000000 -0.4696000000 H 0.7570000000 0.0000000000 -0.4696000000 @@ -60,5 +60,4 @@ atnums = np.array(atom_list) atcoords = mol.atom_coords(unit='B') -np.savez('density.npz', points=points, weights=weights, \ - density=density, atnums=atnums, atcoords=atcoords) \ No newline at end of file +np.savez('density.npz', points=points, weights=weights, density=density, atnums=atnums, atcoords=atcoords) \ No newline at end of file diff --git a/gpu4pyscf/df/tests/test_df_grad.py b/gpu4pyscf/df/tests/test_df_grad.py index 9156c2c0..4db5cf58 100644 --- a/gpu4pyscf/df/tests/test_df_grad.py +++ b/gpu4pyscf/df/tests/test_df_grad.py @@ -61,7 +61,7 @@ def _check_grad(grid_response=False, xc=xc0, disp=disp0, tol=1e-6): mf.nlcgrids.level = nlcgrids_level mf.conv_tol = 1e-10 mf.verbose = 1 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() g.auxbasis_response = True diff --git a/gpu4pyscf/df/tests/test_df_hessian.py b/gpu4pyscf/df/tests/test_df_hessian.py index fd5a8bd7..04dde225 100644 --- a/gpu4pyscf/df/tests/test_df_hessian.py +++ b/gpu4pyscf/df/tests/test_df_hessian.py @@ -15,7 +15,6 @@ import numpy as np import pyscf -from pyscf import scf, dft from gpu4pyscf import dft, scf import unittest diff --git a/gpu4pyscf/df/tests/test_geomopt.py b/gpu4pyscf/df/tests/test_geomopt.py index 4960278f..e9df65c9 100644 --- a/gpu4pyscf/df/tests/test_geomopt.py +++ b/gpu4pyscf/df/tests/test_geomopt.py @@ -15,7 +15,6 @@ import pyscf import numpy as np -import dftd3.pyscf as disp import unittest from pyscf import lib from gpu4pyscf import scf @@ -32,8 +31,8 @@ xc='B3LYP' bas='def2-tzvpp' -auxbasis='ccpvtz-jkfit' disp='d3bj' +auxbasis='ccpvtz-jkfit' grids_level = 8 def setUpModule(): @@ -46,14 +45,14 @@ def tearDownModule(): global mol mol.stdout.close() del mol - + eps = 1e-3 class KnownValues(unittest.TestCase): - def test_geomopt(self): + def test_rks_geomopt(self): mf = rks.RKS(mol, xc=xc, disp=disp).density_fit() mf.grids.level = grids_level - e_tot = mf.kernel() + mf.kernel() mol_eq = optimize(mf, maxsteps=20) coords = mol_eq.atom_coords(unit='Ang') # reference from q-chem @@ -61,22 +60,22 @@ def test_geomopt(self): [ 0.0000000000, 0.0000000000, 0.1164022656], [-0.7617088263, -0.0000000000, -0.4691011328], [0.7617088263, -0.0000000000, -0.4691011328]]) - + assert np.linalg.norm(coords - coords_qchem) < 1e-4 - - def test_geomopt(self): - mf_GPU = scf.RHF(mol).density_fit() - e_tot = mf_GPU.kernel() - mol_eq = optimize(mf_GPU, maxsteps=20) + + def test_rhf_geomopt(self): + mf = scf.RHF(mol).density_fit() + mf.kernel() + mol_eq = optimize(mf, maxsteps=20) coords = mol_eq.atom_coords(unit='Ang') # reference from q-chem coords_qchem = np.array([ [0.0000000000, 0.0000000000, 0.1021249784], [-0.7519034531, -0.0000000000, -0.4619624892], [0.7519034531, -0.0000000000, -0.4619624892]]) - + assert np.linalg.norm(coords - coords_qchem) < 1e-4 - + if __name__ == "__main__": print("Full Tests for geometry optimization") unittest.main() diff --git a/gpu4pyscf/hessian/rks.py b/gpu4pyscf/hessian/rks.py index 82269257..be30fa66 100644 --- a/gpu4pyscf/hessian/rks.py +++ b/gpu4pyscf/hessian/rks.py @@ -617,7 +617,7 @@ def _get_vxc_deriv1(hessobj, mo_coeff, mo_occ, max_memory): ao_dm0 = aow = None elif xctype == 'MGGA': if grids.level < 5: - logger.warn(mol, 'MGGA Hessian is sensitive to dft grids.') + log.warn(mol, 'MGGA Hessian is sensitive to dft grids.') ao_deriv = 2 for ao, mask, weight, coords \ in ni.block_loop(opt.mol, grids, nao, ao_deriv, max_memory): diff --git a/gpu4pyscf/lib/cutensor.py b/gpu4pyscf/lib/cutensor.py index aca3b082..36bb6737 100644 --- a/gpu4pyscf/lib/cutensor.py +++ b/gpu4pyscf/lib/cutensor.py @@ -15,27 +15,24 @@ import numpy as np import cupy -from cupy._environment import _preload_libs -from cupyx import cutensor -from cupy_backends.cuda.libs import cutensor as cutensor_backend -from cupy_backends.cuda.libs.cutensor import Handle from gpu4pyscf.lib import logger -libcutensor = None -for lib_path in _preload_libs['cutensor']: - try: - libcutensor = _preload_libs['cutensor'][lib_path] - break - except Exception: - continue +try: + import cupy_backends.cuda.libs.cutensor # NOQA + from cupyx import cutensor + from cupy_backends.cuda.libs import cutensor as cutensor_backend + from cupy_backends.cuda.libs.cutensor import Handle + + _handle = Handle() + _modes = {} + _contraction_descriptors = {} + _contraction_plans = {} + _contraction_finds = {} -_handle = Handle() -_modes = {} -_contraction_descriptors = {} -_contraction_plans = {} -_contraction_finds = {} + cutensor_backend.init(_handle) -cutensor_backend.init(_handle) +except ImportError: + cutensor = None def _create_mode_with_cache(mode): integer_mode = [] @@ -156,8 +153,8 @@ def contraction(pattern, a, b, alpha, beta, out=None): else: contract_engine = None -if libcutensor is None: - contract_engine = 'cupy' +if cutensor is None: + contract_engine = 'cupy' # default contraction engine # override the 'contract' function if einsum is customized or cutensor is not found if contract_engine is not None: diff --git a/gpu4pyscf/solvent/tests/test_pcm_grad.py b/gpu4pyscf/solvent/tests/test_pcm_grad.py index 677aa285..2cacfbd7 100644 --- a/gpu4pyscf/solvent/tests/test_pcm_grad.py +++ b/gpu4pyscf/solvent/tests/test_pcm_grad.py @@ -109,14 +109,14 @@ def test_grad_CPCM(self): mf = scf.RHF(mol).PCM(cm) mf.verbose = 0 mf.conv_tol = 1e-12 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() grad = g.kernel() g0 = numpy.asarray([ - [0.49773047433563E-15, -0.12128126037559E-15, -0.58936988992306E-01], - [0.22810111996954E-01, -0.68951901317025E-17, 0.29468494708267E-01], + [0.49773047433563E-15, -0.12128126037559E-15, -0.58936988992306E-01], + [0.22810111996954E-01, -0.68951901317025E-17, 0.29468494708267E-01], [-0.22810111996957E-01, 0.12949813945902E-15, 0.29468494708266E-01]]) print(f"Gradient error in CPCM: {numpy.linalg.norm(g0 - grad)}") @@ -131,7 +131,7 @@ def test_grad_COSMO(self): mf = scf.RHF(mol).PCM(cm) mf.verbose = 0 mf.conv_tol = 1e-12 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() grad = g.kernel() @@ -153,14 +153,14 @@ def test_grad_IEFPCM(self): mf = scf.RHF(mol).PCM(cm) mf.verbose = 0 mf.conv_tol = 1e-12 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() grad = g.kernel() g0 = numpy.asarray([ - [0.18357915015649E-14, 0.14192681822347E-15, -0.58988087999658E-01], - [0.22822709179063E-01, -0.10002010417168E-15, 0.29494044211805E-01], + [0.18357915015649E-14, 0.14192681822347E-15, -0.58988087999658E-01], + [0.22822709179063E-01, -0.10002010417168E-15, 0.29494044211805E-01], [-0.22822709179066E-01, -0.31051364515588E-16, 0.29494044211806E-01]]) print(f"Gradient error in IEFPCM: {numpy.linalg.norm(g0 - grad)}") assert numpy.linalg.norm(g0 - grad) < 1e-9 @@ -174,14 +174,14 @@ def test_grad_SSVPE(self): mf = scf.RHF(mol).PCM(cm) mf.verbose = 0 mf.conv_tol = 1e-12 - e_tot = mf.kernel() + mf.kernel() g = mf.nuc_grad_method() grad = g.kernel() g0 = numpy.asarray([ - [0.76104817971710E-15, 0.11185701540547E-15, -0.58909172879217E-01], - [0.22862990009767E-01, -0.13861633974903E-15, 0.29454586651678E-01], + [0.76104817971710E-15, 0.11185701540547E-15, -0.58909172879217E-01], + [0.22862990009767E-01, -0.13861633974903E-15, 0.29454586651678E-01], [-0.22862990009769E-01, 0.34988765678591E-16, 0.29454586651679E-01]]) print(f"Gradient error in SS(V)PE: {numpy.linalg.norm(g0 - grad)}") assert numpy.linalg.norm(g0 - grad) < 1e-9