Beispiel #1
0
    def test_init(self):
        self.assertTrue(isinstance(cc.CCSD(mf), ccsd.CCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.density_fit()), dfccsd.RCCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.newton()), ccsd.CCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.density_fit().newton()), dfccsd.RCCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.newton().density_fit()), ccsd.CCSD))
        self.assertTrue(not isinstance(cc.CCSD(mf.newton().density_fit()), dfccsd.RCCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.density_fit().newton().density_fit()), dfccsd.RCCSD))

        self.assertTrue(isinstance(cc.UCCSD(mf), uccsd.UCCSD))
#        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.UCCSD(mf.newton()), uccsd.UCCSD))
#        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit().newton()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.UCCSD(mf.newton().density_fit()), uccsd.UCCSD))
#        self.assertTrue(not isinstance(cc.UCCSD(mf.newton().density_fit()), dfccsd.UCCSD))
#        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit().newton().density_fit()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.CCSD(scf.GHF(mol)), gccsd.GCCSD))

        umf = scf.convert_to_uhf(mf, scf.UHF(mol))
        self.assertTrue(isinstance(cc.CCSD(umf), uccsd.UCCSD))
#        self.assertTrue(isinstance(cc.CCSD(umf.density_fit()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.CCSD(umf.newton()), uccsd.UCCSD))
#        self.assertTrue(isinstance(cc.CCSD(umf.density_fit().newton()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.CCSD(umf.newton().density_fit()), uccsd.UCCSD))
#        self.assertTrue(not isinstance(cc.CCSD(umf.newton().density_fit()), dfccsd.UCCSD))
#        self.assertTrue(isinstance(cc.CCSD(umf.density_fit().newton().density_fit()), dfccsd.UCCSD))

        self.assertTrue(isinstance(cc.CCSD(mf, mo_coeff=mf.mo_coeff*1j), rccsd.RCCSD))
Beispiel #2
0
    def test_init(self):
        from pyscf.cc import ccsd
        from pyscf.cc import uccsd
        from pyscf.cc import dfccsd
        self.assertTrue(isinstance(cc.CCSD(mf), ccsd.CCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.density_fit()), dfccsd.RCCSD))
        self.assertTrue(isinstance(cc.CCSD(mf.newton()), ccsd.CCSD))
        self.assertTrue(
            isinstance(cc.CCSD(mf.density_fit().newton()), dfccsd.RCCSD))
        self.assertTrue(
            isinstance(cc.CCSD(mf.newton().density_fit()), ccsd.CCSD))
        self.assertTrue(
            not isinstance(cc.CCSD(mf.newton().density_fit()), dfccsd.RCCSD))
        self.assertTrue(
            isinstance(cc.CCSD(mf.density_fit().newton().density_fit()),
                       dfccsd.RCCSD))

        self.assertTrue(isinstance(cc.UCCSD(mf), uccsd.UCCSD))
        #        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.UCCSD(mf.newton()), uccsd.UCCSD))
        #        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit().newton()), dfccsd.UCCSD))
        self.assertTrue(
            isinstance(cc.UCCSD(mf.newton().density_fit()), uccsd.UCCSD))
        #        self.assertTrue(not isinstance(cc.UCCSD(mf.newton().density_fit()), dfccsd.UCCSD))
        #        self.assertTrue(isinstance(cc.UCCSD(mf.density_fit().newton().density_fit()), dfccsd.UCCSD))

        umf = scf.convert_to_uhf(mf, scf.UHF(mol))
        self.assertTrue(isinstance(cc.CCSD(umf), uccsd.UCCSD))
        #        self.assertTrue(isinstance(cc.CCSD(umf.density_fit()), dfccsd.UCCSD))
        self.assertTrue(isinstance(cc.CCSD(umf.newton()), uccsd.UCCSD))
        #        self.assertTrue(isinstance(cc.CCSD(umf.density_fit().newton()), dfccsd.UCCSD))
        self.assertTrue(
            isinstance(cc.CCSD(umf.newton().density_fit()), uccsd.UCCSD))
Beispiel #3
0
def setUpModule():
    global mol, mf, mol1, mf0, mf1, gmf, ucc, ucc0, ucc1, eris1
    global ecc, orbspin, nocca, noccb, nvira, nvirb, r1, r2
    mol = gto.Mole()
    mol.verbose = 0
    mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
                [1, (0., 0.757, 0.587)]]
    mol.basis = '631g'
    mol.spin = 0
    mol.build()
    mf = scf.UHF(mol).run(conv_tol=1e-12)

    mol1 = mol.copy()
    mol1.spin = 2
    mol1.build()
    mf0 = scf.UHF(mol1).run(conv_tol=1e-12)
    mf1 = copy.copy(mf0)

    nocca, noccb = mol1.nelec
    nmo = mol1.nao_nr()
    nvira, nvirb = nmo - nocca, nmo - noccb
    numpy.random.seed(12)
    mf1.mo_coeff = numpy.random.random((2, nmo, nmo)) - .5
    gmf = scf.addons.convert_to_ghf(mf1)
    orbspin = gmf.mo_coeff.orbspin

    ucc1 = cc.UCCSD(mf1)
    eris1 = ucc1.ao2mo()

    numpy.random.seed(11)
    no = nocca + noccb
    nv = nvira + nvirb
    r1 = numpy.random.random((no, nv)) - .9
    r2 = numpy.random.random((no, no, nv, nv)) - .9
    r2 = r2 - r2.transpose(1, 0, 2, 3)
    r2 = r2 - r2.transpose(0, 1, 3, 2)
    r1 = cc.addons.spin2spatial(r1, orbspin)
    r2 = cc.addons.spin2spatial(r2, orbspin)
    r1, r2 = eom_uccsd.vector_to_amplitudes_ee(
        eom_uccsd.amplitudes_to_vector_ee(r1, r2), ucc1.nmo, ucc1.nocc)
    ucc1.t1 = r1
    ucc1.t2 = r2

    ucc = cc.UCCSD(mf)
    ucc.max_space = 0
    ucc.conv_tol = 1e-8
    ecc = ucc.kernel()[0]

    ucc0 = cc.UCCSD(mf0)
    ucc0.conv_tol = 1e-8
    ucc0.direct = True
    ucc0.kernel()
Beispiel #4
0
def setUpModule():
    global mol, rhf, mf, myucc, mol_s2, mf_s2, eris
    mol = gto.Mole()
    mol.verbose = 7
    mol.output = '/dev/null'
    mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
                [1, (0., 0.757, 0.587)]]

    mol.basis = '631g'
    mol.build()
    rhf = scf.RHF(mol)
    rhf.conv_tol_grad = 1e-8
    rhf.kernel()
    mf = scf.addons.convert_to_uhf(rhf)

    myucc = cc.UCCSD(mf).run(conv_tol=1e-10)

    mol_s2 = gto.Mole()
    mol_s2.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
                   [1, (0., 0.757, 0.587)]]
    mol_s2.basis = '631g'
    mol_s2.spin = 2
    mol_s2.verbose = 5
    mol_s2.output = '/dev/null'
    mol_s2.build()
    mf_s2 = scf.UHF(mol_s2).run()
    eris = uccsd.UCCSD(mf_s2).ao2mo()
