Пример #1
0
    def test_multistate(self):
        # See issue #1081
        mol = gto.M(atom='''
        O  0.0000000000 0.0000000000 -0.1302052882
        H  1.4891244004 0.0000000000  1.0332262019
        H -1.4891244004 0.0000000000  1.0332262019
        ''',
            basis = '631g',
            symmetry = False)
        mf = scf.RHF(mol).run()

        mc = mcscf.CASSCF(mf, 6, [4,4])
        mc.fcisolver=fci.solver(mol,singlet=True)
        mc.fcisolver.nroots=2
        mc = mcscf.state_average_(mc, [0.5,0.5])
        mc.kernel()
        orbital = mc.mo_coeff.copy()

        mc = mcscf.CASCI(mf, 6, 8)
        mc.fcisolver=fci.solver(mol,singlet=True)
        mc.fcisolver.nroots=2
        mc.kernel(orbital)
        
        # Ground State
        mp0 = nevpt2.NEVPT(mc, root=0)
        mp0.kernel()
        e0 = mc.e_tot[0] + mp0.e_corr
        self.assertAlmostEqual(e0, -75.867171, 4) # From ORCA (4.2.1)

        # First Excited State
        mp1 = nevpt2.NEVPT(mc, root=1)
        mp1.kernel()
        e1 = mc.e_tot[1] + mp1.e_corr
        self.assertAlmostEqual(e1, -75.828469, 4) # From ORCA (4.2.1)
Пример #2
0
 def __init__(self, mf_or_mol, ncas, nelecas, ncore=None, frozen=None):
     newton_casscf.CASSCF.__init__(self, mf_or_mol, ncas, nelecas, ncore,
                                   frozen)
     assert (self.mol.symmetry)
     #self.fcisolver = fci.solver(self.mol, self.nelecas[0]==self.nelecas[1], True)
     self.fcisolver = fci.solver(self.mol, False, True)
     self.fcisolver.max_cycle = 25
Пример #3
0
def kernel(mc, fnal, e_mcscf, ci, ncas, nelecas):
    mo_coeff = mc.mo_coeff[:, :mc.ncore + mc.ncas].copy()
    mo_occ = np.zeros(mo_coeff.shape[-1], dtype=mo_coeff.dtype)
    mo_occ[:mc.ncore] = 2.0
    dm_cas = fci.solver(mc._scf.mol, singlet=False,
                        symm=False).make_rdm12(ci, ncas, nelecas)[0]
    no_occ, no_umat = linalg.eigh(dm_cas)
    mo_coeff[:, mc.ncore:] = mo_coeff[:, mc.ncore:] @ no_umat
    mo_occ[mc.ncore:] = no_occ
    dm1 = (mo_coeff * mo_occ[None, :]) @ mo_coeff.T
    dm1 = tag_array(dm1, mo_coeff=mo_coeff, mo_occ=mo_occ)

    hcore = mc._scf.get_hcore()
    vj = mc._scf.get_j(dm=dm1)

    logger.debug(mc, 'CAS energy decomposition:')
    Vnn = mc._scf.energy_nuc()
    logger.debug(mc, 'Vnn = %s', Vnn)
    Te_Vne = np.tensordot(hcore, dm1)
    logger.debug(mc, 'Te + Vne = %s', Te_Vne)
    Ej = 0.5 * np.tensordot(vj, dm1)
    logger.debug(mc, 'Ej = %s', Ej)
    Exc_wfn = e_mcscf - (Vnn + Te_Vne + Ej)
    logger.debug(mc, 'Exc (wfn) = %s', Exc_wfn)
    Exc = fnal.kernel(dm1, max_memory=mc.max_memory)
    logger.debug(mc, 'Exc (UDFT) = %s', Exc)
    return Vnn + Te_Vne + Ej + Exc, Exc
Пример #4
0
def ref_pt(HHdist, gga_fnal, hyb_gga_fnal, basis='sto3g', output=None):
    if output is None:
        output = 'H2_{:.1f}_ref.log'.format(HHdist)
    mol = gto.M(atom='H 0 0 0; H 0 0 {:.1f}'.format(HHdist),
                basis=basis,
                symmetry=True,
                output=output,
                verbose=4)
    hf = scf.RHF(mol)
    hf.kernel()
    ks = dft.RKS(mol)
    #ks.grids.level = 9
    ks.xc = gga_fnal
    e_gga = ks.kernel()
    ks.xc = hyb_gga_fnal
    e_hyb_gga = ks.kernel()
    ks.xc = gga_fnal
    ot = otfnal.transfnal(ks)
    mc = mcscf.CASSCF(hf, 2, 2)
    mc.fcisolver = fci.solver(mol, singlet=True, symm=True)
    #mc.fix_spin_(ss=0)
    e_cas = mc.kernel()[0]
    assert (mc.converged)
    e_pdft = mcpdft.kernel(mc, ot)
    return e_cas, e_gga, e_hyb_gga, e_pdft
Пример #5
0
def cas_22_pt(HHdist, xfnal, cfnal, hybs, basis='sto3g', output=None):
    ''' Don't include 0 in hybs; will do this automatically
    '''
    if output is None:
        output = 'H2_{:.1f}.log'.format(HHdist)
    mol = gto.M(atom='H 0 0 0; H 0 0 {:.1f}'.format(HHdist),
                basis=basis,
                symmetry=True,
                output=output,
                verbose=4)
    hf = scf.RHF(mol)
    hf.kernel()
    ks = dft.RKS(mol)
    #ks.grids.level = 9
    ks.xc = xfnal + ', ' + cfnal
    e_rks = ks.kernel()
    ot = otfnal.transfnal(ks)
    mc = mcscf.CASSCF(hf, 2, 2)
    mc.fcisolver = fci.solver(mol, singlet=True, symm=True)
    #mc.fix_spin_(ss=0)
    e_cas = mc.kernel()[0]
    assert (mc.converged)
    e_pdft = mcpdft.kernel(mc, ot)
    e_hyb = []
    for hyb in hybs:
        ks.xc = '{0:.2f}*HF + {1:.2f}*{2:s}, {3:s}'.format(
            hyb, 1.0 - hyb, xfnal, cfnal)
        e_hyb.append(ks.kernel())
        ot.otxc = 't{0:.2f}*HF + {1:.2f}*{2:s}, {3:s}'.format(
            hyb, 1.0 - hyb, xfnal, cfnal)
        e_hyb.append(mcpdft.kernel(mc, ot))
    return [e_cas, e_pdft, e_rks] + [e for e in e_hyb]
Пример #6
0
 def __init__(self, mf, ncas, nelecas, ncore=None):
     assert (mf.mol.symmetry)
     # Ag, A1 or A
     #TODO:        self.wfnsym = symm.param.CHARACTER_TABLE[mol.groupname][0][0]
     casci.CASCI.__init__(self, mf, ncas, nelecas, ncore)
     #self.fcisolver = fci.solver(mf.mol, self.nelecas[0]==self.nelecas[1], True)
     self.fcisolver = fci.solver(mf.mol, singlet=False, symm=True)
Пример #7
0
    def __init__(self, mf, ncas, nelecas, ncore=None):
        assert(mf.mol.symmetry)
# Ag, A1 or A
#TODO:        self.wfnsym = symm.param.CHARACTER_TABLE[mol.groupname][0][0]
        self.orbsym = []
        casci.CASCI.__init__(self, mf, ncas, nelecas, ncore)
        self.fcisolver = fci.solver(mf.mol, self.nelecas[0]==self.nelecas[1], True)
