From 1fd6dff61199cd8bf22cfd665c7a8127e1c78fab Mon Sep 17 00:00:00 2001 From: Qiming Sun Date: Mon, 11 Sep 2023 01:32:30 -0700 Subject: [PATCH] Update tests --- pyscf/ci/test/test_ucisd.py | 45 +++++++++++++++++- pyscf/ci/ucisd.py | 70 ---------------------------- pyscf/dft/test/test_libxc.py | 28 +++++------ pyscf/dft/test/test_xcfun.py | 22 ++++----- pyscf/fci/direct_spin1_cyl_sym.py | 2 +- pyscf/pbc/dft/test/test_multigrid.py | 2 +- pyscf/pbc/gto/test/test_cell.py | 4 +- pyscf/pbc/lib/kpts.py | 2 +- 8 files changed, 73 insertions(+), 102 deletions(-) diff --git a/pyscf/ci/test/test_ucisd.py b/pyscf/ci/test/test_ucisd.py index 539ce155d1..d6fed8c331 100644 --- a/pyscf/ci/test/test_ucisd.py +++ b/pyscf/ci/test/test_ucisd.py @@ -214,6 +214,47 @@ def test_rdm_h4(self): e2 += mol.energy_nuc() self.assertAlmostEqual(myci.e_tot, e2, 9) + def test_rdm12(self): + mol = gto.Mole() + mol.verbose = 0 + mol.atom = [ + ['O', ( 0., 0. , 0. )], + ['H', ( 0., -0.757, 0.587)], + ['H', ( 0., 0.757 , 0.587)],] + mol.basis = {'H': 'sto-3g', + 'O': 'sto-3g',} + mol.build() + mf = scf.UHF(mol).run(conv_tol=1e-12) + myci = mf.CISD() + eris = myci.ao2mo() + ecisd, civec = myci.kernel(eris=eris) + self.assertAlmostEqual(ecisd, -0.048878084082066106, 8) + + nmoa = mf.mo_energy[0].size + nmob = mf.mo_energy[1].size + rdm1 = myci.make_rdm1(civec) + rdm2 = myci.make_rdm2(civec) + eri_aa = ao2mo.kernel(mf._eri, mf.mo_coeff[0], compact=False).reshape([nmoa]*4) + eri_bb = ao2mo.kernel(mf._eri, mf.mo_coeff[1], compact=False).reshape([nmob]*4) + eri_ab = ao2mo.kernel(mf._eri, [mf.mo_coeff[0], mf.mo_coeff[0], + mf.mo_coeff[1], mf.mo_coeff[1]], compact=False) + eri_ab = eri_ab.reshape(nmoa,nmoa,nmob,nmob) + h1a = reduce(numpy.dot, (mf.mo_coeff[0].T, mf.get_hcore(), mf.mo_coeff[0])) + h1b = reduce(numpy.dot, (mf.mo_coeff[1].T, mf.get_hcore(), mf.mo_coeff[1])) + e2 = (numpy.einsum('ij,ji', h1a, rdm1[0]) + + numpy.einsum('ij,ji', h1b, rdm1[1]) + + numpy.einsum('ijkl,ijkl', eri_aa, rdm2[0]) * .5 + + numpy.einsum('ijkl,ijkl', eri_ab, rdm2[1]) + + numpy.einsum('ijkl,ijkl', eri_bb, rdm2[2]) * .5) + self.assertAlmostEqual(ecisd + mf.e_tot - mol.energy_nuc(), e2, 8) + + from_dm2 = (numpy.einsum('ijkk->ji', rdm2[0]) + + numpy.einsum('ijkk->ji', rdm2[1]))/(mol.nelectron-1) + self.assertAlmostEqual(abs(rdm1[0] - from_dm2).max(), 0, 8) + from_dm2 = (numpy.einsum('ijkk->ji', rdm2[2]) + + numpy.einsum('kkij->ji', rdm2[1]))/(mol.nelectron-1) + self.assertAlmostEqual(abs(rdm1[1] - from_dm2).sum(), 0, 8) + def test_ao_direct(self): mol = gto.Mole() mol.verbose = 0 @@ -266,13 +307,13 @@ def test_overlap(self): nocc = nocca, noccb = (4,3) numpy.random.seed(2) nvira, nvirb = nmo-nocca, nmo-noccb - cibra = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), + cibra = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1)[0], (numpy.random.rand(nocca,nvira), numpy.random.rand(noccb,nvirb)), (numpy.random.rand(nocca,nocca,nvira,nvira), numpy.random.rand(nocca,noccb,nvira,nvirb), numpy.random.rand(noccb,noccb,nvirb,nvirb))) - ciket = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), + ciket = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1)[0], (numpy.random.rand(nocca,nvira), numpy.random.rand(noccb,nvirb)), (numpy.random.rand(nocca,nocca,nvira,nvira), diff --git a/pyscf/ci/ucisd.py b/pyscf/ci/ucisd.py index b4022429c8..376c4fb093 100644 --- a/pyscf/ci/ucisd.py +++ b/pyscf/ci/ucisd.py @@ -977,73 +977,3 @@ def nuc_grad_method(self): def _cp(a): return numpy.array(a, copy=False, order='C') - - -if __name__ == '__main__': - from pyscf import gto - - mol = gto.Mole() - mol.verbose = 0 - mol.atom = [ - ['O', ( 0., 0. , 0. )], - ['H', ( 0., -0.757, 0.587)], - ['H', ( 0., 0.757 , 0.587)],] - mol.basis = {'H': 'sto-3g', - 'O': 'sto-3g',} -# mol.build() -# mf = scf.UHF(mol).run(conv_tol=1e-14) -# myci = CISD(mf) -# eris = myci.ao2mo() -# ecisd, civec = myci.kernel(eris=eris) -# print(ecisd - -0.048878084082066106) -# -# nmoa = mf.mo_energy[0].size -# nmob = mf.mo_energy[1].size -# rdm1 = myci.make_rdm1(civec) -# rdm2 = myci.make_rdm2(civec) -# eri_aa = ao2mo.kernel(mf._eri, mf.mo_coeff[0], compact=False).reshape([nmoa]*4) -# eri_bb = ao2mo.kernel(mf._eri, mf.mo_coeff[1], compact=False).reshape([nmob]*4) -# eri_ab = ao2mo.kernel(mf._eri, [mf.mo_coeff[0], mf.mo_coeff[0], -# mf.mo_coeff[1], mf.mo_coeff[1]], compact=False) -# eri_ab = eri_ab.reshape(nmoa,nmoa,nmob,nmob) -# h1a = reduce(numpy.dot, (mf.mo_coeff[0].T, mf.get_hcore(), mf.mo_coeff[0])) -# h1b = reduce(numpy.dot, (mf.mo_coeff[1].T, mf.get_hcore(), mf.mo_coeff[1])) -# e2 = (numpy.einsum('ij,ji', h1a, rdm1[0]) + -# numpy.einsum('ij,ji', h1b, rdm1[1]) + -# numpy.einsum('ijkl,ijkl', eri_aa, rdm2[0]) * .5 + -# numpy.einsum('ijkl,ijkl', eri_ab, rdm2[1]) + -# numpy.einsum('ijkl,ijkl', eri_bb, rdm2[2]) * .5) -# print(ecisd + mf.e_tot - mol.energy_nuc() - e2) # = 0 -# -# print(abs(rdm1[0] - (numpy.einsum('ijkk->ji', rdm2[0]) + -# numpy.einsum('ijkk->ji', rdm2[1]))/(mol.nelectron-1)).sum()) -# print(abs(rdm1[1] - (numpy.einsum('ijkk->ji', rdm2[2]) + -# numpy.einsum('kkij->ji', rdm2[1]))/(mol.nelectron-1)).sum()) - - if 1: - from pyscf.ci import ucisd - from pyscf import fci - nmo = 8 - nocc = nocca, noccb = (4,3) - numpy.random.seed(2) - nvira, nvirb = nmo-nocca, nmo-noccb - cibra = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), - (numpy.random.rand(nocca,nvira), - numpy.random.rand(noccb,nvirb)), - (numpy.random.rand(nocca,nocca,nvira,nvira), - numpy.random.rand(nocca,noccb,nvira,nvirb), - numpy.random.rand(noccb,noccb,nvirb,nvirb))) - ciket = ucisd.amplitudes_to_cisdvec(numpy.random.rand(1), - (numpy.random.rand(nocca,nvira), - numpy.random.rand(noccb,nvirb)), - (numpy.random.rand(nocca,nocca,nvira,nvira), - numpy.random.rand(nocca,noccb,nvira,nvirb), - numpy.random.rand(noccb,noccb,nvirb,nvirb))) - fcibra = ucisd.to_fcivec(cibra, nmo, nocc) - fciket = ucisd.to_fcivec(ciket, nmo, nocc) - s_mo = (numpy.random.random((nmo,nmo)), - numpy.random.random((nmo,nmo))) - s_mo = (s_mo[0], s_mo[0]) - s0 = fci.addons.overlap(fcibra, fciket, nmo, nocc, s_mo) - s1 = ucisd.overlap(cibra, ciket, nmo, nocc, s_mo) - print(s1, s0, 9) diff --git a/pyscf/dft/test/test_libxc.py b/pyscf/dft/test/test_libxc.py index 59ef8cdc07..4bacb402e5 100644 --- a/pyscf/dft/test/test_libxc.py +++ b/pyscf/dft/test/test_libxc.py @@ -250,38 +250,38 @@ def test_m05x(self): test_ref = numpy.array([-1.57730394, -2.12127045,-2.11297165,-0.00315462, 0.00000000, -0.00446935, 3.45640232, 4.42563831]) # libxc-5.1.2 exc, vxc, fxc, kxc = dft.libxc.eval_xc('1.38888888889*m05,', rho, 1, deriv=1) - self.assertAlmostEqual(float(exc)*1.8, test_ref[0], 5) + self.assertAlmostEqual(float(exc[0])*1.8, test_ref[0], 5) self.assertAlmostEqual(abs(vxc[0]-test_ref[1:3]).max(), 0, 6) self.assertAlmostEqual(abs(vxc[1]-test_ref[3:6]).max(), 0, 6) self.assertAlmostEqual(abs(vxc[3]-test_ref[6:8]).max(), 0, 5) exc, vxc, fxc, kxc = dft.libxc.eval_xc('1.38888888889*m05,', rho[0], 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.5746231988116002, 5) - self.assertAlmostEqual(float(vxc[0]), -0.8806121005703862, 6) - self.assertAlmostEqual(float(vxc[1]), -0.0032300155406846756, 7) - self.assertAlmostEqual(float(vxc[3]), 0.4474953100487698, 5) + self.assertAlmostEqual(float(exc[0]), -0.5746231988116002, 5) + self.assertAlmostEqual(float(vxc[0][0]), -0.8806121005703862, 6) + self.assertAlmostEqual(float(vxc[1][0]), -0.0032300155406846756, 7) + self.assertAlmostEqual(float(vxc[3][0]), 0.4474953100487698, 5) def test_camb3lyp(self): rho = numpy.array([1., 1., 0.1, 0.1]).reshape(-1,1) exc, vxc, fxc, kxc = dft.libxc.eval_xc('camb3lyp', rho, 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.5752559666317147, 7) - self.assertAlmostEqual(float(vxc[0]), -0.7709812578936763, 7) - self.assertAlmostEqual(float(vxc[1]), -0.0029862221286189846, 7) + self.assertAlmostEqual(float(exc[0]), -0.5752559666317147, 7) + self.assertAlmostEqual(float(vxc[0][0]), -0.7709812578936763, 7) + self.assertAlmostEqual(float(vxc[1][0]), -0.0029862221286189846, 7) self.assertEqual(dft.libxc.rsh_coeff('camb3lyp'), (0.33, 0.65, -0.46)) rho = numpy.array([1., 1., 0.1, 0.1]).reshape(-1,1) exc, vxc, fxc, kxc = dft.libxc.eval_xc('RSH(0.5,0.65,-0.46) + 0.46*ITYH + .35*B88,', rho, 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.48916154057161476, 9) - self.assertAlmostEqual(float(vxc[0]), -0.6761177630311709, 9) - self.assertAlmostEqual(float(vxc[1]), -0.002949151742087167, 9) + self.assertAlmostEqual(float(exc[0]), -0.48916154057161476, 9) + self.assertAlmostEqual(float(vxc[0][0]), -0.6761177630311709, 9) + self.assertAlmostEqual(float(vxc[1][0]), -0.002949151742087167, 9) def test_ityh(self): rho = numpy.array([1., 1., 0.1, 0.1]).reshape(-1,1) exc, vxc, fxc, kxc = dft.libxc.eval_xc('ityh,', rho, 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.6359945579326314, 7) - self.assertAlmostEqual(float(vxc[0]), -0.8712041561251518, 7) - self.assertAlmostEqual(float(vxc[1]), -0.003911167644579979, 7) + self.assertAlmostEqual(float(exc[0]), -0.6359945579326314, 7) + self.assertAlmostEqual(float(vxc[0][0]), -0.8712041561251518, 7) + self.assertAlmostEqual(float(vxc[1][0]), -0.003911167644579979, 7) self.assertEqual(dft.libxc.rsh_coeff('ityh,'), (0.2, 0.0, 0.0)) def test_deriv_order(self): diff --git a/pyscf/dft/test/test_xcfun.py b/pyscf/dft/test/test_xcfun.py index 5cb563a1e2..70bf9e7da1 100644 --- a/pyscf/dft/test/test_xcfun.py +++ b/pyscf/dft/test/test_xcfun.py @@ -203,31 +203,31 @@ def test_m05x(self): test_ref = numpy.array([-1.57876583, -2.12127045,-2.11264351,-0.00315462, 0.00000000, -0.00444560, 3.45640232, 4.4349756]) exc, vxc, fxc, kxc = dft.xcfun.eval_xc('m05,', rho, 1, deriv=3) - self.assertAlmostEqual(float(exc)*1.8, test_ref[0], 5) + self.assertAlmostEqual(float(exc[0])*1.8, test_ref[0], 5) self.assertAlmostEqual(abs(vxc[0]-test_ref[1:3]).max(), 0, 6) self.assertAlmostEqual(abs(vxc[1]-test_ref[3:6]).max(), 0, 6) self.assertAlmostEqual(abs(vxc[3]-test_ref[6:8]).max(), 0, 5) exc, vxc, fxc, kxc = dft.xcfun.eval_xc('m05,', rho[0], 0, deriv=3) - self.assertAlmostEqual(float(exc), -0.5746231988116002, 5) - self.assertAlmostEqual(float(vxc[0]), -0.8806121005703862, 6) - self.assertAlmostEqual(float(vxc[1]), -0.0032300155406846756, 7) - self.assertAlmostEqual(float(vxc[3]), 0.4474953100487698, 5) + self.assertAlmostEqual(float(exc[0]), -0.5746231988116002, 5) + self.assertAlmostEqual(float(vxc[0][0]), -0.8806121005703862, 6) + self.assertAlmostEqual(float(vxc[1][0]), -0.0032300155406846756, 7) + self.assertAlmostEqual(float(vxc[3][0]), 0.4474953100487698, 5) def test_camb3lyp(self): rho = numpy.array([1., 1., 0.1, 0.1]).reshape(-1,1) exc, vxc, fxc, kxc = dft.xcfun.eval_xc('camb3lyp', rho, 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.5752559666317147, 5) - self.assertAlmostEqual(float(vxc[0]), -0.7709812578936763, 5) - self.assertAlmostEqual(float(vxc[1]), -0.0029862221286189846, 7) + self.assertAlmostEqual(float(exc[0]), -0.5752559666317147, 5) + self.assertAlmostEqual(float(vxc[0][0]), -0.7709812578936763, 5) + self.assertAlmostEqual(float(vxc[1][0]), -0.0029862221286189846, 7) self.assertEqual(dft.xcfun.rsh_coeff('camb3lyp'), (0.33, 0.65, -0.46)) rho = numpy.array([1., 1., 0.1, 0.1]).reshape(-1,1) exc, vxc, fxc, kxc = dft.xcfun.eval_xc('RSH(0.65;-0.46;0.5) + BECKECAMX', rho, 0, deriv=1) - self.assertAlmostEqual(float(exc), -0.48916154057161476, 9) - self.assertAlmostEqual(float(vxc[0]), -0.6761177630311709, 9) - self.assertAlmostEqual(float(vxc[1]), -0.002949151742087167, 9) + self.assertAlmostEqual(float(exc[0]), -0.48916154057161476, 9) + self.assertAlmostEqual(float(vxc[0][0]), -0.6761177630311709, 9) + self.assertAlmostEqual(float(vxc[1][0]), -0.002949151742087167, 9) def test_define_xc(self): def eval_xc(xc_code, rho, spin=0, relativity=0, deriv=1, verbose=None): diff --git a/pyscf/fci/direct_spin1_cyl_sym.py b/pyscf/fci/direct_spin1_cyl_sym.py index a666a393f2..63f7725b11 100644 --- a/pyscf/fci/direct_spin1_cyl_sym.py +++ b/pyscf/fci/direct_spin1_cyl_sym.py @@ -62,7 +62,7 @@ def contract_2e(eri, fcivec, norb, nelec, link_index=None, orbsym=None, wfnsym=0 wfn_momentum = symm.basis.linearmole_irrep2momentum(wfnsym) wfnsym_in_d2h = wfnsym % 10 - wfn_ungerade = wfnsym_in_d2h >= 4 + wfn_ungerade = int(wfnsym_in_d2h >= 4) orbsym_d2h = orbsym % 10 orb_ungerade = orbsym_d2h >= 4 if np.any(orb_ungerade) or wfn_ungerade: diff --git a/pyscf/pbc/dft/test/test_multigrid.py b/pyscf/pbc/dft/test/test_multigrid.py index dce220db73..2cd11e7732 100644 --- a/pyscf/pbc/dft/test/test_multigrid.py +++ b/pyscf/pbc/dft/test/test_multigrid.py @@ -466,7 +466,7 @@ def test_rcut_vs_ke_cut(self): self.assertAlmostEqual(exc1, exc2, 7) self.assertAlmostEqual(abs(v1-v2).max(), 0, 7) - def test_multigrid_krks_1(self): + def test_multigrid_krks_high_cost(self): cell = gto.M( a = numpy.eye(3)*3.5668, atom = '''C 0. 0. 0. diff --git a/pyscf/pbc/gto/test/test_cell.py b/pyscf/pbc/gto/test/test_cell.py index 7d9f23528b..c035d271dc 100644 --- a/pyscf/pbc/gto/test/test_cell.py +++ b/pyscf/pbc/gto/test/test_cell.py @@ -158,12 +158,12 @@ def test_ewald_vs_supercell(self): celldims = [2, 1, 1] scell = pbctools.super_cell(cell, celldims) - e_nuc_2 = scell.energy_nuc() / np.product(celldims) + e_nuc_2 = scell.energy_nuc() / np.prod(celldims) self.assertAlmostEqual(e_nuc_1, e_nuc_2, 8) celldims = [2, 2, 1] scell = pbctools.super_cell(cell, celldims) - e_nuc_2 = scell.energy_nuc() / np.product(celldims) + e_nuc_2 = scell.energy_nuc() / np.prod(celldims) self.assertAlmostEqual(e_nuc_1, e_nuc_2, 8) def test_ewald_2d_inf_vacuum(self): diff --git a/pyscf/pbc/lib/kpts.py b/pyscf/pbc/lib/kpts.py index 959d174483..ced0443c4f 100644 --- a/pyscf/pbc/lib/kpts.py +++ b/pyscf/pbc/lib/kpts.py @@ -273,7 +273,7 @@ def make_k4_ibz(kpts, sym='s1', return_ops=False): idx = np.lexsort(k4_s2.T[::-1,:]) bz2ibz_s2 = np.arange(len(bz2ibz)) for i in range(len(bz2ibz)): - bz2ibz_s2[i] = np.where(idx == ibz_s22ibz_s2_refine[ibz2ibz_s2[bz2ibz[i]]])[0] + bz2ibz_s2[i] = np.where(idx == ibz_s22ibz_s2_refine[ibz2ibz_s2[bz2ibz[i]]])[0][0] return k4_s2[idx], weight_s2[idx], bz2ibz_s2 else: k4_s4 = []