Beispiel #5
0
    def test_uccsd_t(self):
        mf1 = copy.copy(mf)
        nao, nmo = mf.mo_coeff[0].shape
        numpy.random.seed(10)
        mf1.mo_coeff = numpy.random.random((2, nao, nmo))
        numpy.random.seed(12)
        nocca, noccb = mol.nelec
        nmo = mf1.mo_occ[0].size
        nvira = nmo - nocca
        nvirb = nmo - noccb
        t1a = .1 * numpy.random.random((nocca, nvira))
        t1b = .1 * numpy.random.random((noccb, nvirb))
        t2aa = .1 * numpy.random.random((nocca, nocca, nvira, nvira))
        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))
        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))
        t1 = t1a, t1b
        t2 = t2aa, t2ab, t2bb
        mycc = cc.UCCSD(mf1)
        eris = mycc.ao2mo(mf1.mo_coeff)
        e3a = uccsd_t.kernel(mycc, eris, [t1a, t1b], [t2aa, t2ab, t2bb])
        self.assertAlmostEqual(e3a, 8193.064821311109, 5)

        e3a = mcc.ccsd_t()
        self.assertAlmostEqual(e3a, -0.0009857042572475674, 11)
Beispiel #6
0
    def test_uccsd_grad(self):
        mol = gto.Mole()
        mol.verbose = 0
        mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
                    [1, (0., 0.757, 0.587)]]
        mol.spin = 2
        mol.basis = '631g'
        mol.build(0, 0)
        mf = scf.UHF(mol)
        mf.conv_tol_grad = 1e-8
        mf.kernel()
        mycc = cc.UCCSD(mf)
        mycc.max_memory = 1
        mycc.conv_tol = 1e-10
        eris = mycc.ao2mo()
        ecc, t1, t2 = mycc.kernel(eris=eris)
        l1, l2 = mycc.solve_lambda(eris=eris)
        g_scan = mycc.nuc_grad_method().as_scanner()
        g1 = g_scan(mol.atom)[1]
        self.assertAlmostEqual(lib.finger(g1), -0.22892720804519961, 6)

        cc_scanner = mycc.as_scanner()
        mol.set_geom_(
            [[8,
              (0., 0., 0.001)], [1,
                                 (0., -0.757, 0.587)], [1,
                                                        (0., 0.757, 0.587)]],
            unit='Ang')
        e1 = cc_scanner(mol)
        mol.set_geom_([[8, (0., 0., -0.001)], [1, (0., -0.757, 0.587)],
                       [1, (0., 0.757, 0.587)]],
                      unit='Ang')
        e2 = cc_scanner(mol)
        self.assertAlmostEqual(g1[0, 2], (e1 - e2) / .002 * lib.param.BOHR, 5)
Beispiel #7
0
 def get_ccsdt_energy(self):
     #if self.spin == 0:
     mf = scf.RHF(self.mol).run()
     mycc = cc.UCCSD(mf)
     t = mycc.kernel()
     e = uccsd_t.kernel(mycc, mycc.ao2mo())
     return e
Beispiel #8
0
 def test_frozen(self):
     mf1 = scf.UHF(mol1).run()
     # Freeze 1s electrons
     frozen = [[0, 1], [0, 1]]
     ucc = cc.UCCSD(mf1, frozen=frozen)
     ecc, t1, t2 = ucc.kernel()
     self.assertAlmostEqual(ecc, -0.34869875247588372, 8)
Beispiel #9
0
    def test_uccsd_t(self):
        mf1 = copy.copy(mf)
        nao, nmo = mf.mo_coeff[0].shape
        numpy.random.seed(10)
        mf1.mo_coeff = numpy.random.random((2,nao,nmo)) - .5
        numpy.random.seed(12)
        nocca, noccb = mol.nelec
        nmo = mf1.mo_occ[0].size
        nvira = nmo - nocca
        nvirb = nmo - noccb
        t1a  = .1 * numpy.random.random((nocca,nvira))
        t1b  = .1 * numpy.random.random((noccb,nvirb))
        t2aa = .1 * numpy.random.random((nocca,nocca,nvira,nvira))
        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))
        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))
        t1 = t1a, t1b
        t2 = t2aa, t2ab, t2bb
        mycc = cc.UCCSD(mf1)
        eris = mycc.ao2mo(mf1.mo_coeff)
        mycc.incore_complete = True
        e3a = mycc.ccsd_t([t1a,t1b], [t2aa, t2ab, t2bb], eris)
        self.assertAlmostEqual(e3a, 15.582860941071505, 8)

        mycc.incore_complete = False
        mycc.max_memory = 0
        e3a = uccsd_t.kernel(mycc, eris, [t1a,t1b], [t2aa, t2ab, t2bb])
        self.assertAlmostEqual(e3a, 15.582860941071505, 8)

        e3a = mcc.ccsd_t()
        self.assertAlmostEqual(e3a, -0.0009857042572475674, 11)
Beispiel #10
0
 def test_uccsd_frozen(self):
     # Freeze 1s electrons
     frozen = [[0, 1], [0, 1]]
     ucc = cc.UCCSD(mf_s2, frozen=frozen)
     ucc.diis_start_cycle = 1
     ecc, t1, t2 = ucc.kernel()
     self.assertAlmostEqual(ecc, -0.07414978284611283, 8)
Beispiel #11
0
 def test_eomee(self):
     ucc = cc.UCCSD(mf)
     ecc, t1, t2 = ucc.kernel()
     self.assertAlmostEqual(ecc, -0.2133432430989155, 6)
     e, v = ucc.eeccsd(nroots=4)
     self.assertAlmostEqual(e[0], 0.2757159395886167, 6)
     self.assertAlmostEqual(e[1], 0.2757159395886167, 6)
     self.assertAlmostEqual(e[2], 0.2757159395886167, 6)
     self.assertAlmostEqual(e[3], 0.3005716731825082, 6)