Пример #8
0
    def test_state_average_mix_scanner(self):
        mc = mcscf.CASSCF(mf, 4, 4)
        mc.conv_tol = 1e-10  # B/c high sensitivity in the numerical test
        fcisolvers = [fci.solver(mol, singlet=bool(i)) for i in range(2)]
        fcisolvers[0].conv_tol = fcisolvers[1].conv_tol = 1e-10
        fcisolvers[0].spin = 2
        mc = mcscf.addons.state_average_mix_(mc, fcisolvers, (.5, .5))
        gs = mc.nuc_grad_method().as_scanner()
        e_avg, de_avg = gs(mol)
        e_0, de_0 = gs(mol, state=0)
        e_1, de_1 = gs(mol, state=1)
        mcs = gs.base
        pmol = mol.copy()
        mcs(pmol.set_geom_('N 0 0 0; N 0 0 1.201; H 1 1 0; H 1 1 1.2'))
        e1_avg = mcs.e_average
        e1_0 = mcs.e_states[0]
        e1_1 = mcs.e_states[1]
        mcs(pmol.set_geom_('N 0 0 0; N 0 0 1.199; H 1 1 0; H 1 1 1.2'))
        e2_avg = mcs.e_average
        e2_0 = mcs.e_states[0]
        e2_1 = mcs.e_states[1]

        self.assertAlmostEqual(e_avg, -1.083838462141992e+02, 9)
        self.assertAlmostEqual(lib.fp(de_avg), -1.034392760319145e-01, 7)
        self.assertAlmostEqual(e_0, -1.083902661656155e+02, 9)
        self.assertAlmostEqual(lib.fp(de_0), -6.398921123988113e-02, 7)
        self.assertAlmostEqual(e_1, -1.083774262627830e+02, 9)
        self.assertAlmostEqual(lib.fp(de_1), -1.428891618903179e-01, 7)
        self.assertAlmostEqual(de_avg[1, 2],
                               (e1_avg - e2_avg) / 0.002 * lib.param.BOHR, 4)
        self.assertAlmostEqual(de_0[1, 2],
                               (e1_0 - e2_0) / 0.002 * lib.param.BOHR, 4)
        self.assertAlmostEqual(de_1[1, 2],
                               (e1_1 - e2_1) / 0.002 * lib.param.BOHR, 4)
Пример #9
0
    def __init__(self, mf, ncas, nelecas, ncore=None):
        mol = mf.mol
        self.mol = mol
        self._scf = mf
        self.verbose = mol.verbose
        self.stdout = mol.stdout
        self.max_memory = mf.max_memory
        self.ncas = ncas
        if isinstance(nelecas, (int, numpy.integer)):
            nelecb = (nelecas - mol.spin) // 2
            neleca = nelecas - nelecb
            self.nelecas = (neleca, nelecb)
        else:
            self.nelecas = (nelecas[0], nelecas[1])
        if ncore is None:
            ncorelec = mol.nelectron - (self.nelecas[0] + self.nelecas[1])
            assert ncorelec % 2 == 0
            self.ncore = ncorelec // 2
        else:
            assert isinstance(ncore, (int, numpy.integer))
            self.ncore = ncore
        # self.fcisolver = fci.direct_spin0.FCISolver(mol)
        self.fcisolver = fci.solver(mol, self.nelecas[0] == self.nelecas[1])
        # CI solver parameters are set in fcisolver object
        self.fcisolver.lindep = 1e-10
        self.fcisolver.max_cycle = 50
        self.fcisolver.conv_tol = 1e-8

        ##################################################
        # don't modify the following attributes, they are not input options
        self.mo_coeff = mf.mo_coeff
        self.ci = None
        self.e_tot = 0

        self._keys = set(self.__dict__.keys())
Пример #10
0
 def __init__(self, mf, ncas, nelecas, ncore=None, frozen=None):
     assert (mf.mol.symmetry)
     self.orbsym = []
     newton_casscf.CASSCF.__init__(self, mf, ncas, nelecas, ncore, frozen)
     self.fcisolver = fci.solver(mf.mol, self.nelecas[0] == self.nelecas[1],
                                 True)
     self.fcisolver.max_cycle = 25
Пример #11
0
def make_rdms_mcpdft (mc, ot=None, mo_coeff=None, ci=None):
    ''' Build the necessary density matrices for an MC-PDFT calculation 

        Args:
            mc : an instance of CASSCF or CASCI class
                Note: this function does not currently run the CASSCF or CASCI calculation itself

        Kwargs:
            ot : an instance of on-top density functional class - see otfnal.py
            mo_coeff : ndarray of shape (nao, nmo)
                Molecular orbital coefficients
            ci : ndarray or list
                CI vector or vectors. If a list of many CI vectors, mc must be a 
                state-average object with the correct nroots

        Returns:
            dm1s : ndarray of shape (2,nao,nao)
                Spin-separated 1-RDM
            adm : (adm1s, adm2s)
                adm1s : ndarray of shape (2,ncas,ncas)
                    Spin-separated 1-RDM for the active orbitals
                adm2s : 3 ndarrays of shape (ncas,ncas,ncas,ncas)
                    First ndarray is spin-summed casdm2
                    Second ndarray is casdm2_aa + casdm2_bb
                    Third ndarray is casdm2_ab
    '''
    if ci is None: ci = mc.ci
    if ot is None: ot = mc.otfnal
    if mo_coeff is None: mo_coeff = mc.mo_coeff
    ncore, ncas, nelecas = mc.ncore, mc.ncas, mc.nelecas
    nocc = ncore + ncas

    # figure out the correct RDMs to build (state-average or state-specific?)
    nelecas = _unpack_nelec (nelecas)
    ndet = [cistring.num_strings (ncas, n) for n in nelecas]
    ci = np.asarray (ci).reshape (-1, ndet[0], ndet[1])
    nroots = ci.shape[0]
    if nroots > 1:
        _casdms = mc.fcisolver
    else:
        ci = ci[0]
        _casdms = fci.solver (mc._scf.mol, singlet=False, symm=False)

    # Make the rdms
    # make_rdm12s returns (a, b), (aa, ab, bb)
    mo_cas = mo_coeff[:,ncore:nocc]
    moH_cas = mo_cas.conj ().T
    mo_core = mo_coeff[:,:ncore]
    moH_core = mo_core.conj ().T
    adm1s = np.stack (_casdms.make_rdm1s (ci, ncas, nelecas), axis=0)
    adm2s = _casdms.make_rdm12s (ci, ncas, nelecas)[1]
    adm2s = get_2CDMs_from_2RDMs (adm2s, adm1s)
    adm2_ss = adm2s[0] + adm2s[2]
    adm2_os = adm2s[1]
    adm2 = adm2_ss + adm2_os + adm2_os.transpose (2,3,0,1)
    dm1s = np.dot (adm1s, moH_cas)
    dm1s = np.dot (mo_cas, dm1s).transpose (1,0,2)
    dm1s += np.dot (mo_core, moH_core)[None,:,:]
    return dm1s, (adm1s, (adm2, adm2_ss, adm2_os))
