def test_UXYGJOS_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename from pyxdh.Utilities import FormchkInterface CH3_sc = Mol_CH3(xc="B3LYPg") CH3_nc = Mol_CH3(xc="0.7731*HF + 0.2269*LDA, 0.2309*VWN3 + 0.2754*LYP") grids_cphf = CH3_sc.gen_grids(atom_grid=(50, 194)) helper = GradUXDH({"scf_eng": CH3_sc.gga_eng, "nc_eng": CH3_nc.gga_eng, "cc": 0.4364, "ss": 0., "cphf_tol": 1e-10, "cphf_grids": grids_cphf}) formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-XYGJOS-force.fchk")) assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-5, rtol=1e-4))
def test_UXYG3_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename from pyxdh.Utilities import FormchkInterface CH3_sc = Mol_CH3(xc="B3LYPg") CH3_nc = Mol_CH3(xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") grids_cphf = CH3_sc.gen_grids(atom_grid=(50, 194)) helper = GradUXDH({ "scf_eng": CH3_sc.gga_eng, "nc_eng": CH3_nc.gga_eng, "cc": 0.3211, "cphf_tol": 1e-10, "cphf_grids": grids_cphf}) formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-XYG3-force.fchk")) assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-5, rtol=1e-4))
def test_UB3LYP_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename from pyxdh.Utilities import FormchkInterface CH3 = Mol_CH3() helper = GradUSCF({"scf_eng": CH3.gga_eng}) formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-B3LYP-freq.fchk")) assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-6, rtol=1e-4))
def test_UMP2_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename from pyxdh.Utilities import FormchkInterface CH3 = Mol_CH3() helper = GradUMP2({"scf_eng": CH3.hf_eng.run(), "cphf_tol": 1e-10}) formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-MP2-freq.fchk")) assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-5, rtol=1e-4))
def test_UHF_UB3LYP_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename import pickle CH3 = Mol_CH3() helper = GradUNCDFT({"scf_eng": CH3.hf_eng, "nc_eng": CH3.gga_eng, "cphf_tol": 1e-10}) with open(resource_filename("pyxdh", "Validation/numerical_deriv/ncdft_derivonce_uhf_ub3lyp.dat"), "rb") as f: ref_grad = pickle.load(f)["grad"].reshape(-1, 3) assert (np.allclose(helper.E_1, ref_grad, atol=1e-5, rtol=1e-4))
def test_UB2PLYP_grad(self): from pyxdh.Utilities.test_molecules import Mol_CH3 from pkg_resources import resource_filename from pyxdh.Utilities import FormchkInterface CH3 = Mol_CH3(xc="0.53*HF + 0.47*B88, 0.73*LYP") grids_cphf = CH3.gen_grids(atom_grid=(50, 194)) helper = GradUMP2({"scf_eng": CH3.gga_eng.run(), "cphf_tol": 1e-10, "cc": 0.27, "cphf_grids": grids_cphf}) formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/CH3-B2PLYP-freq.fchk")) assert(np.allclose(helper.E_1, formchk.grad(), atol=1e-5, rtol=1e-4))
def dipole_generator(component, interval): CH3 = Mol_CH3() mol = CH3.mol def get_hcore(mol=mol): return scf.rhf.get_hcore( mol) - interval * mol.intor("int1e_r")[component] CH3.hf_eng.get_hcore = get_hcore CH3.gga_eng.get_hcore = get_hcore config = {"scf_eng": CH3.hf_eng, "nc_eng": CH3.gga_eng} helper = GradUNCDFT(config) return helper
def test_UHF_hess(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_CH3 from pyxdh.Utilities import FormchkInterface from pyxdh.DerivOnce import GradUSCF CH3 = Mol_CH3() config = { "scf_eng": CH3.hf_eng, "cphf_tol": 1e-10, } grad_helper = GradUSCF(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, "cphf_tol": 1e-10, } helper = HessUSCF(config) E_2 = helper.E_2 formchk = FormchkInterface( resource_filename("pyxdh", "Validation/gaussian/CH3-HF-freq.fchk")) assert (np.allclose(E_2, formchk.hessian(), atol=1e-5, rtol=1e-4))
def mol_to_grad_helper(mol): CH3 = Mol_CH3(mol=mol) CH3.hf_eng.kernel() config = {"scf_eng": CH3.hf_eng, "nc_eng": CH3.gga_eng} helper = GradUNCDFT(config) return helper
mol = CH3.mol def get_hcore(mol=mol): return scf.rhf.get_hcore( mol) - interval * mol.intor("int1e_r")[component] CH3.hf_eng.get_hcore = get_hcore CH3.gga_eng.get_hcore = get_hcore config = {"scf_eng": CH3.hf_eng, "nc_eng": CH3.gga_eng} helper = GradUNCDFT(config) return helper if __name__ == '__main__': result_dict = {} CH3 = Mol_CH3() mol = CH3.mol num_obj = NucCoordDerivGenerator(CH3.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_uhf_ub3lyp.dat", "wb") as f: pickle.dump(result_dict, f, pickle.HIGHEST_PROTOCOL)