Beispiel #12
0
    def test_uccsd_rdm2_mo2ao(self):
        mol = gto.Mole()
        mol.verbose = 0
        mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
                    [1, (0., 0.757, 0.587)]]
        mol.spin = 2
        mol.basis = '631g'
        mol.build(0, 0)
        mf = scf.UHF(mol)
        mf.conv_tol_grad = 1e-8
        mf.kernel()
        mycc = cc.UCCSD(mf)
        mycc.diis_start_cycle = 1
        mycc.conv_tol = 1e-10
        eris = mycc.ao2mo()
        ecc, t1, t2 = mycc.kernel(eris=eris)
        l1, l2 = mycc.solve_lambda(eris=eris)
        fdm2 = lib.H5TmpFile()
        d2 = cc.uccsd_rdm._gamma2_outcore(mycc, t1, t2, l1, l2, fdm2, True)

        nao = mycc.mo_coeff[0].shape[0]
        ref = cc.uccsd_rdm._make_rdm2(mycc, None, d2, with_dm1=False)
        aa = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[0], mycc.mo_coeff[0],
                        mycc.mo_coeff[0], mycc.mo_coeff[0], mycc.mo_coeff[0])
        ab = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[1], mycc.mo_coeff[0],
                        mycc.mo_coeff[0], mycc.mo_coeff[1], mycc.mo_coeff[1])
        bb = lib.einsum('ijkl,pi,qj,rk,sl->pqrs', ref[2], mycc.mo_coeff[1],
                        mycc.mo_coeff[1], mycc.mo_coeff[1], mycc.mo_coeff[1])
        aa = aa + aa.transpose(0, 1, 3, 2)
        aa = aa + aa.transpose(1, 0, 2, 3)
        aa = ao2mo.restore(4, aa, nao) * .5
        bb = bb + bb.transpose(0, 1, 3, 2)
        bb = bb + bb.transpose(1, 0, 2, 3)
        bb = ao2mo.restore(4, bb, nao) * .5
        ab = ab + ab.transpose(0, 1, 3, 2)
        ab = ab + ab.transpose(1, 0, 2, 3)
        ab = ao2mo.restore(4, ab, nao) * .5
        ref = (aa + ab, bb + ab.T)
        rdm2 = uccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff)
        self.assertAlmostEqual(abs(ref[0] - rdm2[0]).max(), 0, 10)
        self.assertAlmostEqual(abs(ref[1] - rdm2[1]).max(), 0, 10)
        uccsd_grad._rdm2_mo2ao(mycc, d2, mycc.mo_coeff, fdm2)
        self.assertAlmostEqual(
            abs(ref[0] - fdm2['dm2aa+ab'].value).max(), 0, 10)
        self.assertAlmostEqual(
            abs(ref[1] - fdm2['dm2bb+ab'].value).max(), 0, 10)
        self.assertAlmostEqual(lib.finger(rdm2[0]), -1.6247203743431637, 7)
        self.assertAlmostEqual(lib.finger(rdm2[1]), -0.44062825991527471, 7)
Beispiel #13
0
def setUpModule():
    global mol, mol1, mf, myucc, mygcc
    mol = gto.Mole()
    mol.verbose = 5
    mol.output = '/dev/null'
    mol.atom = [[8, (0., 0., 0.)], [1, (0., -.757, .587)],
                [1, (0., .757, .587)]]
    mol.spin = 2
    mol.basis = '3-21g'
    mol.symmetry = 'C2v'
    mol.build()
    mol1 = copy.copy(mol)
    mol1.symmetry = False

    mf = scf.UHF(mol1).run(conv_tol=1e-14)
    myucc = cc.UCCSD(mf).run()
    mygcc = cc.GCCSD(mf).run()
Beispiel #14
0
mol = gto.Mole()
mol.verbose = 5
mol.output = '/dev/null'
mol.atom = [
    [8 , (0. , 0.     , 0.)],
    [1 , (0. , -.757 , .587)],
    [1 , (0. ,  .757 , .587)]]
mol.spin = 2
mol.basis = '3-21g'
mol.symmetry = 'C2v'
mol.build()
mol1 = copy.copy(mol)
mol1.symmetry = False

mf = scf.UHF(mol1).run(conv_tol=1e-14)
myucc = cc.UCCSD(mf).run()
mygcc = cc.GCCSD(mf).run()

def tearDownModule():
    global mol, mol1, mf, myucc, mygcc
    mol.stdout.close()
    del mol, mol1, mf, myucc, mygcc

class KnownValues(unittest.TestCase):
    def test_gccsd_t_compare_uccsd_t(self):
        self.assertAlmostEqual(myucc.ccsd_t(), mygcc.ccsd_t(t1=None), 7)

    def test_gccsd_t(self):
        mf1 = copy.copy(mf)
        nao, nmo = mf.mo_coeff[0].shape
        numpy.random.seed(10)
Beispiel #15
0
    mol = gto.Mole()
    mol.atom = [[8, (0., 0., 0.)], [1, (0., -.757, .587)],
                [1, (0., .757, .587)]]

    mol.basis = '631g'
    mol.build()
    rhf = scf.RHF(mol)
    rhf.conv_tol = 1e-14
    rhf.scf()
    mcc = cc.CCSD(rhf)
    mcc.conv_tol = 1e-12
    mcc.ccsd()
    t1a = t1b = mcc.t1
    t2ab = mcc.t2
    t2aa = t2bb = t2ab - t2ab.transpose(1, 0, 2, 3)
    mycc = cc.UCCSD(scf.addons.convert_to_uhf(rhf))
    eris = mycc.ao2mo()
    e3a = kernel(mycc, eris, (t1a, t1b), (t2aa, t2ab, t2bb))
    print(e3a - -0.00099642337843278096)

    mol = gto.Mole()
    mol.atom = [[8, (0., 0., 0.)], [1, (0., -.757, .587)],
                [1, (0., .757, .587)]]
    mol.spin = 2
    mol.basis = '3-21g'
    mol.build()
    mf = scf.UHF(mol).run(conv_tol=1e-14)
    nao, nmo = mf.mo_coeff[0].shape
    numpy.random.seed(10)
    mf.mo_coeff = numpy.random.random((2, nao, nmo))
Beispiel #16
0
from pyscf.fci import direct_uhf

mol = gto.Mole()
mol.verbose = 7
mol.output = '/dev/null'
mol.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
            [1, (0., 0.757, 0.587)]]

mol.basis = '631g'
mol.build()
rhf = scf.RHF(mol)
rhf.conv_tol_grad = 1e-8
rhf.kernel()
mf = scf.addons.convert_to_uhf(rhf)

myucc = cc.UCCSD(mf).run(conv_tol=1e-10)

mol_s2 = gto.Mole()
mol_s2.atom = [[8, (0., 0., 0.)], [1, (0., -0.757, 0.587)],
               [1, (0., 0.757, 0.587)]]
mol_s2.basis = '631g'
mol_s2.spin = 2
mol_s2.verbose = 5
mol_s2.output = '/dev/null'
mol_s2.build()
mf_s2 = scf.UHF(mol_s2).run()
eris = uccsd.UCCSD(mf_s2).ao2mo()


def tearDownModule():
    global mol, rhf, mf, myucc, mol_s2, mf_s2, eris
Beispiel #17
0
mol.verbose = 4
mol.atom = '''
O      0.000000      0.000000      0.118351
H      0.000000      0.761187     -0.469725
H      0.000000     -0.761187     -0.469725
'''
mol.basis = 'sto-3g'
mol.symmetry = 0
mol.build()

a = scf.UHF(mol)
ehf = a.scf()

####### This can be removed ######
frozen = [[0], [0]]  # 1sa and 1sb
mcc = cc.UCCSD(a)
mcc.frozen = frozen
mcc.diis_space = 10
mcc.conv_tol = 1e-6
mcc.conv_tol_normt = 1e-6
mcc.max_cycle = 150
ecc = mcc.kernel()[0]
eris = mcc.ao2mo()
e3 = uccsd_t.kernel(mcc, eris, mcc.t1, mcc.t2)
lib.logger.info(mcc, "* CCSD(T) energy : %12.6f" % (ehf + ecc + e3))
l1, l2 = uccsd_t_lambda.kernel(mcc, eris, mcc.t1, mcc.t2)[1:]