Пример #12
0
 def test_state_average(self):
     mc = mcscf.CASSCF(mfr, 4, 4)
     mc.fcisolver = fci.solver(mol, singlet=False)
     mc.state_average_((.64,.36))
     e = mc.kernel()[0]
     self.assertAlmostEqual(e[0]*.64+e[1]*.36, -108.83342083775061, 7)
     dm1 = mc.analyze()
     self.assertAlmostEqual(lib.finger(dm1[0]), 0.52396929381500434, 4)
Пример #13
0
 def test_state_average(self):
     mc = mcscf.CASSCF(mfr, 4, 4)
     mc.fcisolver = fci.solver(mol, singlet=False)
     mc.state_average_((.64, .36))
     e = mc.kernel()[0]
     self.assertAlmostEqual(e, -108.83342083775061, 7)
     dm1 = mc.analyze()
     self.assertAlmostEqual(lib.finger(dm1[0]), 0.52396929381500434, 4)
Пример #14
0
 def test_spin_sa4_newton (self):
     fcisolvers = [fci.solver (mol, singlet=not(bool(i)), symm=False) for i in range (2)]
     fcisolvers[0].nroots = fcisolvers[1].nroots = 2
     fcisolvers[1].spin = 2
     mc = mcscf.addons.state_average_mix (mcscf.CASSCF (m, 4, 4), fcisolvers, [0.25,]*4).newton ().run ()
     self.assertAlmostEqual (mc.e_tot, mc_ref.e_tot, 8)
     for e1, e0 in zip (numpy.sort (mc.e_states), mc_ref.e_states):
         self.assertAlmostEqual (e1, e0, 5)
Пример #15
0
def kernel(mc, ot, root=-1):
    ''' Calculate MC-DCFT total energy

        Args:
            mc : an instance of CASSCF or CASCI class
                Note: this function does not currently run the CASSCF or CASCI calculation itself
                prior to calculating the MC-DCFT energy. Call mc.kernel () before passing to this function!
            ot : an instance of on-top density functional class - see otfnal.py

        Kwargs:
            root : int
                If mc describes a state-averaged calculation, select the root (0-indexed)
                Negative number requests state-averaged MC-DCFT results (i.e., using state-averaged density matrices)

        Returns:
            Total MC-DCFT energy including nuclear repulsion energy.
    '''
    t0 = (time.clock (), time.time ())

    mc_1root = mc
    if isinstance (mc, StateAverageMCSCFSolver) and root >= 0:
        mc_1root = mcscf.CASCI (mc._scf, mc.ncas, mc.nelecas)
        mc_1root.fcisolver = fci.solver (mc._scf.mol, singlet = False, symm = False)
        mc_1root.mo_coeff = mc.mo_coeff
        mc_1root.ci = mc.ci[root]
        mc_1root.e_tot = mc.e_states[root]
    dm1s = np.asarray (mc_1root.make_rdm1s ())
    adm1s = np.stack (mc_1root.fcisolver.make_rdm1s (mc_1root.ci, mc.ncas, mc.nelecas), axis=0)
    spin = abs(mc.nelecas[0] - mc.nelecas[1])
    omega, alpha, hyb = ot._numint.rsh_and_hybrid_coeff(ot.otxc, spin=spin)
    hyb_x, hyb_c = hyb
    t0 = logger.timer (ot, 'rdms', *t0)

    Vnn = mc._scf.energy_nuc ()
    h = mc._scf.get_hcore ()
    dm1 = dm1s[0] + dm1s[1]
    vj, vk = mc._scf.get_jk (dm=dm1s)
    vj = vj[0] + vj[1]
    Te_Vne = np.tensordot (h, dm1)
    # (vj_a + vj_b) * (dm_a + dm_b)
    E_j = np.tensordot (vj, dm1) / 2  
    # (vk_a * dm_a) + (vk_b * dm_b) Mind the difference!
    E_x = -(np.tensordot (vk[0], dm1s[0]) + np.tensordot (vk[1], dm1s[1])) / 2
    logger.debug (ot, 'CAS energy decomposition:')
    logger.debug (ot, 'Vnn = %s', Vnn)
    logger.debug (ot, 'Te + Vne = %s', Te_Vne)
    logger.debug (ot, 'E_j = %s', E_j)
    logger.debug (ot, 'E_x = %s', E_x)
    t0 = logger.timer (ot, 'Vnn, Te, Vne, E_j, E_x', *t0)

    E_ot = get_E_ot (ot, dm1s)
    t0 = logger.timer (ot, 'E_ot', *t0)
    e_tot = Vnn + Te_Vne + E_j + (hyb_x * E_x) + E_ot
    logger.note (ot, 'MC-DCFT E = %s, Eot(%s) = %s', e_tot, ot.otxc, E_ot)

    chkdata = {'Vnn':Vnn, 'Te_Vne':Te_Vne, 'E_j':E_j, 'E_x':E_x, 'dm1s':dm1s, 'spin':spin}

    return e_tot, E_ot, chkdata
Пример #16
0
 def test_spin_and_pointgroup_sa4_newton (self):
     fcisolvers = [fci.solver (molsym, singlet = not(bool(i%2))) for i in range (4)]
     fcisolvers[0].wfnsym = fcisolvers[1].wfnsym = 'B2'
     fcisolvers[2].wfnsym = fcisolvers[3].wfnsym = 'A1'
     fcisolvers[1].spin = fcisolvers[3].spin = 2
     mc = mcscf.addons.state_average_mix (mcscf.CASSCF (msym, 4, 4), fcisolvers, [0.25,]*4).newton ().run ()
     self.assertAlmostEqual (mc.e_tot, mc_ref.e_tot, 8)
     for e1, e0 in zip (numpy.sort (mc.e_states), mc_ref.e_states):
         self.assertAlmostEqual (e1, e0, 5)
Пример #17
0
 def test_pointgroup_sa4_newton (self):
     fcisolvers = [fci.solver (molsym, symm=True, singlet=False) for i in range (2)]
     fcisolvers[0].nroots = fcisolvers[1].nroots = 2
     fcisolvers[0].wfnsym = 'A1'
     fcisolvers[1].wfnsym = 'B2'
     mc = mcscf.addons.state_average_mix (mcscf.CASSCF (msym, 4, 4), fcisolvers, [0.25,]*4).newton ().run ()
     self.assertAlmostEqual (mc.e_tot, mc_ref.e_tot, 8)
     for e1, e0 in zip (numpy.sort (mc.e_states), mc_ref.e_states):
         self.assertAlmostEqual (e1, e0, 5)
Пример #18
0
    def __init__(self, mf_or_mol, ncas, nelecas, ncore=None, frozen=None):
        mc1step.CASSCF.__init__(self, mf_or_mol, ncas, nelecas, ncore, frozen)

        assert(self.mol.symmetry)
        singlet = (getattr(__config__, 'mcscf_mc1step_CASCI_fcisolver_direct_spin0', False)
                   and self.nelecas[0] == self.nelecas[1])
        self.fcisolver = fci.solver(self.mol, singlet, symm=True)
        self.fcisolver.max_cycle = getattr(__config__,
                                           'mcscf_mc1step_CASSCF_fcisolver_max_cycle', 50)
        self.fcisolver.conv_tol = getattr(__config__,
                                          'mcscf_mc1step_CASSCF_fcisolver_conv_tol', 1e-8)
        self.fcisolver.lindep = getattr(__config__,
                                        'mcscf_mc1step_CASCI_fcisolver_lindep', 1e-10)
