def test_0163_bse_h2o_spin2_uhf_rpa(self): """ This """ mol = gto.M(verbose=1,atom='O 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074',basis='cc-pvdz',spin=2) gto_mf = scf.UKS(mol) gto_mf.xc = 'hf' gto_mf.kernel() gto_td = tddft.dRPA(gto_mf) gto_td.nstates = 190 gto_td.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j*0.03 p_ave = -polariz_freq_osc_strength(gto_td.e, gto_td.oscillator_strength(), omegas).imag data = np.array([omegas.real*HARTREE2EV, p_ave]) np.savetxt('test_0163_bse_h2o_spin2_uhf_rpa_pyscf.txt', data.T, fmt=['%f','%f']) data_ref = np.loadtxt('test_0163_bse_h2o_spin2_uhf_rpa_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='RPA') polariz = -nao_td.comp_polariz_inter_ave(omegas).imag data = np.array([omegas.real*HARTREE2EV, polariz]) np.savetxt('test_0163_bse_h2o_spin2_uhf_rpa_nao.txt', data.T, fmt=['%f','%f']) data_ref = np.loadtxt('test_0163_bse_h2o_spin2_uhf_rpa_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-5, rtol=1e-3), \ msg="{}".format(abs(data_ref-data).sum()/data.size))
def test_153_bse_h2b_uks_rpa(self): """ This example is with discrepancies... """ mol = gto.M(verbose=1, atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074', basis='cc-pvdz', spin=3) gto_mf = scf.UKS(mol) gto_mf.kernel() gto_td = tddft.dRPA(gto_mf) gto_td.nstates = 190 gto_td.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j * 0.03 p_ave = -polariz_freq_osc_strength( gto_td.e, gto_td.oscillator_strength(), omegas).imag data = np.array([omegas.real * HARTREE2EV, p_ave]) np.savetxt('test_0153_bse_h2b_uks_rpa_pyscf.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0153_bse_h2b_uks_rpa_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = bse_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='RPA') polariz = -nao_td.comp_polariz_inter_ave(omegas).imag data = np.array([omegas.real * HARTREE2EV, polariz]) np.savetxt('test_0153_bse_h2b_uks_rpa_nao.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0153_bse_h2b_uks_rpa_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
def opt_no(): mol = gto.M(atom=""" N 0.0 0.0 0.0 O 0.0 0.0 1.5 """, basis="sto-3g", spin=1) mf = scf.UKS(mol) mol1 = optimize(mf)
def gga_eng(self): if self._gga_eng is not NotImplemented: return self._gga_eng grids = self.gen_grids(atom_grid=self.atom_grid) gga_eng = scf.UKS(self.mol) gga_eng.grids = grids gga_eng.conv_tol = 1e-11 gga_eng.conv_tol_grad = 1e-9 gga_eng.max_cycle = 100 gga_eng.xc = self.xc self._gga_eng = gga_eng return self._gga_eng
def test_init(self): hf = scf.UHF(mol) ks = scf.UKS(mol) kshf = scf.UKS(mol).set(xc='HF') self.assertTrue(isinstance(tdscf.TDA(hf), tdscf.uhf.TDA)) self.assertTrue(isinstance(tdscf.TDA(ks), tdscf.uks.TDA)) self.assertTrue(isinstance(tdscf.TDA(kshf), tdscf.uks.TDA)) self.assertTrue(isinstance(tdscf.RPA(hf), tdscf.uhf.TDHF)) self.assertTrue(isinstance(tdscf.RPA(ks), tdscf.uks.TDDFTNoHybrid)) self.assertTrue(isinstance(tdscf.RPA(kshf), tdscf.uks.TDDFT)) self.assertTrue(isinstance(tdscf.TDDFT(hf), tdscf.uhf.TDHF)) self.assertTrue(isinstance(tdscf.TDDFT(ks), tdscf.uks.TDDFTNoHybrid)) self.assertTrue(isinstance(tdscf.TDDFT(kshf), tdscf.uks.TDDFT)) self.assertRaises(RuntimeError, tdscf.dRPA, hf) self.assertTrue(isinstance(tdscf.dRPA(kshf), tdscf.uks.dRPA)) self.assertTrue(isinstance(tdscf.dRPA(ks), tdscf.uks.dRPA)) self.assertRaises(RuntimeError, tdscf.dTDA, hf) self.assertTrue(isinstance(tdscf.dTDA(kshf), tdscf.uks.dTDA)) self.assertTrue(isinstance(tdscf.dTDA(ks), tdscf.uks.dTDA))
def Q3OptimizerDriver(pre_energy,mol,method,shell,**conv_params): if pre_energy ==True: opt_pre = optimize(pre_energy, **conv_params) opt_geom = opt_pre.tofile(working_dir+'/opt-geom.xyz') else: if method==KS: if shell == O: SPE =scf.UKS(mol).run() else shell == R: SPE =scf.RKS(mol).run() else method == HF: if shell == O: SPE =scf.UHF(mol).run() else shell == R: SPE =scf.RHF(mol).run()
def calc_energy_h2plus(mol, z): """ Calculate the energy for H2+ at various bond length Input: mol: molecule object of pySCF z(float): z distance between the atoms returns: total energy(float) """ mol.atom = [["H", (0, 0, 0)], ["H", (0, 0, z)]] mol.charge = 1 mol.spin = 1 mol.basis = '6-311+g2dp.nw' #from basis set exchange mol.build() mf = scf.UKS(mol) mf.small_rho_cutoff = 1e-12 mf.xc = 'pbe' mf.kernel() return mf.e_tot
def test_155_tddft_h2b_uks_nonin(self): """ This example is with discrepancies... """ mol = gto.M(verbose=1, atom='B 0 0 0; H 0 0.489 1.074; H 0 0.489 -1.074', basis='cc-pvdz', spin=3) gto_mf = scf.UKS(mol) gto_mf.kernel() omegas = np.arange(0.0, 2.0, 0.01) + 1j * 0.03 p_ave = -polariz_nonin_ave(gto_mf, mol, omegas).imag data = np.array([omegas.real * HARTREE2EV, p_ave]) np.savetxt('test_0155_tddft_h2b_uks_nonin_pyscf.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0155_tddft_h2b_uks_nonin_pyscf.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) nao_td = tddft_iter(mf=gto_mf, gto=mol, verbosity=0, xc_code='RPA') polariz = -nao_td.polariz_nonin_ave_matelem(omegas).imag data = np.array([omegas.real * HARTREE2EV, polariz]) np.savetxt('test_0155_tddft_h2b_uks_nonin_matelem.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt( 'test_0155_tddft_h2b_uks_nonin_matelem.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3)) polariz = -nao_td.comp_polariz_nonin_ave(omegas).imag data = np.array([omegas.real * HARTREE2EV, polariz]) np.savetxt('test_0155_tddft_h2b_uks_nonin_nao.txt', data.T, fmt=['%f', '%f']) data_ref = np.loadtxt('test_0155_tddft_h2b_uks_nonin_nao.txt-ref').T self.assertTrue(np.allclose(data_ref, data, atol=1e-6, rtol=1e-3))
def gen_scf_obj(mol, scf_method, **kwargs): """Generates an scf object setting all relevant parameters for use later in embedding""" if 'unrestricted' in kwargs and kwargs.get('unrestricted'): if 'hf' in scf_method: scf_obj = scf.UHF(mol) else: scf_obj = scf.UKS(mol) scf_obj.xc = scf_method elif mol.spin != 0: if 'hf' in scf_method: scf_obj = scf.ROHF(mol) else: scf_obj = scf.ROKS(mol) scf_obj.xc = scf_method else: if 'hf' in scf_method: scf_obj = scf.RHF(mol) else: scf_obj = scf.RKS(mol) scf_obj.xc = scf_method if 'diis_num' in kwargs: if kwargs.get('diis_num') == 0: scf_obj.DIIS = None if kwargs.get('diis_num') == 1: scf_obj.DIIS = scf.CDIIS if kwargs.get('diis_num') == 2: scf_obj.DIIS = scf.EDIIS if kwargs.get('diis_num') == 3: scf_obj.DIIS = scf.ADIIS if 'grid_level' in kwargs: grids = dft.gen_grid.Grids(mol) grids.level = kwargs.pop('grid_level') grids.build() scf_obj.grids = grids if 'dynamic_level_shift' in kwargs and kwargs.get('dynamic_level_shift'): if 'level_shift_factor' in kwargs: lev_shift_factor = kwargs.pop('level_shift_factor') scf.addons.dynamic_level_shift_(scf_obj, lev_shift_factor) else: scf.addons.dynamic_level_shift_(scf_obj) if 'newton' in kwargs and kwargs.pop('newton'): scf_obj = scf.newton(scf_obj) if 'fast_newton' in kwargs and kwargs.pop('fast_newton'): scf_obj.use_fast_newton = True if 'frac_occ' in kwargs and kwargs.get('frac_occ'): scf_obj = scf.addons.frac_occ(mf) if 'remove_linear_dep' in kwargs and kwargs.get('remove_linear_dep'): scf_obj = scf_obj.apply(scf.addons.remove_linear_dep) if 'density_fitting' in kwargs and kwargs.get('density_fitting'): scf_obj = scf_obj.density_fit() if 'excited' in kwargs: scf_obj.excited = kwargs['excited'] #Setup excited object. pass for key in kwargs: setattr(scf_obj, key, kwargs[key]) return scf_obj
def automatic_guessing(ase_nuclei, charge, spin, basis, xc, method='FB', ecp=None, newton=False, grid=3, BS=None, calc='UKS', symmetry=False, verbose=4): # ase_nuclei_atoms ... ase.atoms.object containg only nuclei positions # charge ... charge of the system # spin ... spin state of the system # basis ... basis set # xc ... exchange-correlation functional # method ... localization method (FB, ER, PM etc.) # Note: FB seems to give very reasonable guesses. # ecp ... effective core potential file # newton ... second order Newton Raphston scf solver works for LDA and GGA not for SCAN # grid ... grid level # BS ... broken symmetry # calc ,,, UKS or UHF # Performe a DFT calculation. method = method.upper() calc = calc.upper() ase_atoms = ase_nuclei if ecp is None: mol = gto.M(atom=ase2pyscf(ase_atoms), basis=basis, spin=spin, charge=charge, symmetry=symmetry) if ecp is not None: mol = gto.M(atom=ase2pyscf(ase_atoms), basis=basis, ecp=ecp, spin=spin, charge=charge, symmetry=symmetry) mol.verbose = verbose if calc == 'UKS': mf = scf.UKS(mol) if calc == 'UHF': mf = scf.UHF(mol) if calc == 'RHF': mf = scf.RHF(mol) mf.grids.level = grid mf.max_cycle = 3000 mf.xc = xc # Broken symmetry if BS != None: mf.kernel() idx_flip = mol.search_ao_label(BS) dma, dmb = mf.make_rdm1() dma_flip = dma[idx_flip.reshape(-1, 1), idx_flip].copy() dmb_flip = dmb[idx_flip.reshape(-1, 1), idx_flip].copy() dma[idx_flip.reshape(-1, 1), idx_flip] = dmb_flip dmb[idx_flip.reshape(-1, 1), idx_flip] = dma_flip dm = [dma, dmb] if ecp is None: mol = gto.M(atom=ase2pyscf(ase_atoms), basis=basis, spin=0, charge=charge, symmetry=symmetry) if ecp is not None: mol = gto.M(atom=ase2pyscf(ase_atoms), basis=basis, ecp=ecp, spin=0, charge=charge, symmetry=symmetry) mol.verbose = verbose mf = scf.UKS(mol) mf.grids.level = grid mf.max_cycle = 3000 mf.xc = xc if newton == True: mf = mf.as_scanner() mf = mf.newton() if BS == None: mf.kernel() if BS != None: mf.run(dm) if calc == 'RHF': mf = scf.addons.convert_to_uhf(mf) # Performe a localization calculation. # Both spin channels are localized separately. # The orbitals are written out as cube files. calc_localized_orbitals(mf, mol, method=method) # Collect all cube files per spin channel. f1 = glob.glob('*orb*spin1.cube') f2 = glob.glob('*orb*spin2.cube') # test for nuclei positions # for ASE Version: 3.15.1b1 # we neede the file handle and not the string # new: f_cube = open(f1[0]) ase_atoms = cube.read_cube(f_cube) # previous: ase_atoms = cube.read_cube(f1[0]) f_cube.close() # Calculate the guess. get_guess(atoms=ase_atoms, spin1_cube=f1, spin2_cube=f2, method=method)
basis='ccpvdz', spin=1, charge=1, verbose=3) mf = dft.UKS(mol).run() hfc = HFC(mf) hfc.verbose = 4 hfc.so_eff_charge = False print(lib.finger(hfc.kernel()) - 255.92807696823797) mol = gto.M(atom='H 0 0 0; H 0 0 1.', basis='ccpvdz', spin=1, charge=-1, verbose=3) mf = scf.UKS(mol).run() hfc = HFC(mf) hfc.cphf = True print(lib.finger(hfc.kernel()) - -25.896662045941071) mol = gto.M(atom=''' Li 0 0 1 ''', basis='ccpvdz', spin=1, charge=0, verbose=3) mf = scf.UKS(mol).run() hfc = HFC(mf) hfc.cphf = True print(lib.finger(hfc.kernel()) - 65.396568554095523)
from pyscf import gto from pyscf import scf ''' Applying scalar relativistic effects by decorating the scf object with scf.sfx2c function. Similar to the density fitting decoration, the relativistic effects can also be applied without affecting the existed scf object. ''' mol = gto.Mole() mol.build( verbose=0, atom='''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.sfx2c(scf.RHF(mol)) energy = mf.kernel() print('E = %.12f, ref = -76.075429084850' % energy) mol.spin = 1 mol.charge = 1 mol.build(0, 0) mf = scf.sfx2c(scf.UKS(mol)) energy = mf.kernel() print('E = %.12f, ref = -75.439160951099' % energy)
be applied without affecting the existed scf object. ''' from pyscf import gto from pyscf import scf mol = gto.M( verbose=0, atom='''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.RHF(mol).x2c().run() print('E = %.12f, ref = -76.075429084850' % mf.e_tot) mol.spin = 1 mol.charge = 1 mol.build(0, 0) # .x2c1e is the same to the .x2c method in the current implementation mf = scf.UKS(mol).x2c1e() energy = mf.kernel() print('E = %.12f, ref = -75.439160951099' % energy) # Switch off x2c mf.with_x2c = False energy = mf.kernel() print('E = %.12f, ref = %.12f' % (energy, scf.UKS(mol).kernel()))
Applying scalar relativistic effects by decorating the scf object with scf.sfx2c function. Similar to the density fitting decoration, the relativistic effects can also be applied without affecting the existed scf object. ''' mol = gto.Mole() mol.build( verbose = 0, atom = '''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis = 'ccpvdz', ) mf = scf.sfx2c(scf.RHF(mol)) energy = mf.kernel() print('E = %.12f, ref = -76.075429084850' % energy) mol.spin = 1 mol.charge = 1 mol.build(0, 0) mf = scf.UKS(mol).x2c() # Using stream style energy = mf.kernel() print('E = %.12f, ref = -75.439160951099' % energy)
def test_init(self): from pyscf import dft from pyscf import x2c mol_r = mol mol_u = gto.M(atom='Li', spin=1, verbose=0) mol_r1 = gto.M(atom='H', spin=1, verbose=0) sym_mol_r = molsym sym_mol_u = gto.M(atom='Li', spin=1, symmetry=1, verbose=0) sym_mol_r1 = gto.M(atom='H', spin=1, symmetry=1, verbose=0) self.assertTrue(isinstance(scf.RKS(mol_r), dft.rks.RKS)) self.assertTrue(isinstance(scf.RKS(mol_u), dft.roks.ROKS)) self.assertTrue(isinstance(scf.UKS(mol_r), dft.uks.UKS)) self.assertTrue(isinstance(scf.ROKS(mol_r), dft.roks.ROKS)) self.assertTrue(isinstance(scf.GKS(mol_r), dft.gks.GKS)) self.assertTrue(isinstance(scf.KS(mol_r), dft.rks.RKS)) self.assertTrue(isinstance(scf.KS(mol_u), dft.uks.UKS)) self.assertTrue(isinstance(scf.RHF(mol_r), scf.hf.RHF)) self.assertTrue(isinstance(scf.RHF(mol_u), scf.rohf.ROHF)) self.assertTrue(isinstance(scf.RHF(mol_r1), scf.rohf.ROHF)) self.assertTrue(isinstance(scf.UHF(mol_r), scf.uhf.UHF)) self.assertTrue(isinstance(scf.UHF(mol_u), scf.uhf.UHF)) self.assertTrue(isinstance(scf.UHF(mol_r1), scf.uhf.HF1e)) self.assertTrue(isinstance(scf.ROHF(mol_r), scf.rohf.ROHF)) self.assertTrue(isinstance(scf.ROHF(mol_u), scf.rohf.ROHF)) self.assertTrue(isinstance(scf.ROHF(mol_r1), scf.rohf.HF1e)) self.assertTrue(isinstance(scf.HF(mol_r), scf.hf.RHF)) self.assertTrue(isinstance(scf.HF(mol_u), scf.uhf.UHF)) self.assertTrue(isinstance(scf.HF(mol_r1), scf.rohf.HF1e)) self.assertTrue(isinstance(scf.GHF(mol_r), scf.ghf.GHF)) self.assertTrue(isinstance(scf.GHF(mol_u), scf.ghf.GHF)) self.assertTrue(isinstance(scf.GHF(mol_r1), scf.ghf.HF1e)) #TODO: self.assertTrue(isinstance(scf.DHF(mol_r), scf.dhf.RHF)) self.assertTrue(isinstance(scf.DHF(mol_u), scf.dhf.UHF)) self.assertTrue(isinstance(scf.DHF(mol_r1), scf.dhf.HF1e)) self.assertTrue(isinstance(scf.RHF(sym_mol_r), scf.hf_symm.RHF)) self.assertTrue(isinstance(scf.RHF(sym_mol_u), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.RHF(sym_mol_r1), scf.hf_symm.HF1e)) self.assertTrue(isinstance(scf.UHF(sym_mol_r), scf.uhf_symm.UHF)) self.assertTrue(isinstance(scf.UHF(sym_mol_u), scf.uhf_symm.UHF)) self.assertTrue(isinstance(scf.UHF(sym_mol_r1), scf.uhf_symm.HF1e)) self.assertTrue(isinstance(scf.ROHF(sym_mol_r), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.ROHF(sym_mol_u), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.ROHF(sym_mol_r1), scf.hf_symm.HF1e)) self.assertTrue(isinstance(scf.HF(sym_mol_r), scf.hf_symm.RHF)) self.assertTrue(isinstance(scf.HF(sym_mol_u), scf.uhf_symm.UHF)) self.assertTrue(isinstance(scf.HF(sym_mol_r1), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.GHF(sym_mol_r), scf.ghf_symm.GHF)) self.assertTrue(isinstance(scf.GHF(sym_mol_u), scf.ghf_symm.GHF)) self.assertTrue(isinstance(scf.GHF(sym_mol_r1), scf.ghf_symm.HF1e)) self.assertTrue(isinstance(scf.X2C(mol_r), x2c.x2c.UHF)) self.assertTrue(isinstance(scf.sfx2c1e(scf.HF(mol_r)), scf.rhf.RHF)) self.assertTrue(isinstance(scf.sfx2c1e(scf.HF(mol_u)), scf.uhf.UHF)) self.assertTrue(isinstance(scf.sfx2c1e(scf.HF(mol_r1)), scf.rohf.ROHF)) self.assertTrue( isinstance(scf.sfx2c1e(scf.HF(sym_mol_r)), scf.rhf_symm.RHF)) self.assertTrue( isinstance(scf.sfx2c1e(scf.HF(sym_mol_u)), scf.uhf_symm.UHF)) self.assertTrue( isinstance(scf.sfx2c1e(scf.HF(sym_mol_r1)), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.density_fit(scf.HF(mol_r)), scf.rhf.RHF)) self.assertTrue(isinstance(scf.density_fit(scf.HF(mol_u)), scf.uhf.UHF)) self.assertTrue( isinstance(scf.density_fit(scf.HF(mol_r1)), scf.rohf.ROHF)) self.assertTrue( isinstance(scf.density_fit(scf.HF(sym_mol_r)), scf.rhf_symm.RHF)) self.assertTrue( isinstance(scf.density_fit(scf.HF(sym_mol_u)), scf.uhf_symm.UHF)) self.assertTrue( isinstance(scf.density_fit(scf.HF(sym_mol_r1)), scf.hf_symm.ROHF)) self.assertTrue(isinstance(scf.newton(scf.HF(mol_r)), scf.rhf.RHF)) self.assertTrue(isinstance(scf.newton(scf.HF(mol_u)), scf.uhf.UHF)) self.assertTrue(isinstance(scf.newton(scf.HF(mol_r1)), scf.rohf.ROHF)) self.assertTrue( isinstance(scf.newton(scf.HF(sym_mol_r)), scf.rhf_symm.RHF)) self.assertTrue( isinstance(scf.newton(scf.HF(sym_mol_u)), scf.uhf_symm.UHF)) self.assertTrue( isinstance(scf.newton(scf.HF(sym_mol_r1)), scf.hf_symm.ROHF))
''' Second order SCF algorithm by decorating the scf object with scf.newton function. (New in PySCF-1.1) Second order SCF method need orthonormal orbitals and the corresponding occupancy as the initial guess. ''' mol = gto.Mole() mol.build( verbose=0, atom='''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.RHF(mol) mf.conv_tol = 1e-1 mf.kernel() mo_init = mf.mo_coeff mocc_init = mf.mo_occ mf = scf.newton(scf.RHF(mol)) energy = mf.kernel(mo_init, mocc_init) print('E = %.12f, ref = -76.026765672992' % energy) mf = scf.newton(scf.UKS(mol)) energy = mf.kernel((mo_init, mo_init), (mocc_init * .5, mocc_init * .5)) print('E = %.12f, ref = -75.854689662296' % energy)
NOTE scf.density_fit function generates a new object, which works exactly the same way as the regular scf method. The density fitting scf object is an independent object to the regular scf object which is to be decorated. By doing so, density fitting can be applied anytime, anywhere in your script without affecting the exsited scf object. ''' mol = gto.Mole() mol.build( verbose=0, atom='''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.density_fit(scf.RHF(mol)) energy = mf.kernel() print('E = %.12f, ref = -76.0259362997' % energy) mol.spin = 1 mol.charge = 1 mol.build(0, 0) mf = scf.density_fit(scf.UKS(mol)) # the default auxiliary basis is Weigend Coulomb Fitting basis. mf.auxbasis = 'cc-pvdz-fit' energy = mf.kernel() print('E = %.12f, ref = -75.390940646297' % energy)
dmb = np.zeros((nao, nao)) dma[idx_h0_1s, idx_h0_1s] = dmb[idx_h1_1s, idx_h1_1s] = dma[idx_h0_2s, idx_h0_2s] = dmb[idx_h1_2s, idx_h1_2s] = 1 dm0 = [dma, dmb] # Restricted mean-field base of MC-SCF objects mf = scf.RHF(mol).run() # 'Full CI' object fci = ci.CISD(mf).run().as_scanner() # UKS comparison (initial guess must break symmetry!) pks = scf.UKS(mol) pks.xc = fnal pks.kernel(dm0) pks = pks.as_scanner() hks = scf.UKS(mol) hks.xc = kshfnal hks.kernel(dm0) hks = hks.as_scanner() # MC-PDFT objects if gsbasis: gsmo = np.load(gsmofile) mc = mcpdft.CASSCF(mf, transl_type + fnal, ncas, nelecas, grids_level=3) mo = mcscf.project_init_guess(mc, gsmo, prev_mol=gsmol) molden.from_mo(mol, 'check_projection.molden', mo) mc.kernel(mo)
) mf = scf.density_fit(scf.RHF(mol)) energy = mf.kernel() print('E = %.12f, ref = -76.026744737355' % energy) # # Stream style: calling .density_fit method to return a DF-SCF object. # mf = scf.RHF(mol).density_fit() energy = mf.kernel() print('E = %.12f, ref = -76.026744737355' % energy) # # By default optimal auxiliary basis (if possible) or even-tempered gaussian # functions are used fitting basis. You can assign with_df.auxbasis to change # the change the fitting basis. # mol.spin = 1 mol.charge = 1 mol.build(0, 0) mf = scf.UKS(mol).density_fit() mf.with_df.auxbasis = 'cc-pvdz-jkfit' energy = mf.kernel() print('E = %.12f, ref = -75.390366559552' % energy) # Switch off density fitting mf.with_df = False energy = mf.kernel() print('E = %.12f, ref = %.12f' % (energy, scf.UKS(mol).kernel()))
def calculate(self, atoms, properties=['energy'], system_changes=['positions']): self.num_iter += 1 atoms = self.get_atoms() self.atoms = atoms Calculator.calculate(self, atoms, properties, system_changes) if self.mode == 'dft': # DFT only mode from pyscf import gto, scf, grad, dft [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) nuclei = ase2pyscf(nuclei) mol = gto.M(atom=nuclei, basis=self.basis, spin=self.spin, charge=self.charge) mf = scf.UKS(mol) mf.xc = self.xc # Verbosity of the mol object (o lowest output, 4 might enough output for debugging) mf.verbose = self.verbose mf.max_cycle = self.max_cycle mf.conv_tol = self.conv_tol mf.grids.level = self.grid if self.n_rad is not None and self.n_ang is not None: mf.grids.atom_grid = (self.n_rad, self.n_ang) mf.grids.prune = prune_dict[self.prune] e = mf.kernel() self.mf = mf self.results['energy'] = e * Ha self.results['dipole'] = dipole = mf.dip_moment(verbose=0) self.results['evalues'] = mf.mo_energy if self.mode == 'flosic-os': # FLOSIC SCF mode from pyscf import gto, scf [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) # FLOSIC one shot mode #mf = flosic(self.atoms,charge=self.charge,spin=self.spin,xc=self.xc,basis=self.basis,debug=False,verbose=self.verbose) # Effective core potentials need so special treatment. if self.ecp == None: if self.ghost == False: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge) if self.ghost == True: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge) mol.basis = { 'default': self.basis, 'GHOST1': gto.basis.load('sto3g', 'H'), 'GHOST2': gto.basis.load('sto3g', 'H') } if self.ecp != None: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge, ecp=self.ecp) mf = scf.UKS(mol) mf.xc = self.xc # Verbosity of the mol object (o lowest output, 4 might enough output for debugging) mf.verbose = self.verbose # Binary output format of pyscf. # Save MOs, orbital energies, etc. if self.use_chk == True and self.use_newton == False: mf.chkfile = 'pyflosic.chk' # Load from previous run, if exist, the checkfile. # Hopefully this will speed up the calculation. if self.use_chk == True and self.use_newton == False and os.path.isfile( 'pyflosic.chk'): mf.init_guess = 'chk' mf.update('pyflosic.chk') if self.use_newton == True: mf = mf.as_scanner() mf = mf.newton() mf.max_cycle = self.max_cycle mf.conv_tol = self.conv_tol mf.grids.level = self.grid if self.n_rad is not None and self.n_ang is not None: mf.grids.atom_grid = (self.n_rad, self.n_ang) mf.grids.prune = prune_dict[self.prune] e = mf.kernel() self.mf = mf mf = flosic(mol, mf, fod1, fod2, sysname=None, datatype=np.float64, print_dm_one=False, print_dm_all=False, debug=self.debug, calc_forces=True, ham_sic=self.ham_sic) self.results['energy'] = mf['etot_sic'] * Ha # unit conversion from Ha/Bohr to eV/Ang #self.results['fodforces'] = -1*mf['fforces']/(Ha/Bohr) self.results['fodforces'] = -1 * mf['fforces'] * (Ha / Bohr) print('Analytical FOD force [Ha/Bohr]') print(mf['fforces']) print('fmax = %0.6f [Ha/Bohr]' % np.sqrt( (mf['fforces']**2).sum(axis=1).max())) self.results['dipole'] = mf['dipole'] self.results['evalues'] = mf['evalues'] if self.mode == 'flosic-scf': #if self.mf is None: # FLOSIC SCF mode from pyscf import gto [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) # Effective core potentials need so special treatment. if self.ecp == None: if self.ghost == False: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge) if self.ghost == True: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge) mol.basis = { 'default': self.basis, 'GHOST1': gto.basis.load('sto3g', 'H'), 'GHOST2': gto.basis.load('sto3g', 'H') } if self.ecp != None: mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis, spin=self.spin, charge=self.charge, ecp=self.ecp) if self.efield != None: m0 = FLOSIC(mol=mol, xc=self.xc, fod1=fod1, fod2=fod2, grid_level=self.grid, debug=self.debug, l_ij=self.l_ij, ods=self.ods, fixed_vsic=self.fixed_vsic, num_iter=self.num_iter, vsic_every=self.vsic_every, ham_sic=self.ham_sic) # test efield to enforce some pseudo chemical environment # and break symmetry of density m0.grids.level = self.grid m0.conv_tol = self.conv_tol # small efield m0.max_cycle = 1 h = -0.0001 #-0.1 apply_field(mol, m0, E=(0, 0, 0 + h)) m0.kernel() mf = FLOSIC(mol=mol, xc=self.xc, fod1=fod1, fod2=fod2, grid_level=self.grid, calc_forces=self.calc_forces, debug=self.debug, l_ij=self.l_ij, ods=self.ods, fixed_vsic=self.fixed_vsic, num_iter=self.num_iter, vsic_every=self.vsic_every, ham_sic=self.ham_sic) # Verbosity of the mol object (o lowest output, 4 might enough output for debugging) mf.verbose = self.verbose # Binary output format of pyscf. # Save MOs, orbital energies, etc. if self.use_chk == True and self.use_newton == False: mf.chkfile = 'pyflosic.chk' # Load from previous run, if exist, the checkfile. # Hopefully this will speed up the calculation. if self.use_chk == True and self.use_newton == False and os.path.isfile( 'pyflosic.chk'): mf.init_guess = 'chk' mf.update('pyflosic.chk') if self.use_newton == True: mf = mf.as_scanner() mf = mf.newton() mf.max_cycle = self.max_cycle mf.conv_tol = self.conv_tol mf.grids.level = self.grid if self.n_rad is not None and self.n_ang is not None: mf.grids.atom_grid = (self.n_rad, self.n_ang) mf.calc_uks.grids.atom_grid = (self.n_rad, self.n_ang) mf.grids.prune = prune_dict[self.prune] mf.calc_uks.grids.prune = prune_dict[self.prune] e = mf.kernel() self.mf = mf # Return some results to the pyflosic_ase_caculator object. self.results['esic'] = mf.esic * Ha self.results['energy'] = e * Ha self.results['fixed_vsic'] = mf.fixed_vsic # if self.mf is not None: # from pyscf import gto # [geo,nuclei,fod1,fod2,included] = xyz_to_nuclei_fod(self.atoms) # # Effective core potentials need so special treatment. # if self.ecp == None: # if self.ghost == False: # mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis,spin=self.spin,charge=self.charge) # if self.ghost == True: # mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis,spin=self.spin,charge=self.charge) # mol.basis ={'default':self.basis,'GHOST1':gto.basis.load('sto3g', 'H'),'GHOST2':gto.basis.load('sto3g', 'H')} # if self.ecp != None: # mol = gto.M(atom=ase2pyscf(nuclei), basis=self.basis,spin=self.spin,charge=self.charge,ecp=self.ecp) # self.mf.num_iter = self.num_iter # self.mf.max_cycle = self.max_cycle # self.mf.mol = mol # self.mf.fod1 = fod1 # self.mf.fod2 = fod2 # e = self.mf.kernel() # # Return some results to the pyflosic_ase_caculator object. # self.results['esic'] = self.mf.esic*Ha # self.results['energy'] = e*Ha # self.results['fixed_vsic'] = self.mf.fixed_vsic # if self.fopt == 'force' or self.fopt == 'esic-force': # # The standard optimization uses # the analytical FOD forces # fforces = self.mf.get_fforces() #fforces = -1*fforce # unit conversion Hartree/Bohr to eV/Angstroem #self.results['fodforces'] = -1*fforces*(Ha/Bohr) self.results['fodforces'] = fforces * (Ha / Bohr) print('Analytical FOD force [Ha/Bohr]') print(fforces) print('fmax = %0.6f [Ha/Bohr]' % np.sqrt( (fforces**2).sum(axis=1).max())) if self.fopt == 'lij': # # This is under development. # Trying to replace the FOD forces. # self.lambda_ij = self.mf.lambda_ij self.results['lambda_ij'] = self.mf.lambda_ij #fforces = [] #nspin = 2 #for s in range(nspin): # # printing the lampda_ij matrix for both spin channels # print 'lambda_ij' # print lambda_ij[s,:,:] # print 'RMS lambda_ij' # M = lambda_ij[s,:,:] # fforces_tmp = (M-M.T)[np.triu_indices((M-M.T).shape[0])] # fforces.append(fforces_tmp.tolist()) #print np.array(fforces).shape try: # # Try to calculate the FOD forces from the differences # of SIC eigenvalues # evalues_old = self.results['evalues'] print(evalues_old) evalues_new = self.mf.evalues print(evalues_new) delta_evalues_up = (evalues_old[0][0:len(fod1)] - evalues_new[0][0:len(fod1)]).tolist() delta_evalues_dn = (evalues_old[1][0:len(fod2)] - evalues_new[1][0:len(fod2)]).tolist() print(delta_evalues_up) print(delta_evalues_dn) lij_force = delta_evalues_up lij_force.append(delta_evalues_dn) lij_force = np.array(lij_force) lij_force = np.array(lij_force, (np.shape(lij_force)[0], 3)) print('FOD force evalued from evalues') print(lij_force) self.results['fodforces'] = lij_force except: # # If we are in the first iteration # we can still use the analystical FOD forces # as starting values # fforces = self.mf.get_fforces() print(fforces) #self.results['fodforces'] = -1*fforces*(Ha/Bohr) self.results['fodforces'] = fforces * (Ha / Bohr) print('Analytical FOD force [Ha/Bohr]') print(fforces) print('fmax = %0.6f [Ha/Bohr]' % np.sqrt( (fforces**2).sum(axis=1).max())) self.results['dipole'] = self.mf.dip_moment() self.results['evalues'] = self.mf.evalues if atoms is not None: self.atoms = atoms.copy()
1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.RHF(mol) mf.conv_tol = 1e-1 mf.kernel() mo_init = mf.mo_coeff mocc_init = mf.mo_occ mf = scf.newton(scf.RHF(mol)) energy = mf.kernel(mo_init, mocc_init) print('E = %.12f, ref = -76.026765672992' % energy) mf = scf.UKS(mol).newton() # Using stream style # The newton algorithm will automatically generate initial orbitals if initial # guess is not given. energy = mf.kernel() print('E = %.12f, ref = -75.854702461713' % energy) # Note You should first set mf.xc then apply newton method because this will # correctly set up the underlying orbital gradients. If you first create # mf = mf.newton() then change mf.xc, the orbital Hessian will be computed # with the updated xc functional while the orbital gradients are computed with # the old xc functional. # In some scenario, you can use this character to approximate the # orbital Hessian, ie, computing the orbital Hessian with approximated XC # functional if the accurate Hessian is not applicable. mf = scf.UKS(mol) mf.xc = 'pbe,pbe'
def pycom_guess(ase_nuclei, charge, spin, basis, xc, method='FB', ecp=None, newton=False, grid=3, BS=None, calc='UKS', symmetry=False, verbose=4, write_cubes=False, stability_analysis=True): ''' generate PyCOM guess Args: ase_nuclei: ase_atoms object charge: float charge of the system spin: integer 2S of the system xc: string exchange-correlation functional basis: string basis set to be used method: string, e.g., FB (prefered), ER , PM localization method to be used ecp: string, for effective core potentials newton: bool, True or False use 2nd order scf grid: integer, 0 - 9 corresponds to pyscf grids.level BS: bool, None or True generate a broken symmetry ground state calc: string, UKS, UHF, RHF computational method symmetry: bool, True or False use symmetry for pyscf mol verbose: integer, 4 (default) corresponds to pyscf verbosity level write_cubes: bool, True or False True is the original slow PyCOM method False is the new fast PyCOM method stability analysis : bool, True or False True performs a simple stability analysis, tested for FB pc0 Returns: a guess for Fermi-orbital descriptors as xyz file ''' method = method.upper() calc = calc.upper() mol = gto.M(atom=ase2pyscf(ase_nuclei), basis=basis, ecp=ecp, spin=spin, charge=charge, symmetry=symmetry) mol.verbose = verbose if calc == 'UKS': mf = scf.UKS(mol) elif calc == 'UHF': mf = scf.UHF(mol) elif calc == 'RHF': mf = scf.RHF(mol) mf.grids.level = grid mf.max_cycle = 3000 mf.xc = xc # Broken symmetry if BS != None: mf.kernel() idx_flip = mol.search_ao_label(BS) dma, dmb = mf.make_rdm1() dma_flip = dma[idx_flip.reshape(-1, 1), idx_flip].copy() dmb_flip = dmb[idx_flip.reshape(-1, 1), idx_flip].copy() dma[idx_flip.reshape(-1, 1), idx_flip] = dmb_flip dmb[idx_flip.reshape(-1, 1), idx_flip] = dma_flip dm = [dma, dmb] mol = gto.M(atom=ase2pyscf(ase_nuclei), basis=basis, ecp=ecp, spin=0, charge=charge, symmetry=symmetry) mol.verbose = verbose mf = scf.UKS(mol) mf.grids.level = grid mf.max_cycle = 3000 mf.xc = xc if newton == True: mf = mf.as_scanner() mf = mf.newton() if BS == None: mf.kernel() else: mf.run(dm) if calc == 'RHF': mf = scf.addons.convert_to_uhf(mf) # generate parameters object p = parameters() p.nuclei = ase_nuclei p.pycom_loc = method # use simple stability analysis p.stability_analysis = stability_analysis p.write_cubes = write_cubes pc = pycom(mf=mf, p=p) pc.kernel()
# # Author: Qiming Sun <*****@*****.**> # from pyscf import gto, scf, dft, mcscf ''' CASSCF can be called with UHF/UKS objects. The UHF/UKS orbitals will be used as initial guess for CASSCF. But the CASSCF solver is the RHF-based CASSCF which assumes the degeneracy between alpha orbitals and beta orbitals for the core orbitals. ''' mol = gto.M(atom='N 0 0 0; N 0 0 1.2', basis='ccpvdz', symmetry=1) mf = scf.UHF(mol) mf.kernel() #mf.analyze() # 6 orbitals, 6 electrons mc = mcscf.CASSCF(mf, 6, 6) e = mc.kernel()[0] print('CASSCF based on UHF, E = %.12f, ref = -109.075063732553' % e) mf = scf.UKS(mol) mf.kernel() #mf.analyze() # 6 orbitals, 6 electrons mc = mcscf.CASSCF(mf, 6, 6) e = mc.kernel()[0] print('CASSCF based on UKS, E = %.12f, ref = -109.075063732553' % e)
verbose=0, atom='''8 0 0. 0 1 0 -0.757 0.587 1 0 0.757 0.587''', basis='ccpvdz', ) mf = scf.density_fit(scf.RHF(mol)) energy = mf.kernel() print('E = %.12f, ref = -76.026744737355' % energy) # # Stream style: calling .density_fit method to return a DF-SCF object. # mf = scf.RHF(mol).density_fit() energy = mf.kernel() print('E = %.12f, ref = -76.026744737355' % energy) # # By default optimal auxiliary basis (if possible) or even-tempered gaussian # functions are used fitting basis. You can assign with_df.auxbasis to change # the change the fitting basis. # mol.spin = 1 mol.charge = 1 mol.build(0, 0) mf = scf.UKS(mol).density_fit() mf.with_df.auxbasis = 'cc-pvdz-jkfit' energy = mf.kernel() print('E = %.12f, ref = -75.390366559552' % energy)
self.mo10, self.mo_e10 = self.solve_mo1() mo10 = self.mo10 return para(self, mo10, mo_coeff, mo_occ) #make_para_soc2e = make_para_soc2e get_fock = uks_nmr.get_fock if __name__ == '__main__': from pyscf import gto, scf mol = gto.M(atom='H 0 0.1 0; H 0 0 1.', basis='ccpvdz', spin=1, charge=-1, verbose=3) mf = scf.UKS(mol).set(xc='bp86').run() esr_obj = ESR(mf) esr_obj.gauge_orig = (0, 0, 0) esr_obj.para_soc2e = False esr_obj.so_eff_charge = True print(esr_obj.kernel()) mol = gto.M(atom=''' H 0 0 1 H 1.2 0 1 H .1 1.1 0.3 H .8 .7 .6 ''', basis='ccpvdz', spin=1, charge=1,
def get_forces(self, atoms=None): if atoms != None: self.atoms = atoms # get nuclei and FOD forces # calculates forces if required if self.atoms == None: self.atoms = atoms # Note: The gradients for UKS are only available in the dev branch of pyscf. if self.mode == 'dft' or self.mode == 'both': from pyscf.grad import uks if self.mf == None: from pyscf import gto, scf, dft [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) nuclei = ase2pyscf(nuclei) mol = gto.M(atom=nuclei, basis=self.basis, spin=self.spin, charge=self.charge) mf = scf.UKS(mol) mf.xc = self.xc mf.conv_tol = self.conv_tol mf.max_cycle = self.max_cycle mf.verbose = self.verbose mf.grids.level = self.grid if self.n_rad is not None and self.n_ang is not None: mf.grids.atom_grid = (self.n_rad, self.n_ang) mf.grids.prune = prune_dict[self.prune] if self.xc == 'LDA,PW' or self.xc == 'PBE,PBE': # The 2nd order scf cycle (Newton) speed up calculations, # but does not work for MGGAs like SCAN,SCAN. mf = mf.as_scanner() mf = mf.newton() mf.kernel() self.mf = mf gf = uks.Gradients(mf) forces = gf.kernel() #if self.mf != None: # gf = uks.Gradients(self.mf) # forces = gf.kernel() gf = uks.Gradients(self.mf) forces = gf.kernel() * (Ha / Bohr) #print(forces) if self.mode == 'flosic-os' or self.mode == 'flosic-scf': [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) forces = np.zeros_like(nuclei.get_positions()) if self.mode == 'dft': # mode for nuclei only optimization (fods fixed) forces = forces.tolist() totalforces = [] totalforces.extend(forces) [geo, nuclei, fod1, fod2, included] = xyz_to_nuclei_fod(self.atoms) fod1forces = np.zeros_like(fod1.get_positions()) fod2forces = np.zeros_like(fod2.get_positions()) totalforces.extend(fod1forces) totalforces.extend(fod2forces) totalforces = np.array(totalforces) # pyscf gives the gradient not the force totalforces = -1 * totalforces if self.mode == 'flosic-os' or self.mode == 'flosic-scf': # mode for FOD only optimization (nuclei fixed) if self.results['fodforces'] is None: fodforces = self.get_fodforces(self.atoms) fodforces = self.results['fodforces'] # fix nuclei with zeroing the forces forces = forces forces = forces.tolist() totalforces = [] totalforces.extend(forces) totalforces.extend(fodforces) totalforces = np.array(totalforces) if self.mode == 'both': # mode for both (nuclei+fods) optimzation if self.results['fodforces'] is None: fodforces = self.get_fodforces(self.atoms) fodforces = self.results['fodforces'] forces = forces.tolist() totalforces = [] totalforces.extend(forces) totalforces.extend(fodforces) totalforces = np.array(totalforces) return totalforces