rdm1a, rdm1b = uccsd_t_rdm.make_rdm1(mcc, mcc.t1, mcc.t2, l1, l2, eris=eris)
rdm2aa, rdm2ab, rdm2bb = uccsd_t_rdm.make_rdm2(mcc,
                                               mcc.t1,
                                               mcc.t2,
Beispiel #18
0
 def test_with_df_s2(self):
     mf = scf.UHF(mol_s2).density_fit(auxbasis='weigend').run()
     mycc = cc.UCCSD(mf).run()
     self.assertAlmostEqual(mycc.e_tot, -75.83360033370676, 7)
Beispiel #19
0
numpy.random.seed(12)
mf1.mo_coeff = numpy.random.random((2, n, n))
dm = mf1.make_rdm1(mf1.mo_coeff, mf1.mo_occ)
fockao = mf1.get_hcore() + mf1.get_veff(mol1, dm)
mo_energya = numpy.einsum('pi,pq,qi->i', mf1.mo_coeff[0], fockao[0],
                          mf1.mo_coeff[0])
mo_energyb = numpy.einsum('pi,pq,qi->i', mf1.mo_coeff[1], fockao[1],
                          mf1.mo_coeff[1])
idxa = numpy.hstack(
    [mo_energya[:no // 2].argsort(), no // 2 + mo_energya[no // 2:].argsort()])
idxb = numpy.hstack(
    [mo_energyb[:no // 2].argsort(), no // 2 + mo_energyb[no // 2:].argsort()])
mf1.mo_coeff[0] = mf1.mo_coeff[0][:, idxa]
mf1.mo_coeff[1] = mf1.mo_coeff[1][:, idxb]

ucc1 = cc.UCCSD(mf1)
ucc1.eris = eris = ucc1.ao2mo()

numpy.random.seed(12)
r1 = numpy.random.random((no, nv)) - .9
r2 = numpy.random.random((no, no, nv, nv)) - .9
r1, r2 = ucc1.vector_to_amplitudes_ee(ucc1.amplitudes_to_vector_ee(r1, r2))
r1 = ucc1.spin2spatial(r1, eris.orbspin)
r2 = ucc1.spin2spatial(r2, eris.orbspin)
ucc1.eris = eris
ucc1.t1 = [x * 1e-5 for x in r1]
ucc1.t2 = [x * 1e-5 for x in r2]


class KnowValues(unittest.TestCase):
    def test_frozen(self):
Beispiel #20
0
    def test_ERIS(self):
        ucc1 = cc.UCCSD(mf)
        nao, nmo = mf.mo_coeff[0].shape
        numpy.random.seed(1)
        mo_coeff = numpy.random.random((2, nao, nmo))
        eris = cc.uccsd._make_eris_incore(ucc1, mo_coeff)

        self.assertAlmostEqual(lib.finger(eris.oooo), 4.9638849382825754, 11)
        self.assertAlmostEqual(lib.finger(eris.ovoo), -1.3623681896983584, 11)
        self.assertAlmostEqual(lib.finger(eris.ovov), 125.81550684442163, 11)
        self.assertAlmostEqual(lib.finger(eris.oovv), 55.123681017639598, 11)
        self.assertAlmostEqual(lib.finger(eris.ovvo), 133.48083527898248, 11)
        self.assertAlmostEqual(lib.finger(eris.ovvv), 59.421927525288183, 11)
        self.assertAlmostEqual(lib.finger(eris.vvvv), 43.556602622204778, 11)
        self.assertAlmostEqual(lib.finger(eris.OOOO), -407.05319440524585, 11)
        self.assertAlmostEqual(lib.finger(eris.OVOO), 56.284299937160796, 11)
        self.assertAlmostEqual(lib.finger(eris.OVOV), -287.72899895597448, 11)
        self.assertAlmostEqual(lib.finger(eris.OOVV), -85.484299959144522, 11)
        self.assertAlmostEqual(lib.finger(eris.OVVO), -228.18996145476956, 11)
        self.assertAlmostEqual(lib.finger(eris.OVVV), -10.715902258877399, 11)
        self.assertAlmostEqual(lib.finger(eris.VVVV), -89.908425473958303, 11)
        self.assertAlmostEqual(lib.finger(eris.ooOO), -336.65979260175226, 11)
        self.assertAlmostEqual(lib.finger(eris.ovOO), -16.405125847288176, 11)
        self.assertAlmostEqual(lib.finger(eris.ovOV), 231.59042209500075, 11)
        self.assertAlmostEqual(lib.finger(eris.ooVV), 20.338077193028354, 11)
        self.assertAlmostEqual(lib.finger(eris.ovVO), 206.48662856981386, 11)
        self.assertAlmostEqual(lib.finger(eris.ovVV), -71.273249852220516, 11)
        self.assertAlmostEqual(lib.finger(eris.vvVV), 172.47130671068496, 11)
        self.assertAlmostEqual(lib.finger(eris.OVoo), -19.927660309103977, 11)
        self.assertAlmostEqual(lib.finger(eris.OOvv), -27.761433381797019, 11)
        self.assertAlmostEqual(lib.finger(eris.OVvo), -140.09648311337384, 11)
        self.assertAlmostEqual(lib.finger(eris.OVvv), 40.700983950220547, 11)

        uccsd.MEMORYMIN, bak = 0, uccsd.MEMORYMIN
        ucc1.max_memory = 0
        eris1 = ucc1.ao2mo(mo_coeff)
        uccsd.MEMORYMIN = bak
        self.assertAlmostEqual(
            abs(numpy.array(eris1.oooo) - eris.oooo).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovoo) - eris.ovoo).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovov) - eris.ovov).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.oovv) - eris.oovv).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovvo) - eris.ovvo).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovvv) - eris.ovvv).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.vvvv) - eris.vvvv).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OOOO) - eris.OOOO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVOO) - eris.OVOO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVOV) - eris.OVOV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OOVV) - eris.OOVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVVO) - eris.OVVO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVVV) - eris.OVVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.VVVV) - eris.VVVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ooOO) - eris.ooOO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovOO) - eris.ovOO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovOV) - eris.ovOV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ooVV) - eris.ooVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovVO) - eris.ovVO).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.ovVV) - eris.ovVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.vvVV) - eris.vvVV).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVoo) - eris.OVoo).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OOvv) - eris.OOvv).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVvo) - eris.OVvo).max(), 0, 11)
        self.assertAlmostEqual(
            abs(numpy.array(eris1.OVvv) - eris.OVvv).max(), 0, 11)

        # Testing the complex MO integrals
        def ao2mofn(mos):
            if isinstance(mos, numpy.ndarray) and mos.ndim == 2:
                mos = [mos] * 4
            nmos = [mo.shape[1] for mo in mos]
            eri_mo = ao2mo.kernel(mf._eri, mos, compact=False).reshape(nmos)
            return eri_mo * 1j

        eris1 = cc.uccsd._make_eris_incore(ucc1, mo_coeff, ao2mofn=ao2mofn)
        self.assertAlmostEqual(abs(eris1.oooo.imag - eris.oooo).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovoo.imag - eris.ovoo).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovov.imag - eris.ovov).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.oovv.imag - eris.oovv).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovvo.imag - eris.ovvo).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.ovvv.imag-eris.ovvv).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.vvvv.imag-eris.vvvv).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OOOO.imag - eris.OOOO).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OVOO.imag - eris.OVOO).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OVOV.imag - eris.OVOV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OOVV.imag - eris.OOVV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OVVO.imag - eris.OVVO).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.OVVV.imag-eris.OVVV).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.VVVV.imag-eris.VVVV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ooOO.imag - eris.ooOO).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovOO.imag - eris.ovOO).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovOV.imag - eris.ovOV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ooVV.imag - eris.ooVV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.ovVO.imag - eris.ovVO).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.ovVV.imag-eris.ovVV).max(), 0, 11)
        #self.assertAlmostEqual(abs(eris1.vvVV.imag-eris.vvVV).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OVoo.imag - eris.OVoo).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OOvv.imag - eris.OOvv).max(), 0, 11)
        self.assertAlmostEqual(abs(eris1.OVvo.imag - eris.OVvo).max(), 0, 11)
