def test_DipoleDerivGenerator_by_SCF(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2() mol = H2O2.mol hf_eng = H2O2.hf_eng hf_eng.kernel() def mf_func(t, interval): mf = scf.RHF(mol) mf.get_hcore = lambda mol_: scf.rhf.get_hcore(mol_) - interval * mol_.intor("int1e_r")[t] return mf.kernel() generator = DipoleDerivGenerator(mf_func) diff = NumericDiff(generator) dip_nuc = np.einsum('i,ix->x', mol.atom_charges(), mol.atom_coords()) assert(np.allclose( diff.derivative + dip_nuc, hf_eng.dip_moment(unit="A.U."), atol=1e-6, rtol=1e-4 )) generator = DipoleDerivGenerator(mf_func, stencil=5) diff = NumericDiff(generator) assert(np.allclose( diff.derivative + dip_nuc, hf_eng.dip_moment(unit="A.U."), atol=1e-6, rtol=1e-4 ))
def test_NucCoordDerivGenerator_by_SCFgrad(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2() mol = H2O2.mol hf_grad = H2O2.hf_grad generator = NucCoordDerivGenerator(mol, lambda mol_: scf.RHF(mol_).run()) diff = NumericDiff(generator, lambda mf: mf.kernel()) assert(np.allclose( hf_grad.kernel(), diff.derivative.reshape(mol.natm, 3), atol=1e-6, rtol=1e-4 )) generator = NucCoordDerivGenerator(mol, lambda mol_: scf.RHF(mol_).run(), stencil=5) diff = NumericDiff(generator, lambda mf: mf.kernel()) assert(np.allclose( hf_grad.kernel(), diff.derivative.reshape(mol.natm, 3), atol=1e-6, rtol=1e-4 ))
import pickle from pyscf import scf, gto, dft from pyxdh.Utilities import NumericDiff, NucCoordDerivGenerator def mol_to_eng(mol): grids = dft.Grids(mol) grids.atom_grid = (99, 590) grids.build() scf_eng = scf.RHF(mol) scf_eng.conv_tol_grad = 1e-8 scf_eng.run() nc_eng = dft.RKS(mol, xc="B3LYPg") nc_eng.grids = grids return nc_eng.energy_tot(dm=scf_eng.make_rdm1()) if __name__ == '__main__': name = __file__.split("/")[-1].split(".")[0] mol = gto.Mole(atom="N 0. 0. 0.; H .9 0. 0.; H 0. 1. 0.; H 0. 0. 1.1", basis="6-31G", verbose=0).build() num_obj = NucCoordDerivGenerator(mol, mol_to_eng) num_dif = NumericDiff(num_obj).derivative with open(name + ".dat", "wb") as f: pickle.dump(num_dif, f, pickle.HIGHEST_PROTOCOL)
import pickle from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import NumericDiff, NucCoordDerivGenerator from pyxdh.DerivOnce import GradMP2 def mol_to_grad_helper(mol): print("Processing...") H2O2 = Mol_H2O2(mol=mol, xc="0.53*HF + 0.47*B88, 0.73*LYP") config = { "scf_eng": H2O2.gga_eng, "cc": 0.27 } helper = GradMP2(config) return helper if __name__ == '__main__': result_dict = {} H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") mol = H2O2.mol num_obj = NucCoordDerivGenerator(H2O2.mol, mol_to_grad_helper) num_dif = NumericDiff(num_obj, lambda helper: helper.E_1.reshape(-1)) result_dict["hess"] = num_dif.derivative with open("mp2_hessian_b2plyp.dat", "wb") as f: pickle.dump(result_dict, f, pickle.HIGHEST_PROTOCOL)
mol = H2O2.mol def get_hcore(mol=mol): return scf.rhf.get_hcore( mol) - interval * mol.intor("int1e_r")[component] H2O2.hf_eng.get_hcore = get_hcore H2O2.gga_eng.get_hcore = get_hcore config = {"scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng} helper = GradNCDFT(config) return helper if __name__ == '__main__': result_dict = {} H2O2 = Mol_H2O2() mol = H2O2.mol num_obj = NucCoordDerivGenerator(H2O2.mol, mol_to_grad_helper) num_dif = NumericDiff(num_obj, lambda helper: helper.eng) result_dict["grad"] = num_dif.derivative num_obj = DipoleDerivGenerator(dipole_generator) num_dif = NumericDiff(num_obj, lambda helper: helper.eng) dip_nuc = np.einsum("A, At -> t", mol.atom_charges(), mol.atom_coords()) result_dict["dipole"] = num_dif.derivative + dip_nuc with open("ncdft_derivonce_hf_b3lyp.dat", "wb") as f: pickle.dump(result_dict, f, pickle.HIGHEST_PROTOCOL)
N 0. 0. 0. H 1.5 0. 0.2 H 0.1 1.2 0. H 0. 0. 1. """ mol.basis = "cc-pVDZ" mol.verbose = 0 mol.build() def mol_to_eng_true(mol): mf_scf = scf.RHF(mol).density_fit(auxbasis="cc-pVDZ-jkfit").run() mf_mp2 = mp.MP2(mf_scf) mf_mp2.with_df = df.DF(mol, auxbasis="cc-pVDZ-ri") return mf_mp2.run().e_tot def mol_to_eng_false(mol): mf_scf = scf.RHF(mol).density_fit(auxbasis="cc-pVDZ-jkfit").run() mf_mp2 = mp.MP2(mf_scf) return mf_mp2.run().e_tot # Check deviation of using jkfit and ri auxbasis in MP2 gradient num_true = NucCoordDerivGenerator(mol, mol_to_eng_true) num_false = NucCoordDerivGenerator(mol, mol_to_eng_false) val_true = NumericDiff(num_true).derivative val_false = NumericDiff(num_false).derivative assert (np.allclose(val_true, val_false, atol=1e-5, rtol=1e-4) is False) result_dict = {"grad": val_true} with open("df_mp2_grad_mp2.dat", "wb") as f: pickle.dump(result_dict, f, pickle.HIGHEST_PROTOCOL)