from pyscf import gto, scf, mcscf, fci mol = gto.Mole() mol.build( verbose=0, atom='N, 0., 0., 0. ; N, 0., 0., 1.4', basis='cc-pvdz', symmetry=True, ) m = scf.RHF(mol) m.kernel() ncas = 6 nelec = 6 mc = mcscf.CASSCF(m, 6, 6) mc.kernel() # Output all determinants coefficients print(' det-alpha, det-beta, CI coefficients') occslst = fci.cistring._gen_occslst(range(ncas), nelec // 2) for i, occsa in enumerate(occslst): for j, occsb in enumerate(occslst): print(' %s %s %.12f' % (occsa, occsb, mc.ci[i, j])) # Only output determinants which have coefficients > 0.05 print(' det-alpha, det-beta, CI coefficients') for c, ia, ib in mc.fcisolver.large_ci(mc.ci, ncas, nelec, tol=.05,
['N',( 0.000000, 0.000000, -b/2)], ['N',( 0.000000, 0.000000, b/2)], ], basis = {'N': 'ccpvdz', }, ) # Create HF molecule mf = scf.RHF( mol ) mf.conv_tol = 1e-9 mf.scf() # Number of orbital and electrons norb = 8 nelec = 10 dimer_atom = 'N' mc = mcscf.CASSCF( mf, norb, nelec ) e_CASSCF = mc.mc2step()[0] # Create SHCI molecule for just variational opt. # Active spaces chosen to reflect valence active space. mch = shci.SHCISCF( mf, norb, nelec ) mch.fcisolver.nPTiter = 0 # Turn off perturbative calc. mch.fcisolver.outputFile = 'no_PT.dat' mch.fcisolver.sweep_iter = [ 0, 3 ] mch.fcisolver.DoRDM = True # Setting large epsilon1 thresholds highlights improvement from perturbation. mch.fcisolver.sweep_epsilon = [ 1e-2, 1e-2 ] e_noPT = mch.mc1step()[0] # Run a single SHCI iteration with perturbative correction. mch.fcisolver.stochastic = False # Turns on deterministic PT calc.
def test_frozen1s(self): mc = mcscf.CASSCF(msym, 4, 4) mc.frozen = 3 emc = mc.mc1step()[0] self.assertAlmostEqual(emc, -108.91373646206542, 7)
'convergence_gmax': 7.5e-5, # Eh/Bohr 'convergence_drms': 1.0e-4, # Angstrom 'convergence_dmax': 1.5e-4, # Angstrom } h2co_casscf66_631g_xyz = '''C 0.534004 0.000000 0.000000 O -0.676110 0.000000 0.000000 H 1.102430 0.000000 0.920125 H 1.102430 0.000000 -0.920125''' mol = gto.M(atom=h2co_casscf66_631g_xyz, basis='6-31g', symmetry=False, verbose=lib.logger.INFO, output='h2co_sa2_casscf66_631g_opt.log') mf_conv = scf.RHF(mol).run() mc_conv = mcscf.CASSCF(mf_conv, 6, 6) mc_conv.fcisolver = csf_solver(mol, smult=1) mc_conv = mc_conv.state_average_([0.5, 0.5]) mc_conv.conv_tol = 1e-10 mc_conv.kernel() mf = scf.RHF(mol).density_fit(auxbasis=df.aug_etb(mol)).run() mc_df = mcscf.CASSCF(mf, 6, 6) mc_df.fcisolver = csf_solver(mol, smult=1) mc_df = mc_df.state_average_([0.5, 0.5]) mc_df.conv_tol = 1e-10 mc_df.kernel() print("First state") mc_opt_conv = mc_conv.nuc_grad_method().as_scanner(state=0).optimizer()
from pyscf import solvent mol = gto.M(atom=''' C 0.000000 0.000000 -0.542500 O 0.000000 0.000000 0.677500 H 0.000000 0.9353074360871938 -1.082500 H 0.000000 -0.9353074360871938 -1.082500 ''', basis='ccpvdz', verbose=4) mf = scf.RHF(mol).run() # # 1. Allow solvent response to CASSCF optimization # mc = mcscf.CASSCF(mf, 8, 8) mc = solvent.ddCOSMO(mc) mc.run() # # Freeze solvent effects in the CASSCF optimization # # Solvent is fully relaxed in the HF calculation. # mf = solvent.ddCOSMO(scf.RHF(mol)).run() mc = mcscf.CASSCF(mf, 4, 4) # By passing density to solvent model, the solvent potential is frozen at the # HF level. mc = solvent.ddCOSMO(mc, dm=mf.make_rdm1()) mc.run()
mol.verbose = 4 mol.output = 'fepor.out' mol.spin = 4 mol.symmetry = True mol.build() mf = scf.ROHF(mol) mf = scf.fast_newton(mf) # # CAS(8e, 11o) # # mcscf.approx_hessian approximates the orbital hessian. It does not affect # CASSCF results. # mc = mcscf.approx_hessian(mcscf.CASSCF(mf, 11, 8)) idx = [ i for i, s in enumerate(mol.spheric_labels(1)) if 'Fe 3d' in s or 'Fe 4d' in s ] mo = dmet_cas(mc, mf.make_rdm1(), idx) mc.fcisolver.wfnsym = 'Ag' mc.kernel(mo) #mc.analyze() e_q = mc.e_tot # -2244.82910509839 cas_q = mc.mo_coeff[:, mc.ncore:mc.ncore + mc.ncas] ################################################## # # Triplet
def test_mc1step_6o6e_high_cost(self): mc = mcscf.CASSCF(mf, 6, 6) mc.conv_tol = 1e-8 emc = mc.mc1step()[0] self.assertAlmostEqual(emc, -78.0390051207, 7)
from pyscf import fci from pyscf import mcscf b = 1.4 mol = gto.M( verbose = 5, output = '/dev/null', atom = [ ['N',( 0.000000, 0.000000, -b/2)], ['N',( 0.000000, 0.000000, b/2)], ], basis = {'N': '631g', }, ) m = scf.RHF(mol) m.conv_tol = 1e-10 m.scf() mc0 = mcscf.CASSCF(m, 4, 4).run() molsym = gto.M( verbose = 5, output = '/dev/null', atom = [ ['N',( 0.000000, 0.000000, -b/2)], ['N',( 0.000000, 0.000000, b/2)], ], basis = {'N': '631g', }, symmetry = True ) msym = scf.RHF(molsym) msym.conv_tol = 1e-10 msym.scf() def tearDownModule():
def test_chk(self): mc2 = mcscf.CASSCF(m, 4, 4) mc2.update(mc0.chkfile) mc2.max_cycle = 0 mc2.kernel() self.assertAlmostEqual(mc0.e_tot, mc2.e_tot, 8)
mol.build() mf = scf.RHF(mol) mf.chkfile = 'hf_porphin.chk' mf.kernel() PiAtoms = range(1, 25) N_Core, N_Act, N_Virt, nelec, coeff = N_Core, N_Act, N_Virt, nelec, coeff = MakePiOS( mol, mf, PiAtoms, 2, 2) print " # of core orbs ", N_Core print " # of active orbs ", N_Act print " # of virtual orbs ", N_Virt nalpha = (nelec + mol.spin) / 2 nbeta = (nelec - mol.spin) / 2 mycas = mcscf.CASSCF(mf, N_Act, [nalpha, nbeta]) AS = range(N_Core, N_Core + N_Act) mycas = mycas.state_average_([0.2, 0.2, 0.2, 0.2, 0.2]) mycas.chkfile = 'cas_porphin.chk' mycas.fcisolver.nroots = 5 activeMO = mcscf.sort_mo(mycas, coeff, AS, base=0) mycas.fix_spin_(ss=0) mycas.verbose = 5 mycas.max_cycle_macro = 30 mycas.kernel(activeMO) mycas = mcscf.CASCI(mf, N_Act, [nalpha, nbeta]) mycas.__dict__.update(scf.chkfile.load('cas_porphin.chk', 'mcscf')) mycas.fix_spin_(ss=0) mycas.fcisolver.nroots = 5 mycas.verbose = 5
if __name__ == '__main__': from pyscf import gto from pyscf import mcscf from pyscf.tools import ring mol = gto.Mole() mol.verbose = 0 mol.output = None mol.atom = [['H', c] for c in ring.make(6, 1.2)] mol.basis = '6-31g' mol.build() m = scf.RHF(mol) ehf = m.scf() mc = mcscf.CASSCF(m, 6, 6) mc.verbose = 4 emc, e_ci, fcivec, mo, mo_energy = mc.mc1step() print(ehf, emc, emc - ehf) print(emc - -3.272089958) rdm1 = make_rdm1(mc, mo, fcivec) rdm1, rdm2 = make_rdm12(mc, mo, fcivec) print(rdm1) mo1 = cas_natorb(mc)[0] numpy.set_printoptions(2) print( reduce(numpy.dot, (mo1[:, :6].T, mol.intor('int1e_ovlp_sph'), mo[:, :6])))
mol.verbose = 4 mol.output = 'fepor.out' mol.spin = 4 mol.symmetry = True mol.build() mf = scf.ROHF(mol) mf = scf.fast_newton(mf) # ao_labels are keyword(s) to match the AOs generated by mol.ao_labels() # function. ao_labels can be a string, or a pattern of regular expression # or a list of strings/patterns. The code can detect the format and find # the AOs to be taken into the active space. # See also function gto.mole.search_ao_label for the rules of ao_pattern ao_pattern = ['Fe 3d', 'Fe 4d'] ncas, nelecas, mo = dmet_cas.guess_cas(mf, mf.make_rdm1(), ao_pattern) mc = mcscf.approx_hessian(mcscf.CASSCF(mf, ncas, nelecas)) mc.kernel(mo) e_q = mc.e_tot # -2244.82910509839 ################################################## # # Triplet # ##################################################
from pyscf import scf, mcscf, symm from pyscf.tools import molden mol = gto.M(atom='N 0 0 0; N 0 0 2.88972599', unit='B', basis='ccpvtz', verbose=4, symmetry=1, symmetry_subgroup='d2h') mf = scf.RHF(mol).run() coeff = mf.mo_coeff[:, mf.mo_occ > 0] energy = mf.mo_energy[mf.mo_occ > 0] occ = mf.mo_occ[mf.mo_occ > 0] with open('n2_hf.wfn', 'w') as f2: write_mo(f2, mol, coeff, energy, occ) # mc = mcscf.CASSCF(mf, 10, 10) mc.kernel() nmo = mc.ncore + mc.ncas nelecas = mc.nelecas[0] + mc.nelecas[1] casdm1, casdm2 = mc.fcisolver.make_rdm12(mc.ci, mc.ncas, mc.nelecas) rdm1, rdm2 = mcscf.addons._make_rdm12_on_mo(casdm1, casdm2, mc.ncore, mc.ncas, nmo) den_file = 'n2_cas.den' fspt = open(den_file, 'w') fspt.write( 'CCIQA ENERGY = 0.000000000000 THE VIRIAL(-V/T)= 2.00000000\n' ) fspt.write('La matriz D es:\n') for i in range(nmo): for j in range(nmo): fspt.write('%i %i %.16f\n' % ((i + 1), (j + 1), rdm1[i, j]))
mf = scf.newton(mf) mf = scf.addons.remove_linear_dep_(mf) mf.kernel() dm = mf.make_rdm1() mf.level_shift = 0.0 ehf = mf.kernel(dm) aolst1 = ['N 2s'] aolst2 = ['N 2p'] aolst3 = ['N 3s'] aolst4 = ['N 3p'] aolst = aolst1 + aolst2 dm = mf.make_rdm1() ncas, nelecas, mo = dmet_cas.dmet_cas(mf, dm, aolst, threshold=0.1) mc = mcscf.CASSCF(mf, ncas, nelecas) mc.max_cycle_macro = 250 mc.max_cycle_micro = 7 mc.chkfile = name + '.chk' mc.fcisolver = fci.selected_ci_spin0_symm.SCI(mol) mc.fix_spin_(shift=.5, ss=0.0000) mc.fcisolver.ci_coeff_cutoff = 0.0005 mc.fcisolver.select_cutoff = 0.0005 #mc.__dict__.update(scf.chkfile.load(name+'.chk', 'mcscf')) #mo = lib.chkfile.load(name+'.chk', 'mcscf/mo_coeff') mc.kernel(mo) nmo = mc.ncore + mc.ncas rdm1, rdm2 = mc.fcisolver.make_rdm12(mc.ci, mc.ncas, mc.nelecas) rdm1, rdm2 = mcscf.addons._make_rdm12_on_mo(rdm1, rdm2, mc.ncore, mc.ncas, nmo)
0.000000000, 3.370137329, 3.370137329 3.370137329, 0.000000000, 3.370137329 3.370137329, 3.370137329, 0.000000000''' cell.unit = 'B' cell.verbose = 4 cell.build() mf = scf.RHF(cell).density_fit(auxbasis='def2-svp-jkfit') mf.exxdiv = None ehf = mf.kernel() from pyscf.mcscf import avas ao_labels = ['C 2s', 'C 2p'] norb, ne_act, orbs = avas.avas(mf, ao_labels, canonicalize=True, ncore=2) mc = mcscf.CASSCF(mf, norb, ne_act) #mc.fcisolver = fci.selected_ci_spin0.SCI() #mc.fix_spin_(shift=.5, ss=0.0000) #mc.fcisolver.ci_coeff_cutoff = 0.005 #mc.fcisolver.select_cutoff = 0.005 mc.kernel(orbs) nmo = mc.ncore + mc.ncas rdm1, rdm2 = mc.fcisolver.make_rdm12(mc.ci, mc.ncas, mc.nelecas) rdm1, rdm2 = mcscf.addons._make_rdm12_on_mo(rdm1, rdm2, mc.ncore, mc.ncas, nmo) #natocc, natorb = numpy.linalg.eigh(-rdm1) #for i, k in enumerate(numpy.argmax(abs(natorb), axis=0)): # if natorb[k,i] < 0: # natorb[:,i] *= -1 #natorb = numpy.dot(mc.mo_coeff[:,:nmo], natorb)
def test_casci_in_casscf(self): mc1 = mcscf.CASSCF(m, 4, 4) e_tot, e_ci, fcivec = mc1.casci(mc1.mo_coeff) self.assertAlmostEqual(e_tot, -108.83741684447352, 9)
''' n = 4 mol = gto.M() mol.nelectron = n mol.incore_anyway = True U = 1.0 h1 = numpy.zeros((n, n)) for i in range(n - 1): h1[i, i + 1] = h1[i + 1, i] = -1.0 for i in range(n): h1[i, i] = -U / 2 for i in range(n): print h1[i, :] eri = numpy.zeros((n, n, n, n)) for i in range(n): eri[i, i, i, i] = U mf = scf.RHF(mol) mf.get_hcore = lambda *args: h1 mf.get_ovlp = lambda *args: numpy.eye(n) mf._eri = ao2mo.restore(8, eri, n) mf.init_guess = '1e' mf.kernel() mycas = mcscf.CASSCF(mf, ncas=n, nelecas=(n // 2, n // 2)) mycas.kernel()
def test_scanner(self): mc_scan = mcscf.CASSCF(scf.RHF(mol), 4, 4).as_scanner().as_scanner() mc_scan(mol) self.assertAlmostEqual(mc_scan.e_tot, -108.85974001740854, 8)
m = scf.RHF(mol) m.scf() mc = DMRGSCF(m, 4, 4) mc.fcisolver.tol = 1e-9 emc_1 = mc.mc2step()[0] mc = mcscf.CASCI(m, 4, 4) mc.fcisolver = DMRGCI(mol) mc.fcisolver.scheduleSweeps = [] emc_0 = mc.casci()[0] b = 1.4 mol = gto.Mole() mol.build(verbose=7, output='out-casscf', atom=[['H', (0., 0., i - 3.5)] for i in range(8)], basis={'H': 'sto-3g'}, symmetry=True) m = scf.RHF(mol) m.scf() mc = mcscf.CASSCF(m, 4, 4) emc_1ref = mc.mc2step()[0] mc = mcscf.CASCI(m, 4, 4) emc_0ref = mc.casci()[0] print('DMRGCI = %.15g CASCI = %.15g' % (emc_0, emc_0ref)) print('DMRGSCF = %.15g CASSCF = %.15g' % (emc_1, emc_1ref))
m = scf.RHF(mol) m.conv_tol = 1e-10 m.scf() molsym = gto.M(verbose=5, output='/dev/null', atom=[["O", (0., 0., 0.)], [1, (0., -0.757, 0.587)], [1, (0., 0.757, 0.587)]], basis='631g', symmetry=True) msym = scf.RHF(molsym) msym.conv_tol = 1e-10 msym.scf() mc_ref = mcscf.CASSCF(m, 4, 4).state_average_([ 0.25, ] * 4) mc_ref.kernel() def tearDownModule(): global mol, molsym, m, msym, mc_ref mol.stdout.close() molsym.stdout.close() del mol, molsym, m, msym, mc_ref # 4 states, in order: 1^A1, 3^B2, 1^B2, 3^A1 # 3 distinct ways of using state_average_mix to specify these states class KnownValues(unittest.TestCase): def test_nosymm_sa4_newton(self):
def test_mc2step_4o4e_high_cost(self): mc = mcscf.CASSCF(mf, 4, 4) mc.conv_tol = 1e-8 emc = mc.mc2step()[0] #?self.assertAlmostEqual(emc, -78.0103838390, 6) self.assertAlmostEqual(emc, -77.9916207871, 6)
mol = gto.Mole() mol.verbose = 0 mol.atom = [ ["O", (0., 0., 0.7)], ["O", (0., 0., -0.7)],] mol.basis = 'cc-pvdz' mol.spin = 2 mol.build() mf = scf.RHF(mol) mf.kernel() # # Default CASSCF # mc = mcscf.CASSCF(mf, 4, (4,2)) emc1 = mc.kernel()[0] print('* Triplet, using default CI solver, E = %.15g' % emc1) # # fcisolver is fci.direct_spin1 # mc = mcscf.CASSCF(mf, 4, 6) # change the CAS space FCI solver. e.g. to DMRG, FCIQMC mc.fcisolver = fci.direct_spin1 emc1 = mc.kernel()[0] print('* Triplet, using fci.direct_spin1 solver, E = %.15g' % emc1) # # fcisolver is fci.direct_spin0 #
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
Defining Hamiltonian once for SCF object, the derivate post-HF method get the Hamiltonian automatically. ''' mol = gto.M() mol.nelectron = 6 mol.incore_anyway = True n = 6 h1 = numpy.zeros((n,n)) for i in range(n-1): h1[i,i+1] = h1[i+1,i] = -1.0 for i in range(n): print h1[i,:] eri = numpy.zeros((n,n,n,n)) for i in range(n): eri[i,i,i,i] = 10.0 mf = scf.RHF(mol) mf.get_hcore = lambda *args: h1 mf.get_ovlp = lambda *args: numpy.eye(n) mf._eri = ao2mo.restore(8, eri, n) mf.init_guess = '1e' mf.kernel() mycas = mcscf.CASSCF(mf,ncas=n,nelecas=(3,3)) mycas.kernel()
# Active space one-body integrals norb = ncore + nact h1e = h1[ncore:norb, ncore:norb].copy() h1e += 2 * numpy.einsum('pqii->pq', eri_mo[ncore:norb, ncore:norb, :ncore, :ncore]) h1e -= numpy.einsum('piiq->pq', eri_mo[ncore:norb, :ncore, :ncore, ncore:norb]) # Active space two-body integrals h2e = eri_mo[ncore:norb, ncore:norb, ncore:norb, ncore:norb] h2e = ao2mo.restore(8, h2e, nact) e = fci.direct_spin1.kernel(h1e, h2e, nact, nelec, ecore=ecore, verbose=4)[0] lib.logger.info(mf, 'Core energy: %s' % ecore) lib.logger.info(mf, 'Total energy: %s' % e) # Generating the active space Hamiltonian with get_h1eff and get_h2eff function lib.logger.info(mf, '################') lib.logger.info(mf, 'Reference values') lib.logger.info(mf, '################') mc = mcscf.CASSCF(mf, nact, nelec) h1e_cas, ecore = mc.get_h1eff() h2e_cas = mc.get_h2eff() e = fci.direct_spin1.kernel(h1e_cas, h2e_cas, nact, nelec, ecore=ecore, verbose=4)[0] lib.logger.info(mc, 'Core energy: %s' % ecore) lib.logger.info(mc, 'Total energy: %s' % e)
mol = gto.Mole() mol.atom = [ ['O', (0., 0., 0.)], ['H', (0., -0.757, 0.587)], ['H', (0., 0.757, 0.587)], ] mol.basis = { 'H': 'cc-pvdz', 'O': 'cc-pvdz', } mol.build() m = scf.RHF(mol) ehf = m.scf() mc = approx_hessian(mcscf.CASSCF(m, 6, 4)) mc.verbose = 4 mo = addons.sort_mo(mc, m.mo_coeff, (3, 4, 6, 7, 8, 9), 1) emc = mc.kernel(mo)[0] print(ehf, emc, emc - ehf) #-76.0267656731 -76.0873922924 -0.0606266193028 print(emc - -76.0873923174, emc - -76.0926176464) mc = approx_hessian(mcscf.CASSCF(m, 6, (3, 1))) mc.verbose = 4 emc = mc.mc2step(mo)[0] print(emc - -75.7155632535814) mf = scf.density_fit(m) mf.kernel() #mc = density_fit(mcscf.CASSCF(mf, 6, 4))
def test_mc2step_symm_6o6e_high_cost(self): mc = mcscf.CASSCF(msym, 6, 6) emc = mc.mc2step()[0] self.assertAlmostEqual(emc, -108.980105451388, 7)
from pyscf import gto from pyscf import scf from pyscf import mcscf from pyscf.dmrgscf import dmrgci b = 1.2 mol = gto.M( verbose=4, atom='N 0 0 0; N 0 0 %f' % b, basis='cc-pvdz', symmetry=True, ) m = scf.RHF(mol) m.kernel() mc = mcscf.CASSCF(m, 8, 8) mc.fcisolver = dmrgci.DMRGCI(mol) mc.kernel() dm1 = mc.fcisolver.make_rdm1(0, mc.ncas, mc.nelecas) dm2 = mc.fcisolver.make_rdm12(0, mc.ncas, mc.nelecas)[1] dm3 = mc.fcisolver.make_rdm123(0, mc.ncas, mc.nelecas)[2] # # or computing DMs all together in one DMRG call # dm1, dm2 = mc.fcisolver.make_rdm12(0, mc.ncas, mc.nelecas) dm1, dm2, dm3 = mc.fcisolver.make_rdm123(0, mc.ncas, mc.nelecas)
def test_frozenselect(self): mc = mcscf.CASSCF(msym, 4, 4) mc.frozen = [i - 1 for i in [19, 20, 26, 27]] emc = mc.mc1step()[0] self.assertAlmostEqual(emc, -108.91238513746941, 7)
mcscf.mc1step.CASSCF.Gradients = lib.class_as_method(Gradients) if __name__ == '__main__': from pyscf import gto from pyscf import scf from pyscf import mcscf from pyscf import df from mrh.my_pyscf.grad import numeric mol = gto.Mole() mol.atom = 'N 0 0 0; N 0 0 1.2; H 1 1 0; H 1 1 1.2' mol.basis = '631g' mol.build() aux = df.aug_etb(mol) mf = scf.RHF(mol).density_fit(auxbasis=aux).run() mc = mcscf.CASSCF(mf, 4, 4).run() mc.conv_tol = 1e-10 de = Gradients(mc).kernel() de_num = numeric.Gradients(mc).kernel() #print(lib.finger(de) - 0.019602220578635747) print(lib.finger(de) - lib.finger(de_num)) mol = gto.Mole() mol.verbose = 0 mol.atom = 'N 0 0 0; N 0 0 1.2' mol.basis = 'sto3g' mol.build() mf = scf.RHF(mol).density_fit(auxbasis=aux).run() mc = mcscf.CASSCF(mf, 4, 4) mc.conv_tol = 1e-10 mc.kernel()