Beispiel #21
0
    print(abs(trdm2 - dm2ref).max())

    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.charge = 2
    mol.build()
    mf0 = mf = scf.UHF(mol).run(conv_tol=1)
    mf = scf.addons.convert_to_ghf(mf)

    from pyscf.cc import uccsd_t_slow
    from pyscf.cc import uccsd_t_lambda
    from pyscf.cc import uccsd_t_rdm
    mycc0 = cc.UCCSD(mf0)
    eris0 = mycc0.ao2mo()
    mycc0.kernel(eris=eris0)
    t1 = mycc0.t1
    t2 = mycc0.t2
    imds = uccsd_t_lambda.make_intermediates(mycc0, t1, t2, eris0)
    l1, l2 = uccsd_t_lambda.update_lambda(mycc0, t1, t2, t1, t2, eris0, imds)
    dm1ref = uccsd_t_rdm.make_rdm1(mycc0, t1, t2, l1, l2, eris0)
    dm2ref = uccsd_t_rdm.make_rdm2(mycc0, t1, t2, l1, l2, eris0)

    mycc = cc.GCCSD(mf)
    eris = mycc.ao2mo()
    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)
Beispiel #22
0
mol1 = mol.copy()
mol1.spin = 2
mol1.build()
mf0 = scf.UHF(mol1).run(conv_tol=1e-12)
mf1 = copy.copy(mf0)

nocca, noccb = mol1.nelec
nmo = mol1.nao_nr()
nvira, nvirb = nmo-nocca, nmo-noccb
numpy.random.seed(12)
mf1.mo_coeff = numpy.random.random((2,nmo,nmo)) - .5
gmf = scf.addons.convert_to_ghf(mf1)
orbspin = gmf.mo_coeff.orbspin

ucc1 = cc.UCCSD(mf1)
eris1 = ucc1.ao2mo()

numpy.random.seed(11)
no = nocca + noccb
nv = nvira + nvirb
r1 = numpy.random.random((no,nv)) - .9
r2 = numpy.random.random((no,no,nv,nv)) - .9
r2 = r2 - r2.transpose(1,0,2,3)
r2 = r2 - r2.transpose(0,1,3,2)
r1 = cc.addons.spin2spatial(r1, orbspin)
r2 = cc.addons.spin2spatial(r2, orbspin)
r1,r2 = eom_uccsd.vector_to_amplitudes_ee(
    eom_uccsd.amplitudes_to_vector_ee(r1,r2), ucc1.nmo, ucc1.nocc)
ucc1.t1 = r1
ucc1.t2 = r2
Beispiel #23
0
    def ecw_energy(self, method, dim):

        mf_energy, Vemb, Vxc = self.imp_mf_energy(dim)

        Ne_frag = self.Ne_frag
        ops = self.ops

        subOEI = ops["subKin"] + ops["subVnuc1"] + Vemb
        subTEI = ops["subTEI"]

        mf = qcwrap.qc_scf(Ne_frag,
                           dim,
                           'hf',
                           mol=self.mol_frag,
                           oei=subOEI,
                           tei=subTEI,
                           dm0=self.P_imp)
        mf.runscf()
        e_hf = mf.elec_energy  # - np.trace(np.dot(mf.rdm1,Vemb))
        if (self.plot_mo):
            self.ints.submo_molden(mf.mo_coeff, mf.mo_occ, self.loc2sub,
                                   "mo_frag.molden", self.mol_frag)

        exc = 0.0
        if (Vxc is not None):
            #           exc = np.einsum('ij,ji', Vxc, mf.rdm1-self.P_imp)
            exc = np.einsum('ij,ji', Vxc, self.P_imp)
        print('exc = ', exc)

        if (method == 'hf'):
            energy = e_hf
        elif (method == 'mp2'):
            mp2 = mp.MP2(mf)
            mp2.kernel()
            energy = e_hf + mp2.e_corr
        elif (method == 'ccsd' or method == 'ccsd(t)'):
            mycc = cc.CCSD(mf)
            mycc.max_cycle = 200
            #mycc.conv_tol = 1e-6
            #mycc.conv_tol_normt = 1e-4
            mycc.kernel()

            et = 0.0
            if (method == 'ccsd(t)'):
                print('CCSD(T) correction')
                et = mycc.ccsd_t()

            energy = e_hf + mycc.e_corr + et
        elif (method == 'eomccsd'):
            mf.verbose = 5
            mycc = cc.RCCSD(mf)
            mycc.kernel()
            es, vs = mycc.eomee_ccsd_singlet(nroots=self.ex_nroots)

            if (self.ex_nroots == 1):
                r1, r2 = mycc.vector_to_amplitudes(vs)
                print('debug: ', r1.shape)
            else:
                for vn in vs:
                    r1, r2 = mycc.vector_to_amplitudes(vn)

            energy = e_hf + mycc.e_corr
        elif (method == 'eomsfccsd'):
            self.mol_frag.spin = 2
            mf = qcwrap.pyscf_rks.rohf_pyscf(Ne_frag,
                                             dim,
                                             mol=self.mol_frag,
                                             oei=subOEI,
                                             tei=subTEI,
                                             dm0=np.array((.5 * self.P_imp,
                                                           .5 * self.P_imp)))
            mf.kernel(dm0=mf.dm_guess)

            mf1 = qcwrap.pyscf_rks.uhf_pyscf(Ne_frag,
                                             dim,
                                             mol=self.mol_frag,
                                             oei=subOEI,
                                             tei=subTEI,
                                             dm0=None)
            mf1.convert_from_rhf(mf)

            mycc = cc.UCCSD(mf1)
            mycc.verbose = 5
            mycc.kernel()
            es, vs = mycc.eomsf_ccsd(nroots=self.ex_nroots)

            if (self.ex_nroots == 1):
                r1, r2 = cc.eom_uccsd.vector_to_amplitudes_eomsf(
                    vs, mycc.nmo, mycc.nocc)
                tools.print_eomcc_t1(r1[0])
                tools.print_eomcc_t1(r1[1])
            else:
                for vn in vs:
                    r1, r2 = cc.eom_uccsd.vector_to_amplitudes_eomsf(
                        vn, mycc.nmo, mycc.nocc)
                    tools.print_eomcc_t1(r1[0])
                    tools.print_eomcc_t1(r1[1])

            energy = mf1.e_tot + mycc.e_corr
        else:
            raise Exception("ecw_method not supported!")

        energy -= mf_energy
        energy -= exc

        return energy