Пример #19
0
 def __init__(self, mf, ncas, nelecas, ncore=None, frozen=None):
     assert (mf.mol.symmetry)
     mc1step.CASSCF.__init__(self, mf, ncas, nelecas, ncore, frozen)
     singlet = (getattr(__config__,
                        'mcscf_mc1step_CASCI_fcisolver_direct_spin0', False)
                and self.nelecas[0] == self.nelecas[1])
     self.fcisolver = fci.solver(mf.mol, singlet, symm=True)
     self.fcisolver.max_cycle = getattr(
         __config__, 'mcscf_mc1step_CASSCF_fcisolver_max_cycle', 50)
     self.fcisolver.conv_tol = getattr(
         __config__, 'mcscf_mc1step_CASSCF_fcisolver_conv_tol', 1e-8)
     self.fcisolver.lindep = getattr(
         __config__, 'mcscf_mc1step_CASCI_fcisolver_lindep', 1e-10)
Пример #20
0
    def test_state_average(self):
        mc = mcscf.CASSCF(mfr, 4, 4)
        mc.fcisolver = fci.solver(mol, singlet=False)
        mc.state_average_((.64, .36))
        e = mc.kernel()
        e = mc.e_states
        self.assertAlmostEqual(mc.e_tot, -108.83342083775061, 7)
        self.assertAlmostEqual(mc.e_average, -108.83342083775061, 7)
        self.assertAlmostEqual(e[0] * .64 + e[1] * .36, -108.83342083775061, 7)
        dm1 = mc.analyze()
        self.assertAlmostEqual(lib.fp(dm1[0]), 0.52396929381500434, 4)

        self.assertRaises(TypeError, mc.state_average_, (.64, .36))
Пример #21
0
    def __init__(self, mf_or_mol, ncas, nelecas, ncore=None):
        if isinstance(mf_or_mol, gto.Mole):
            mf = scf.RHF(mf_or_mol)
        else:
            mf = mf_or_mol

        mol = mf.mol
        self.mol = mol
        self._scf = mf
        self.verbose = mol.verbose
        self.stdout = mol.stdout
        self.max_memory = mf.max_memory
        self.ncas = ncas
        if isinstance(nelecas, (int, numpy.integer)):
            nelecb = (nelecas - mol.spin) // 2
            neleca = nelecas - nelecb
            self.nelecas = (neleca, nelecb)
        else:
            self.nelecas = (nelecas[0], nelecas[1])
        if ncore is None:
            ncorelec = mol.nelectron - (self.nelecas[0] + self.nelecas[1])
            assert (ncorelec % 2 == 0)
            self.ncore = ncorelec // 2
        else:
            assert (isinstance(ncore, (int, numpy.integer)))
            self.ncore = ncore
        singlet = (getattr(__config__,
                           'mcscf_casci_CASCI_fcisolver_direct_spin0', False)
                   and self.nelecas[0] == self.nelecas[1]
                   )  # leads to direct_spin1
        self.fcisolver = fci.solver(mol, singlet, symm=False)
        # CI solver parameters are set in fcisolver object
        self.fcisolver.lindep = getattr(__config__,
                                        'mcscf_casci_CASCI_fcisolver_lindep',
                                        1e-10)
        self.fcisolver.max_cycle = getattr(
            __config__, 'mcscf_casci_CASCI_fcisolver_max_cycle', 200)
        self.fcisolver.conv_tol = getattr(
            __config__, 'mcscf_casci_CASCI_fcisolver_conv_tol', 1e-8)

        ##################################################
        # don't modify the following attributes, they are not input options
        self.e_tot = 0
        self.e_cas = None
        self.ci = None
        self.mo_coeff = mf.mo_coeff
        self.mo_energy = mf.mo_energy
        self.converged = False

        keys = set(('natorb', 'canonicalization', 'sorting_mo_energy'))
        self._keys = set(self.__dict__.keys()).union(keys)
Пример #22
0
    def __init__(self, mf, ncas, nelecas, ncore=None):
        assert(mf.mol.symmetry)
# Ag, A1 or A
#TODO:        self.wfnsym = symm.param.CHARACTER_TABLE[mol.groupname][0][0]
        casci.CASCI.__init__(self, mf, ncas, nelecas, ncore)
        singlet = (getattr(__config__, 'mcscf_casci_CASCI_fcisolver_direct_spin0', False)
                   and self.nelecas[0] == self.nelecas[1])
        self.fcisolver = fci.solver(mf.mol, singlet, symm=True)
        self.fcisolver.lindep = getattr(__config__,
                                        'mcscf_casci_CASCI_fcisolver_lindep', 1e-10)
        self.fcisolver.max_cycle = getattr(__config__,
                                           'mcscf_casci_CASCI_fcisolver_max_cycle', 200)
        self.fcisolver.conv_tol = getattr(__config__,
                                          'mcscf_casci_CASCI_fcisolver_conv_tol', 1e-8)
Пример #23
0
    def __init__(self, mf_or_mol, ncas, nelecas, ncore=None):
# Ag, A1 or A
#TODO:        self.wfnsym = symm.param.CHARACTER_TABLE[mol.groupname][0][0]
        casci.CASCI.__init__(self, mf_or_mol, ncas, nelecas, ncore)

        assert(self.mol.symmetry)
        singlet = (getattr(__config__, 'mcscf_casci_CASCI_fcisolver_direct_spin0', False)
                   and self.nelecas[0] == self.nelecas[1])
        self.fcisolver = fci.solver(self.mol, singlet, symm=True)
        self.fcisolver.lindep = getattr(__config__,
                                        'mcscf_casci_CASCI_fcisolver_lindep', 1e-10)
        self.fcisolver.max_cycle = getattr(__config__,
                                           'mcscf_casci_CASCI_fcisolver_max_cycle', 200)
        self.fcisolver.conv_tol = getattr(__config__,
                                          'mcscf_casci_CASCI_fcisolver_conv_tol', 1e-8)
Пример #24
0
    def test_state_specific(self):
        mc = mcscf.CASSCF(mfr, 4, 4)
        mc.fcisolver = fci.solver(mol, singlet=False)
        mc.state_specific_(state=1)
        e = mc.kernel()[0]
        self.assertAlmostEqual(e, -108.70065770892457, 7)
        dm1 = mc.analyze()
        self.assertAlmostEqual(lib.finger(dm1[0]), 0.54605283139098515, 4)

        mc = mcscf.CASSCF(mfr, 4, 4)
        mc.state_specific_(state=0)
        e = mc.kernel()[0]
        self.assertAlmostEqual(mc.e_tot, mcr.e_tot, 7)
        dm1 = mc.analyze()
        dmref = mcr.analyze()
        self.assertAlmostEqual(float(abs(dm1[0]-dmref[0]).max()), 0, 4)
Пример #25
0
    def test_state_specific(self):
        mc = mcscf.CASSCF(mfr, 4, 4)
        mc.fcisolver = fci.solver(mol, singlet=False)
        mc.state_specific_(state=1)
        e = mc.kernel()[0]
        self.assertAlmostEqual(e, -108.70065770892457, 7)
        dm1 = mc.analyze()
        self.assertAlmostEqual(lib.finger(dm1[0]), 0.54605283139098515, 4)

        mc = mcscf.CASSCF(mfr, 4, 4)
        mc.state_specific_(state=0)
        e = mc.kernel()[0]
        self.assertAlmostEqual(mc.e_tot, mcr.e_tot, 7)
        dm1 = mc.analyze()
        dmref = mcr.analyze()
        self.assertAlmostEqual(float(abs(dm1[0] - dmref[0]).max()), 0, 4)
