From 713e8c9c33e66b2eceacd8c0d1309128f359cc7f Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Sun, 15 Oct 2023 16:19:13 -0700 Subject: [PATCH] Restore r_numint tests --- pyscf/dft/r_numint.py | 2 +- pyscf/dft/test/test_r_numint.py | 167 +++++++++++++++----------------- pyscf/gto/mole.py | 6 -- 3 files changed, 80 insertions(+), 95 deletions(-) diff --git a/pyscf/dft/r_numint.py b/pyscf/dft/r_numint.py index b9a0faa0a3..83befddede 100644 --- a/pyscf/dft/r_numint.py +++ b/pyscf/dft/r_numint.py @@ -576,7 +576,7 @@ def r_fxc(ni, mol, grids, xc_code, dm0, dms, spin=0, relativity=1, hermi=0, make_rho1, nset, nao = ni._gen_rho_evaluator(mol, dms, hermi) with_s = (nao == n2c*2) # 4C DM - matLL = numpy.zeros((nset,n2c,n2c), dtype=dms.dtype) + matLL = numpy.zeros((nset,n2c,n2c), dtype=numpy.complex128) matSS = numpy.zeros_like(matLL) if xctype in ('LDA', 'GGA', 'MGGA'): f_eval_mat = { diff --git a/pyscf/dft/test/test_r_numint.py b/pyscf/dft/test/test_r_numint.py index b5b5b3982e..deaa0b10df 100644 --- a/pyscf/dft/test/test_r_numint.py +++ b/pyscf/dft/test/test_r_numint.py @@ -6,6 +6,10 @@ from pyscf import dft from pyscf import lib from pyscf.dft import r_numint, xc_deriv +try: + import mcfun +except ImportError: + mcfun = None def setUpModule(): global mol @@ -46,26 +50,6 @@ def test_eval_rho(self): self.assertAlmostEqual(abs(rho0-rho1[0]).max(), 0, 9) self.assertAlmostEqual(abs(m0 - rho1[1:4]).max(), 0, 9) -# def test_eval_mat(self): -# numpy.random.seed(10) -# ngrids = 100 -# coords = numpy.random.random((ngrids,3))*20 -# rho = numpy.random.random((ngrids)) -# m = numpy.random.random((3,ngrids)) * .05 -# vxc = [numpy.random.random((2,ngrids)).T, None, None, None] -# weight = numpy.random.random(ngrids) -# aoLa, aoLb, aoSa, aoSb = r_numint.eval_ao(mol, coords, deriv=1) -# -# s = numpy.linalg.norm(m, axis=0) -# m_pauli = numpy.einsum('xp,xij,p->pij', m, lib.PauliMatrices, 1./(s+1e-300)) -# aoL = numpy.array([aoLa[0],aoLb[0]]) -# -# mat0 = numpy.einsum('pi,p,pj->ij', aoLa[0].conj(), weight*vxc[0][:,0], aoLa[0]) -# mat0+= numpy.einsum('pi,p,pj->ij', aoLb[0].conj(), weight*vxc[0][:,0], aoLb[0]) -# mat0+= numpy.einsum('api,p,pab,bpj->ij', aoL.conj(), weight*vxc[0][:,1], m_pauli, aoL) -# mat1 = r_numint.eval_mat(mol, (aoLa[0], aoLb[0]), weight, (rho, m), vxc, xctype='LDA') -# self.assertTrue(numpy.allclose(mat0, mat1)) - def test_rsh_omega(self): rho0 = numpy.array([[1., 1., 0.1, 0.1], [0, 0, 0, 0], @@ -116,74 +100,81 @@ def test_vxc1(self): res = mf._numint.get_vxc(mol, mf.grids, mf.xc, dm, spin=0) self.assertAlmostEqual(res[1], -8.631807003163278, 11) -# def test_vxc_col(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'c' -# dm = mf.get_init_guess(mol, 'minao') -# n, e, v = ni.nr_vxc(mol, mf.grids, 'B88,', dm) -# self.assertAlmostEqual(n, 9.984666945, 5) -# self.assertAlmostEqual(e, -8.8304689765, 6) -# self.assertAlmostEqual(lib.fp(v), -2.5543189495160217, 8) -# -# def test_vxc_ncol(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'n' -# dm = mf.get_init_guess(mol, 'minao') -# n, e, v = ni.nr_vxc(mol, mf.grids, 'LDA,', dm) -# self.assertAlmostEqual(n, 9.984666945, 5) -# self.assertAlmostEqual(e, -7.9647540011, 6) -# self.assertAlmostEqual(lib.fp(v), -2.3201735523227227+0j, 8) -# -# @unittest.skipIf(mcfun is None, "mcfun library not found.") -# def test_vxc_mcol(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'm' -# ni.spin_samples = 14 -# dm = mf.get_init_guess(mol, 'minao') -# n, e, v = ni.nr_vxc(mol, mf.grids, 'LDA,', dm) -# self.assertAlmostEqual(n, 9.984666945, 6) -# self.assertAlmostEqual(e, -7.9647540011, 6) -# self.assertAlmostEqual(lib.fp(v), -2.3201735523227227+0j, 8) -# -# n, e, v = ni.nr_vxc(mol, mf.grids, 'B88,', dm) -# self.assertAlmostEqual(n, 9.984666945, 5) -# self.assertAlmostEqual(e, -8.8337325415, 6) -# self.assertAlmostEqual(lib.fp(v), -2.5920046321400507+0j, 8) -# -# def test_fxc_col(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'c' -# dm = mf.get_init_guess(mol, 'minao') -# np.random.seed(10) -# dm1 = np.random.random(dm.shape) -# v = ni.nr_fxc(mol, mf.grids, 'B88,', dm, dm1) -# self.assertAlmostEqual(lib.fp(v), 1.0325167632577212, 6) -# -# @unittest.skipIf(mcfun is None, "mcfun library not found.") -# def test_fxc_mcol(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'm' -# ni.spin_samples = 14 -# dm = mf.get_init_guess(mol, 'minao') -# np.random.seed(10) -# dm1 = np.random.random(dm.shape) -# v = ni.nr_fxc(mol, mf.grids, 'LDA,', dm, dm1) -# self.assertAlmostEqual(lib.fp(v), 1.9147702820070673+0j, 6) -# -# v = ni.nr_fxc(mol, mf.grids, 'M06', dm, dm1) -# self.assertAlmostEqual(lib.fp(v), 0.7599330879272782+0j, 6) -# -# def test_get_rho(self): -# ni = numint2c.NumInt2C() -# ni.collinear = 'c' -# dm = mf.get_init_guess(mol, 'minao') -# rho = ni.get_rho(mol, dm, mf.grids) -# self.assertAlmostEqual(lib.fp(rho), -361.4682369790235, 8) -# -# ni.collinear = 'm' -# ni.spin_samples = 50 -# rho = ni.get_rho(mol, dm, mf.grids) -# self.assertAlmostEqual(lib.fp(rho), -361.4682369790235, 8) + def test_vxc_col(self): + ni = r_numint.RNumInt() + ni.collinear = 'c' + mf = mol.DKS() + dm = mf.get_init_guess(mol, 'minao') + n, e, v = ni.get_vxc(mol, mf.grids, 'B88,', dm) + self.assertAlmostEqual(n, 9.984666945, 5) + self.assertAlmostEqual(e, -8.8304689765, 6) + self.assertAlmostEqual(lib.fp(v), 0.05882536730070975+0.37093946710262393j, 8) + + def test_vxc_ncol(self): + ni = r_numint.RNumInt() + ni.collinear = 'n' + mf = mol.DKS() + dm = mf.get_init_guess(mol, 'minao') + n, e, v = ni.get_vxc(mol, mf.grids, 'LDA,', dm) + self.assertAlmostEqual(n, 9.984666945, 5) + self.assertAlmostEqual(e, -7.9613152012, 6) + self.assertAlmostEqual(lib.fp(v), 0.09939730962851079+0.35765389167296235j, 8) + + @unittest.skipIf(mcfun is None, "mcfun library not found.") + def test_vxc_mcol(self): + ni = r_numint.RNumInt() + ni.collinear = 'm' + ni.spin_samples = 14 + mf = mol.DKS() + dm = mf.get_init_guess(mol, 'minao') + n, e, v = ni.get_vxc(mol, mf.grids, 'LDA,', dm) + self.assertAlmostEqual(n, 9.984666945, 5) + self.assertAlmostEqual(e, -7.9613152012, 6) + self.assertAlmostEqual(lib.fp(v), 0.09939730962851079+0.35765389167296235j, 8) + + n, e, v = ni.get_vxc(mol, mf.grids, 'B88,', dm) + self.assertAlmostEqual(n, 9.984666945, 5) + self.assertAlmostEqual(e, -8.8304689765, 6) + self.assertAlmostEqual(lib.fp(v), 0.058825367300710196+0.37093946710262404j, 8) + + def test_fxc_col(self): + ni = r_numint.RNumInt() + ni.collinear = 'c' + mf = mol.DKS() + dm = mf.get_init_guess(mol, 'minao') + numpy.random.seed(10) + dm1 = numpy.random.random(dm.shape) + v = ni.get_fxc(mol, mf.grids, 'B88,', dm, dm1) + self.assertAlmostEqual(lib.fp(v), -0.6265667390145981+0.5244965520637875j, 6) + + @unittest.skipIf(mcfun is None, "mcfun library not found.") + def test_fxc_mcol(self): + ni = r_numint.RNumInt() + ni.collinear = 'm' + ni.spin_samples = 14 + mf = mol.DKS() + dm = mf.get_init_guess(mol, 'minao') + numpy.random.seed(10) + dm1 = numpy.random.random(dm.shape) + v = ni.get_fxc(mol, mf.grids, 'LDA,', dm, dm1) + self.assertAlmostEqual(lib.fp(v), -1.9850498137636299+1.4541338353513784j, 6) + + v = ni.get_fxc(mol, mf.grids, 'M06', dm, dm1) + self.assertAlmostEqual(lib.fp(v), -1.0864202540818795+0.06981358086231704j, 6) + + def test_get_rho(self): + ni = r_numint.RNumInt() + ni.collinear = 'c' + mf = mol.DKS() + grids = mf.grids.build() + dm = mf.get_init_guess(mol, 'minao') + rho = ni.get_rho(mol, dm, grids) + self.assertAlmostEqual(lib.fp(rho), -361.4682369790235, 8) + + ni.collinear = 'm' + ni.spin_samples = 50 + rho = ni.get_rho(mol, dm, grids) + self.assertAlmostEqual(lib.fp(rho), -361.4682369790235, 8) if __name__ == "__main__": diff --git a/pyscf/gto/mole.py b/pyscf/gto/mole.py index 64ae6a4ab4..12119b5f4f 100644 --- a/pyscf/gto/mole.py +++ b/pyscf/gto/mole.py @@ -98,9 +98,6 @@ BASE = getattr(__config__, 'BASE', 0) NORMALIZE_GTO = getattr(__config__, 'NORMALIZE_GTO', True) DISABLE_EVAL = getattr(__config__, 'DISABLE_EVAL', False) -# Whether to disable the explicit call to gc.collect(). gc.collect() may cause -# non-negligible overhead (https://github.com/pyscf/pyscf/issues/1038). -DISABLE_GC = getattr(__config__, 'DISABLE_GC', False) ARGPARSE = getattr(__config__, 'ARGPARSE', False) def M(*args, **kwargs): @@ -2480,9 +2477,6 @@ def build(self, dump_input=True, parse_arg=ARGPARSE, Collinear spin of each atom. Default is [0.0,]*natm ''' - if not DISABLE_GC: - gc.collect() # To release circular referred objects - if isinstance(dump_input, str): sys.stderr.write('Assigning the first argument %s to mol.atom\n' % dump_input)