Beispiel #24
0
    eris0 = mycc0.ao2mo()
    mycc0.kernel(eris=eris0)
    t1 = mycc0.t1
    t2 = mycc0.t2
    imds = ccsd_t_lambda.make_intermediates(mycc0, t1, t2, eris0)
    l1, l2 = ccsd_t_lambda.update_lambda(mycc0, t1, t2, t1, t2, eris0, imds)
    dm1ref = ccsd_t_rdm.make_rdm1(mycc0, t1, t2, l1, l2, eris0)
    dm2ref = ccsd_t_rdm.make_rdm2(mycc0, t1, t2, l1, l2, eris0)

    t1 = (t1, t1)
    t2aa = t2 - t2.transpose(1, 0, 2, 3)
    t2 = (t2aa, t2, t2aa)
    l1 = (l1, l1)
    l2aa = l2 - l2.transpose(1, 0, 2, 3)
    l2 = (l2aa, l2, l2aa)
    mycc = cc.UCCSD(mf)
    eris = mycc.ao2mo()
    dm1 = make_rdm1(mycc, t1, t2, l1, l2, eris)
    dm2 = make_rdm2(mycc, t1, t2, l1, l2, eris)
    trdm1 = dm1[0] + dm1[1]
    trdm2 = dm2[0] + dm2[1] + dm2[1].transpose(2, 3, 0, 1) + dm2[2]
    print(abs(trdm1 - dm1ref).max())
    print(abs(trdm2 - dm2ref).max())

    ecc = mycc.kernel(eris=eris)[0]
    l1, l2 = mycc.solve_lambda(eris=eris)
    e3ref = mycc.e_tot + mycc.ccsd_t()

    nmoa, nmob = mycc.nmo
    eri_aa = ao2mo.kernel(mf._eri, mf.mo_coeff[0],
                          compact=False).reshape([nmoa] * 4)
Beispiel #25
0
 def test_with_df_s0(self):
     mf = scf.UHF(mol).density_fit(auxbasis='weigend').run()
     mycc = cc.UCCSD(mf).run()
     self.assertAlmostEqual(mycc.e_tot, -76.118403942938741, 7)
Beispiel #26
0
    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)