Пример #26
0
def _get_e_decomp (mc, ot, mo_coeff, ci, e_mcscf, e_nuc, h, xfnal, cfnal):
    ncore, ncas, nelecas = mc.ncore, mc.ncas, mc.nelecas
    _rdms = mcscf.CASCI (mc._scf, ncas, nelecas)
    _rdms.fcisolver = fci.solver (mc._scf.mol, singlet = False, symm = False)
    _rdms.mo_coeff = mo_coeff
    _rdms.ci = ci
    _casdms = _rdms.fcisolver
    dm1s = np.stack (_rdms.make_rdm1s (), axis=0)
    dm1 = dm1s[0] + dm1s[1]
    j = _rdms._scf.get_j (dm=dm1)
    e_core = np.tensordot (h, dm1, axes=2)
    e_coul = np.tensordot (j, dm1, axes=2) / 2
    adm1s = np.stack (_casdms.make_rdm1s (ci, ncas, nelecas), axis=0)
    adm2 = get_2CDM_from_2RDM (_casdms.make_rdm12 (_rdms.ci, ncas, nelecas)[1], adm1s)
    mo_cas = mo_coeff[:,ncore:][:,:ncas]
    e_otx = get_E_ot (xfnal, dm1s, adm2, mo_cas, max_memory=mc.max_memory)
    e_otc = get_E_ot (cfnal, dm1s, adm2, mo_cas, max_memory=mc.max_memory)
    e_wfnxc = e_mcscf - e_nuc - e_core - e_coul
    return e_core, e_coul, e_otx, e_otc, e_wfnxc
Пример #27
0
    def __init__(self, mf_or_mol, ncas, nelecas, ncore=None):
        if isinstance(mf_or_mol, gto.Mole):
            mf = scf.RHF(mf_or_mol)
        else:
            mf = mf_or_mol

        mol = mf.mol
        self.mol = mol
        self._scf = mf
        self.verbose = mol.verbose
        self.stdout = mol.stdout
        self.max_memory = mf.max_memory
        self.ncas = ncas
        if isinstance(nelecas, (int, numpy.integer)):
            nelecb = (nelecas-mol.spin)//2
            neleca = nelecas - nelecb
            self.nelecas = (neleca, nelecb)
        else:
            self.nelecas = (nelecas[0],nelecas[1])
        self.ncore = ncore
        singlet = (getattr(__config__, 'mcscf_casci_CASCI_fcisolver_direct_spin0', False)
                   and self.nelecas[0] == self.nelecas[1])  # leads to direct_spin1
        self.fcisolver = fci.solver(mol, singlet, symm=False)
# CI solver parameters are set in fcisolver object
        self.fcisolver.lindep = getattr(__config__,
                                        'mcscf_casci_CASCI_fcisolver_lindep', 1e-10)
        self.fcisolver.max_cycle = getattr(__config__,
                                           'mcscf_casci_CASCI_fcisolver_max_cycle', 200)
        self.fcisolver.conv_tol = getattr(__config__,
                                          'mcscf_casci_CASCI_fcisolver_conv_tol', 1e-8)

##################################################
# don't modify the following attributes, they are not input options
        self.e_tot = 0
        self.e_cas = None
        self.ci = None
        self.mo_coeff = mf.mo_coeff
        self.mo_energy = mf.mo_energy
        self.converged = False

        keys = set(('natorb', 'canonicalization', 'sorting_mo_energy'))
        self._keys = set(self.__dict__.keys()).union(keys)
Пример #28
0
def ham(las, h1, h2, ci_fr, idx_root, orbsym=None, wfnsym=None):
    mol = las.mol
    norb_f = las.ncas_sub
    nelec_fr = [[
        _unpack_nelec(fcibox._get_nelec(solver, nelecas))
        for solver, ix in zip(fcibox.fcisolvers, idx_root) if ix
    ] for fcibox, nelecas in zip(las.fciboxes, las.nelecas_sub)]
    ci, nelec = ci_outer_product(ci_fr, norb_f, nelec_fr)
    solver = fci.solver(mol).set(orbsym=orbsym, wfnsym=wfnsym)
    norb = sum(norb_f)
    h2eff = solver.absorb_h1e(h1, h2, norb, nelec, 0.5)
    ham_ci = [solver.contract_2e(h2eff, c, norb, nelec) for c in ci]
    s2_ci = [contract_ss(c, norb, nelec) for c in ci]
    ham_eff = np.array([[c.ravel().dot(hc.ravel()) for hc in ham_ci]
                        for c in ci])
    s2_eff = np.array([[c.ravel().dot(s2c.ravel()) for s2c in s2_ci]
                       for c in ci])
    ovlp_eff = np.array([[bra.ravel().dot(ket.ravel()) for ket in ci]
                         for bra in ci])
    return ham_eff, s2_eff, ovlp_eff
Пример #29
0
def _get_e_decomp(mc, ot, mo_coeff, ci, e_mcscf, e_nuc, h, xfnal, cfnal):
    mc_1root = mcscf.CASCI(mc._scf, mc.ncas, mc.nelecas)
    mc_1root.fcisolver = fci.solver(mc._scf.mol, singlet=False, symm=False)
    mc_1root.mo_coeff = mo_coeff
    mc_1root.ci = ci
    dm1s = np.stack(mc_1root.make_rdm1s(), axis=0)
    dm1 = dm1s[0] + dm1s[1]
    j = mc_1root._scf.get_j(dm=dm1)
    e_core = np.tensordot(h, dm1, axes=2)
    e_coul = np.tensordot(j, dm1, axes=2) / 2
    adm1s = np.stack(mc_1root.fcisolver.make_rdm1s(ci, mc.ncas, mc.nelecas),
                     axis=0)
    adm2 = get_2CDM_from_2RDM(
        mc_1root.fcisolver.make_rdm12(mc_1root.ci, mc.ncas, mc.nelecas)[1],
        adm1s)
    mo_cas = mo_coeff[:, mc.ncore:][:, :mc.ncas]
    e_otx = get_E_ot(xfnal, dm1s, adm2, mo_cas)
    e_otc = get_E_ot(cfnal, dm1s, adm2, mo_cas)
    e_wfnxc = e_mcscf - e_nuc - e_core - e_coul
    return e_core, e_coul, e_otx, e_otc, e_wfnxc
Пример #30
0
    def __init__(self, mf, ncas, nelecas, ncore=None):
        mol = mf.mol
        self.mol = mol
        self._scf = mf
        self.verbose = mol.verbose
        self.stdout = mol.stdout
        self.max_memory = mf.max_memory
        self.ncas = ncas
        if isinstance(nelecas, (int, numpy.integer)):
            nelecb = (nelecas - mol.spin) // 2
            neleca = nelecas - nelecb
            self.nelecas = (neleca, nelecb)
        else:
            self.nelecas = (nelecas[0], nelecas[1])
        if ncore is None:
            ncorelec = mol.nelectron - (self.nelecas[0] + self.nelecas[1])
            assert (ncorelec % 2 == 0)
            self.ncore = ncorelec // 2
        else:
            assert (isinstance(ncore, (int, numpy.integer)))
            self.ncore = ncore
        #self.fcisolver = fci.solver(mol, self.nelecas[0]==self.nelecas[1], False)
        self.fcisolver = fci.solver(mol, singlet=False, symm=False)
        # CI solver parameters are set in fcisolver object
        self.fcisolver.lindep = 1e-10
        self.fcisolver.max_cycle = 200
        self.fcisolver.conv_tol = 1e-8
        self.natorb = False
        self.canonicalization = True
        self.sorting_mo_energy = False

        ##################################################
        # don't modify the following attributes, they are not input options
        self.e_tot = 0
        self.e_cas = None
        self.ci = None
        self.mo_coeff = mf.mo_coeff
        self.mo_energy = mf.mo_energy

        self._keys = set(self.__dict__.keys())
