def test_gs(): from pysisyphus.calculators.XTB import XTB educt = geom_from_library("ciscis_24hexadiene_xtbopt.xyz") product = geom_from_library("trans34dimethylcyclobutene.xyz") images = (educt, product) def calc_getter(): return XTB(pal=4) for img in images: img.set_calculator(calc_getter()) gs_kwargs = { "max_nodes": 9, "reparam_every": 3, } gs = GrowingString(images, calc_getter, **gs_kwargs) from pysisyphus.optimizers.StringOptimizer import StringOptimizer opt_kwargs = { "dump": True, "max_cycles": 40, "align": True, } opt = StringOptimizer(gs, **opt_kwargs) opt.run()
def test_lst(): initial = geom_from_library("dipeptide_init.xyz") final = geom_from_library("dipeptide_fin.xyz") geoms = (initial, final) lst = LST(geoms, 28, align=True) geoms = lst.interpolate_all() lst.all_geoms_to_trj("lst_opt.trj")
def test_idpp(): initial = geom_from_library("09_htransfer_product.xyz") final = geom_from_library("10_po_diss_product_xtbopt.xyz") geoms = (initial, final) idpp = IDPP(geoms, 18, align=True) geoms = idpp.interpolate_all() # idpp.all_geoms_to_trj("idpp_opt.trj") assert len(geoms) == 20
def test_ala_dipeptide_interpol(interpol_cls): initial = geom_from_library("dipeptide_init.xyz") final = geom_from_library("dipeptide_fin.xyz") geoms = (initial, final) interpolator = interpol_cls(geoms, 28, align=True) geoms = interpolator.interpolate_all() # interpolator.all_geoms_to_trj("interpolated.trj") assert len(geoms) == 30
def test_pyscf(): # pyscf_ = PySCF(basis="3-21g", pal=4) pyscf_ = PySCF(basis="3-21g", pal=4) # pyscf_ = PySCF(basis="3-21g", method="mp2", pal=4) # geom = geom_from_library("birkholz/vitamin_c.xyz") # geom = geom_from_library("hcn.xyz") geom = geom_from_library("hcn_iso_ts.xyz") geom.set_calculator(pyscf_) f = geom.forces.reshape(-1, 3) print("PySCF") print(f) H = geom.hessian print("PySCF hessian") print(H.reshape(-1, 9)) ref_geom = geom.copy() from pysisyphus.calculators.Gaussian16 import Gaussian16 g16 = Gaussian16("hf/3-21G", pal=4) # g16 = Gaussian16("mp2/3-21G", pal=4) ref_geom.set_calculator(g16) f_ref = ref_geom.forces.reshape(-1, 3) print("Gaussian16") print(f_ref) # np.testing.assert_allclose(f, f_ref, rtol=5e-3) H_ref = ref_geom.hessian print("G16 Hess") print(H_ref)
def test_augment_coordinates_silyl(augment, ref_cycle): geom = geom_from_library("baker_ts/18_silyene_insertion.xyz", coord_type="redund") opt_kwargs = { "thresh": "baker", "max_cycles": 100, "dump": True, "trust_radius": 0.3, "trust_max": 0.3, "augment_bonds": augment, } calc_kwargs = { "charge": 0, "mult": 1, "pal": 4, } calc = PySCF(basis="321g", **calc_kwargs) geom.set_calculator(calc) opt = RSIRFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == ref_cycle ref_en = -367.20778 assert opt.geometry.energy == pytest.approx(ref_en)
def test_bias_rotation(): # geom = geom_from_library("claisen_forward.xyz") # bonds = ((10, 11, 1), (12, 13, -1)) # calc_kwargs = { # "xc": "pbe", # # "basis": "def2svp", # # "auxbasis": "weigend", # "basis": "321g", # "pal": 4, # } # calc = PySCF(**calc_kwargs) geom = geom_from_library("baker_ts/01_hcn.xyz") bonds = ((1, 2, -1), (2, 0, 1)) calc = PySCF("321g", pal=2) dimer_kwargs = { "rotation_method": "fourier", "calculator": calc, "bonds": bonds, # "rotation_thresh": 2e-4, "bias_rotation": True, } dimer = Dimer(**dimer_kwargs) geom.set_calculator(dimer) f = geom.forces print(f)
def test_dimer_hcn(bonds, ref_cycle): geom = geom_from_library("baker_ts/01_hcn.xyz") ref_energy = -92.24604 N_raw = " 0.5858 0. 0.0543 " \ "-0.7697 -0. 0.061 " \ "0.2027 0. -0.1295".split() if bonds is not None: N_raw = None calc = PySCF("321g", pal=2) dimer_kwargs = { "rotation_method": "fourier", "calculator": calc, "N_raw": N_raw, "bonds": bonds, } dimer = Dimer(**dimer_kwargs) geom.set_calculator(dimer) opt_kwargs = { "precon": True, "max_step_element": 0.25, "max_cycles": 15, } opt = PreconLBFGS(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == ref_cycle assert geom.energy == pytest.approx(ref_energy)
def test_psi4(): geom = geom_from_library("hcn_iso_ts.xyz") psi4_kwargs = { "pal": 4, "mem": 2000, # "method": "hf", "method": "b3lyp", "basis": "def2-svp", # "mult": 1, # "charge": 2, } psi4 = Psi4(**psi4_kwargs) geom.set_calculator(psi4) print(psi4.base_cmd) # en = geom.energy # print(en) f = geom.forces print(f) e = geom.energy print(e) start = time() h = geom.hessian end = time() print(h) dur = end - start print("hess calc took", int(dur), "seconds")
def test_pyxtb(): geom = geom_from_library("benzene.xyz") pyxtb = PyXTB() geom.set_calculator(pyxtb) forces = geom.forces energy = geom.energy
def test_cytosin_s1_opt(calc_cls, calc_kwargs, this_dir): geom = geom_from_library("cytosin.xyz", coord_type="redund") if "control_path" in calc_kwargs: calc_kwargs["control_path"] = this_dir / calc_kwargs["control_path"] calc_kwargs.update({ "mem": 2000, "pal": 4, "ovlp_type": "tden", "track": True, }) calc = calc_cls(**calc_kwargs) geom.set_calculator(calc) opt_kwargs = { "thresh": "gau", "overachieve_factor": 2., # "trust_radius": 0.3, # "trust_max": 0.3, "line_search": True, "gdiis": True, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged # assert geom.energy == pytest.approx(-394.06081796) assert calc.root_flips[2] assert calc.root == 2
def bond_ref(): xyz_fn = "h2.xyz" geom = geom_from_library(xyz_fn, coord_type="redund") def d2(geom): c3d = geom.coords3d n, m = 0, 1 N_ = c3d[n] M_ = c3d[m] U_ = M_ - N_ u_norm = np.linalg.norm(U_) U = U_ / u_norm inds = (n, m) derivs = list() kr = lambda i, j: 1 if i == j else 0 for a, i in it.product((0, 1), (0, 1, 2)): for b, j in it.product((0, 1), (0, 1, 2)): d = (-1)**kr(a, b) * (U[i] * U[j] - kr(i, j)) / u_norm print(a, b, i, j, d) derivs.append(d) return derivs # import pdb; pdb.set_trace() d = d2(geom) d = np.array(d) d = d.reshape(-1, 6) import pdb pdb.set_trace() np.testing.assert_allclose(d, d.T) import pdb pdb.set_trace() pass
def test_cytosin_gs_opt(calc_cls, calc_kwargs_, this_dir): geom = geom_from_library("cytosin.xyz", coord_type="redund") print("@Using", calc_cls) calc_kwargs = { "pal": 2, "mem": 1000, } calc_kwargs.update(calc_kwargs_) calc = calc_cls(**calc_kwargs) if "control_path" in calc_kwargs: calc_kwargs["control_path"] = this_dir / calc_kwargs["control_path"] geom.set_calculator(calc) opt_kwargs = { "thresh": "gau_tight", "overachieve_factor": 2., "line_search": True, "gdiis": True, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert geom.energy == pytest.approx(-387.54925356)
def test_autodiff(): geom = geom_from_library("h2o.xyz", coord_type="redund") # geom.jmol() print(geom) B_ref = geom.internal.B print(B_ref) sg = autodiff.stretch_grad bg = autodiff.bend_grad auto_funcs = { 2: sg, 3: bg, } int_ = geom.internal ref_funcs = { 2: int_.calc_stretch, 3: int_.calc_bend, } c3d = geom.coords3d for i, pc in enumerate(geom.internal._prim_internals): inds = pc.inds print(i, inds) l = len(inds) ag = auto_funcs[l](c3d, inds) _, rg = ref_funcs[l](c3d, inds, grad=True) np.testing.assert_allclose(ag.flatten(), rg)
def test_hcn_iso_dimer(trans_opt, trans_memory): calc_kwargs = { "route": "PM6", "pal": 4, "mem": 1000, } def calc_getter(): return Gaussian16(**calc_kwargs) geom = geom_from_library("hcn_iso_pm6_near_ts.xyz") geom.set_calculator(calc_getter()) geoms = [geom, ] N_init = np.array( (0.6333, 0.1061, 0.5678, 0.171, 0.11, 0.3373, 0.0308, 0.1721, 0.282) ) dimer_kwargs = { "max_step": 0.04, "dR_base": 0.01, "N_init": N_init, # "rot_opt": "mb", "trans_opt": trans_opt, "trans_memory": trans_memory, "angle_tol": 5, # "rot_f_thresh": 1e-4, # "rot_type": "direct", "f_thresh": 1e-4, "f_tran_mod": True, "multiple_translations": True, } dimer_result = dimer_method(geoms, calc_getter, **dimer_kwargs) return dimer_result
def test_restart(calc_cls, calc_kwargs, chk_exts, this_dir): geom = geom_from_library("benzene.xyz") if calc_cls == Turbomole: calc_kwargs["control_path"] = this_dir / calc_kwargs["control_path"] calc = calc_cls(**calc_kwargs) geom.set_calculator(calc) forces = geom.forces ref_energy = -230.534572588 assert geom.energy == pytest.approx(ref_energy) restart_info = calc.get_restart_info() chkfiles = restart_info["chkfiles"] assert len(chkfiles) >= 1 assert all([ext in chkfiles for ext in chk_exts]) calc2 = calc_cls(**calc_kwargs) calc2.set_restart_info(restart_info) geom2 = geom.copy() geom2.set_calculator(calc2) forces2 = geom2.forces np.testing.assert_allclose(forces2, forces, atol=1e-5)
def test_artemisin(): # geom = geom_from_library("birkholz/artemisin.xyz", coord_type="redund") geom = geom_from_library("birkholz/artemisin.xyz") calc = XTB(charge=0, mult=1, pal=4) # geom = geom_from_library("birkholz/zn_edta.xyz", coord_type="redund") # geom = geom_from_library("birkholz/zn_edta.xyz") # calc = XTB(charge=-2, mult=1, pal=4) geom.set_calculator(calc) opt_kwargs_base = { "max_cycles": 50, # "max_cycles": 15, # "max_cycles": 8, "thresh": "gau", "trust_radius": 0.5, "trust_update": True, "hessian_update": "damped_bfgs", # "hessian_init": "fischer", "hessian_init": "calc", "hessian_recalc": 1, "line_search": True, # "hybrid": True, # "dump": True, } opt = RFOptimizer(geom, **opt_kwargs_base) opt.run() H = opt.H w, v = np.linalg.eigh(H) print(w)
def test_hess_proj(): geom = geom_from_library("birkholz/zn_edta.xyz") H = np.eye(geom.cart_coords.size) mwH = geom.mass_weigh_hessian(H) eH = geom.eckart_projection(mwH) w, v = np.linalg.eigh(eH) import pdb pdb.set_trace()
def test_psi4_neb(): start = geom_from_library("hcn.xyz") ts = geom_from_library("hcn_iso_ts_guess.xyz") end = geom_from_library("nhc.xyz") images = interpolate_all((start, ts, end), 4, kind="lst") for i, img in enumerate(images): img.set_calculator(Psi4("tpss", "sto-3g", pal=4, calc_number=i)) neb = NEB(images, climb=True) with open("interpolated.trj", "w") as handle: handle.write(neb.as_xyz()) # qm = QuickMin(neb, align=True, dump=True, max_cycles=10) # qm.run() fire = FIRE(neb, align=True, dump=True, max_cycles=10) fire.run() lbfgs = LBFGS(neb, align=True, dump=True) lbfgs.run()
def test_mopac(): geom = geom_from_library("h2o.xyz") geom2 = geom.copy() calc = MOPAC() geom.set_calculator(calc) grad = geom.gradient energy = geom.energy hessian = geom.hessian
def test_benzene_wfoverlap(): control_path = THIS_DIR / "benzene_3states" td_vec_fn = str(control_path / "ciss_a") mos_fn = str(control_path / "mos") turbo = Turbomole(control_path) geom = geom_from_library("benzene_bp86sto3g_opt.xyz") geom.set_calculator(turbo) turbo.td_vec_fn = td_vec_fn turbo.mos = mos_fn
def test_baker_16(): calc_kwargs = { "route": "HF/3-21G", "pal": 4, "mem": 1000, "charge": -1, "mult": 1, } def calc_getter(): return Gaussian16(**calc_kwargs) # return XTB(**calc_kwargs) geom = geom_from_library("baker_ts/16_h2po4_anion.xyz") geom.set_calculator(calc_getter()) geoms = [geom, ] downhill = geom_from_library("baker_ts/16_h2po4_anion_downhill.xyz") N_init = geom.coords - downhill.coords N_init /= np.linalg.norm(N_init) dimer_kwargs = { "max_step": 0.5, # 1e-2 Angstroem in bohr "dR_base": 0.0189, "rot_opt": "lbfgs", "trans_opt": "lbfgs", # "trans_opt": "mb", # "trans_memory": 10, "angle_tol": 5, "f_thresh": 1e-3, "max_cycles": 50, "f_tran_mod": True, "rot_type": "fourier", "multiple_translations": True, "N_init": N_init, "max_cycles": 15, "rot_f_thresh": 1.5e-3, } # 55 cycles; no multi trans, no f_tran_mod # import pdb; pdb.set_trace() dimer_result = dimer_method(geoms, calc_getter, **dimer_kwargs) return dimer_result
def test_gradient(): geom = geom_from_library("benzene.xyz") geom.set_calculator(XTB(gfn=1)) grad = geom.gradient reference_fn = THIS_DIR / "gradient.reference" reference = np.loadtxt(reference_fn) # Quite some difference between xTB 5.8 and 4.9.4 ... np.testing.assert_allclose(reference.flatten(), grad, rtol=1e-5)
def test_water_hf_precon_opt(opt_cls, precon, ref_cycles): geom = geom_from_library("h2o_shaken.xyz") calc = PySCF(basis="sto3g") geom.set_calculator(calc) opt = opt_cls(geom, thresh="gau_tight", precon=precon) opt.run() assert opt.is_converged assert opt.cur_cycle == ref_cycles assert geom.energy == pytest.approx(-74.96590119)
def test_parse_charges(calc_cls, calc_kwargs, ref_energy, ref_charges): geom = geom_from_library("methane_bp86_def2svp_opt.xyz") calc = calc_cls(**calc_kwargs) geom.set_calculator(calc) results = calc.get_forces(geom.atoms, geom.coords) assert results["energy"] == pytest.approx(ref_energy) charges = calc.parse_charges() np.testing.assert_allclose(charges, ref_charges, atol=1e-5)
def test_dlc_constraints(): xyz_fn = "fluorethylene.xyz" geom = geom_from_library(xyz_fn, coord_type="dlc") freeze = ((0, 1), (2, 0, 3)) int_ = geom.internal int_.freeze_primitives(freeze) U = int_.U constraints = int_.constraints assert U[0, len(freeze):].sum() == approx(0) assert U[8, len(freeze):].sum() == approx(0) int_.reset_constraints() constraints_ = int_.constraints
def test_geometry_get_restart_info(): geom = geom_from_library("benzene.xyz") calc = PySCF(method="scf", basis="def2svp") geom.set_calculator(calc) restart = geom.get_restart_info() atoms = restart["atoms"] coords = restart["cart_coords"] assert atoms == geom.atoms assert len(coords) == len(geom.atoms * 3) assert "calc_info" in restart
def test_rsprfo_hcn_ts_xtb(): geom = geom_from_library("hcn_iso_ts.xyz", coord_type="redund") xtb = XTB() geom.set_calculator(xtb) opt_kwargs = { "thresh": "gau_tight", "max_micro_cycles": 1, } opt = RSPRFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 7
def get_geom(): init_logging("./") geom = geom_from_library("h2.xyz") kwargs = { "keywords": "BP86 def2-SV(P)", "charge": 0, "mult": 1, "blocks": "%tddft nroots 2 iroot 1 end", "track": True, } orca = ORCA(**kwargs) geom.set_calculator(orca) return geom
def test_ar_cluster(): geom = geom_from_library("ar14cluster.xyz") geom.set_calculator(LennardJones()) opt_kwargs = { "max_cycles": 150, "gediis": True, "thresh": "gau_vtight", } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 93