Beispiel #27
0
def do_scf(inp):
    '''Do the requested SCF.'''

    from pyscf import gto, scf, dft, cc, fci, ci, ao2mo, mcscf, mrpt, lib, mp, tdscf
    from pyscf.cc import ccsd_t, uccsd_t
    import numpy as np
    from .fcidump import fcidump

    # sort out the method
    mol = inp.mol
    method = inp.scf.method.lower()

    # UHF
    if method == 'uhf':
        ehf, mSCF = do_hf(inp, unrestricted=True)
        print_energy('UHF', ehf)

        if inp.scf.exci is not None:
            inp.timer.start('TDHF')
            mtd = do_TDDFT(inp, mSCF)
            inp.timer.end('TDHF')

    # RHF
    elif method in ('rhf', 'hf'):
        ehf, mSCF = do_hf(inp)
        print_energy('RHF', ehf)

        if inp.scf.exci is not None:
            inp.timer.start('TDHF')
            mtd = do_TDDFT(inp, mSCF)
            inp.timer.end('TDHF')

    # CCSD and CCSD(T)
    elif method in ('ccsd', 'ccsd(t)', 'uccsd', 'uccsd(t)', 'eomccsd'):
        if 'u' in method:
            ehf, tSCF = do_hf(inp, unrestricted=True)
            print_energy('UHF', ehf)
        else:
            ehf, tSCF = do_hf(inp)
            print_energy('RHF', ehf)

        inp.timer.start('ccsd')
        frozen = 0
        if inp.scf.freeze is not None: frozen = inp.scf.freeze
        if 'u' in method:
            mSCF = cc.UCCSD(tSCF, frozen=frozen)
        else:
            mSCF = cc.CCSD(tSCF, frozen=frozen)
        mSCF.max_cycle = inp.scf.maxiter
        eccsd, t1, t2 = mSCF.kernel()
        print_energy('CCSD', ehf + eccsd)
        inp.timer.end('ccsd')

        if method == 'eomccsd':
            inp.timer.start('eomccsd')
            ee = mSCF.eomee_ccsd_singlet(nroots=4)[0]
            inp.timer.end('eomccsd')
            for i in range(len(ee)):
                print_energy('EOM-CCSD {0} (eV)'.format(i+1), ee[i] * 27.2114)

        if method in ('ccsd(t)', 'uccsd(t)'):
            inp.timer.start('ccsd(t)')
            eris = mSCF.ao2mo()

            if method == 'ccsd(t)':
                e3 = ccsd_t.kernel(mSCF, eris)
            else:
                e3 = uccsd_t.kernel(mSCF, eris)
            print_energy('CCSD(T)', ehf + eccsd + e3)
            inp.timer.end('ccsd(t)')

    # MP2
    elif method == 'mp2':
        ehf, tSCF = do_hf(inp)
        print_energy('RHF', ehf)

        inp.timer.start('mp2')
        frozen = 0 
        if inp.scf.freeze is not None: frozen = inp.scf.freeze
        mSCF = mp.MP2(tSCF, frozen=frozen)
        emp2, t2 = mSCF.kernel()
        print_energy('MP2', ehf+emp2)
        inp.timer.end('mp2')

    # CISD
    elif method == 'cisd' or method == 'cisd(q)':
        ehf, tSCF = do_hf(inp)
        print_energy('RHF', ehf)

        inp.timer.start('cisd')
        frozen = 0
        if inp.scf.freeze is not None: frozen = inp.scf.freeze
        mSCF = ci.CISD(tSCF, frozen=frozen)
        ecisd = mSCF.kernel()[0]
        print_energy('CISD', ehf + ecisd)
        inp.timer.end('cisd')

        # perform Davison quadruples correction
        c0 = np.max(np.abs(mSCF.ci))
        c02 = c0**2
        ne = mSCF.mol.nelectron
        eq = ( 1.0 - c02 ) * ecisd
        print_energy('CISD(Q) Davidson', ehf + ecisd + eq)
        eq = ( 1.0 - c02 ) / c02 * ecisd
        print_energy('CISD(Q) Renomalized-Davidson', ehf + ecisd + eq)
        eq = ( 1.0 - c02 ) / ( 2.0 * c02 - 1.0 ) * ecisd
        print_energy('CISD(Q) Davison-Silver', ehf + ecisd + eq)
        eq = (( 2.0 * c02 ) / ((2.0*c02-1.0)*(1.0 + np.sqrt(1.0 + (8.0*c02*(1.0-c02))
         / ( ne * (2.0*c02-1.0)**2 )))) - 1.0 ) * ecisd
        print_energy('CISD(Q) PC', ehf + ecisd + eq)
        eq = ( 1.0 - c02 ) / c02 * ((ne-2)*(ne-3.0)) / (ne*(ne-1.0)) * ecisd
        print_energy('CISD(Q) MC', ehf + ecisd + eq)
        if ne > 2:
            eq = ( 2.0 - c02 ) / (2.0 * (ne-1.0)/(ne-2.0) * c02 - 1.0)
        else:
            eq = 0.0
        print_energy('CISD(Q) DD', ehf + ecisd + eq)


    # UKS
    elif method in ('uks' or 'udft'):
        inp.timer.start('uks')
        inp.timer.start('grids')
        grids = dft.gen_grid.Grids(mol)
        grids.level = inp.scf.grid
        grids.build()
        inp.timer.end('grids')
        mSCF = dft.UKS(mol)
        mSCF.grids = grids
        mSCF.xc = inp.scf.xc
        mSCF.conv_tol = inp.scf.conv
        mSCF.conv_tol_grad = inp.scf.grad
        mSCF.max_cycle = inp.scf.maxiter
        mSCF.init_guess = inp.scf.guess
        mSCF.small_rho_cutoff = 1e-20
        eks = mSCF.kernel()
        print_energy('UKS', eks)
        inp.timer.end('uks')

        if inp.scf.exci is not None:
            inp.timer.start('TDDFT')
            mtd = do_TDDFT(inp, mSCF)
            inp.timer.end('TDDFT')

    # RKS
    elif method in ('rks', 'ks', 'rdft', 'dft'):
        inp.timer.start('ks')
        inp.timer.start('grids')
        grids = dft.gen_grid.Grids(mol)
        grids.level = inp.scf.grid
        grids.build()
        inp.timer.end('grids')
        if mol.nelectron%2 == 0:
            mSCF = dft.RKS(mol)
        else:
            mSCF = dft.ROKS(mol)
        mSCF.grids = grids
        mSCF.xc = inp.scf.xc
        mSCF.conv_tol = inp.scf.conv
        mSCF.conv_tol_grad = inp.scf.grad
        mSCF.max_cycle = inp.scf.maxiter
        mSCF.init_guess = inp.scf.guess
        mSCF.small_rho_cutoff = 1e-20
        mSCF.damp = inp.scf.damp
        mSCF.level_shift = inp.scf.shift
        eks = mSCF.kernel()
        print_energy('RKS', eks)
        inp.timer.end('ks')

        if inp.scf.exci is not None:
            inp.timer.start('TDDFT')
            mtd = do_TDDFT(inp, mSCF)
            inp.timer.end('TDDFT')

    # Unrestricted FCI
    elif method == 'ufci':
        ehf, mSCF = do_hf(inp, unrestricted=True)
        print_energy('UHF', ehf)

        inp.timer.start('fci')
        cis = fci.direct_uhf.FCISolver(mol)
        norb = mSCF.mo_energy[0].size
        nea = (mol.nelectron+mol.spin) // 2
        neb = (mol.nelectron-mol.spin) // 2
        nelec = (nea, neb)
        mo_a = mSCF.mo_coeff[0]
        mo_b = mSCF.mo_coeff[1]
        h1e_a = reduce(np.dot, (mo_a.T, mSCF.get_hcore(), mo_a))
        h1e_b = reduce(np.dot, (mo_b.T, mSCF.get_hcore(), mo_b))
        g2e_aa = ao2mo.incore.general(mSCF._eri, (mo_a,)*4, compact=False)
        g2e_aa = g2e_aa.reshape(norb,norb,norb,norb)
        g2e_ab = ao2mo.incore.general(mSCF._eri, (mo_a,mo_a,mo_b,mo_b), compact=False)
        g2e_ab = g2e_ab.reshape(norb,norb,norb,norb)
        g2e_bb = ao2mo.incore.general(mSCF._eri, (mo_b,)*4, compact=False)
        g2e_bb = g2e_bb.reshape(norb,norb,norb,norb)
        h1e = (h1e_a, h1e_b)
        eri = (g2e_aa, g2e_ab, g2e_bb)

        eci = fci.direct_uhf.kernel(h1e, eri, norb, nelec)[0]
        print_energy('FCI', eci)
        inp.timer.end('fci')

    # FCI
    elif method in ('fci'):
        ehf, mSCF = do_hf(inp)
        print_energy('RHF', ehf)

        inp.timer.start('fci')
        if inp.scf.freeze is None:
            mCI = fci.FCI(mSCF)
            if inp.scf.roots is not None:
                mCI.nroots = inp.scf.roots
            mCI.kernel()[0]
            eci = mCI.eci

        else:
            nel  = mol.nelectron - inp.scf.freeze * 2
            ncas = mol.nao_nr() - inp.scf.freeze
            if mol.spin == 0:
                nelecas = nel
                mCI = mcscf.CASCI(mSCF, ncas, nelecas)
                mCI.fcisolver = fci.solver(mol)
            else:
                if mol.spin%2 == 0:
                    nelecas = (nel//2+mol.spin//2, nel//2-mol.spin//2)
                else:
                    nelecas = (nel//2+mol.spin//2+1, nel//2-mol.spin//2)
                mCI = mcscf.CASCI(mSCF, ncas, nelecas)
                mCI.fcisolver = fci.direct_spin1.FCISolver(mol)
            eci = mCI.kernel()[0]
            dm = mCI.make_rdm1()
            dip = mSCF.dip_moment(dm=dm)

        if inp.scf.roots is None:
            print_energy('FCI', eci)
        else:
            for i in range(inp.scf.roots):
                print_energy('FCI {0}'.format(i), eci[i])
        inp.timer.end('fci')

    # CASCI
    elif method == 'casci':
        if inp.scf.cas is None and inp.scf.casorb is None:
            print ('ERROR: Must specify CAS space or CASORB')
            return inp

        ehf, mSCF = do_hf(inp)
        print_energy('RHF', ehf)

        # get cas space
        if inp.scf.cas is not None:
            if mol.spin == 0:
                nelecas = inp.scf.cas[0]
            else:
                nelecas = (inp.scf.cas[0]//2 + mol.spin//2,
                           inp.scf.cas[0]//2 - mol.spin//2)
            ncasorb = inp.scf.cas[1]
        elif inp.scf.casorb is not None:
            ncasorb = len(inp.scf.casorb)
            nelecas = int(np.sum(mSCF.mo_occ[inp.scf.casorb]))
            if inp.scf.casspin is not None:
                nelecas = (nelecas//2 + inp.scf.casspin//2,
                           nelecas//2 - inp.scf.casspin//2)

        inp.timer.start('casci')
        mCI = mcscf.CASCI(mSCF, ncasorb, nelecas)

        if inp.scf.casorb is not None:
            mo = mcscf.addons.sort_mo(mCI, np.copy(mSCF.mo_coeff), inp.scf.casorb, 0)
        else:
            mo = np.copy(mSCF.mo_coeff)

        eci = mCI.kernel(mo)[0]
        print_energy('CASCI', eci)
        inp.timer.end('casci')

    # CASSCF
    elif method == 'casscf' or method == 'ucasscf':
        if inp.scf.cas is None and inp.scf.casorb is None:
            print ('ERROR: Must specify CAS space or CASORB')
            return inp

        lunrestricted = (method == 'ucasscf')
        ehf, mSCF = do_hf(inp, unrestricted=lunrestricted)
        print_energy('HF', ehf)

        # get cas space
        if inp.scf.cas is not None:
            if mol.spin == 0:
                nelecas = inp.scf.cas[0]
            else:
                nelecas = (inp.scf.cas[0]//2 + mol.spin//2,
                           inp.scf.cas[0]//2 - mol.spin//2)
            ncasorb = inp.scf.cas[1]
        elif inp.scf.casorb is not None:
            ncasorb = len(inp.scf.casorb)
            nelecas = int(np.sum(mSCF.mo_occ[inp.scf.casorb]))
            if inp.scf.casspin is not None:
                nelecas = (nelecas//2 + inp.scf.casspin//2,
                           nelecas//2 - inp.scf.casspin//2)

        inp.timer.start('casscf')
        mCI = mcscf.CASSCF(mSCF, ncasorb, nelecas)

        if inp.scf.casorb is not None:
            mo = mcscf.addons.sort_mo(mCI, np.copy(mSCF.mo_coeff), inp.scf.casorb, 0)
        else:
            mo = np.copy(mSCF.mo_coeff)

        eci = mCI.kernel(mo)[0]
        print_energy('CASSCF', eci)
        inp.timer.end('casscf')

    # NEVPT2
    elif method == 'nevpt2' or method == 'unevpt2':
        if inp.scf.cas is None and inp.scf.casorb is None:
            print ('ERROR: Must specify CAS space or CASORB')
            return inp

        ehf, mSCF = do_hf(inp)
        print_energy('RHF', ehf)

        inp.timer.start('casscf')

        # get cas space
        if inp.scf.cas is not None:
            if mol.spin == 0:
                nelecas = inp.scf.cas[0]
            else:
                nelecas = (inp.scf.cas[0]//2 + mol.spin//2,
                           inp.scf.cas[0]//2 - mol.spin//2)
            ncasorb = inp.scf.cas[1]
        elif inp.scf.casorb is not None:
            ncasorb = len(inp.scf.casorb)
            nelecas = int(np.sum(mSCF.mo_occ[inp.scf.casorb]))
            if inp.scf.casspin is not None:
                nelecas = (nelecas//2 + inp.scf.casspin//2,
                           nelecas//2 - inp.scf.casspin//2)

        mCI = mcscf.CASSCF(mSCF, ncasorb, nelecas, frozen=inp.scf.freeze)

        if inp.scf.casorb is not None:
            mo = mcscf.addons.sort_mo(mCI, np.copy(mSCF.mo_coeff), inp.scf.casorb, 0)
        else:
            mo = np.copy(mSCF.mo_coeff)

        eci = mCI.kernel(mo)[0]
        print_energy('CASSCF', eci)
        inp.timer.end('casscf')

        inp.timer.start('nevpt2')
        mpt2 = mrpt.NEVPT(mCI)
        ept2 = mpt2.kernel() + eci
        print_energy('NEVPT2', ept2)
        inp.timer.end('nevpt2')

    else:
        print ('ERROR: Unrecognized SCF method!')
        raise SystemExit

    # dump fcidump file if needed
    if inp.fcidump:
        if inp.filename[-4:].lower() == '.inp':
            fcifile = inp.filename[:-4] + '.fcidump'
        else:
            fcifile = inp.filename + '.fcidump'
        fcidump(mSCF, filename=fcifile, tol=1e-6)

    # plot MOs if needed
    if inp.mo2cube:
        from .mo_2_cube import save_MOs
        save_MOs(inp, mSCF, mSCF.mo_coeff)

    # save molden file if needed
    if inp.molden:
        from pyscf.tools import molden
        molden_file = inp.filename[:-4] + '.molden'
        molden.from_mo(inp.mol, molden_file, mSCF.mo_coeff, ene=mSCF.mo_energy)

    # save and return
    inp.mf = mSCF
    return inp
Beispiel #28
0
    eri1 *= .1
    eris.ovov = eri1[0,nocca:,:nocca,nocca:,:nocca].transpose(1,0,3,2).conj()
    eris.ovvv = eri1[0,nocca:,:nocca,nocca:,nocca:].transpose(1,0,3,2).conj()
    eris.ovoo = eri1[0,nocca:,:nocca,:nocca,:nocca].transpose(1,0,3,2).conj()
    eris.OVOV = eri1[2,noccb:,:noccb,noccb:,:noccb].transpose(1,0,3,2).conj()
    eris.OVVV = eri1[2,noccb:,:noccb,noccb:,noccb:].transpose(1,0,3,2).conj()
    eris.OVOO = eri1[2,noccb:,:noccb,:noccb,:noccb].transpose(1,0,3,2).conj()
    eris.ovOV = eri1[1,nocca:,:nocca,noccb:,:noccb].transpose(1,0,3,2).conj()
    eris.ovVV = eri1[1,nocca:,:nocca,noccb:,noccb:].transpose(1,0,3,2).conj()
    eris.ovOO = eri1[1,nocca:,:nocca,:noccb,:noccb].transpose(1,0,3,2).conj()
    eris.OVov = eri1[1,nocca:,:nocca,noccb:,:noccb].transpose(3,2,1,0).conj()
    eris.OVvv = eri1[1,nocca:,nocca:,noccb:,:noccb].transpose(3,2,1,0).conj()
    eris.OVoo = eri1[1,:nocca,:nocca,noccb:,:noccb].transpose(3,2,1,0).conj()
    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))
    t1 = t1a, t1b
    t2 = t2aa, t2ab, t2bb
    mcc = cc.UCCSD(scf.UHF(mol))
    print(kernel(mcc, eris, t1, t2) - (-0.056092415718338388-0.011390417704868244j))
Beispiel #29
0
#!/usr/bin/env python
#
# Author: Qiming Sun <*****@*****.**>
#
'''
A simple example to run CCSD(T) and UCCSD(T) calculation.
'''

from pyscf import gto, scf, cc

mol = gto.M(atom='H 0 0 0; F 0 0 1.1', basis='ccpvdz')

mf = scf.RHF(mol).run()
mycc = cc.CCSD(mf).run()
et = mycc.ccsd_t()
print('CCSD(T) correlation energy', mycc.e_corr + et)

mf = scf.UHF(mol).run()
mycc = cc.UCCSD(mf).run()
et = mycc.ccsd_t()
print('UCCSD(T) correlation energy', mycc.e_corr + et)