Пример #31
0
def _get_e_decomp(mc, ot, mo_coeff, ci, e_mcscf):
    if ot is not None:
        xfnal, cfnal = ot.split_x_c()
    ncore, ncas, nelecas = mc.ncore, mc.ncas, mc.nelecas
    if isinstance(mc, mcscf.casci.CASCI):
        _rdms = mc
        _casdms = mc.fcisolver
    else:
        _rdms = mcscf.CASCI(mc._scf, ncas, nelecas)
        _rdms.fcisolver = fci.solver(mc._scf.mol, singlet=False, symm=False)
        _rdms.mo_coeff = mo_coeff
        _rdms.ci = ci
        _casdms = _rdms.fcisolver
    _scf = _rdms._scf.to_uhf()
    dm1s = np.stack(_rdms.make_rdm1s(), axis=0)
    dm1 = dm1s[0] + dm1s[1]
    h = _scf.get_hcore()
    j, k = _scf.get_jk(dm=dm1s)
    e_nn = _scf.energy_nuc()
    e_core = np.tensordot(h, dm1, axes=2)
    #print(j.shape, k.shape, dm1.shape)
    e_coul = np.tensordot(j[0] + j[1], dm1, axes=2) / 2
    e_x = -(np.tensordot(k[0], dm1s[0]) + np.tensordot(k[1], dm1s[1])) / 2
    e_otx = 0.0
    e_otc = 0.0
    if ot is not None:
        adm1s = np.stack(_casdms.make_rdm1s(ci, ncas, nelecas), axis=0)
        adm2s = _casdms.make_rdm12s(_rdms.ci, ncas, nelecas)[1]
        #print(adm2s)
        adm2s = get_2CDMs_from_2RDMs(adm2s, adm1s)
        adm2_ss = adm2s[0] + adm2s[2]
        adm2_os = adm2s[1]
        adm2 = adm2_ss + adm2_os + adm2_os.transpose(2, 3, 0, 1)
        mo_cas = mo_coeff[:, ncore:][:, :ncas]
        e_otx = get_E_ot(xfnal, dm1s, adm2, mo_cas, max_memory=mc.max_memory)
        e_otc = get_E_ot(cfnal, dm1s, adm2, mo_cas, max_memory=mc.max_memory)
    e_c = e_mcscf - e_nn - e_core - e_coul - e_x
    return e_nn, e_core, e_coul, e_x, e_otx, e_otc, e_c
Пример #32
0
def roots_make_rdm12s(las, ci_fr, idx_root, si, orbsym=None, wfnsym=None):
    mol = las.mol
    norb_f = las.ncas_sub
    nelec_fr = [[
        _unpack_nelec(fcibox._get_nelec(solver, nelecas))
        for solver, ix in zip(fcibox.fcisolvers, idx_root) if ix
    ] for fcibox, nelecas in zip(las.fciboxes, las.nelecas_sub)]
    ci_r, nelec = ci_outer_product(ci_fr, norb_f, nelec_fr)
    norb = sum(norb_f)
    solver = fci.solver(mol).set(orbsym=orbsym, wfnsym=wfnsym)
    nroots = len(ci_r)
    ci_r = np.tensordot(si.conj().T, np.stack(ci_r, axis=0), axes=1)
    rdm1s = np.zeros((nroots, 2, norb, norb), dtype=ci_r.dtype)
    rdm2s = np.zeros((nroots, 2, norb, norb, 2, norb, norb), dtype=ci_r.dtype)
    for ix, ci in enumerate(ci_r):
        d1s, d2s = solver.make_rdm12s(ci, norb, nelec)
        rdm1s[ix, 0, :, :] = d1s[0]
        rdm1s[ix, 1, :, :] = d1s[1]
        rdm2s[ix, 0, :, :, 0, :, :] = d2s[0]
        rdm2s[ix, 0, :, :, 1, :, :] = d2s[1]
        rdm2s[ix, 1, :, :, 0, :, :] = d2s[1].transpose(2, 3, 0, 1)
        rdm2s[ix, 1, :, :, 1, :, :] = d2s[2]
    return rdm1s, rdm2s
Пример #33
0
def make_stdm12s(las, ci_fr, idx_root, orbsym=None, wfnsym=None):
    mol = las.mol
    norb_f = las.ncas_sub
    nelec_fr = [[
        _unpack_nelec(fcibox._get_nelec(solver, nelecas))
        for solver, ix in zip(fcibox.fcisolvers, idx_root) if ix
    ] for fcibox, nelecas in zip(las.fciboxes, las.nelecas_sub)]
    ci_r, nelec = ci_outer_product(ci_fr, norb_f, nelec_fr)
    norb = sum(norb_f)
    solver = fci.solver(mol).set(orbsym=orbsym, wfnsym=wfnsym)
    nroots = len(ci_r)
    stdm1s = np.zeros((nroots, nroots, 2, norb, norb),
                      dtype=ci_r[0].dtype).transpose(0, 2, 3, 4, 1)
    stdm2s = np.zeros((nroots, nroots, 2, norb, norb, 2, norb, norb),
                      dtype=ci_r[0].dtype).transpose(0, 2, 3, 4, 5, 6, 7, 1)
    for i, ci in enumerate(ci_r):
        rdm1s, rdm2s = solver.make_rdm12s(ci, norb, nelec)
        stdm1s[i, 0, :, :, i] = rdm1s[0]
        stdm1s[i, 1, :, :, i] = rdm1s[1]
        stdm2s[i, 0, :, :, 0, :, :, i] = rdm2s[0]
        stdm2s[i, 0, :, :, 1, :, :, i] = rdm2s[1]
        stdm2s[i, 1, :, :, 0, :, :, i] = rdm2s[1].transpose(2, 3, 0, 1)
        stdm2s[i, 1, :, :, 1, :, :, i] = rdm2s[2]
    for (i, ci_bra), (j, ci_ket) in combinations(enumerate(ci_r), 2):
        tdm1s, tdm2s = solver.trans_rdm12s(ci_bra, ci_ket, norb, nelec)
        # Transpose for 1TDM is backwards because of stupid PySCF convention
        stdm1s[i, 0, :, :, j] = tdm1s[0].T
        stdm1s[i, 1, :, :, j] = tdm1s[1].T
        stdm1s[j, 0, :, :, i] = tdm1s[0]
        stdm1s[j, 1, :, :, i] = tdm1s[1]
        for spin, tdm2 in enumerate(tdm2s):
            p = spin // 2
            q = spin % 2
            stdm2s[i, p, :, :, q, :, :, j] = tdm2
            stdm2s[j, p, :, :, q, :, :, i] = tdm2.transpose(1, 0, 3, 2)
    return stdm1s, stdm2s
