def test_XYG3_dipderiv(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.DerivOnce import DipoleXDH, GradXDH 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") grids_cphf = H2O2_sc.gen_grids(50, 194) config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, "cphf_grids": grids_cphf } dip_helper = DipoleXDH(config) grad_helper = GradXDH(config) config = { "deriv_A": dip_helper, "deriv_B": grad_helper, } helper = DipDerivXDH(config) E_2 = helper.E_2 with open(resource_filename("pyxdh", "Validation/numerical_deriv/xdh_dipderiv_xyg3.dat"), "rb") as f: ref_polar = pickle.load(f)["dipderiv"] assert(np.allclose(E_2.T, ref_polar, atol=1e-6, rtol=1e-4))
def test_SCF_dipderiv(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.DerivOnce import DipoleSCF, GradSCF # HF H2O2 = Mol_H2O2() dip_deriv = DipoleSCF({"scf_eng": H2O2.hf_eng, "cphf_tol": 1e-10}) grad_deriv = GradSCF({"scf_eng": H2O2.hf_eng, "cphf_tol": 1e-10}) self.valid_assert(dip_deriv, grad_deriv, "Validation/gaussian/H2O2-HF-freq.fchk") # B3LYP H2O2 = Mol_H2O2() grids_cphf = H2O2.gen_grids(50, 194) dip_deriv = DipoleSCF({ "scf_eng": H2O2.gga_eng, "cphf_grids": grids_cphf, "cphf_tol": 1e-10 }) grad_deriv = GradSCF({ "scf_eng": H2O2.gga_eng, "cphf_grids": grids_cphf, "cphf_tol": 1e-10 }) self.valid_assert(dip_deriv, grad_deriv, "Validation/gaussian/H2O2-B3LYP-freq.fchk")
def test_XYG3_polar(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.DerivOnce import DipoleXDH import pickle with open( resource_filename( "pyxdh", "Validation/numerical_deriv/xdh_polarizability_xyg3.dat"), "rb") as f: ref_polar = pickle.load(f)["polarizability"] H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2( xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") grids_cphf = H2O2_sc.gen_grids(50, 194) dip_config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, "cphf_grids": grids_cphf } # With rotation dip_helper = DipoleXDH(dip_config) polar_config = {"deriv_A": dip_helper, "deriv_B": dip_helper} helper = PolarXDH(polar_config) assert (np.allclose(-helper.E_2, ref_polar, atol=1e-6, rtol=1e-4)) # No rotation dip_config["rotation"] = False dip_helper = DipoleXDH(dip_config) polar_config = {"deriv_A": dip_helper, "deriv_B": dip_helper} helper = PolarXDH(polar_config) assert (np.allclose(-helper.E_2, ref_polar, atol=1e-6, rtol=1e-4))
def mol_to_dip_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 = DipoleXDH(config) return helper
def test_dipole_xyg3(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2( xc="0.8033*HF - 0.0140*LDA + 0.2107*B88, 0.6789*LYP") grids_cphf = H2O2_sc.gen_grids(50, 194) config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.3211, "cphf_grids": grids_cphf } helper = DipoleXDH(config) self.valid_assert(helper, "Validation/gaussian/H2O2-XYG3-force.fchk")
def test_dipole_xygjos(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2_sc = Mol_H2O2(xc="B3LYPg") H2O2_nc = Mol_H2O2( xc="0.7731*HF + 0.2269*LDA, 0.2309*VWN3 + 0.2754*LYP") grids_cphf = H2O2_sc.gen_grids(50, 194) config = { "scf_eng": H2O2_sc.gga_eng, "nc_eng": H2O2_nc.gga_eng, "cc": 0.4364, "ss": 0., "cphf_grids": grids_cphf } helper = DipoleXDH(config) self.valid_assert(helper, "Validation/gaussian/H2O2-XYGJOS-force.fchk")
def test_HF_B3LYP_hess(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "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 test_MP2_polar(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.DerivOnce import DipoleMP2 H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng} 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("pyxdh", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(-E_2, formchk.polarizability(), atol=1e-6, rtol=1e-4))
def test_B3LYP_hess(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.DerivOnce import GradSCF H2O2 = Mol_H2O2() grids_cphf = H2O2.gen_grids(50, 194) config = { "scf_eng": H2O2.gga_eng } grad_helper = GradSCF(config) config = { "deriv_A": grad_helper, "deriv_B": grad_helper, "cphf_grids": grids_cphf } helper = HessSCF(config) E_2 = helper.E_2 formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/H2O2-B3LYP-freq.fchk")) assert(np.allclose( E_2, formchk.hessian(), atol=1e-5, rtol=1e-4 ))
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_B2PLYP_polar(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.DerivOnce import DipoleMP2 H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") grids_cphf = H2O2.gen_grids(50, 194) config = { "scf_eng": H2O2.gga_eng, "cc": 0.27, "cphf_grids": grids_cphf } 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("pyxdh", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert (np.allclose(-E_2, formchk.polarizability(), atol=1e-6, rtol=1e-4))
def test_B2PLYP_hess(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.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("pyxdh", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert (np.allclose(E_2, formchk.hessian(), atol=1e-5, rtol=1e-4)) with open( resource_filename( "pyxdh", "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 pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.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("pyxdh", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(E_2, formchk.hessian(), atol=1e-6, rtol=1e-4))
def test_SCF_dipole(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2() grids_cphf = H2O2.gen_grids(50, 194) self.valid_assert({"scf_eng": H2O2.hf_eng}, "Validation/gaussian/H2O2-HF-freq.fchk") self.valid_assert({"scf_eng": H2O2.gga_eng, "cphf_grids": grids_cphf}, "Validation/gaussian/H2O2-B3LYP-freq.fchk")
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
def mol_to_dip_helper(mol): H2O2 = Mol_H2O2(mol=mol) H2O2.hf_eng.kernel() config = { "scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng } helper = DipoleNCDFT(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 test_dipole_b2plyp(self): from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2(xc="0.53*HF + 0.47*B88, 0.73*LYP") grids_cphf = H2O2.gen_grids(50, 194) config = { "scf_eng": H2O2.gga_eng, "cc": 0.27, "cphf_grids": grids_cphf } helper = DipoleMP2(config) self.valid_assert(helper, "Validation/gaussian/H2O2-B2PLYP-freq.fchk")
def test_XYG3_grad(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "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 test_HF_B3LYP_dipole(self): import pickle from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng, "nc_eng": H2O2.gga_eng} helper = DipoleNCDFT(config) with open(resource_filename("pyxdh", "Validation/numerical_deriv/ncdft_derivonce_hf_b3lyp.dat"), "rb") as f: ref_dipole = pickle.load(f)["dipole"] assert (np.allclose(helper.E_1, ref_dipole, atol=1e-6, rtol=1e-4))
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_MP2_dipole(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng} helper = DipoleMP2(config) formchk = FormchkInterface( resource_filename("pyxdh", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(helper.E_1, formchk.dipole(), atol=1e-6, rtol=1e-4))
def test_B2PLYP_dipole(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "Validation/gaussian/H2O2-B2PLYP-freq.fchk")) assert (np.allclose(helper.E_1, formchk.dipole(), atol=1e-6, rtol=1e-4))
def test_B2PLYP_grad(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "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))
def test_MP2_dipderiv(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.Utilities import FormchkInterface from pyxdh.DerivOnce import DipoleMP2, GradMP2 H2O2 = Mol_H2O2() config = {"scf_eng": H2O2.hf_eng, "cphf_tol": 1e-10} dip_helper = DipoleMP2(config) grad_helper = GradMP2(config) config = {"deriv_A": dip_helper, "deriv_B": grad_helper} helper = DipDerivMP2(config) E_2 = helper.E_2 formchk = FormchkInterface(resource_filename("pyxdh", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert(np.allclose(E_2.T, formchk.dipolederiv(), atol=1e-6, rtol=1e-4))
def test_HF_grad(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "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 pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "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_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 ))
def test_HF_B3LYP_polar(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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( "pyxdh", "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_MP2_grad(self): from pkg_resources import resource_filename from pyxdh.Utilities.test_molecules import Mol_H2O2 from pyxdh.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("pyxdh", "Validation/gaussian/H2O2-MP2-freq.fchk")) assert (np.allclose(gmh.E_1, formchk.grad(), atol=1e-6, rtol=1e-4))