def test_XYG3_hess(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.DerivOnce import GradXDH from pyxdhalpha.DerivTwice import HessXDH import pickle H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2( xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, } grad_helper = GradXDH(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, } helper = HessXDH(config) E_2 = helper.E_2 with open( resource_filename( "pyxdhalpha", "Validation/numerical_deriv/xdh_hessian_xyg3.dat"), "rb") as f: ref_hess = pickle.load(f)["hess"] assert (np.allclose(E_2, ref_hess, atol=1e-6, rtol=1e-4))
def test_XYG3_polar(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.DerivOnce import DipoleXDH import pickle H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2(xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, } dip_helper = DipoleXDH(config) config = { "deriv_A": dip_helper, "deriv_B": dip_helper, } helper = PolarXDH(config) E_2 = helper.E_2 with open(resource_filename("pyxdhalpha", "Validation/numerical_deriv/xdh_polarizability_xyg3.dat"), "rb") as f: ref_polar = pickle.load(f)["polarizability"] assert(np.allclose( - E_2, ref_polar, atol=1e-6, rtol=1e-4 ))
def test_XYG3_grad(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2(xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211 } gmh = GradXDH(config) formchk = FormchkInterface(resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-XYG3-force.fchk")) assert(np.allclose( gmh.eng, formchk.total_energy() )) assert(np.allclose( gmh.E_1, formchk.grad(), atol=1e-5, rtol=1e-4 ))
def mol_to_grad_helper(mol): print("Processing...") H2O2_sc = Mol_H2O2(mol=mol, xc="B3LYPg") H2O2_nc = Mol_H2O2(mol=mol, xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211 } helper = GradXDH(config) return helper
def test_B2PLYP_polar(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface from pyxdhalpha.DerivOnce import DipoleMP2 H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") config = { "scf_eng": H2O2.gga_eng, "cc": 0.27 } dip_helper = DipoleMP2(config) config = { "deriv_A": dip_helper, "deriv_B": dip_helper, } helper = PolarMP2(config) E_2 = helper.E_2 formchk = FormchkInterface(resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert(np.allclose( - E_2, formchk.polarizability(), atol=1e-6, rtol=1e-4 ))
def test_DipoleDerivGenerator_by_SCF(self): from pyxdhalpha.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_HF_B3LYP_polar(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.DerivOnce import DipoleNCDFT import pickle H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng} dip_helper = DipoleNCDFT(config) config = { "deriv_A": dip_helper, "deriv_B": dip_helper, } helper = PolarNCDFT(config) E_2 = helper.E_2 with open( resource_filename( "pyxdhalpha", "Validation/numerical_deriv/ncdft_polarizability_hf_b3lyp.dat" ), "rb") as f: ref_polar = pickle.load(f)["polarizability"] assert (np.allclose(-E_2, ref_polar, atol=1e-6, rtol=1e-4))
def test_HF_polar(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface from pyxdhalpha.DerivOnce import DipoleSCF H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng} dip_helper = DipoleSCF(config) config = { "deriv_A": dip_helper, "deriv_B": dip_helper, } helper = PolarSCF(config) E_2 = helper.E_2 formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-HF-freq.fchk")) assert (np.allclose(-E_2, formchk.polarizability(), atol=1e-6, rtol=1e-4))
def test_MP2_grad(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface from pyscf import mp, grad H2O2 = Mol_H2O2() config = { "scf_eng": H2O2.hf_eng } gmh = GradMP2(config) mp2_eng = mp.MP2(gmh.scf_eng) mp2_eng.kernel() mp2_grad = grad.mp2.Gradients(mp2_eng) mp2_grad.kernel() assert(np.allclose( gmh.E_1, mp2_grad.de, atol=1e-6, rtol=1e-4 )) formchk = FormchkInterface(resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert(np.allclose( gmh.E_1, formchk.grad(), atol=1e-6, rtol=1e-4 ))
def test_B2PLYP_hess(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface from pyxdhalpha.DerivOnce import GradMP2 import pickle H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") config = {"scf_eng": H2O2.gga_eng, "cc": 0.27} grad_helper = GradMP2(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, } helper = HessMP2(config) E_2 = helper.E_2 formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert (np.allclose(E_2, formchk.hessian(), atol=1e-5, rtol=1e-4)) with open( resource_filename( "pyxdhalpha", "Validation/numerical_deriv/mp2_hessian_b2plyp.dat"), "rb") as f: ref_hess = pickle.load(f)["hess"] assert (np.allclose(E_2, ref_hess, atol=1e-6, rtol=1e-4))
def test_MP2_hess(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface from pyxdhalpha.DerivOnce import GradMP2 H2O2 = Mol_H2O2() config = { "scf_eng": H2O2.hf_eng, "rotation": True, } grad_helper = GradMP2(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, } helper = HessMP2(config) E_2 = helper.E_2 formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(E_2, formchk.hessian(), atol=1e-6, rtol=1e-4))
def test_HF_B3LYP_hess(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.DerivOnce import GradNCDFT import pickle H2O2 = Mol_H2O2() config = { "scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng, "rotation": True, } grad_helper = GradNCDFT(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, } helper = HessNCDFT(config) E_2 = helper.E_2 with open( resource_filename( "pyxdhalpha", "Validation/numerical_deriv/ncdft_hessian_hf_b3lyp.dat"), "rb") as f: ref_hess = pickle.load(f)["hess"] assert (np.allclose(E_2, ref_hess, atol=1e-6, rtol=1e-4))
def mol_to_grad_helper(mol): H2O2 = Mol_H2O2(mol=mol) H2O2.hf_eng.kernel() config = { "scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng } helper = GradNCDFT(config) return helper
def dipole_generator(component, interval): H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2(xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") mol = H2O2_sc.mol def get_hcore(mol=mol): return scf.rhf.get_hcore( mol) - interval * mol.intor("int1e_r")[component] H2O2_sc.gga_eng.get_hcore = get_hcore H2O2_nc.gga_eng.get_hcore = get_hcore config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, } helper = DipoleXDH(config) return helper
def dipole_generator(component, interval): H2O2 = Mol_H2O2() 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
def test_B2PLYP_dipole(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") config = {"scf_eng": H2O2.gga_eng, "cc": 0.27} helper = DipoleMP2(config) formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert (np.allclose(helper.E_1, formchk.dipole(), atol=1e-6, rtol=1e-4))
def test_MP2_dipole(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng} helper = DipoleMP2(config) formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(helper.E_1, formchk.dipole(), atol=1e-6, rtol=1e-4))
def test_HF_B3LYP_grad(self): import pickle from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng} helper = GradNCDFT(config) with open( resource_filename( "pyxdhalpha", "Validation/numerical_deriv/ncdft_derivonce_hf_b3lyp.dat"), "rb") as f: ref_grad = pickle.load(f)["grad"].reshape(-1, 3) assert (np.allclose(helper.E_1, ref_grad, atol=1e-6, rtol=1e-4))
def test_HF_grad(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng} helper = GradSCF(config) hf_grad = helper.scf_grad assert (np.allclose(helper.E_1, hf_grad.grad(), atol=1e-6, rtol=1e-4)) formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-HF-freq.fchk")) assert (np.allclose(helper.E_1, formchk.grad(), atol=1e-6, rtol=1e-4))
def test_B3LYP_grad(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.gga_eng} helper = GradSCF(config) gga_grad = helper.scf_grad assert (np.allclose(helper.E_1, gga_grad.grad(), atol=1e-6, rtol=1e-4)) formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B3LYP-freq.fchk")) # TODO: This is a weaker compare! Try to modulize that someday. assert (np.allclose(helper.E_1, formchk.grad(), atol=1e-5, rtol=1e-4))
def test_B3LYP_dipole(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.gga_eng} helper = DipoleSCF(config) assert (np.allclose(helper.E_1, helper.scf_eng.dip_moment(unit="A.U."), atol=1e-6, rtol=1e-4)) formchk = FormchkInterface( resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B3LYP-freq.fchk")) assert (np.allclose(helper.E_1, formchk.dipole(), atol=1e-6, rtol=1e-4))
def test_NucCoordDerivGenerator_by_SCFgrad(self): from pyxdhalpha.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))
def test_B2PLYP_grad(self): from pkg_resources import resource_filename from pyxdhalpha.Utilities.test_molecules import Mol_H2O2 from pyxdhalpha.Utilities import FormchkInterface H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") config = { "scf_eng": H2O2.gga_eng, "cc": 0.27 } gmh = GradMP2(config) formchk = FormchkInterface(resource_filename("pyxdhalpha", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert(np.allclose( gmh.eng, formchk.total_energy() )) assert(np.allclose( gmh.E_1, formchk.grad(), atol=1e-5, rtol=1e-4 ))
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)