Пример #34
0
    # state average
    mol.atom = [
        ['O', (0., 0., 0.)],
        ['H', (0., -0.757, 0.587)],
        ['H', (0., 0.757, 0.587)],
    ]
    mol.basis = '6-31g'
    mol.symmetry = 1
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()

    mc = mcscf.CASSCF(m, 4, 4)
    mc.verbose = 4
    mc.fcisolver = fci.solver(mol, False)  # to mix the singlet and triplet
    mc = state_average_(mc, (.64, .36))
    emc, e_ci, fcivec, mo, mo_energy = mc.mc1step()[:5]
    mc = mcscf.CASCI(m, 4, 4)
    emc = mc.casci(mo)[0]
    print(ehf, emc, emc - ehf)
    print(emc - -76.003352190262532)

    mc = mcscf.CASSCF(m, 4, 4)
    mc.verbose = 4
    mc = state_average_(mc, (.64, .36))
    emc, e_ci, fcivec, mo, mo_energy = mc.mc1step()[:5]
    mc = mcscf.CASCI(m, 4, 4)
    emc = mc.casci(mo)[0]
    print(ehf, emc, emc - ehf)
    print(emc - -75.982520334896776)
Пример #35
0
        ['H', (0., -0.5, -1.)],
        ['H', (0., -0.5, -0.)],
        ['H', (0., -0., -1.)],
        ['H', (1., -0.5, 0.)],
        ['H', (0., 1., 1.)],
        ['H', (0., -1., -2.)],
        ['H', (1., -1.5, 1.)],
    ]

    mol.basis = {'H': 'sto-3g'}
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()

    cis = fci.solver(mol)
    cis.verbose = 5
    norb = m.mo_coeff.shape[1]
    nelec = mol.nelectron
    h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff))
    eri = ao2mo.incore.full(m._eri, m.mo_coeff)
    e, ci0 = cis.kernel(h1e, eri, norb, nelec)
    ss = spin_square(ci0, norb, nelec)
    print(ss)
    ss = spin_square0(ci0, norb, nelec)
    print(ss)
    ss = local_spin(ci0, norb, nelec, m.mo_coeff, m.get_ovlp(), range(5))
    print('local spin for H1..H5 = 0.999', ss[0])
    ci1 = numpy.zeros((4, 4))
    ci1[0, 0] = 1
    print(spin_square(ci1, 4, (3, 1)))
Пример #36
0
 def __init__(self, mf, ncas, nelecas, ncore=None, frozen=None):
     assert(mf.mol.symmetry)
     self.orbsym = []
     mc1step.CASSCF.__init__(self, mf, ncas, nelecas, ncore, frozen)
     self.fcisolver = fci.solver(mf.mol, self.nelecas[0]==self.nelecas[1], True)
Пример #37
0
    mol = gto.Mole()
    mol.verbose = 0
    mol.output = None#"out_h2o"
    mol.atom = [
        ['O', ( 0., 0.    , 0.   )],
        ['H', ( 0., -0.757, 0.587)],
        ['H', ( 0., 0.757 , 0.587)],]

    mol.basis = {'H': 'sto-3g',
                 'O': '6-31g',}
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()
    mc = mcscf.CASSCF(m, 4, 4)
    mc.fcisolver = fci.solver(mol)
    mc.natorb = 1
    emc = mc.kernel()[0]
    print(ehf, emc, emc-ehf)
    #-75.9577817425 -75.9624554777 -0.00467373522233
    print(emc+75.9624554777)

#    mc = CASCI(m, 4, (3,1))
#    #mc.fcisolver = fci.direct_spin1
#    mc.fcisolver = fci.solver(mol, False)
#    emc = mc.casci()[0]
#    print(emc - -75.439016172976)
#
#    mol = gto.Mole()
#    mol.verbose = 0
#    mol.output = "out_casci"
Пример #38
0
# state average
    mol.atom = [
        ['O', ( 0., 0.    , 0.   )],
        ['H', ( 0., -0.757, 0.587)],
        ['H', ( 0., 0.757 , 0.587)],]
    mol.basis = '6-31g'
    mol.symmetry = 1
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()


    mc = mcscf.CASSCF(m, 4, 4)
    mc.verbose = 4
    mc.fcisolver = fci.solver(mol, False) # to mix the singlet and triplet
    mc = state_average_(mc, (.64,.36))
    emc, e_ci, fcivec, mo, mo_energy = mc.mc1step()[:4]
    mc = mcscf.CASCI(m, 4, 4)
    emc = mc.casci(mo)[0]
    print(ehf, emc, emc-ehf)
    print(emc - -76.003352190262532)

    mc = mcscf.CASSCF(m, 4, 4)
    mc.verbose = 4
    mc = state_average_(mc, (.64,.36))
    emc, e_ci, fcivec, mo, mo_energy = mc.mc1step()[:4]
    mc = mcscf.CASCI(m, 4, 4)
    emc = mc.casci(mo)[0]
    print(ehf, emc, emc-ehf)
    print(emc - -75.982520334896776)
