def make_nevpt_object(self, dirname=None): # dirname None implies exact solve, i.e. no NECI invokation to supply RDMs mol = self.get_mol_obj() m = scf.RHF(mol) m.conv_tol = 1e-10 if dirname is not None: try: casci = mcscf.CASCI(m, self.ncas, self.nelecas) pyscf_serializer.selective_deserialize( casci, pfile='{}/casci.pkl'.format(dirname)) casci.fcisolver = fciqmcscf.FCIQMCCI(mol) casci.fcisolver.dirname = dirname except IOError: m.kernel() m.analyze(with_meta_lowdin=0) casci = mcscf.CASCI(m, self.ncas, self.nelecas) casci.fcisolver = fciqmcscf.FCIQMCCI(mol) casci.fcisolver.only_ints = True try: casci.kernel() except TypeError: pass pyscf_serializer.selective_serialize( casci, ['mo_coeff', '_scf.mo_occ', '_scf.mo_coeff'], pfile='{}/casci.pkl'.format(dirname)) if not os.path.abspath(dirname) == os.path.abspath('.'): shutil.move('FCIDUMP', dirname) return else: m.kernel() casci = mcscf.CASCI(m, self.ncas, self.nelecas) casci.kernel() return mrpt.NEVPT(casci)
def test_with_x2c_scanner(self): mc1 = mcscf.CASCI(m, 4, 4).x2c().run() self.assertAlmostEqual(mc1.e_tot, -108.89264146901512, 7) mc1 = mcscf.CASCI(m, 4, 4).x2c().as_scanner().as_scanner() mc1(mol) self.assertAlmostEqual(mc1.e_tot, -108.89264146901512, 7)
def test_with_qmmm_scanner(self): from pyscf import qmmm mol = gto.Mole() mol.atom = ''' O 0.00000000 0.00000000 -0.11081188 H -0.00000000 -0.84695236 0.59109389 H -0.00000000 0.89830571 0.52404783 ''' mol.verbose = 0 mol.basis = '6-31g' mol.build() coords = [(0.5, 0.6, 0.1)] #coords = [(0.0,0.0,0.0)] charges = [-0.1] mf = qmmm.add_mm_charges(scf.RHF(mol), coords, charges) mc = mcscf.CASCI(mf, 4, 4).as_scanner() e_tot, g = mc.nuc_grad_method().as_scanner()(mol) self.assertAlmostEqual(e_tot, -75.98156095286714, 9) self.assertAlmostEqual(lib.finger(g), 0.08335504754051845, 6) e1 = mc(''' O 0.00100000 0.00000000 -0.11081188 H -0.00000000 -0.84695236 0.59109389 H -0.00000000 0.89830571 0.52404783 ''') e2 = mc(''' O -0.00100000 0.00000000 -0.11081188 H -0.00000000 -0.84695236 0.59109389 H -0.00000000 0.89830571 0.52404783 ''') ref = (e1 - e2) / 0.002 * lib.param.BOHR self.assertAlmostEqual(g[0, 0], ref, 4) mf = scf.RHF(mol) mc = qmmm.add_mm_charges( mcscf.CASCI(mf, 4, 4).as_scanner(), coords, charges) e_tot, g = mc.nuc_grad_method().as_scanner()(mol) self.assertAlmostEqual(e_tot, -75.98156095286714, 7) self.assertAlmostEqual(lib.finger(g), 0.08335504754051845, 6)
def __init__(self, fciqmc_dir=None, mol_kwargs=None, fname='nevpt2_store.pkl', casorbs=None, norb=None, nelecas=None, save_dms=False, threshs=(1e-14, ), analyze=False): self.save_dms = save_dms self.casorbs = casorbs if isinstance(mol_kwargs, dict): self.mol_kwargs = mol_kwargs mol = self.mol() hf = scf.RHF(mol) hf.conv_tol = 1e-10 hf.kernel() if analyze: hf.analyze() self.hf_canon_mo = hf.mo_coeff self.hf_mo_energy = hf.mo_energy self.norb, self.nelecas = norb, nelecas if casorbs is not None: self.norb = len(casorbs) casci = mcscf.CASCI(hf, self.norb, self.nelecas) if casorbs is not None: casci.mo_coeff = casci.sort_mo(casorbs) self.hf_canon_mo = casci.mo_coeff if fciqmc_dir is not None: print '### Initial invokation for subsequent FCIQMC RDM sampling' output_fcidump(casci, mol, fciqmc_dir) self.save(fname) else: print '### Exact CASCI+NEVPT2 invokation' output_fcidump(casci, mol) casci.kernel() self.casci_canon_mo = casci.mo_coeff self.casci_mo_energy = casci.mo_energy nevpt2 = mrpt.NEVPT(casci) nevpt2.threshs = threshs nevpt2.kernel(self.save_dms) self.save(fname) elif isinstance(fname, str) and os.path.exists(fname): self.load(fname) mol = self.mol() hf = scf.RHF(mol) casci = mcscf.CASCI(hf, self.norb, self.nelecas) casci.mo_coeff = self.casci_canon_mo if self.casci_canon_mo is not None else self.hf_canon_mo casci.mo_energy = self.casci_mo_energy if self.casci_canon_mo is not None else self.hf_mo_energy if fciqmc_dir is not None: print '### Stochastic NEVPT2 invokation with FCIQMC RDMs' casci.fcisolver = fciqmcscf.FCIQMCCI(mol) casci.fcisolver.dirname = fciqmc_dir else: print '### Exact NEVPT2 invokation from previous CASCI' nevpt2 = mrpt.NEVPT(casci) nevpt2.threshs = threshs nevpt2.canonicalized = self.casci_canon_mo is not None nevpt2.kernel(self.save_dms) else: raise Exception('Neither a mol dict nor a valid path was provided')
def test_get_h2eff(self): mc1 = mcscf.approx_hessian(mcscf.CASCI(m, 4, 4)) eri1 = mc1.get_h2eff(m.mo_coeff[:, 5:9]) eri2 = mc1.get_h2cas(m.mo_coeff[:, 5:9]) self.assertAlmostEqual(abs(eri1 - eri2).max(), 0, 12) mc1 = mcscf.density_fit(mcscf.CASCI(m, 4, 4)) eri1 = mc1.get_h2eff(m.mo_coeff[:, 5:9]) eri2 = mc1.get_h2cas(m.mo_coeff[:, 5:9]) self.assertTrue(abs(eri1 - eri2).max() > 1e-5)
def test_with_ci_init_guess(self): mc1 = mcscf.CASCI(msym, 4, 4) ci0 = numpy.zeros((6, 6)) ci0[0, 1] = 1 mc1.kernel(ci0=ci0) mc2 = mcscf.CASCI(msym, 4, 4) mc2.wfnsym = 'A1u' mc2.kernel() self.assertAlmostEqual(mc1.e_tot, mc2.e_tot, 9)
def test_casci_grad(self): mf = scf.RHF(mol0).ddCOSMO().run() mc = solvent.ddCOSMO(mcscf.CASCI(mf, 2, 2)) e, de = mc.nuc_grad_method().as_scanner()(mol0) self.assertAlmostEqual(e, -1.1844606066401635, 7) self.assertAlmostEqual(lib.fp(de), -0.18558925270492277, 5) mf = scf.RHF(mol1).run() mc1 = solvent.ddCOSMO(mcscf.CASCI(mf, 2, 2)).run() e1 = mc1.e_tot mf = scf.RHF(mol2).run() mc2 = solvent.ddCOSMO(mcscf.CASCI(mf, 2, 2)).run() e2 = mc2.e_tot self.assertAlmostEqual((e2 - e1) / dx, de[0, 2], 3)
def test_casci_from_uhf(self): mf = scf.UHF(mol).run() mc = mcscf.CASCI(mf.density_fit('weigend'), 4, 4) emc = mc.casci()[0] self.assertAlmostEqual(emc, -108.88669369639578, 6) self.assertAlmostEqual(numpy.linalg.norm(mc.analyze()), 2.6910275883606078, 4)
def test_nr_CASCI(self): mf = scf.RHF(mol) mf.verbose = 0 mf.kernel() mc = cosmo.cosmo_(mcscf.CASCI(mf, 4, 4)) self.assertAlmostEqual( mc.kernel(mc.sort_mo([3, 4, 6, 7]))[0], -76.0107164465, 8)
def pyqmc_from_hdf(chkfile): """ Loads pyqmc objects from a pyscf checkfile """ mol = lib.chkfile.load_mol(chkfile) mol.output = None mol.stdout = None mf = scf.RHF(mol) mf.__dict__.update(scf.chkfile.load(chkfile, "scf")) with h5py.File(chkfile, "r") as f: mc = mcscf.CASCI(mf, ncas=int(f["mc/ncas"][...]), nelecas=f["mc/nelecas"][...]) mc.ci = f["mc/ci"][...] wf, to_opt, freeze = pyqmc.default_msj(mol, mf, mc) freeze["wf1det_coeff"][...] = False pgrad = pyqmc.gradient_generator(mol, wf, to_opt, freeze) return { "mol": mol, "mf": mf, "to_opt": to_opt, "freeze": freeze, "wf": wf, "pgrad": pgrad, }
def test_lasuccsd_total_energy (self): mc = mcscf.CASCI (mf, 4, 4) mc.mo_coeff = las.mo_coeff mc.fcisolver = lasuccsd.FCISolver (mol) mc.fcisolver.norb_f = [2,2] mc.kernel () with self.subTest (from_='reported'): self.assertAlmostEqual (mc.e_tot, ref.e_tot, 6) psi = mc.fcisolver.psi x = psi.x h1, h0 = mc.get_h1eff () h2 = mc.get_h2eff () h = [h0, h1, h2] energy, gradient = psi.e_de (x, h) with self.subTest (from_='obj fn'): self.assertAlmostEqual (energy, mc.e_tot, 9) c = np.squeeze (fockspace.fock2hilbert (psi.get_fcivec (x), 4, (2,2))) h2eff = direct_spin1.absorb_h1e (h1, h2, 4, (2,2), 0.5) hc = direct_spin1.contract_2e (h2eff, c, 4, (2,2)) chc = c.conj ().ravel ().dot (hc.ravel ()) + h0 with self.subTest (from_='h2 contraction'): self.assertAlmostEqual (chc, mc.e_tot, 9) c_f = psi.ci_f for ifrag, c in enumerate (c_f): heff = psi.get_dense_heff (x, h, ifrag) hc = np.dot (heff.full, c.ravel ()) chc = np.dot (c.conj ().ravel (), hc) with self.subTest (from_='frag {} Fock-space dense effective Hamiltonian'.format (ifrag)): self.assertAlmostEqual (chc, mc.e_tot, 9) heff, _ = heff.get_number_block ((1,1),(1,1)) c = np.squeeze (fockspace.fock2hilbert (c, 2, (1,1))).ravel () hc = np.dot (heff, c) chc = np.dot (c.conj (), hc) with self.subTest (from_='frag {} Hilbert-space dense effective Hamiltonian'.format (ifrag)): self.assertAlmostEqual (chc, mc.e_tot, 9)
def test_make_natural_orbitals_from_unrestricted(self): from pyscf import mp, ci, cc npt = numpy.testing mol = gto.M(atom = 'O 0 0 0; O 0 0 1.2', basis = '3-21g', spin = 2) myhf = scf.UHF(mol).run() ncas, nelecas = (8, 12) mymc = mcscf.CASCI(myhf, ncas, nelecas) # Test MP2 # Trusted results from ORCA v4.2.1 rmp2_noons = [1.99992786,1.99992701,1.99414062,1.98906552,1.96095173,1.96095165,1.95280755,1.02078458,1.02078457,0.04719006,0.01274288,0.01274278,0.00728679,0.00582683,0.00543964,0.00543962,0.00290772,0.00108258] mymp = mp.UMP2(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mymp) npt.assert_array_almost_equal(rmp2_noons, noons) mymc.kernel(natorbs) # The tests below are only to ensure that `make_natural_orbitals` can # run at all since we've confirmed above that the NOONs are correct. # Test CISD mycisd = ci.CISD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mycisd) mymc.kernel(natorbs) # Test CCSD myccsd = cc.CCSD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(myccsd) mymc.kernel(natorbs)
def test_cas_natorb(self): mc1 = mcscf.CASCI(msym, 4, 4, ncore=5) mo = mc1.sort_mo([4, 5, 10, 13]) mc1.sorting_mo_energy = True mc1.kernel(mo) mo0 = mc1.mo_coeff ci0 = mc1.ci self.assertAlmostEqual(mc1.e_tot, -108.71841138528966, 9) mo2, ci2, mo_e = mc1.canonicalize(sort=False, cas_natorb=True, with_meta_lowdin=False, verbose=7) casdm1 = mc1.fcisolver.make_rdm1(mc1.ci, 4, 4) mc1.ci = None # Force cas_natorb_ to recompute CI coefficients mc1.cas_natorb_(casdm1=casdm1, sort=True, with_meta_lowdin=True) mo1 = mc1.mo_coeff ci1 = mc1.ci s = numpy.einsum('pi,pq,qj->ij', mo0[:, 5:9], msym.get_ovlp(), mo1[:, 5:9]) self.assertAlmostEqual(fci.addons.overlap(ci0, ci1, 4, 4, s), 1, 9) self.assertAlmostEqual(float(abs(mo1 - mo2).max()), 0, 9) self.assertAlmostEqual(ci1.ravel().dot(ci2.ravel()), 1, 9) # Make sure that mc.mo_occ has been set and that the NOONs add to nelectron mo_occ = getattr(mc1, "mo_occ", numpy.array([])) self.assertNotEqual(mo_occ, numpy.array([])) self.assertAlmostEqual(numpy.sum(mo_occ), mc1.mol.nelectron, 9)
def test_lasci_ominus1 (self): mc = mcscf.CASCI (mf, 4, 4) mc.mo_coeff = las.mo_coeff mc.fcisolver = lasci_ominus1.FCISolver (mol) mc.fcisolver.norb_f = [2,2] mc.kernel () self.assertAlmostEqual (mc.e_tot, las.e_tot, 6)
def test_make_natural_orbitals_from_restricted(self): from pyscf import mp, ci, cc npt = numpy.testing mol = gto.M(atom='C 0 0 0; O 0 0 1.2', basis='3-21g', spin=0) myhf = scf.RHF(mol).run() ncas, nelecas = (8, 10) mymc = mcscf.CASCI(myhf, ncas, nelecas) # Test MP2 # Trusted results from ORCA v4.2.1 rmp2_noons = [ 1.99992732, 1.99989230, 1.99204357, 1.98051334, 1.96825487, 1.94377615, 1.94376239, 0.05792320, 0.05791037, 0.02833335, 0.00847013, 0.00531989, 0.00420320, 0.00420280, 0.00257965, 0.00101638, 0.00101606, 0.00085503 ] mymp = mp.MP2(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mymp) npt.assert_array_almost_equal(rmp2_noons, noons, decimal=5) mymc.kernel(natorbs) # The tests below are only to ensure that `make_natural_orbitals` can # run at all since we've confirmed above that the NOONs are correct. # Test CISD mycisd = ci.CISD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(mycisd) mymc.kernel(natorbs) # Test CCSD myccsd = cc.CCSD(myhf).run() noons, natorbs = mcscf.addons.make_natural_orbitals(myccsd) mymc.kernel(natorbs)
def wavefunction(return_mf = False): """ Returns Full CI wave function multiplied by a Jastrow with cusp conditions applied """ from pyqmc import default_msj, default_multislater mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 3.015", basis='cc-pvqz', unit="bohr", spin=0) mf = scf.ROHF(mol).run() mc = mcscf.CASCI(mf,ncas=6,nelecas=(2,2)) mc.kernel() wf, to_opt, freeze = default_msj(mol, mf, mc) #Only need to take derivatives wrt determinant coefficients to_opt = ['wf1det_coeff'] freeze = {'wf1det_coeff': freeze['wf1det_coeff']} #Only need to take derivatives wrt the highest energy det #, will have least overlap with nodes! freeze = {'wf1det_coeff': np.ones(freeze['wf1det_coeff'].shape).astype(bool)} freeze['wf1det_coeff'][10] = False wf.parameters['wf1det_coeff'] *= 0 wf.parameters['wf1det_coeff'][[0,10]] = 1./np.sqrt(2.) if(return_mf): return mol, mf, mc, wf, to_opt, freeze else: return mol, wf, to_opt, freeze
def test(): # Default multi-Slater wave function mol = gto.M(atom="Li 0. 0. 0.; H 0. 0. 1.5", basis="cc-pvtz", unit="bohr", spin=0) mf = scf.RHF(mol).run() mc = mcscf.CASCI(mf, ncas=2, nelecas=(1, 1)) mc.kernel() wf, to_opt = default_msj(mol, mf, mc) old_parms = wf.parameters lt = LinearTransform(wf.parameters, to_opt) # Test serialize parameters x0 = lt.serialize_parameters(wf.parameters) x0 += np.random.normal(size=x0.shape) wf.parameters = lt.deserialize(x0) assert wf.parameters["wf1det_coeff"][0] == old_parms["wf1det_coeff"][0] assert np.sum(wf.parameters["wf2bcoeff"][0] - old_parms["wf2bcoeff"][0]) == 0 # Test serialize gradients configs = OpenConfigs(np.random.randn(10, 4, 3)) wf.recompute(configs) pgrad = wf.pgradient() pgrad_serial = lt.serialize_gradients(pgrad) assert np.sum(pgrad_serial[:, :3] - pgrad["wf1det_coeff"][:, 1:4]) == 0
def setUpModule(): global mol, mf, mc, eris, dms, norb, nelec mol = gto.Mole() mol.verbose = 5 mol.output = '/dev/null' #None mol.atom = [ ['H', (0., 0., 0.)], ['H', (0., 0., 0.8)], ['H', (0., 0., 2.)], ['H', (0., 0., 2.8)], ['H', (0., 0., 4.)], ['H', (0., 0., 4.8)], ['H', (0., 0., 6.)], ['H', (0., 0., 6.8)], ['H', (0., 0., 8.)], ['H', (0., 0., 8.8)], ['H', (0., 0., 10.)], ['H', (0., 0., 10.8)], ['H', (0., 0., 12)], ['H', (0., 0., 12.8)], ] mol.basis = 'sto3g' mol.build() mf = scf.RHF(mol) mf.conv_tol = 1e-16 mf.kernel() norb = 6 nelec = 8 mc = mcscf.CASCI(mf, norb, nelec) mc.fcisolver.conv_tol = 1e-15 mc.kernel() mc.canonicalize_() eris, dms = nevpt2_dms(mc)
def test_state_average_bad_init_guess(self): mc = mcscf.CASCI(mfr, 4, 4) mc.run() mc.state_average_([.8, .2]) mscan = mc.as_scanner() e = mscan(mol) self.assertAlmostEqual(e, -108.84390277715984, 9)
def test_casci_uhf(self): mf = scf.UHF(mol) mf.scf() mc = mcscf.CASCI(mf, 4, 4) emc = mc.casci()[0] self.assertAlmostEqual(emc, -108.8896744464714, 7) self.assertAlmostEqual(numpy.linalg.norm(mc.analyze()), 0, 7)
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)
def test_fix_spin_(self): mc1 = mcscf.CASCI(m, 4, 4) mc1.fix_spin_(ss=2).run() self.assertAlmostEqual(mc1.e_tot, -108.03741684418183, 7) mc1.fix_spin_(ss=0).run() self.assertAlmostEqual(mc1.e_tot, -108.83741684447352, 7)
def test_reset(self): myci = mcscf.CASCI(scf.RHF(molsym), 4, 4).density_fit() myci.reset(mol) self.assertTrue(myci.mol is mol) self.assertTrue(myci._scf.mol is mol) self.assertTrue(myci.fcisolver.mol is mol) self.assertTrue(myci.with_df.mol is mol)
def test_state_average_mix(self): mc = mcscf.CASCI(m, 4, 4) cis1 = copy.copy(mc.fcisolver) cis1.spin = 2 cis1.nroots = 3 mc = mcscf.addons.state_average_mix(mc, [cis1, mc.fcisolver], [.25, .25, .25, .25]) mc.run() self.assertAlmostEqual(mc.e_states[0], -108.72522194135607, 9) self.assertAlmostEqual(mc.e_states[1], -108.67148843338228, 9) self.assertAlmostEqual(mc.e_states[2], -108.67148843338228, 9) self.assertAlmostEqual(mc.e_states[3], -108.83741684447352, 9) mc.analyze() mo_coeff, civec, mo_occ = mc.cas_natorb(sort=True) mc.kernel(mo_coeff=mo_coeff) self.assertAlmostEqual(mc.e_states[0], -108.72522194135607, 9) self.assertAlmostEqual(mc.e_states[1], -108.67148843338228, 9) #FIXME: with the initial guess from mc, FCI solver may converge to # another state #self.assertAlmostEqual(mc.e_states[2], -108.67148843338228, 9) self.assertAlmostEqual(mc.e_states[3], -108.83741684447352, 9) self.assertAlmostEqual(abs((civec[0] * mc.ci[0]).sum()), 1, 9) self.assertAlmostEqual(abs((civec[3] * mc.ci[3]).sum()), 1, 9)
def grab_3d_ls(mf): mol = mf.mol mo_coeff = mf.mo_coeff mc = mcscf.CASCI(mf, 5, 6) ncore = mol.nelectron // 2 symms = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo_coeff) idx_virt_ag = symms == 'Ag' idx_virt_ag[:ncore] = False mo_coeff[:, idx_virt_ag] = grab_ao(mol, mo_coeff[:, idx_virt_ag], 'Fe 3d', sorting=-1) for ir in ('B1g', 'B2g', 'B3g'): idx_occ_ir = symms == ir idx_occ_ir[ncore:] = False mo_coeff[:, idx_occ_ir] = grab_ao(mol, mo_coeff[:, idx_occ_ir], 'Fe 3d', sorting=1) irrep_cmo = {} for irrep in np.unique(symms[:ncore]): irrep_cmo[irrep] = np.count_nonzero(symms[:ncore] == irrep) irrep_cmo['B1g'] -= 1 irrep_cmo['B2g'] -= 1 irrep_cmo['B3g'] -= 1 mo_coeff = sort_mo_by_irrep(mc, mo_coeff, { 'Ag': 2, 'B1g': 1, 'B2g': 1, 'B3g': 1 }, cas_irrep_ncore=irrep_cmo) symms = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo_coeff) return mo_coeff
def grab_3d_hs(mf): mol = mf.mol mo_coeff = mf.mo_coeff mc = mcscf.CASCI(mf, 5, 6) ncore = (mol.nelectron // 2) - 2 nocc = ncore + 4 symms = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo_coeff) irrep_cmo = {} for irrep in np.unique(symms[:ncore]): irrep_cmo[irrep] = np.count_nonzero(symms[:ncore] == irrep) cas_irreps = {'Ag': 2, 'B1g': 1, 'B2g': 1, 'B3g': 1} for iorb in range(ncore, nocc): cas_irreps[symms[iorb]] -= 1 print("Found {} singly-occupied orbital".format(symms[iorb])) for key in cas_irreps: if cas_irreps[key] == 1: missing_3d = key irrep_cmo[missing_3d] -= 1 cas_irreps = {'Ag': 2, 'B1g': 1, 'B2g': 1, 'B3g': 1} print("Missing D orbital from SOMOs is apparently {}".format(missing_3d)) print("SOMOs: {}".format(symms[ncore:nocc])) idx_3d = symms == missing_3d idx_3d[ncore:] = False mo_coeff[:, idx_3d] = grab_ao(mol, mo_coeff[:, idx_3d], 'Fe 3d', sorting=1) mo_coeff = sort_mo_by_irrep(mc, mo_coeff, cas_irreps, cas_irrep_ncore=irrep_cmo) symms = symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb, mo_coeff) return mo_coeff
def setuph2(chkfile, identity, r=2.0 / 0.529177, ncas=2): """ Run pyscf and save results in chkfile """ # ccECP from A. Annaberdiyev et al. Journal of Chemical Physics 149, 134108 (2018) basis = { "H": gto.basis.parse(""" H S 23.843185 0.00411490 10.212443 0.01046440 4.374164 0.02801110 1.873529 0.07588620 0.802465 0.18210620 0.343709 0.34852140 0.147217 0.37823130 0.063055 0.11642410 H S 0.040680 1.00000000 H S 0.139013 1.00000000 H P 0.166430 1.00000000 H P 0.740212 1.00000000 """) } ecp = { "H": gto.basis.parse_ecp(""" H nelec 0 H ul 1 21.24359508259891 1.00000000000000 3 21.24359508259891 21.24359508259891 2 21.77696655044365 -10.85192405303825 """) } mol = gto.M(atom=f"H 0. 0. 0.; H 0. 0. {r}", unit="bohr", basis=basis, ecp=ecp, verbose=1) mf = scf.RHF(mol) mf.chkfile = chkfile mf.kernel() nelecas = (1, 1) mc = mcscf.CASCI(mf, ncas=ncas, nelecas=nelecas) mc.kernel() mf.output = None mf.stdout = None mf.chkfile = None mc.output = None mc.stdout = None # print(dir(mc)) with h5py.File(chkfile) as f: f.attrs["uuid"] = identity f.attrs["r"] = r f.create_group("mc") f["mc/ncas"] = ncas f["mc/nelecas"] = list(nelecas) f["mc/ci"] = mc.ci
def test_slight_symmetry_broken(self): mf = copy.copy(msym) mf.mo_coeff = numpy.array(msym.mo_coeff) u = numpy.linalg.svd(numpy.random.random((4, 4)))[0] mf.mo_coeff[:, 5:9] = mf.mo_coeff[:, 5:9].dot(u) mc1 = mcscf.CASCI(mf, 4, 4) mc1.kernel() self.assertAlmostEqual(mc1.e_tot, -108.83741684445798, 9)
def twoElectronAB(basis, flg): mol = gto.Mole() mol.atom = [['H', (0., 0., 0.)], ['H', (0., 0., 0.5)], ['H', (0., 0.5, 0.)]] #mol.atom = [['H', (0.,0.,0.)], ['H', (0.,0.,0.5)]] #mol.atom = [['H', (0.,0.,0.,)], ['F', (0.,0.,0.9)]] #mol.atom = [['H', (0.,0.,0.,)], ['B', (0.,0.,1.2325)]] #mol.atom = [['H', (0.,0.,0.,)], ['Li', (0.,0.,5.)]] #mol.atom = [['H', (0.,0.,0.,)], ['Li', (0.,0.,1.5949)]] #mol.atom = [['H', (0.,0.,0.,)], ['B', (0.,0.,2.5)]] mol.atom = [['Be', (0., 0., 0.)]] #mol.atom = [['H', (0.,0.,0.)]] mol.spin = 0 mol.basis = 'STO-3G' #'6-31g' #mol.charge = -1 mol.verbose = 0 mol.build() mf = scf.RHF(mol) mf.scf() r = mol.nao_nr() Na, Nb = mol.nelec mc = mcscf.CASCI(mf, mol.nao_nr(), (Na, Nb)) mc.verbose = 3 #mc.fcisolver = fci.solver(mol, singlet=True) mc.kernel() (d1a, d1b), (d2aa, d2ab, d2bb) = mc.fcisolver.make_rdm12s(mc.ci, mc.ncas, (Na, Nb)) v2aa = ao2mo.kernel(mol, mf.mo_coeff, compact=False) v2aa = v2aa.reshape(r, r, r, r).swapaxes(1, 2) k1 = reduce(numpy.dot, (mf.mo_coeff.T, mf.get_hcore(), mf.mo_coeff)) print('Nuclear Rep', mol.energy_nuc()) N = mol.nelectron Ns = N // 2 t0 = time.time() tF = time.time() print('Time to prep Hamiltonian ', str(round(tF - t0, 5)), ' seconds') if flg == 'sdp': # return SDP problem k2 = makeK2(v2aa.reshape(r, r, r, r), k1, r, N) k2a = asym_K2(k2, r) k2a = compactK2(k2a, r) c = numpy.append(k2a.flatten(), k2.flatten()) c = numpy.append(c, k2a.flatten()) t0 = time.time() #sdp = makeSDPAB.makeSDPConstraints((['D2aa', 'D2ab', 'D2bb', 'D1a', 'D1b', 'Q1a', 'Q1b', 'Q2aa', 'Q2ab', 'Q2bb', 'G2ab', 'G2ba', 'G2big'], mol.nao_nr(), (Na, Nb))) sdp = makeSDPAB.makeSDPConstraints(('DQG', mol.nao_nr(), (Na, Nb))) tF = time.time() print('Time to make Constraints ', str(round(tF - t0, 5)), ' seconds') sdp.makeAB() t1 = time.time() print('Time to make SDP ', str(round(t1 - tF, 5)), ' seconds') pad_c = numpy.zeros((sdp.numVars - len(c))) c = numpy.append(c, pad_c) A, b, blocks = sdp.A, sdp.b, sdp.blocks return A, b, c, blocks, sdp.varlist
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