def test_ucc_update_amps(self): gcc1 = gccsd.GCCSD(gmf) r1g = gcc1.spatial2spin(ucc1.t1, orbspin) r2g = gcc1.spatial2spin(ucc1.t2, orbspin) r1g, r2g = gcc1.update_amps(r1g, r2g, gcc1.ao2mo()) u1g = gcc1.spin2spatial(r1g, orbspin) u2g = gcc1.spin2spatial(r2g, orbspin) t1, t2 = ucc1.update_amps(ucc1.t1, ucc1.t2, eris1) self.assertAlmostEqual(abs(u1g[0] - t1[0]).max(), 0, 7) self.assertAlmostEqual(abs(u1g[1] - t1[1]).max(), 0, 7) self.assertAlmostEqual(abs(u2g[0] - t2[0]).max(), 0, 6) self.assertAlmostEqual(abs(u2g[1] - t2[1]).max(), 0, 6) self.assertAlmostEqual(abs(u2g[2] - t2[2]).max(), 0, 6) self.assertAlmostEqual( float(abs(r1g - gcc1.spatial2spin(t1, orbspin)).max()), 0, 6) self.assertAlmostEqual( float(abs(r2g - gcc1.spatial2spin(t2, orbspin)).max()), 0, 6) self.assertAlmostEqual(uccsd.energy(ucc1, r1, r2, eris1), -7.2775115532675771, 8) e0, t1, t2 = ucc1.init_amps(eris1) self.assertAlmostEqual(lib.finger(cc.addons.spatial2spin(t1, orbspin)), 148.57054876656397, 8) self.assertAlmostEqual(lib.finger(cc.addons.spatial2spin(t2, orbspin)), -349.94207953071475, 8) self.assertAlmostEqual(e0, 30.640616265644827, 2)
def test_eaccsd_koopmans(self): e,v = ucc.eaccsd(nroots=6, koopmans=True) self.assertAlmostEqual(e[0], 0.19050592137699729, 6) self.assertAlmostEqual(e[2], 0.28345228891172214, 6) self.assertAlmostEqual(e[4], 1.02136493172648370, 6) gcc1 = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)).run() e1 = gcc1.eaccsd(nroots=6, koopmans=True)[0] self.assertAlmostEqual(abs(e1-e).max(), 0, 6)
def test_ccsd_t_complex(self): mol = gto.M() numpy.random.seed(12) nocc, nvir = 3, 4 nmo = nocc + nvir eris = cc.rccsd._ChemistsERIs() eri1 = (numpy.random.random( (nmo, nmo, nmo, nmo)) + numpy.random.random( (nmo, nmo, nmo, nmo)) * .8j - .5 - .4j) eri1 = eri1 + eri1.transpose(1, 0, 2, 3) eri1 = eri1 + eri1.transpose(0, 1, 3, 2) eri1 = eri1 + eri1.transpose(2, 3, 0, 1) eri1 *= .1 eris.ovvv = eri1[:nocc, nocc:, nocc:, nocc:] eris.ovoo = eri1[:nocc, nocc:, :nocc, :nocc] eris.ovov = eri1[:nocc, nocc:, :nocc, nocc:] t1 = (numpy.random.random((nocc, nvir)) * .1 + numpy.random.random( (nocc, nvir)) * .1j) t2 = (numpy.random.random( (nocc, nocc, nvir, nvir)) * .1 + numpy.random.random( (nocc, nocc, nvir, nvir)) * .1j) t2 = t2 + t2.transpose(1, 0, 3, 2) mf = scf.RHF(mol) mcc = cc.CCSD(mf) f = (numpy.random.random((nmo, nmo)) * .1 + numpy.random.random( (nmo, nmo)) * .1j) eris.fock = f + f.T.conj() + numpy.diag(numpy.arange(nmo)) eris.mo_energy = eris.fock.diagonal().real e0 = ccsd_t.kernel(mcc, eris, t1, t2) eri2 = numpy.zeros((nmo * 2, nmo * 2, nmo * 2, nmo * 2), dtype=numpy.complex128) orbspin = numpy.zeros(nmo * 2, dtype=int) orbspin[1::2] = 1 eri2[0::2, 0::2, 0::2, 0::2] = eri1 eri2[1::2, 1::2, 0::2, 0::2] = eri1 eri2[0::2, 0::2, 1::2, 1::2] = eri1 eri2[1::2, 1::2, 1::2, 1::2] = eri1 eri2 = eri2.transpose(0, 2, 1, 3) - eri2.transpose(0, 2, 3, 1) fock = numpy.zeros((nmo * 2, nmo * 2), dtype=numpy.complex128) fock[0::2, 0::2] = eris.fock fock[1::2, 1::2] = eris.fock eris1 = gccsd._PhysicistsERIs() eris1.ovvv = eri2[:nocc * 2, nocc * 2:, nocc * 2:, nocc * 2:] eris1.oovv = eri2[:nocc * 2, :nocc * 2, nocc * 2:, nocc * 2:] eris1.ooov = eri2[:nocc * 2, :nocc * 2, :nocc * 2, nocc * 2:] eris1.fock = fock eris1.mo_energy = fock.diagonal().real t1 = gccsd.spatial2spin(t1, orbspin) t2 = gccsd.spatial2spin(t2, orbspin) gcc = gccsd.GCCSD(scf.GHF(gto.M())) e1 = gccsd_t.kernel(gcc, eris1, t1, t2) self.assertAlmostEqual(e0, e1.real, 9) self.assertAlmostEqual(e1, -0.98756910139720788 - 0.0019567929592079489j, 9)
def GCCSD(mf, frozen=None, mo_coeff=None, mo_occ=None): from pyscf.soscf import newton_ah if isinstance(mf, newton_ah._CIAH_SOSCF) or not isinstance(mf, scf.ghf.GHF): mf = scf.addons.convert_to_ghf(mf) if getattr(mf, 'with_df', None): raise NotImplementedError('DF-GCCSD') else: return gccsd.GCCSD(mf, frozen, mo_coeff, mo_occ)
def test_rdm(self): nocc = 5 nvir = 7 mol = gto.M() mf = scf.UHF(mol) mf.mo_occ = numpy.zeros((2, nocc + nvir)) mf.mo_occ[:, :nocc] = 1 mycc = uccsd.UCCSD(mf) def antisym(t2): t2 = t2 - t2.transpose(0, 1, 3, 2) t2 = t2 - t2.transpose(1, 0, 2, 3) return t2 orbspin = numpy.zeros((nocc + nvir) * 2, dtype=int) orbspin[1::2] = 1 numpy.random.seed(1) t1 = numpy.random.random((2, nocc, nvir)) * .1 - .1 t2ab = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1 t2aa = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) t2bb = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) t2 = (t2aa, t2ab, t2bb) l1 = numpy.random.random((2, nocc, nvir)) * .1 - .1 l2ab = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1 l2aa = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) l2bb = antisym(numpy.random.random((nocc, nocc, nvir, nvir)) * .1 - .1) l2 = (l2aa, l2ab, l2bb) dm1a, dm1b = mycc.make_rdm1(t1, t2, l1, l2) dm2aa, dm2ab, dm2bb = mycc.make_rdm2(t1, t2, l1, l2) ia = orbspin == 0 ib = orbspin == 1 oa = orbspin[:nocc * 2] == 0 ob = orbspin[:nocc * 2] == 1 va = orbspin[nocc * 2:] == 0 vb = orbspin[nocc * 2:] == 1 t1 = addons.spatial2spin(t1, orbspin) t2 = addons.spatial2spin(t2, orbspin) l1 = addons.spatial2spin(l1, orbspin) l2 = addons.spatial2spin(l2, orbspin) mf1 = scf.GHF(mol) mf1.mo_occ = numpy.zeros((nocc + nvir) * 2) mf.mo_occ[:, :nocc * 2] = 1 mycc1 = gccsd.GCCSD(mf1) dm1 = mycc1.make_rdm1(t1, t2, l1, l2) dm2 = mycc1.make_rdm2(t1, t2, l1, l2) self.assertAlmostEqual(abs(dm1[ia][:, ia] - dm1a).max(), 0, 9) self.assertAlmostEqual(abs(dm1[ib][:, ib] - dm1b).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ia][:, ia][:, :, ia][:, :, :, ia] - dm2aa).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ia][:, ia][:, :, ib][:, :, :, ib] - dm2ab).max(), 0, 9) self.assertAlmostEqual( abs(dm2[ib][:, ib][:, :, ib][:, :, :, ib] - dm2bb).max(), 0, 9)
def setUpClass(cls): """ Oxygen atom test vs ORCA-MRCC data. ORCA version 3.0.3 Input example: ! cc-pvdz UHF TightSCF %mrcc method "CCSDT" ETol 10 end %pal nprocs 4 end * xyzfile 0 1 initial.xyz ORCA reference energies: HF -74.6652538779 CCS -74.841686696943 CCSD -74.819248718982 CCSDT -74.829163218204 """ cls.mol = gto.Mole() cls.mol.verbose = 0 cls.mol.atom = "O 0 0 0" cls.mol.basis = 'cc-pvdz' cls.mol.build() cls.mf = scf.RHF(cls.mol) cls.mf.conv_tol = 1e-11 cls.mf.kernel() testing.assert_allclose(cls.mf.e_tot, -74.6652538779, atol=1e-4) cls.mf = scf.addons.convert_to_ghf(cls.mf) cls.ccsd = gccsd.GCCSD(cls.mf, frozen=2) cls.ccsd.kernel() cls.ccsd.conv_tol_normt = 1e-8 cls.ccsd.solve_lambda() cls.nroots = 2 cls.eomip = eom_gccsd.EOMIP(cls.ccsd) cls.eomip.conv_tol = 1e-12 cls.eomip.kernel(nroots=cls.nroots, koopmans=True) cls.eomea = eom_gccsd.EOMEA(cls.ccsd) cls.eomea.conv_tol = 1e-12 cls.eomea.kernel(nroots=cls.nroots, koopmans=True)
def test_update_amps(self): mol = gto.M() nocc, nvir = 8, 14 nmo = nocc + nvir nmo_pair = nmo * (nmo + 1) // 2 mf = scf.GHF(mol) numpy.random.seed(12) mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) mf.mo_coeff = numpy.random.random((nmo, nmo)) mf.mo_energy = numpy.arange(0., nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 1 vhf = numpy.random.random((nmo, nmo)) + numpy.random.random( (nmo, nmo)) + 1j vhf = vhf + vhf.conj().T mf.get_veff = lambda *args: vhf cinv = numpy.linalg.inv(mf.mo_coeff) mf.get_hcore = lambda *args: (reduce(numpy.dot, (cinv.T * mf.mo_energy, cinv)) - vhf) nmo_pair = nmo * (nmo // 2 + 1) // 4 mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) mycc = gccsd.GCCSD(mf) eris = mycc.ao2mo() eris.oooo = eris.oooo + numpy.sin(eris.oooo) * 1j eris.oooo = eris.oooo + eris.oooo.conj().transpose(2, 3, 0, 1) eris.ooov = eris.ooov + numpy.sin(eris.ooov) * 1j eris.oovv = eris.oovv + numpy.sin(eris.oovv) * 1j eris.ovov = eris.ovov + numpy.sin(eris.ovov) * 1j eris.ovov = eris.ovov + eris.ovov.conj().transpose(2, 3, 0, 1) eris.ovvv = eris.ovvv + numpy.sin(eris.ovvv) * 1j eris.vvvv = eris.vvvv + numpy.sin(eris.vvvv) * 1j eris.vvvv = eris.vvvv + eris.vvvv.conj().transpose(2, 3, 0, 1) a = numpy.random.random((nmo, nmo)) * .1 eris.fock += a + a.T t1 = numpy.random.random((nocc, nvir)) * .1 + numpy.random.random( (nocc, nvir)) * .1j t2 = (numpy.random.random( (nocc, nocc, nvir, nvir)) * .1 + numpy.random.random( (nocc, nocc, nvir, nvir)) * .1j) t2 = t2 - t2.transpose(0, 1, 3, 2) t2 = t2 - t2.transpose(1, 0, 2, 3) r1, r2 = mycc.vector_to_amplitudes(mycc.amplitudes_to_vector(t1, t2)) self.assertAlmostEqual(abs(t1 - r1).max(), 0, 14) self.assertAlmostEqual(abs(t2 - r2).max(), 0, 14) t1a, t2a = mycc.update_amps(t1, t2, eris) self.assertAlmostEqual(lib.finger(t1a), 20.805393111419136 - 300.1138026015621j, 9) self.assertAlmostEqual(lib.finger(t2a), -313.54117398035567 + 8.3700078645035205j, 9)
def setUpModule(): global mol, mf, gcc1 mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.verbose = 5 mol.output = '/dev/null' mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run(conv_tol=1e-12) mf = scf.addons.convert_to_ghf(mf) gcc1 = gccsd.GCCSD(mf).run(conv_tol=1e-9)
def test_update_lambda_real(self): numpy.random.seed(21) eris = mycc.ao2mo() gcc1 = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) eri1 = gcc1.ao2mo() orbspin = eri1.orbspin nocc = mol.nelectron nvir = mol.nao_nr() * 2 - nocc t1r = numpy.random.random((nocc, nvir)) * .1 t2r = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 t2r = t2r - t2r.transpose(1, 0, 2, 3) t2r = t2r - t2r.transpose(0, 1, 3, 2) l1r = numpy.random.random((nocc, nvir)) * .1 l2r = numpy.random.random((nocc, nocc, nvir, nvir)) * .1 l2r = l2r - l2r.transpose(1, 0, 2, 3) l2r = l2r - l2r.transpose(0, 1, 3, 2) t1r = addons.spin2spatial(t1r, orbspin) t2r = addons.spin2spatial(t2r, orbspin) t1r = addons.spatial2spin(t1r, orbspin) t2r = addons.spatial2spin(t2r, orbspin) l1r = addons.spin2spatial(l1r, orbspin) l2r = addons.spin2spatial(l2r, orbspin) l1r = addons.spatial2spin(l1r, orbspin) l2r = addons.spatial2spin(l2r, orbspin) imds = gccsd_lambda.make_intermediates(gcc1, t1r, t2r, eri1) l1ref, l2ref = gccsd_lambda.update_lambda(gcc1, t1r, t2r, l1r, l2r, eri1, imds) t1 = addons.spin2spatial(t1r, orbspin) t2 = addons.spin2spatial(t2r, orbspin) l1 = addons.spin2spatial(l1r, orbspin) l2 = addons.spin2spatial(l2r, orbspin) imds = uccsd_lambda.make_intermediates(mycc, t1, t2, eris) l1, l2 = uccsd_lambda.update_lambda(mycc, t1, t2, l1, l2, eris, imds) self.assertAlmostEqual( float(abs(addons.spatial2spin(l1, orbspin) - l1ref).max()), 0, 8) self.assertAlmostEqual( float(abs(addons.spatial2spin(l2, orbspin) - l2ref).max()), 0, 8) l1ref = addons.spin2spatial(l1ref, orbspin) l2ref = addons.spin2spatial(l2ref, orbspin) self.assertAlmostEqual(abs(l1[0] - l1ref[0]).max(), 0, 8) self.assertAlmostEqual(abs(l1[1] - l1ref[1]).max(), 0, 8) self.assertAlmostEqual(abs(l2[0] - l2ref[0]).max(), 0, 8) self.assertAlmostEqual(abs(l2[1] - l2ref[1]).max(), 0, 8) self.assertAlmostEqual(abs(l2[2] - l2ref[2]).max(), 0, 8)
def test_ERIS(self): gcc = gccsd.GCCSD(mf, frozen=4) numpy.random.seed(9) mo_coeff0 = numpy.random.random(mf.mo_coeff.shape) - .9 nao = mo_coeff0.shape[0] // 2 orbspin = numpy.array([ 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1 ]) mo_coeff0[nao:, orbspin == 0] = 0 mo_coeff0[:nao, orbspin == 1] = 0 mo_coeff1 = mo_coeff0.copy() mo_coeff1[-1, 0] = 1e-12 eris = gccsd._make_eris_incore(gcc, mo_coeff0) self.assertAlmostEqual(lib.finger(eris.oooo), 15.97533838570434, 9) self.assertAlmostEqual(lib.finger(eris.ooov), -80.97666019169982, 9) self.assertAlmostEqual(lib.finger(eris.oovv), 278.00028168381675, 9) self.assertAlmostEqual(lib.finger(eris.ovov), 2.34326750142844, 9) self.assertAlmostEqual(lib.finger(eris.ovvv), 908.61659731634768, 9) self.assertAlmostEqual(lib.finger(eris.vvvv), 756.77383112217694, 9) eris = gccsd._make_eris_outcore(gcc, mo_coeff0) self.assertAlmostEqual(lib.finger(eris.oooo), 15.97533838570434, 9) self.assertAlmostEqual(lib.finger(eris.ooov), -80.97666019169982, 9) self.assertAlmostEqual(lib.finger(eris.oovv), 278.00028168381675, 9) self.assertAlmostEqual(lib.finger(eris.ovov), 2.34326750142844, 9) self.assertAlmostEqual(lib.finger(eris.ovvv), 908.61659731634768, 9) self.assertAlmostEqual(lib.finger(eris.vvvv), 756.77383112217694, 9) eris = gccsd._make_eris_incore(gcc, mo_coeff1) self.assertAlmostEqual(lib.finger(eris.oooo), 15.97533838570434, 9) self.assertAlmostEqual(lib.finger(eris.ooov), -80.97666019169982, 9) self.assertAlmostEqual(lib.finger(eris.oovv), 278.00028168381675, 9) self.assertAlmostEqual(lib.finger(eris.ovov), 2.34326750142844, 9) self.assertAlmostEqual(lib.finger(eris.ovvv), 908.61659731634768, 9) self.assertAlmostEqual(lib.finger(eris.vvvv), 756.77383112217694, 9) gcc.max_memory = 0 eris = gcc.ao2mo(mo_coeff1) self.assertAlmostEqual(lib.finger(eris.oooo), 15.97533838570434, 9) self.assertAlmostEqual(lib.finger(eris.ooov), -80.97666019169982, 9) self.assertAlmostEqual(lib.finger(eris.oovv), 278.00028168381675, 9) self.assertAlmostEqual(lib.finger(eris.ovov), 2.34326750142844, 9) self.assertAlmostEqual(lib.finger(eris.ovvv), 908.61659731634768, 9) self.assertAlmostEqual(lib.finger(eris.vvvv), 756.77383112217694, 9)
def make_mycc1(): mol = gto.M() nocc, nvir = 8, 14 nmo = nocc + nvir nmo_pair = nmo * (nmo + 1) // 2 mf = scf.GHF(mol) numpy.random.seed(12) mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) mf.mo_coeff = numpy.random.random((nmo, nmo)) mf.mo_energy = numpy.arange(0., nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 1 vhf = numpy.random.random((nmo, nmo)) + numpy.random.random( (nmo, nmo)) + 1j vhf = vhf + vhf.conj().T mf.get_veff = lambda *args: vhf cinv = numpy.linalg.inv(mf.mo_coeff) mf.get_hcore = lambda *args: (reduce(numpy.dot, (cinv.T * mf.mo_energy, cinv)) - vhf) nmo_pair = nmo * (nmo // 2 + 1) // 4 mf._eri = numpy.random.random(nmo_pair * (nmo_pair + 1) // 2) mycc1 = gccsd.GCCSD(mf) eris1 = mycc1.ao2mo() eris1.oooo = eris1.oooo + numpy.sin(eris1.oooo) * 1j eris1.oooo = eris1.oooo + eris1.oooo.conj().transpose(2, 3, 0, 1) eris1.ooov = eris1.ooov + numpy.sin(eris1.ooov) * 1j eris1.oovv = eris1.oovv + numpy.sin(eris1.oovv) * 1j eris1.ovov = eris1.ovov + numpy.sin(eris1.ovov) * 1j eris1.ovvv = eris1.ovvv + numpy.sin(eris1.ovvv) * 1j eris1.vvvv = eris1.vvvv + numpy.sin(eris1.vvvv) * 1j eris1.vvvv = eris1.vvvv + eris1.vvvv.conj().transpose(2, 3, 0, 1) a = numpy.random.random((nmo, nmo)) * .1 eris1.fock += a + a.T t1 = numpy.random.random((nocc, nvir)) * .1 + numpy.random.random( (nocc, nvir)) * .1j t2 = (numpy.random.random( (nocc, nocc, nvir, nvir)) * .1 + numpy.random.random( (nocc, nocc, nvir, nvir)) * .1j) t2 = t2 - t2.transpose(0, 1, 3, 2) t2 = t2 - t2.transpose(1, 0, 2, 3) mycc1.t1 = t1 mycc1.t2 = t2 return mycc1, eris1
def test_rdm_real(self): nocc = 6 nvir = 10 mf = scf.GHF(mol) nmo = nocc + nvir npair = nmo * (nmo // 2 + 1) // 4 numpy.random.seed(12) mf._eri = numpy.random.random(npair * (npair + 1) // 2) * .3 hcore = numpy.random.random((nmo, nmo)) * .5 hcore = hcore + hcore.T + numpy.diag(range(nmo)) * 2 mf.get_hcore = lambda *args: hcore mf.get_ovlp = lambda *args: numpy.eye(nmo) mf.mo_coeff = numpy.eye(nmo) mf.mo_occ = numpy.zeros(nmo) mf.mo_occ[:nocc] = 1 mycc = gccsd.GCCSD(mf) ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() dm1 = gccsd_rdm.make_rdm1(mycc, t1, t2, l1, l2) dm2 = gccsd_rdm.make_rdm2(mycc, t1, t2, l1, l2) nao = nmo // 2 mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] eri = ao2mo.kernel(mf._eri, mo_a) eri += ao2mo.kernel(mf._eri, mo_b) eri1 = ao2mo.kernel(mf._eri, (mo_a, mo_a, mo_b, mo_b)) eri += eri1 eri += eri1.T eri = ao2mo.restore(1, eri, nmo) h1 = reduce(numpy.dot, (mf.mo_coeff.T.conj(), hcore, mf.mo_coeff)) e1 = numpy.einsum('ij,ji', h1, dm1) e1 += numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mycc.e_tot, 7) self.assertAlmostEqual( abs(dm2 - dm2.transpose(1, 0, 3, 2).conj()).max(), 0, 9) self.assertAlmostEqual( abs(dm2 - dm2.transpose(2, 3, 0, 1)).max(), 0, 9) self.assertAlmostEqual( abs(dm2 + dm2.transpose(2, 1, 0, 3)).max(), 0, 9) self.assertAlmostEqual( abs(dm2 + dm2.transpose(0, 3, 2, 1)).max(), 0, 9)
def convert_to_gccsd(mycc): from pyscf import scf from pyscf.cc import gccsd if isinstance(mycc, gccsd.GCCSD): return mycc mf = scf.addons.convert_to_ghf(mycc._scf) gcc = gccsd.GCCSD(mf) assert (mycc._nocc is None) assert (mycc._nmo is None) gcc.__dict__.update(mycc.__dict__) gcc._scf = mf gcc.mo_coeff = mf.mo_coeff gcc.mo_occ = mf.mo_occ if isinstance(mycc.frozen, (int, numpy.integer)): gcc.frozen = mycc.frozen * 2 elif not (mycc.frozen is None or mycc.frozen == 0): raise NotImplementedError gcc.t1 = spatial2spin(mycc.t1, mf.mo_coeff.orbspin) gcc.t2 = spatial2spin(mycc.t2, mf.mo_coeff.orbspin) return gcc
def setUpClass(cls): cls.mol = gto.Mole() cls.mol.verbose = 0 cls.mol.atom = "H 0 0 0; H 0.74 0 0" cls.mol.basis = 'ccpvdz' cls.mol.build() cls.mf = scf.GHF(cls.mol) cls.mf.kernel() cls.ccsd = gccsd.GCCSD(cls.mf) cls.ccsd.kernel() cls.ccsd.conv_tol_normt = 1e-8 cls.ccsd.solve_lambda() cls.nroots = 2 cls.eomip = eom_gccsd.EOMIP(cls.ccsd) cls.eomip.conv_tol = 1e-12 cls.eomip.kernel(nroots=cls.nroots) cls.eomea = eom_gccsd.EOMEA(cls.ccsd) cls.eomea.conv_tol = 1e-12 cls.eomea.kernel(nroots=cls.nroots)
def setUpClass(cls): """ H20 molecule test vs ORCA-MRCC data. ORCA reference energies: HF -75.97354725 CCS -- CCSD -76.185805898396 CCSDT -76.189327633478 """ cls.mol = gto.Mole() cls.mol.verbose = 0 cls.mol.atom = "O 0 0 0; H 0.758602 0.000000 0.504284; H 0.758602 0.000000 -0.504284" cls.mol.unit = "angstrom" cls.mol.basis = 'cc-pvdz' cls.mol.build() cls.mf = scf.GHF(cls.mol) cls.mf.conv_tol = 1e-11 cls.mf.kernel() testing.assert_allclose(cls.mf.e_tot, -75.97354725, atol=1e-4) cls.ccsd = gccsd.GCCSD(cls.mf, frozen=2) cls.ccsd.kernel() cls.ccsd.conv_tol_normt = 1e-8 cls.ccsd.solve_lambda() cls.nroots = 4 cls.eomip = eom_gccsd.EOMIP(cls.ccsd) cls.eomip.conv_tol = 1e-12 cls.eomip.kernel(nroots=cls.nroots) cls.eomea = eom_gccsd.EOMEA(cls.ccsd) cls.eomea.conv_tol = 1e-12 cls.eomea.kernel(nroots=cls.nroots)
def test_update_lambda_complex(self): mo_coeff = mf.mo_coeff + np.sin(mf.mo_coeff) * .01j nao = mo_coeff.shape[0] eri = ao2mo.restore(1, mf._eri, nao) eri0 = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_coeff.conj(), mo_coeff, mo_coeff.conj(), mo_coeff) nocc, nvir = 5, nao - 5 eris = rccsd._ChemistsERIs(mol) eris.oooo = eri0[:nocc, :nocc, :nocc, :nocc].copy() eris.ovoo = eri0[:nocc, nocc:, :nocc, :nocc].copy() eris.oovv = eri0[:nocc, :nocc, nocc:, nocc:].copy() eris.ovvo = eri0[:nocc, nocc:, nocc:, :nocc].copy() eris.ovov = eri0[:nocc, nocc:, :nocc, nocc:].copy() eris.ovvv = eri0[:nocc, nocc:, nocc:, nocc:].copy() eris.vvvv = eri0[nocc:, nocc:, nocc:, nocc:].copy() eris.fock = np.diag(mf.mo_energy) np.random.seed(1) t1 = np.random.random((nocc, nvir)) + np.random.random( (nocc, nvir)) * .1j - .5 t2 = np.random.random((nocc, nocc, nvir, nvir)) - .5 t2 = t2 + np.sin(t2) * .1j t2 = t2 + t2.transpose(1, 0, 3, 2) l1 = np.random.random((nocc, nvir)) + np.random.random( (nocc, nvir)) * .1j - .5 l2 = np.random.random((nocc, nocc, nvir, nvir)) - .5 l2 = l2 + np.sin(l2) * .1j l2 = l2 + l2.transpose(1, 0, 3, 2) mycc = rccsd.RCCSD(mf) imds = rccsd_lambda.make_intermediates(mycc, t1, t2, eris) l1new_ref, l2new_ref = rccsd_lambda.update_lambda( mycc, t1, t2, l1, l2, eris, imds) orbspin = np.zeros(nao * 2, dtype=int) orbspin[1::2] = 1 eri1 = np.zeros([nao * 2] * 4, dtype=np.complex128) eri1[0::2,0::2,0::2,0::2] = \ eri1[0::2,0::2,1::2,1::2] = \ eri1[1::2,1::2,0::2,0::2] = \ eri1[1::2,1::2,1::2,1::2] = eri0 eri1 = eri1.transpose(0, 2, 1, 3) - eri1.transpose(0, 2, 3, 1) erig = gccsd._PhysicistsERIs(mol) nocc *= 2 nvir *= 2 erig.oooo = eri1[:nocc, :nocc, :nocc, :nocc].copy() erig.ooov = eri1[:nocc, :nocc, :nocc, nocc:].copy() erig.ovov = eri1[:nocc, nocc:, :nocc, nocc:].copy() erig.ovvo = eri1[:nocc, nocc:, nocc:, :nocc].copy() erig.oovv = eri1[:nocc, :nocc, nocc:, nocc:].copy() erig.ovvv = eri1[:nocc, nocc:, nocc:, nocc:].copy() erig.vvvv = eri1[nocc:, nocc:, nocc:, nocc:].copy() mo_e = np.array([mf.mo_energy] * 2) erig.fock = np.diag(mo_e.T.ravel()) erig.mo_energy = erig.fock.diagonal() myccg = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) t1, t2 = myccg.amplitudes_from_ccsd(t1, t2) l1, l2 = myccg.amplitudes_from_ccsd(l1, l2) imds = gccsd_lambda.make_intermediates(myccg, t1, t2, erig) l1new, l2new = gccsd_lambda.update_lambda(myccg, t1, t2, l1, l2, erig, imds) self.assertAlmostEqual(float(abs(l1new[0::2, 0::2] - l1new_ref).max()), 0, 9) l2aa = l2new[0::2, 0::2, 0::2, 0::2] l2ab = l2new[0::2, 1::2, 0::2, 1::2] self.assertAlmostEqual(float(abs(l2ab - l2new_ref).max()), 0, 9) self.assertAlmostEqual( float(abs(l2ab - l2ab.transpose(1, 0, 2, 3) - l2aa).max()), 0, 9)
def test_rdm_vs_rccsd(self): mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.verbose = 5 mol.output = '/dev/null' mol.basis = '631g' mol.build() mf = scf.RHF(mol).run() myrcc = ccsd.CCSD(mf).set(diis_start_cycle=1).run() rdm1 = myrcc.make_rdm1() rdm2 = myrcc.make_rdm2() mf = scf.addons.convert_to_ghf(mf) mygcc = gccsd.GCCSD(mf).set(diis_start_cycle=1).run() dm1 = mygcc.make_rdm1() dm2 = mygcc.make_rdm2() nao = mol.nao_nr() mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] nmo = mo_a.shape[1] eri = ao2mo.kernel(mf._eri, mo_a + mo_b, compact=False).reshape([nmo] * 4) orbspin = mf.mo_coeff.orbspin sym_forbid = (orbspin[:, None] != orbspin) eri[sym_forbid, :, :] = 0 eri[:, :, sym_forbid] = 0 hcore = scf.RHF(mol).get_hcore() h1 = reduce(numpy.dot, (mo_a.T.conj(), hcore, mo_a)) h1 += reduce(numpy.dot, (mo_b.T.conj(), hcore, mo_b)) e1 = numpy.einsum('ij,ji', h1, dm1) e1 += numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mygcc.e_tot, 7) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] trdm1 = dm1[idxa[:, None], idxa] trdm1 += dm1[idxb[:, None], idxb] trdm2 = dm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] trdm2 += dm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] dm2ab = dm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] trdm2 += dm2ab trdm2 += dm2ab.transpose(2, 3, 0, 1) self.assertAlmostEqual(abs(trdm1 - rdm1).max(), 0, 6) self.assertAlmostEqual(abs(trdm2 - rdm2).max(), 0, 6) rt1 = myrcc.t1 + numpy.cos(myrcc.t1) * .2j rl1 = myrcc.l1 + numpy.cos(myrcc.l1) * .2j rt2 = myrcc.t2 + numpy.sin(myrcc.t2) * .8j rl2 = myrcc.l2 + numpy.sin(myrcc.l2) * .8j rdm1 = myrcc.make_rdm1(rt1, rt2, rl1, rl2) rdm2 = myrcc.make_rdm2(rt1, rt2, rl1, rl2) gt1 = mygcc.spatial2spin(rt1) gt2 = mygcc.spatial2spin(rt2) gl1 = mygcc.spatial2spin(rl1) gl2 = mygcc.spatial2spin(rl2) gdm1 = mygcc.make_rdm1(gt1, gt2, gl1, gl2) gdm2 = mygcc.make_rdm2(gt1, gt2, gl1, gl2) trdm1 = gdm1[idxa[:, None], idxa] trdm1 += gdm1[idxb[:, None], idxb] trdm2 = gdm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] trdm2 += gdm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] dm2ab = gdm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] trdm2 += dm2ab trdm2 += dm2ab.transpose(2, 3, 0, 1) self.assertAlmostEqual(abs(trdm1 - rdm1).max(), 0, 9) self.assertAlmostEqual(abs(trdm2 - rdm2).max(), 0, 9)
def test_uccsd_t_complex(self): mol = gto.M() numpy.random.seed(12) nocca, noccb, nvira, nvirb = 3, 2, 4, 5 nmo = nocca + nvira eris = cc.uccsd._ChemistsERIs() eris.nocca = nocca eris.noccb = noccb eris.nocc = (nocca, noccb) eri1 = (numpy.random.random((3,nmo,nmo,nmo,nmo)) + numpy.random.random((3,nmo,nmo,nmo,nmo)) * .8j - .5-.4j) eri1 = eri1 + eri1.transpose(0,2,1,4,3).conj() eri1[0] = eri1[0] + eri1[0].transpose(2,3,0,1) eri1[2] = eri1[2] + eri1[2].transpose(2,3,0,1) eri1 *= .1 eris.ovvv = eri1[0,:nocca,nocca:,nocca:,nocca:] eris.ovov = eri1[0,:nocca,nocca:,:nocca,nocca:] eris.ovoo = eri1[0,:nocca,nocca:,:nocca,:nocca] eris.OVVV = eri1[2,:noccb,noccb:,noccb:,noccb:] eris.OVOV = eri1[2,:noccb,noccb:,:noccb,noccb:] eris.OVOO = eri1[2,:noccb,noccb:,:noccb,:noccb] eris.voVP = eri1[1,nocca:,:nocca,noccb:,: ] eris.ovVV = eri1[1,:nocca,nocca:,noccb:,noccb:] eris.ovOV = eri1[1,:nocca,nocca:,:noccb,noccb:] eris.ovOO = eri1[1,:nocca,nocca:,:noccb,:noccb] eris.OVvv = eri1[1,nocca:,nocca:,:noccb,noccb:].transpose(2,3,0,1) eris.OVoo = eri1[1,:nocca,:nocca,:noccb,noccb:].transpose(2,3,0,1) t1a = .1 * numpy.random.random((nocca,nvira)) + numpy.random.random((nocca,nvira))*.1j t1b = .1 * numpy.random.random((noccb,nvirb)) + numpy.random.random((noccb,nvirb))*.1j t2aa = .1 * numpy.random.random((nocca,nocca,nvira,nvira)) + numpy.random.random((nocca,nocca,nvira,nvira))*.1j t2aa = t2aa - t2aa.transpose(0,1,3,2) t2aa = t2aa - t2aa.transpose(1,0,2,3) t2bb = .1 * numpy.random.random((noccb,noccb,nvirb,nvirb)) + numpy.random.random((noccb,noccb,nvirb,nvirb))*.1j t2bb = t2bb - t2bb.transpose(0,1,3,2) t2bb = t2bb - t2bb.transpose(1,0,2,3) t2ab = .1 * numpy.random.random((nocca,noccb,nvira,nvirb)) + numpy.random.random((nocca,noccb,nvira,nvirb))*.1j f = (numpy.random.random((2,nmo,nmo)) * .4 + numpy.random.random((2,nmo,nmo)) * .4j) eris.focka = f[0]+f[0].T.conj() + numpy.diag(numpy.arange(nmo)) eris.fockb = f[1]+f[1].T.conj() + numpy.diag(numpy.arange(nmo)) eris.mo_energy = (eris.focka.diagonal().real, eris.fockb.diagonal().real) t1 = t1a, t1b t2 = t2aa, t2ab, t2bb mcc = cc.UCCSD(scf.UHF(mol)) mcc.nocc = eris.nocc e0 = uccsd_t.kernel(mcc, eris, t1, t2) eri2 = numpy.zeros((nmo*2,nmo*2,nmo*2,nmo*2), dtype=eri1.dtype) orbspin = numpy.zeros(nmo*2,dtype=int) orbspin[1::2] = 1 eri2[0::2,0::2,0::2,0::2] = eri1[0] eri2[1::2,1::2,0::2,0::2] = eri1[1].transpose(2,3,0,1) eri2[0::2,0::2,1::2,1::2] = eri1[1] eri2[1::2,1::2,1::2,1::2] = eri1[2] eri2 = eri2.transpose(0,2,1,3) - eri2.transpose(0,2,3,1) fock = numpy.zeros((nmo*2,nmo*2), dtype=eris.focka.dtype) fock[0::2,0::2] = eris.focka fock[1::2,1::2] = eris.fockb eris1 = gccsd._PhysicistsERIs() nocc = nocca + noccb eris1.ovvv = eri2[:nocc,nocc:,nocc:,nocc:] eris1.oovv = eri2[:nocc,:nocc,nocc:,nocc:] eris1.ooov = eri2[:nocc,:nocc,:nocc,nocc:] eris1.fock = fock eris1.mo_energy = fock.diagonal().real t1 = gccsd.spatial2spin(t1, orbspin) t2 = gccsd.spatial2spin(t2, orbspin) gcc = gccsd.GCCSD(scf.GHF(gto.M())) e1 = gccsd_t.kernel(gcc, eris1, t1, t2) self.assertAlmostEqual(e0, e1.real, 9) self.assertAlmostEqual(e1, -0.056092415718338388-0.011390417704868244j, 9)
from pyscf import ao2mo from pyscf.cc import gccsd from pyscf.cc import addons mol = gto.Mole() mol.atom = [ [8 , (0. , 0. , 0.)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]] mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run(conv_tol=1.) mf = scf.addons.convert_to_ghf(mf) mycc = gccsd.GCCSD(mf) ecc, t1, t2 = mycc.kernel() l1, l2 = mycc.solve_lambda() dm1 = make_rdm1(mycc, t1, t2, l1, l2) dm2 = make_rdm2(mycc, t1, t2, l1, l2) nao = mol.nao_nr() mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] nmo = mo_a.shape[1] eri = ao2mo.kernel(mf._eri, mo_a+mo_b, compact=False).reshape([nmo]*4) orbspin = mf.mo_coeff.orbspin sym_forbid = (orbspin[:,None] != orbspin) eri[sym_forbid,:,:] = 0 eri[:,:,sym_forbid] = 0 hcore = scf.RHF(mol).get_hcore() h1 = reduce(numpy.dot, (mo_a.T.conj(), hcore, mo_a))
np.random.seed(1) np.set_printoptions(4, linewidth=1000, suppress=True) # genrate reference values mol = gto.M(atom='H 0 0 0; F 0 0 1.1', basis='321g', spin=2) mol.verbose = 5 mf = mol.GHF() hcoreX = mf.get_hcore() # a small random potential to break the Sz symmetry: pot = (np.random.random(hcoreX.shape) - 0.5) * 3e-2 pot = pot + pot.T hcoreX += pot mf.get_hcore = lambda *args: hcoreX mf.kernel() mycc_ref = gccsd.GCCSD(mf) mycc_ref.conv_tol = 1e-10 mycc_ref.conv_tol_normt = 1e-6 eris_ref = mycc_ref.ao2mo() # converged Ecc ecc_ref, t1_cc_ref, t2_cc_ref = mycc_ref.kernel() # test class mycc = GCCSD(mf) mycc.conv_tol = 1e-10 mycc.conv_tol_normt = 1e-6 print("test CC converged energy and t1, t2") e_cc, t1_cc, t2_cc = mycc.kernel()
if __name__ == '__main__': from pyscf import gto from pyscf import scf from pyscf.cc import gccsd mol = gto.Mole() mol.atom = [ [8 , (0. , 0. , 0.)], [1 , (0. , -0.757 , 0.587)], [1 , (0. , 0.757 , 0.587)]] mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run() mycc = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) eris = mycc.ao2mo() mycc.kernel() l1, l2 = mycc.solve_lambda(mycc.t1, mycc.t2, eris=eris) l1ref = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2ref = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) mycc = uccsd.UCCSD(mf) eris = mycc.ao2mo() mycc.kernel() conv, l1, l2 = kernel(mycc, eris, mycc.t1, mycc.t2, tol=1e-8) print(abs(l1[0]-l1ref[0]).max()) print(abs(l1[1]-l1ref[1]).max()) print(abs(l2[0]-l2ref[0]).max()) print(abs(l2[1]-l2ref[1]).max()) print(abs(l2[2]-l2ref[2]).max())
def test_update_lambda_complex(self): nocca, noccb = mol.nelec nmo = mol.nao_nr() nvira, nvirb = nmo - nocca, nmo - noccb numpy.random.seed(9) t1 = [ numpy.random.random((nocca, nvira)) - .9, numpy.random.random((noccb, nvirb)) - .9 ] l1 = [ numpy.random.random((nocca, nvira)) - .9, numpy.random.random((noccb, nvirb)) - .9 ] t2 = [ numpy.random.random((nocca, nocca, nvira, nvira)) - .9, numpy.random.random((nocca, noccb, nvira, nvirb)) - .9, numpy.random.random((noccb, noccb, nvirb, nvirb)) - .9 ] t2[0] = t2[0] - t2[0].transpose(1, 0, 2, 3) t2[0] = t2[0] - t2[0].transpose(0, 1, 3, 2) t2[2] = t2[2] - t2[2].transpose(1, 0, 2, 3) t2[2] = t2[2] - t2[2].transpose(0, 1, 3, 2) l2 = [ numpy.random.random((nocca, nocca, nvira, nvira)) - .9, numpy.random.random((nocca, noccb, nvira, nvirb)) - .9, numpy.random.random((noccb, noccb, nvirb, nvirb)) - .9 ] l2[0] = l2[0] - l2[0].transpose(1, 0, 2, 3) l2[0] = l2[0] - l2[0].transpose(0, 1, 3, 2) l2[2] = l2[2] - l2[2].transpose(1, 0, 2, 3) l2[2] = l2[2] - l2[2].transpose(0, 1, 3, 2) # eris = mycc.ao2mo() # imds = make_intermediates(mycc, t1, t2, eris) # l1new, l2new = update_lambda(mycc, t1, t2, l1, l2, eris, imds) # print(lib.finger(l1new[0]) --104.55975252585894) # print(lib.finger(l1new[1]) --241.12677819375281) # print(lib.finger(l2new[0]) --0.4957533529669417) # print(lib.finger(l2new[1]) - 15.46423057451851 ) # print(lib.finger(l2new[2]) - 5.8430776663704407) nocca, noccb = mol.nelec mo_a = mf.mo_coeff[0] + numpy.sin(mf.mo_coeff[0]) * .01j mo_b = mf.mo_coeff[1] + numpy.sin(mf.mo_coeff[1]) * .01j nao = mo_a.shape[0] eri = ao2mo.restore(1, mf._eri, nao) eri0aa = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_a.conj(), mo_a) eri0ab = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_b.conj(), mo_b) eri0bb = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_b.conj(), mo_b, mo_b.conj(), mo_b) eri0ba = eri0ab.transpose(2, 3, 0, 1) nvira = nao - nocca nvirb = nao - noccb eris = uccsd._ChemistsERIs(mol) eris.oooo = eri0aa[:nocca, :nocca, :nocca, :nocca].copy() eris.ovoo = eri0aa[:nocca, nocca:, :nocca, :nocca].copy() eris.oovv = eri0aa[:nocca, :nocca, nocca:, nocca:].copy() eris.ovvo = eri0aa[:nocca, nocca:, nocca:, :nocca].copy() eris.ovov = eri0aa[:nocca, nocca:, :nocca, nocca:].copy() eris.ovvv = eri0aa[:nocca, nocca:, nocca:, nocca:].copy() eris.vvvv = eri0aa[nocca:, nocca:, nocca:, nocca:].copy() eris.OOOO = eri0bb[:noccb, :noccb, :noccb, :noccb].copy() eris.OVOO = eri0bb[:noccb, noccb:, :noccb, :noccb].copy() eris.OOVV = eri0bb[:noccb, :noccb, noccb:, noccb:].copy() eris.OVVO = eri0bb[:noccb, noccb:, noccb:, :noccb].copy() eris.OVOV = eri0bb[:noccb, noccb:, :noccb, noccb:].copy() eris.OVVV = eri0bb[:noccb, noccb:, noccb:, noccb:].copy() eris.VVVV = eri0bb[noccb:, noccb:, noccb:, noccb:].copy() eris.ooOO = eri0ab[:nocca, :nocca, :noccb, :noccb].copy() eris.ovOO = eri0ab[:nocca, nocca:, :noccb, :noccb].copy() eris.ooVV = eri0ab[:nocca, :nocca, noccb:, noccb:].copy() eris.ovVO = eri0ab[:nocca, nocca:, noccb:, :noccb].copy() eris.ovOV = eri0ab[:nocca, nocca:, :noccb, noccb:].copy() eris.ovVV = eri0ab[:nocca, nocca:, noccb:, noccb:].copy() eris.vvVV = eri0ab[nocca:, nocca:, noccb:, noccb:].copy() eris.OOoo = eri0ba[:noccb, :noccb, :nocca, :nocca].copy() eris.OVoo = eri0ba[:noccb, noccb:, :nocca, :nocca].copy() eris.OOvv = eri0ba[:noccb, :noccb, nocca:, nocca:].copy() eris.OVvo = eri0ba[:noccb, noccb:, nocca:, :nocca].copy() eris.OVov = eri0ba[:noccb, noccb:, :nocca, nocca:].copy() eris.OVvv = eri0ba[:noccb, noccb:, nocca:, nocca:].copy() eris.VVvv = eri0ba[noccb:, noccb:, nocca:, nocca:].copy() eris.focka = numpy.diag(mf.mo_energy[0]) eris.fockb = numpy.diag(mf.mo_energy[1]) t1[0] = t1[0] + numpy.sin(t1[0]) * .05j t1[1] = t1[1] + numpy.sin(t1[1]) * .05j t2[0] = t2[0] + numpy.sin(t2[0]) * .05j t2[1] = t2[1] + numpy.sin(t2[1]) * .05j t2[2] = t2[2] + numpy.sin(t2[2]) * .05j l1[0] = l1[0] + numpy.sin(l1[0]) * .05j l1[1] = l1[1] + numpy.sin(l1[1]) * .05j l2[0] = l2[0] + numpy.sin(l2[0]) * .05j l2[1] = l2[1] + numpy.sin(l2[1]) * .05j l2[2] = l2[2] + numpy.sin(l2[2]) * .05j imds = uccsd_lambda.make_intermediates(mycc, t1, t2, eris) l1new_ref, l2new_ref = uccsd_lambda.update_lambda( mycc, t1, t2, l1, l2, eris, imds) nocc = nocca + noccb orbspin = numpy.zeros(nao * 2, dtype=int) orbspin[1::2] = 1 orbspin[nocc - 1] = 0 orbspin[nocc] = 1 eri1 = numpy.zeros([nao * 2] * 4, dtype=numpy.complex) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] eri1[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] = eri0aa eri1[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] = eri0ab eri1[idxb[:, None, None, None], idxb[:, None, None], idxa[:, None], idxa] = eri0ba eri1[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] = eri0bb eri1 = eri1.transpose(0, 2, 1, 3) - eri1.transpose(0, 2, 3, 1) erig = gccsd._PhysicistsERIs() erig.oooo = eri1[:nocc, :nocc, :nocc, :nocc].copy() erig.ooov = eri1[:nocc, :nocc, :nocc, nocc:].copy() erig.ovov = eri1[:nocc, nocc:, :nocc, nocc:].copy() erig.ovvo = eri1[:nocc, nocc:, nocc:, :nocc].copy() erig.oovv = eri1[:nocc, :nocc, nocc:, nocc:].copy() erig.ovvv = eri1[:nocc, nocc:, nocc:, nocc:].copy() erig.vvvv = eri1[nocc:, nocc:, nocc:, nocc:].copy() mo_e = numpy.empty(nao * 2) mo_e[orbspin == 0] = mf.mo_energy[0] mo_e[orbspin == 1] = mf.mo_energy[1] erig.fock = numpy.diag(mo_e) myccg = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) t1 = myccg.spatial2spin(t1, orbspin) t2 = myccg.spatial2spin(t2, orbspin) l1 = myccg.spatial2spin(l1, orbspin) l2 = myccg.spatial2spin(l2, orbspin) imds = gccsd_lambda.make_intermediates(myccg, t1, t2, erig) l1new, l2new = gccsd_lambda.update_lambda(myccg, t1, t2, l1, l2, erig, imds) l1new = myccg.spin2spatial(l1new, orbspin) l2new = myccg.spin2spatial(l2new, orbspin) self.assertAlmostEqual(abs(l1new[0] - l1new_ref[0]).max(), 0, 11) self.assertAlmostEqual(abs(l1new[1] - l1new_ref[1]).max(), 0, 11) self.assertAlmostEqual(abs(l2new[0] - l2new_ref[0]).max(), 0, 11) self.assertAlmostEqual(abs(l2new[1] - l2new_ref[1]).max(), 0, 11) self.assertAlmostEqual(abs(l2new[2] - l2new_ref[2]).max(), 0, 11)
def test_update_amps2(self): # compare to gccsd.update_amps mol = mol_s2 mf = mf_s2 myucc = uccsd.UCCSD(mf) nocca, noccb = 6, 4 nmo = mol.nao_nr() nvira, nvirb = nmo - nocca, nmo - noccb numpy.random.seed(9) t1 = [ numpy.random.random((nocca, nvira)) - .9, numpy.random.random((noccb, nvirb)) - .9 ] t2 = [ numpy.random.random((nocca, nocca, nvira, nvira)) - .9, numpy.random.random((nocca, noccb, nvira, nvirb)) - .9, numpy.random.random((noccb, noccb, nvirb, nvirb)) - .9 ] t2[0] = t2[0] - t2[0].transpose(1, 0, 2, 3) t2[0] = t2[0] - t2[0].transpose(0, 1, 3, 2) t2[2] = t2[2] - t2[2].transpose(1, 0, 2, 3) t2[2] = t2[2] - t2[2].transpose(0, 1, 3, 2) mo_a = mf.mo_coeff[0] + numpy.sin(mf.mo_coeff[0]) * .01j mo_b = mf.mo_coeff[1] + numpy.sin(mf.mo_coeff[1]) * .01j nao = mo_a.shape[0] eri = ao2mo.restore(1, mf._eri, nao) eri0aa = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_a.conj(), mo_a) eri0ab = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_a.conj(), mo_a, mo_b.conj(), mo_b) eri0bb = lib.einsum('pqrs,pi,qj,rk,sl->ijkl', eri, mo_b.conj(), mo_b, mo_b.conj(), mo_b) eri0ba = eri0ab.transpose(2, 3, 0, 1) nvira = nao - nocca nvirb = nao - noccb eris = uccsd._ChemistsERIs(mol) eris.oooo = eri0aa[:nocca, :nocca, :nocca, :nocca].copy() eris.ovoo = eri0aa[:nocca, nocca:, :nocca, :nocca].copy() eris.oovv = eri0aa[:nocca, :nocca, nocca:, nocca:].copy() eris.ovvo = eri0aa[:nocca, nocca:, nocca:, :nocca].copy() eris.ovov = eri0aa[:nocca, nocca:, :nocca, nocca:].copy() eris.ovvv = eri0aa[:nocca, nocca:, nocca:, nocca:].copy() eris.vvvv = eri0aa[nocca:, nocca:, nocca:, nocca:].copy() eris.OOOO = eri0bb[:noccb, :noccb, :noccb, :noccb].copy() eris.OVOO = eri0bb[:noccb, noccb:, :noccb, :noccb].copy() eris.OOVV = eri0bb[:noccb, :noccb, noccb:, noccb:].copy() eris.OVVO = eri0bb[:noccb, noccb:, noccb:, :noccb].copy() eris.OVOV = eri0bb[:noccb, noccb:, :noccb, noccb:].copy() eris.OVVV = eri0bb[:noccb, noccb:, noccb:, noccb:].copy() eris.VVVV = eri0bb[noccb:, noccb:, noccb:, noccb:].copy() eris.ooOO = eri0ab[:nocca, :nocca, :noccb, :noccb].copy() eris.ovOO = eri0ab[:nocca, nocca:, :noccb, :noccb].copy() eris.ooVV = eri0ab[:nocca, :nocca, noccb:, noccb:].copy() eris.ovVO = eri0ab[:nocca, nocca:, noccb:, :noccb].copy() eris.ovOV = eri0ab[:nocca, nocca:, :noccb, noccb:].copy() eris.ovVV = eri0ab[:nocca, nocca:, noccb:, noccb:].copy() eris.vvVV = eri0ab[nocca:, nocca:, noccb:, noccb:].copy() eris.OOoo = eri0ba[:noccb, :noccb, :nocca, :nocca].copy() eris.OVoo = eri0ba[:noccb, noccb:, :nocca, :nocca].copy() eris.OOvv = eri0ba[:noccb, :noccb, nocca:, nocca:].copy() eris.OVvo = eri0ba[:noccb, noccb:, nocca:, :nocca].copy() eris.OVov = eri0ba[:noccb, noccb:, :nocca, nocca:].copy() eris.OVvv = eri0ba[:noccb, noccb:, nocca:, nocca:].copy() eris.VVvv = eri0ba[noccb:, noccb:, nocca:, nocca:].copy() eris.focka = numpy.diag(mf.mo_energy[0]) eris.fockb = numpy.diag(mf.mo_energy[1]) eris.mo_energy = mf.mo_energy t1[0] = t1[0] + numpy.sin(t1[0]) * .05j t1[1] = t1[1] + numpy.sin(t1[1]) * .05j t2[0] = t2[0] + numpy.sin(t2[0]) * .05j t2[1] = t2[1] + numpy.sin(t2[1]) * .05j t2[2] = t2[2] + numpy.sin(t2[2]) * .05j t1new_ref, t2new_ref = uccsd.update_amps(myucc, t1, t2, eris) nocc = nocca + noccb orbspin = numpy.zeros(nao * 2, dtype=int) orbspin[1::2] = 1 orbspin[nocc - 1] = 0 orbspin[nocc] = 1 eri1 = numpy.zeros([nao * 2] * 4, dtype=numpy.complex) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] eri1[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] = eri0aa eri1[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] = eri0ab eri1[idxb[:, None, None, None], idxb[:, None, None], idxa[:, None], idxa] = eri0ba eri1[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] = eri0bb eri1 = eri1.transpose(0, 2, 1, 3) - eri1.transpose(0, 2, 3, 1) erig = gccsd._PhysicistsERIs() erig.oooo = eri1[:nocc, :nocc, :nocc, :nocc].copy() erig.ooov = eri1[:nocc, :nocc, :nocc, nocc:].copy() erig.ovov = eri1[:nocc, nocc:, :nocc, nocc:].copy() erig.ovvo = eri1[:nocc, nocc:, nocc:, :nocc].copy() erig.oovv = eri1[:nocc, :nocc, nocc:, nocc:].copy() erig.ovvv = eri1[:nocc, nocc:, nocc:, nocc:].copy() erig.vvvv = eri1[nocc:, nocc:, nocc:, nocc:].copy() mo_e = numpy.empty(nao * 2) mo_e[orbspin == 0] = mf.mo_energy[0] mo_e[orbspin == 1] = mf.mo_energy[1] erig.fock = numpy.diag(mo_e) erig.mo_energy = mo_e.real myccg = gccsd.GCCSD(scf.addons.convert_to_ghf(mf)) t1 = myccg.spatial2spin(t1, orbspin) t2 = myccg.spatial2spin(t2, orbspin) t1new, t2new = gccsd.update_amps(myccg, t1, t2, erig) t1new = myccg.spin2spatial(t1new, orbspin) t2new = myccg.spin2spatial(t2new, orbspin) self.assertAlmostEqual(abs(t1new[0] - t1new_ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(t1new[1] - t1new_ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[0] - t2new_ref[0]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[1] - t2new_ref[1]).max(), 0, 12) self.assertAlmostEqual(abs(t2new[2] - t2new_ref[2]).max(), 0, 12)
def test_rdm_vs_uccsd(self): mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.verbose = 5 mol.output = '/dev/null' mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run() myucc = uccsd.UCCSD(mf) myucc.frozen = 1 myucc.kernel() udm1 = myucc.make_rdm1() udm2 = myucc.make_rdm2() mf = scf.addons.convert_to_ghf(mf) mygcc = gccsd.GCCSD(mf) mygcc.frozen = 2 ecc, t1, t2 = mygcc.kernel() l1, l2 = mygcc.solve_lambda() dm1 = gccsd_rdm.make_rdm1(mygcc, t1, t2, l1, l2) dm2 = gccsd_rdm.make_rdm2(mygcc, t1, t2, l1, l2) nao = mol.nao_nr() mo_a = mf.mo_coeff[:nao] mo_b = mf.mo_coeff[nao:] nmo = mo_a.shape[1] eri = ao2mo.kernel(mf._eri, mo_a + mo_b, compact=False).reshape([nmo] * 4) orbspin = mf.mo_coeff.orbspin sym_forbid = (orbspin[:, None] != orbspin) eri[sym_forbid, :, :] = 0 eri[:, :, sym_forbid] = 0 hcore = scf.RHF(mol).get_hcore() h1 = reduce(numpy.dot, (mo_a.T.conj(), hcore, mo_a)) h1 += reduce(numpy.dot, (mo_b.T.conj(), hcore, mo_b)) e1 = numpy.einsum('ij,ji', h1, dm1) e1 += numpy.einsum('ijkl,ijkl', eri, dm2) * .5 e1 += mol.energy_nuc() self.assertAlmostEqual(e1, mygcc.e_tot, 7) idxa = numpy.where(orbspin == 0)[0] idxb = numpy.where(orbspin == 1)[0] self.assertAlmostEqual( abs(dm1[idxa[:, None], idxa] - udm1[0]).max(), 0, 7) self.assertAlmostEqual( abs(dm1[idxb[:, None], idxb] - udm1[1]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] - udm2[0]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] - udm2[1]).max(), 0, 7) self.assertAlmostEqual( abs(dm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] - udm2[2]).max(), 0, 7) ut1 = [0] * 2 ul1 = [0] * 2 ut2 = [0] * 3 ul2 = [0] * 3 ut1[0] = myucc.t1[0] + numpy.cos(myucc.t1[0]) * .2j ut1[1] = myucc.t1[1] + numpy.cos(myucc.t1[1]) * .2j ul1[0] = myucc.l1[0] + numpy.cos(myucc.l1[0]) * .2j ul1[1] = myucc.l1[1] + numpy.cos(myucc.l1[1]) * .2j ut2[0] = myucc.t2[0] + numpy.sin(myucc.t2[0]) * .8j ut2[1] = myucc.t2[1] + numpy.sin(myucc.t2[1]) * .8j ut2[2] = myucc.t2[2] + numpy.sin(myucc.t2[2]) * .8j ul2[0] = myucc.l2[0] + numpy.sin(myucc.l2[0]) * .8j ul2[1] = myucc.l2[1] + numpy.sin(myucc.l2[1]) * .8j ul2[2] = myucc.l2[2] + numpy.sin(myucc.l2[2]) * .8j udm1 = myucc.make_rdm1(ut1, ut2, ul1, ul2) udm2 = myucc.make_rdm2(ut1, ut2, ul1, ul2) gt1 = mygcc.spatial2spin(ut1) gt2 = mygcc.spatial2spin(ut2) gl1 = mygcc.spatial2spin(ul1) gl2 = mygcc.spatial2spin(ul2) gdm1 = mygcc.make_rdm1(gt1, gt2, gl1, gl2) gdm2 = mygcc.make_rdm2(gt1, gt2, gl1, gl2) self.assertAlmostEqual( abs(gdm1[idxa[:, None], idxa] - udm1[0]).max(), 0, 9) self.assertAlmostEqual( abs(gdm1[idxb[:, None], idxb] - udm1[1]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxa[:, None, None, None], idxa[:, None, None], idxa[:, None], idxa] - udm2[0]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxa[:, None, None, None], idxa[:, None, None], idxb[:, None], idxb] - udm2[1]).max(), 0, 9) self.assertAlmostEqual( abs(gdm2[idxb[:, None, None, None], idxb[:, None, None], idxb[:, None], idxb] - udm2[2]).max(), 0, 9)
from pyscf.cc import gccsd_rdm from pyscf.cc import ccsd from pyscf.cc import uccsd mol = gto.Mole() mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]] mol.verbose = 5 mol.output = '/dev/null' mol.basis = '631g' mol.spin = 2 mol.build() mf = scf.UHF(mol).run(conv_tol=1e-12) mf = scf.addons.convert_to_ghf(mf) gcc1 = gccsd.GCCSD(mf).run(conv_tol=1e-9) def tearDownModule(): global mol, mf, gcc1 mol.stdout.close() del mol, mf, gcc1 class KnownValues(unittest.TestCase): def test_gccsd(self): self.assertAlmostEqual(gcc1.e_corr, -0.10805861695870976, 7) def test_ERIS(self): gcc = gccsd.GCCSD(mf, frozen=4) numpy.random.seed(9)
def test_mbpt2(self): mygcc = gccsd.GCCSD(mf) e = mygcc.kernel(mbpt2=True)[0] self.assertAlmostEqual(e, -0.096257842171487293, 9) emp2 = mp.MP2(mf).kernel()[0] self.assertAlmostEqual(e, emp2, 10)