Пример #39
0
def kernel(mc, ot, root=-1):
    ''' Calculate MC-PDFT total energy

        Args:
            mc : an instance of CASSCF or CASCI class
                Note: this function does not currently run the CASSCF or CASCI calculation itself
                prior to calculating the MC-PDFT energy. Call mc.kernel () before passing to this function!
            ot : an instance of on-top density functional class - see otfnal.py

        Kwargs:
            root : int
                If mc describes a state-averaged calculation, select the root (0-indexed)
                Negative number requests state-averaged MC-PDFT results (i.e., using state-averaged density matrices)

        Returns:
            Total MC-PDFT energy including nuclear repulsion energy.
    '''
    t0 = (time.clock(), time.time())
    amo = mc.mo_coeff[:, mc.ncore:mc.ncore + mc.ncas]
    # make_rdm12s returns (a, b), (aa, ab, bb)

    mc_1root = mc
    if isinstance(mc.ci, list) and root >= 0:
        mc_1root = mcscf.CASCI(mc._scf, mc.ncas, mc.nelecas)
        mc_1root.fcisolver = fci.solver(mc._scf.mol, singlet=False, symm=False)
        mc_1root.mo_coeff = mc.mo_coeff
        mc_1root.ci = mc.ci[root]
        mc_1root.e_tot = mc.e_tot
    dm1s = np.asarray(mc_1root.make_rdm1s())
    adm1s = np.stack(mc_1root.fcisolver.make_rdm1s(mc_1root.ci, mc.ncas,
                                                   mc.nelecas),
                     axis=0)
    adm2 = get_2CDM_from_2RDM(
        mc_1root.fcisolver.make_rdm12(mc_1root.ci, mc.ncas, mc.nelecas)[1],
        adm1s)
    if ot.verbose >= logger.DEBUG:
        adm2s = get_2CDMs_from_2RDMs(
            mc_1root.fcisolver.make_rdm12s(mc_1root.ci, mc.ncas,
                                           mc.nelecas)[1], adm1s)
        adm2s_ss = adm2s[0] + adm2s[2]
        adm2s_os = adm2s[1]
    spin = abs(mc.nelecas[0] - mc.nelecas[1])
    t0 = logger.timer(ot, 'rdms', *t0)

    omega, alpha, hyb = ot._numint.rsh_and_hybrid_coeff(ot.otxc, spin=spin)
    Vnn = mc._scf.energy_nuc()
    h = mc._scf.get_hcore()
    dm1 = dm1s[0] + dm1s[1]
    if ot.verbose >= logger.DEBUG or abs(hyb) > 1e-10:
        vj, vk = mc._scf.get_jk(dm=dm1s)
        vj = vj[0] + vj[1]
    else:
        vj = mc._scf.get_j(dm=dm1)
    Te_Vne = np.tensordot(h, dm1)
    # (vj_a + vj_b) * (dm_a + dm_b)
    E_j = np.tensordot(vj, dm1) / 2
    # (vk_a * dm_a) + (vk_b * dm_b) Mind the difference!
    if ot.verbose >= logger.DEBUG or abs(hyb) > 1e-10:
        E_x = -(np.tensordot(vk[0], dm1s[0]) +
                np.tensordot(vk[1], dm1s[1])) / 2
    else:
        E_x = 0
    logger.debug(ot, 'CAS energy decomposition:')
    logger.debug(ot, 'Vnn = %s', Vnn)
    logger.debug(ot, 'Te + Vne = %s', Te_Vne)
    logger.debug(ot, 'E_j = %s', E_j)
    logger.debug(ot, 'E_x = %s', E_x)
    if ot.verbose >= logger.DEBUG:
        # g_pqrs * l_pqrs / 2
        #if ot.verbose >= logger.DEBUG:
        aeri = ao2mo.restore(1, mc.get_h2eff(mc.mo_coeff), mc.ncas)
        E_c = np.tensordot(aeri, adm2, axes=4) / 2
        E_c_ss = np.tensordot(aeri, adm2s_ss, axes=4) / 2
        E_c_os = np.tensordot(aeri, adm2s_os, axes=4)  # ab + ba -> factor of 2
        logger.info(ot, 'E_c = %s', E_c)
        logger.info(ot, 'E_c (SS) = %s', E_c_ss)
        logger.info(ot, 'E_c (OS) = %s', E_c_os)
        e_err = E_c_ss + E_c_os - E_c
        assert (abs(e_err) < 1e-8), e_err
        if isinstance(mc_1root.e_tot, float):
            e_err = mc_1root.e_tot - (Vnn + Te_Vne + E_j + E_x + E_c)
            assert (abs(e_err) < 1e-8), e_err
    if abs(hyb) > 1e-10:
        logger.debug(ot, 'Adding %s * %s CAS exchange to E_ot', hyb, E_x)
    t0 = logger.timer(ot, 'Vnn, Te, Vne, E_j, E_x', *t0)

    E_ot = get_E_ot(ot, dm1s, adm2, amo)
    t0 = logger.timer(ot, 'E_ot', *t0)
    e_tot = Vnn + Te_Vne + E_j + (hyb * E_x) + E_ot
    logger.info(ot, 'MC-PDFT E = %s, Eot(%s) = %s', e_tot, ot.otxc, E_ot)

    return e_tot, E_ot
Пример #40
0
        ['H', ( 0.,-0.5   ,-1.   )],
        ['H', ( 0.,-0.5   ,-0.   )],
        ['H', ( 0.,-0.    ,-1.   )],
        ['H', ( 1.,-0.5   , 0.   )],
        ['H', ( 0., 1.    , 1.   )],
        ['H', ( 0.,-1.    ,-2.   )],
        ['H', ( 1.,-1.5   , 1.   )],
    ]

    mol.basis = {'H': 'sto-3g'}
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()

    cis = fci.solver(mol)
    cis.verbose = 5
    norb = m.mo_coeff.shape[1]
    nelec = mol.nelectron
    h1e = reduce(numpy.dot, (m.mo_coeff.T, m.get_hcore(), m.mo_coeff))
    eri = ao2mo.incore.full(m._eri, m.mo_coeff)
    e, ci0 = cis.kernel(h1e, eri, norb, nelec)
    ss = spin_square(ci0, norb, nelec)
    print(ss)
    ss = spin_square0(ci0, norb, nelec)
    print(ss)
    ss = local_spin(ci0, norb, nelec, m.mo_coeff, m.get_ovlp(), range(5))
    print('local spin for H1..H5 = 0.998988389', ss[0])
    ci1 = numpy.zeros((4,4))
    ci1[0,0] = 1
    print(spin_square (ci1, 4, (3,1)))
Пример #41
0
 def test_state_specific(self):
     mc = mcscf.CASSCF(mfr, 4, 4)
     mc.fcisolver = fci.solver(mol, singlet=False)
     mc.state_specific_(state=1)
     e = mc.kernel()[0]
     self.assertAlmostEqual(e, -108.70065770892457, 7)
Пример #42
0
 def test_state_average(self):
     mc = mcscf.CASSCF(mfr, 4, 4)
     mc.fcisolver = fci.solver(mol, singlet=False)
     mc.state_average_((0.64, 0.36))
     e = mc.kernel()[0]
     self.assertAlmostEqual(e, -108.83342083775061, 7)
Пример #43
0
if __name__ == "__main__":
    from pyscf import gto
    from pyscf import scf

    mol = gto.Mole()
    mol.verbose = 0
    mol.output = None  # "out_h2o"
    mol.atom = [["O", (0.0, 0.0, 0.0)], ["H", (0.0, -0.757, 0.587)], ["H", (0.0, 0.757, 0.587)]]

    mol.basis = {"H": "sto-3g", "O": "6-31g"}
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()
    mc = CASCI(m, 4, 4)
    mc.fcisolver = fci.solver(mol)
    emc = mc.casci()[0]
    print(ehf, emc, emc - ehf)
    # -75.9577817425 -75.9624554777 -0.00467373522233
    print(emc + 75.9624554777)

    mc = CASCI(m, 4, (3, 1))
    # mc.fcisolver = fci.direct_spin1
    mc.fcisolver = fci.solver(mol, False)
    emc = mc.casci()[0]
    print(emc - -75.439016172976)

    mol = gto.Mole()
    mol.verbose = 0
    mol.output = "out_casci"
    mol.atom = [
Пример #44
0
    mol.atom = [
        ['O', (0., 0., 0.)],
        ['H', (0., -0.757, 0.587)],
        ['H', (0., 0.757, 0.587)],
    ]

    mol.basis = {
        'H': 'sto-3g',
        'O': '6-31g',
    }
    mol.build()

    m = scf.RHF(mol)
    ehf = m.scf()
    mc = mcscf.CASCI(m, 4, 4)
    mc.fcisolver = fci.solver(mol)
    mc.natorb = 1
    emc = mc.kernel()[0]
    print(ehf, emc, emc - ehf)
    #-75.9577817425 -75.9624554777 -0.00467373522233
    print(emc + 75.9624554777)

#    mc = CASCI(m, 4, (3,1))
#    #mc.fcisolver = fci.direct_spin1
#    mc.fcisolver = fci.solver(mol, False)
#    emc = mc.casci()[0]
#    print(emc - -75.439016172976)
#
#    mol = gto.Mole()
#    mol.verbose = 0
#    mol.output = "out_casci"
Пример #45
0
 def __init__(self, mf_or_mol, ncas, nelecas, ncore=None, frozen=None):
     newton_casscf.CASSCF.__init__(self, mf_or_mol, ncas, nelecas, ncore, frozen)
     assert(self.mol.symmetry)
     #self.fcisolver = fci.solver(self.mol, self.nelecas[0]==self.nelecas[1], True)
     self.fcisolver = fci.solver(self.mol, False, True)
     self.fcisolver.max_cycle = 25