def test_ccsd(self): mol = gto.M() mf = scf.RHF(mol) mcc = cc.CCSD(mf) numpy.random.seed(12) mcc.nocc = nocc = 5 mcc.nmo = nmo = 12 nvir = nmo - nocc eri0 = numpy.random.random((nmo,nmo,nmo,nmo)) eri0 = ao2mo.restore(1, ao2mo.restore(8, eri0, nmo), nmo) fock0 = numpy.random.random((nmo,nmo)) fock0 = fock0 + fock0.T + numpy.diag(range(nmo))*2 t1 = numpy.random.random((nocc,nvir)) t2 = numpy.random.random((nocc,nocc,nvir,nvir)) t2 = t2 + t2.transpose(1,0,3,2) l1 = numpy.random.random((nocc,nvir)) l2 = numpy.random.random((nocc,nocc,nvir,nvir)) l2 = l2 + l2.transpose(1,0,3,2) eris = cc.ccsd._ChemistsERIs() 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() idx = numpy.tril_indices(nvir) eris.ovvv = eri0[:nocc,nocc:,nocc:,nocc:][:,:,idx[0],idx[1]].copy() eris.vvvv = ao2mo.restore(4,eri0[nocc:,nocc:,nocc:,nocc:],nvir) eris.fock = fock0 eris.mo_energy = fock0.diagonal() saved = ccsd_lambda.make_intermediates(mcc, t1, t2, eris) l1new, l2new = ccsd_lambda.update_lambda(mcc, t1, t2, l1, l2, eris, saved) self.assertAlmostEqual(abs(l1new).sum(), 38172.7896467303, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.arange(35)), 739312.005491083, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.sin(numpy.arange(35))), 7019.50937051188, 8) self.assertAlmostEqual(numpy.dot(numpy.sin(l1new.flatten()), numpy.arange(35)), 69.6652346635955, 8) self.assertAlmostEqual(abs(l2new).sum(), 72035.4931071527, 8) self.assertAlmostEqual(abs(l2new-l2new.transpose(1,0,3,2)).sum(), 0, 9) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.arange(35**2)), 48427109.5409886, 7) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.sin(numpy.arange(35**2))), 137.758016736487, 8) self.assertAlmostEqual(numpy.dot(numpy.sin(l2new.flatten()), numpy.arange(35**2)), 507.656936701192, 8) mcc.max_memory = 0 saved = ccsd_lambda.make_intermediates(mcc, t1, t2, eris) l1new, l2new = ccsd_lambda.update_lambda(mcc, t1, t2, l1, l2, eris, saved) self.assertAlmostEqual(abs(l1new).sum(), 38172.7896467303, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.arange(35)), 739312.005491083, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.sin(numpy.arange(35))), 7019.50937051188, 8) self.assertAlmostEqual(numpy.dot(numpy.sin(l1new.flatten()), numpy.arange(35)), 69.6652346635955, 8) self.assertAlmostEqual(abs(l2new).sum(), 72035.4931071527, 8) self.assertAlmostEqual(abs(l2new-l2new.transpose(1,0,3,2)).sum(), 0, 9) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.arange(35**2)), 48427109.5409886, 7) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.sin(numpy.arange(35**2))), 137.758016736487, 8) self.assertAlmostEqual(numpy.dot(numpy.sin(l2new.flatten()), numpy.arange(35**2)), 507.656936701192, 8)
def update_lambda(mycc, t1, t2, l1, l2, eris=None, imds=None): if eris is None: eris = mycc.ao2mo() if imds is None: imds = make_intermediates(mycc, t1, t2, eris) l1, l2 = ccsd_lambda.update_lambda(mycc, t1, t2, l1, l2, eris, imds) l1 += imds.l1_t l2 += imds.l2_t return l1, l2
l1 = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2 = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) print(lib.finger(l1[0]) - -0.0030030170069977758) print(lib.finger(l1[1]) - -0.0030030170069977758) print(lib.finger(l2[0]) - -0.041444910588788492) print(lib.finger(l2[1]) - 0.1077575086912813) print(lib.finger(l2[2]) - -0.041444910588788492) print(abs(l2[1] - l2[1].transpose(1, 0, 2, 3) - l2[0]).max()) print(abs(l2[1] - l2[1].transpose(0, 1, 3, 2) - l2[0]).max()) from pyscf.cc import ccsd mycc0 = ccsd.CCSD(mf0) eris0 = mycc0.ao2mo() mycc0.kernel(eris=eris0) t1 = mycc0.t1 t2 = mycc0.t2 imds = ccsd_lambda.make_intermediates(mycc0, t1, t2, eris0) l1, l2 = ccsd_lambda.update_lambda(mycc0, t1, t2, t1, t2, eris0, imds) l1ref, l2ref = ccsd_lambda.update_lambda(mycc0, t1, t2, l1, l2, eris0, imds) t1 = mycc.spatial2spin(t1, mycc.mo_coeff.orbspin) t2 = mycc.spatial2spin(t2, mycc.mo_coeff.orbspin) l1 = mycc.spatial2spin(l1, mycc.mo_coeff.orbspin) l2 = mycc.spatial2spin(l2, mycc.mo_coeff.orbspin) imds = make_intermediates(mycc, t1, t2, eris) l1, l2 = update_lambda(mycc, t1, t2, l1, l2, eris, imds) l1 = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2 = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) print(abs(l1[0] - l1ref).max()) print(abs(l2[1] - l2ref).max())
def test_ccsd(self): mol = gto.M() mf = scf.RHF(mol) mcc = cc.CCSD(mf) numpy.random.seed(12) mcc.nocc = nocc = 5 mcc.nmo = nmo = 12 nvir = nmo - nocc eri0 = numpy.random.random((nmo, nmo, nmo, nmo)) eri0 = ao2mo.restore(1, ao2mo.restore(8, eri0, nmo), nmo) fock0 = numpy.random.random((nmo, nmo)) fock0 = fock0 + fock0.T + numpy.diag(range(nmo)) * 2 t1 = numpy.random.random((nocc, nvir)) t2 = numpy.random.random((nocc, nocc, nvir, nvir)) t2 = t2 + t2.transpose(1, 0, 3, 2) l1 = numpy.random.random((nocc, nvir)) l2 = numpy.random.random((nocc, nocc, nvir, nvir)) l2 = l2 + l2.transpose(1, 0, 3, 2) eris = cc.ccsd._ChemistsERIs() 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() idx = numpy.tril_indices(nvir) eris.ovvv = eri0[:nocc, nocc:, nocc:, nocc:][:, :, idx[0], idx[1]].copy() eris.vvvv = ao2mo.restore(4, eri0[nocc:, nocc:, nocc:, nocc:], nvir) eris.fock = fock0 eris.mo_energy = fock0.diagonal() saved = ccsd_lambda.make_intermediates(mcc, t1, t2, eris) l1new, l2new = ccsd_lambda.update_lambda(mcc, t1, t2, l1, l2, eris, saved) self.assertAlmostEqual(abs(l1new).sum(), 38172.7896467303, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.arange(35)), 739312.005491083, 8) self.assertAlmostEqual( numpy.dot(l1new.flatten(), numpy.sin(numpy.arange(35))), 7019.50937051188, 8) self.assertAlmostEqual( numpy.dot(numpy.sin(l1new.flatten()), numpy.arange(35)), 69.6652346635955, 8) self.assertAlmostEqual(abs(l2new).sum(), 72035.4931071527, 8) self.assertAlmostEqual( abs(l2new - l2new.transpose(1, 0, 3, 2)).sum(), 0, 9) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.arange(35**2)), 48427109.5409886, 7) self.assertAlmostEqual( numpy.dot(l2new.flatten(), numpy.sin(numpy.arange(35**2))), 137.758016736487, 8) self.assertAlmostEqual( numpy.dot(numpy.sin(l2new.flatten()), numpy.arange(35**2)), 507.656936701192, 8) mcc.max_memory = 0 saved = ccsd_lambda.make_intermediates(mcc, t1, t2, eris) l1new, l2new = ccsd_lambda.update_lambda(mcc, t1, t2, l1, l2, eris, saved) self.assertAlmostEqual(abs(l1new).sum(), 38172.7896467303, 8) self.assertAlmostEqual(numpy.dot(l1new.flatten(), numpy.arange(35)), 739312.005491083, 8) self.assertAlmostEqual( numpy.dot(l1new.flatten(), numpy.sin(numpy.arange(35))), 7019.50937051188, 8) self.assertAlmostEqual( numpy.dot(numpy.sin(l1new.flatten()), numpy.arange(35)), 69.6652346635955, 8) self.assertAlmostEqual(abs(l2new).sum(), 72035.4931071527, 8) self.assertAlmostEqual( abs(l2new - l2new.transpose(1, 0, 3, 2)).sum(), 0, 9) self.assertAlmostEqual(numpy.dot(l2new.flatten(), numpy.arange(35**2)), 48427109.5409886, 7) self.assertAlmostEqual( numpy.dot(l2new.flatten(), numpy.sin(numpy.arange(35**2))), 137.758016736487, 8) self.assertAlmostEqual( numpy.dot(numpy.sin(l2new.flatten()), numpy.arange(35**2)), 507.656936701192, 8)
def update_lambda(mycc, t1, t2, l1, l2, eris=None, imds=None): l1, l2 = ccsd_lambda.update_lambda(mycc, t1, t2, l1, l2, eris, imds) return numpy.zeros_like(l1), l2
l1, l2 = mycc.solve_lambda(mycc.t1, mycc.t2, eris=eris) l1 = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2 = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) print(lib.finger(l1[0]) --0.0030030170069977758) print(lib.finger(l1[1]) --0.0030030170069977758) print(lib.finger(l2[0]) --0.041444910588788492 ) print(lib.finger(l2[1]) - 0.1077575086912813 ) print(lib.finger(l2[2]) --0.041444910588788492 ) print(abs(l2[1]-l2[1].transpose(1,0,2,3)-l2[0]).max()) print(abs(l2[1]-l2[1].transpose(0,1,3,2)-l2[0]).max()) from pyscf.cc import ccsd mycc0 = ccsd.CCSD(mf0) eris0 = mycc0.ao2mo() mycc0.kernel(eris=eris0) t1 = mycc0.t1 t2 = mycc0.t2 imds = ccsd_lambda.make_intermediates(mycc0, t1, t2, eris0) l1, l2 = ccsd_lambda.update_lambda(mycc0, t1, t2, t1, t2, eris0, imds) l1ref, l2ref = ccsd_lambda.update_lambda(mycc0, t1, t2, l1, l2, eris0, imds) t1 = mycc.spatial2spin(t1, mycc.mo_coeff.orbspin) t2 = mycc.spatial2spin(t2, mycc.mo_coeff.orbspin) l1 = mycc.spatial2spin(l1, mycc.mo_coeff.orbspin) l2 = mycc.spatial2spin(l2, mycc.mo_coeff.orbspin) imds = make_intermediates(mycc, t1, t2, eris) l1, l2 = update_lambda(mycc, t1, t2, l1, l2, eris, imds) l1 = mycc.spin2spatial(l1, mycc.mo_coeff.orbspin) l2 = mycc.spin2spatial(l2, mycc.mo_coeff.orbspin) print(abs(l1[0]-l1ref).max()) print(abs(l2[1]-l2ref).max())