def test_rfoptimizer(calc_cls, start, ref_cycle, ref_coords): geom = calc_cls.get_geom(start) print("@Using", calc_cls) opt_kwargs = { "thresh": "gau_tight", "dump": True, "overachieve_factor": 2., # "max_micro_cycles": 1, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() # geom.calculator.plot_opt(opt, show=True) # import matplotlib.pyplot as plt # calc = geom.calculator # calc.plot() # coords = np.array(opt.coords) # ax = calc.ax # ax.plot(*coords.T[:2], "ro-") # plt.show() assert opt.is_converged assert opt.cur_cycle == ref_cycle ref_coords = np.array(ref_coords) diff = ref_coords - geom.coords diff_norm = np.linalg.norm(diff) print(f"@\tnorm(diff)={diff_norm:.8f}") assert diff_norm < 6e-5 print("@\tFinal coords", geom.coords)
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_ohch3f_anion(calc_cls, calc_kwargs, ref_cycle, ccl_dist, oc_dist): """Example (R1) from https://aip.scitation.org/doi/pdf/10.1063/1.3457903?class=pdf See Fig. 2 and Fig. 4 """ geom = geom_loader("lib:ohch3f_anion_cs.xyz") # OH group is a fragment fragment_indices = [ (5, 6), ] gamma = 100 calc = calc_cls(charge=-1, **calc_kwargs) afir = AFIR(calc, fragment_indices, gamma, ignore_hydrogen=True) geom.set_calculator(afir) opt = RFOptimizer(geom, dump=True, trust_max=0.3) opt.run() assert opt.is_converged assert opt.cur_cycle == ref_cycle # Broken C-Cl bond c3d = geom.coords3d assert np.linalg.norm(c3d[0] - c3d[4]) == pytest.approx(ccl_dist, abs=1e-4) # Formed O-C bond assert np.linalg.norm(c3d[0] - c3d[5]) == pytest.approx(oc_dist, abs=1e-4)
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 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_afir(): """Example (R1) from https://aip.scitation.org/doi/pdf/10.1063/1.3457903?class=pdf See Fig. 2 and Fig. 4 """ geom = geom_loader("lib:ohch3f_anion_cs.xyz") # OH group is a fragment fragment_indices = [ (5, 6), ] calc = PySCF(basis="6-31g*", xc="b3lyp", pal=2, charge=-1) gamma = 100 afir = AFIR(calc, fragment_indices, gamma) geom.set_calculator(afir) opt = RFOptimizer(geom, dump=True, trust_max=.3) opt.run() assert opt.is_converged assert opt.cur_cycle == 47 # Broken C-Cl bond c3d = geom.coords3d assert np.linalg.norm(c3d[0] - c3d[4]) == pytest.approx(4.805665, abs=1e-4) # Formed O-C bond assert np.linalg.norm(c3d[0] - c3d[5]) == pytest.approx(2.674330, abs=1e-4)
def test_multi_run(): # NEB geoms = AnaPot().get_path(10) cos = NEB(geoms) neb_kwargs = { "dump": True, "h5_group_name": "neb", } neb_opt = SteepestDescent(cos, **neb_kwargs) neb_opt.run() # GrowingString geoms = AnaPot().get_path(10) gsm = GrowingString(geoms, calc_getter=AnaPot, perp_thresh=0.25) gsm_kwargs = { "dump": True, "h5_group_name": "gsm", "stop_in_when_full": 0, } gsm_opt = StringOptimizer(gsm, **gsm_kwargs) gsm_opt.run() # calc = geoms[0].calculator # calc.anim_opt(opt, show=True) # Simple Optimization geom = AnaPot().get_path(10)[5] opt_kwargs = { "dump": True, "h5_group_name": "opt", } opt = RFOptimizer(geom, **opt_kwargs) opt.run()
def test_opt_dump(): geom = AnaPot().get_geom((1, 1, 0.)) calc = geom.calculator opt_kwargs = { "dump": True, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() calc.plot_opt(opt) #, show=True)
def test_h2o_opt(): geom = geom_loader("lib:h2o.xyz", coord_type="redund") calc = Dalton(basis="3-21G") geom.set_calculator(calc) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert opt.is_converged assert opt.cur_cycle == 4 assert geom.energy == pytest.approx(-75.58595976)
def test_allene_opt(): geom = geom_loader("lib:08_allene.xyz", coord_type="redund") calc = PySCF(basis="321g", pal=1) geom.set_calculator(calc) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert opt.is_converged # assert opt.cur_cycle == 7 assert geom.energy == pytest.approx(-115.21991342)
def test_save_hessian(geom): opt = RFOptimizer(geom, thresh="gau_tight") opt.run() hess_fn = "_h2o_hessian.h5" save_hessian(hess_fn, geom) with h5py.File(hess_fn, "r") as handle: hessian = handle["hessian"][:] np.testing.assert_allclose(hessian, geom.cart_hessian)
def test_rastrigin_minimum(): geom = Rastrigin().get_minima()[0] # calc = geom.calculator # calc.plot(show=True) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert opt.is_converged assert opt.cur_cycle == 0 assert geom.energy == pytest.approx(0.)
def test_opt_linear_dihedrals(): geom = geom_loader("lib:dihedral_gen_test.cjson", coord_type="redund") geom.set_calculator(XTB()) opt_kwargs = { "thresh": "gau_tight", } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 16 assert geom.energy == pytest.approx(-10.48063133)
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
def test_thermostat(): geom = geom_loader("lib:dynamics/10_water.xyz") from pysisyphus.calculators.XTB import XTB geom.set_calculator(XTB(pal=2)) opt = RFOptimizer(geom, thresh="gau_loose", max_cycles=25) opt.run() T = 298.15 seed = 20182503 v0 = get_mb_velocities_for_geom(geom, T, seed=seed).flatten() # steps = 6000 steps = 250 dt = 0.5 md_kwargs = { "v0": v0, "steps": steps, "dt": dt, "remove_com_v": True, "thermostat": "csvr", "timecon": 25, } res = md(geom, **md_kwargs) # assert dt * steps / 1000 == pytest.approx(res.t) import matplotlib.pyplot as plt E_tot = res.E_tot E_tot -= E_tot.mean() from pysisyphus.constants import AU2KJPERMOL E_tot *= AU2KJPERMOL T = res.T fig, (ax0, ax1) = plt.subplots(nrows=2) ax0.plot(E_tot) ax0.axhline(E_tot.mean()) ax0.set_title("$\Delta$E$_{tot}$ / kJ mol⁻¹") ax1.plot(T) T_mean = T.mean() print("T_mean", T_mean, "K") ax1.axhline(T_mean) ax1.set_title(f"T / K, avg. = {T_mean:.2f} K") plt.tight_layout() plt.show() fig.savefig("md.pdf")
def test_cytosin_s1_opt(): geom = geom_loader("lib:cytosin.xyz", coord_type="redund") calc_kwargs = { "parameter": "mio-ext", "root": 1, "track": True, } calc = DFTBp(**calc_kwargs) geom.set_calculator(calc) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert opt.is_converged assert geom.energy == pytest.approx(-19.13130711)
def run_opt(line_search=False, hessian_recalc=None): geom = AnaPot.get_geom((0.667, 1.609, 0.)) rfo_kwargs = { "trust_radius": 0.75, "thresh": "gau_tight", "hessian_recalc": hessian_recalc, "line_search": line_search, # "hessian_init": "calc", } rfo = RFOptimizer(geom, **rfo_kwargs) rfo.run() conv = rfo.is_converged cycs = rfo.cur_cycle return (conv, cycs)
def test_opt_load_save_hessian(this_dir): geom = geom_loader("lib:acetaldehyd.xyz", coord_type="redund") calc = PySCF(basis="321g", pal=2) geom.set_calculator(calc) opt_kwargs = { "hessian_recalc": 1, "hessian_init": this_dir / "inp_hess_calc_cyc_0.h5", } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 2 assert geom.energy == pytest.approx(-152.05524620313963)
def test_guess_hessians(hessian_init, ref_cycle): geom = geom_loader("lib:h2o2_hf_321g_opt.xyz", coord_type="redund") geom.set_calculator(PySCF(basis="def2svp", pal=2)) print("@\tguess_hessian:", hessian_init) opt_kwargs = { "hessian_init": hessian_init, "thresh": "gau_tight", } opt = RFOptimizer(geom, **opt_kwargs) opt.run() print("@\tcur_cycle:", opt.cur_cycle) assert opt.is_converged assert opt.cur_cycle == ref_cycle assert geom.energy == pytest.approx(-150.65298169)
def test_dftbp_opt(slakos, this_dir): geom = geom_loader("lib:h2o.xyz", coord_type="redund") if slakos: slakos = this_dir / slakos calc_kwargs = { "slakos": slakos, "parameter": "mio-ext", } calc = DFTBp(**calc_kwargs) geom.set_calculator(calc) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert opt.is_converged assert geom.energy == pytest.approx(-4.07793793)
def test_acetaldehyde_opt(): """ From https://doi.org/10.1016/S0166-1280(98)00475-8 """ geom = geom_loader("lib:acetaldehyd_oniom.xyz", coord_type="redund") real = set(range(len(geom.atoms))) high = [4, 5, 6] calcs = { "real": { "type": "g16", "route": "hf sto-3g" }, "high": { "type": "g16", "route": "b3lyp d95v" } } for key, calc in calcs.items(): calc["pal"] = 2 calc["mult"] = 1 calc["charge"] = 0 models = { "high": { "inds": high, "calc": "high", }, } oniom = ONIOM(calcs, models, geom, layers=None) geom.set_calculator(oniom) opt = RFOptimizer(geom, thresh="gau_tight") opt.run() assert geom.energy == pytest.approx(-153.07526171) hess_result = do_final_hessian(geom, save_hessian=False) nus = hess_result.nus print("wavenumbers / cm⁻¹:", nus) assert nus[-1] == pytest.approx(3759.5872) assert nus[-3] == pytest.approx(3560.9944)
def test_baker_gs_opt(name, geom, ref_energy, results_bag): opt_kwargs = { "thresh": "baker", } print(f"@Running {name}") pal = min(os.cpu_count(), 4) geom.set_calculator(PySCF(basis="sto3g", pal=pal)) opt = RFOptimizer(geom, **opt_kwargs) opt.run() results_bag.cycles = opt.cur_cycle + 1 results_bag.is_converged = opt.is_converged results_bag.energy = geom.energy results_bag.ref_energy = ref_energy assert np.allclose(geom.energy, ref_energy) return opt.cur_cycle + 1, opt.is_converged
def test_diis(): geom = AnaPot.get_geom((0.4333, 3.14286, 0.)) opt_kwargs = { # "max_step": 0.5, # "trust_max": 0.1, "thresh": "gau_tight", # "eins": True, "zwei": True, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() calc = geom.calculator calc.plot() ax = calc.ax cs = np.array(opt.coords) ax.plot(*cs.T[:2], "ro-") plt.show()
def test_rfoptimizer(): kwargs = { #"max_cycles": 10, "trust_radius": 0.5, "max_step": 0.5, } atoms = ("X", ) # http://www.applied-mathematics.net/optimization/optimizationIntro.html coords = (0.7, -3.3, 0) geom = Geometry(atoms, coords) ap4 = AnaPot4() geom.set_calculator(ap4) opt = RFOptimizer(geom, **kwargs) #opt = SteepestDescent(geom, **kwargs) #opt = BFGS(geom, max_step=1.0) opt.run() rfop = RFOPlotter(ap4, opt, save=True, title=False) rfop.plot() plt.show()
def test_pyscf_cytosin_td_opt(): calc_kwargs = { "xc": "pbe0", "method": "tddft", # "method": "dft", "basis": "def2SVP", # "auxbasis": "weigend", # "basis": "sto3g", "pal": 4, "nstates": 2, "root": 1, "track": True, "ovlp_with": "adapt", "ovlp_type": "tden", } pyscf_ = PySCF(**calc_kwargs) geom = geom_from_library("cytosin.xyz", coord_type="redund") geom.set_calculator(pyscf_) opt = RFOptimizer(geom) opt.run()
def run_geom_opt(self, geom): if self.calc_getter is not None: calc = self.calc_getter(calc_number=self.calc_counter) geom.set_calculator(calc) opt_kwargs = { "gdiis": False, "thresh": "gau_loose", "overachieve_factor": 2, "max_cycles": 75, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() opt_geom = geom if opt.is_converged else None else: calc = XTB(calc_number=self.calc_counter, **self.calc_kwargs) opt_result = calc.run_opt(geom.atoms, geom.coords, keep=False) opt_geom = opt_result.opt_geom self.calc_counter += 1 return opt_geom
def test_three_frag_afir(): geom = geom_loader("lib:afir3test.xyz", coord_type="redund") fragment_indices = [ (0, 1, 2), (3, 4, 5, 6), ] calc = XTB() gamma = 150 afir = AFIR(calc, fragment_indices, gamma, ignore_hydrogen=False) geom.set_calculator(afir) opt = RFOptimizer(geom, dump=True, overachieve_factor=2) opt.run() assert opt.is_converged assert opt.cur_cycle == 28 assert geom.energy == pytest.approx(-22.57975056) c3d = geom.coords3d assert np.linalg.norm(c3d[3] - c3d[9]) == pytest.approx(2.6099288505) assert np.linalg.norm(c3d[2] - c3d[0]) == pytest.approx(3.96382029)
def run(): xyz_fn = "fluorethylene.xyz" geom = geom_from_library(xyz_fn, coord_type="dlc") freeze = ((0, 1), (2, 0, 3)) geom.internal.freeze_primitives(freeze) # XTB calc = XTB() # Psi4 # from pysisyphus.calculators.Psi4 import Psi4 # calc = Psi4(method="hf", basis="sto-3g") geom.set_calculator(calc) opt_kwargs = { # "max_cycles": 1, "thresh": "gau_tight", "trust_max": 0.3, "trust_radius": 0.1, } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged
def test_mb_velocities(): geom = geom_loader("lib:h2o.xyz") geom.set_calculator(PySCF(basis="sto3g")) # Preoptimization opt = RFOptimizer(geom, thresh="gau_tight") opt.run() print() T = 298.15 seed = 20182503 v0 = get_mb_velocities_for_geom(geom, T, seed=seed).flatten() steps = 100 dt = 0.5 res = md(geom, v0, steps, dt) assert dt * steps / 1000 == pytest.approx(res.t) # import pdb; pdb.set_trace() from pysisyphus.xyzloader import coords_to_trj coords = res.coords trj_fn = "md.trj" atoms = geom.atoms coords_to_trj(trj_fn, atoms, coords)
def test_openmolcas_s1_opt(this_dir): geom = geom_loader(this_dir / "trans_butadien.xyz", coord_type="redund") inporb_fn = this_dir / "butadien_vdzp.RasOrb" kwargs = { "basis": "ano-rcc-vdzp", "inporb": inporb_fn, "charge": 0, "mult": 1, "roots": 5, "mdrlxroot": 2, } calc = OpenMolcas(**kwargs) geom.set_calculator(calc) opt_kwargs = { "dump": True, "thresh": "gau", } opt = RFOptimizer(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 6 assert geom.energy == pytest.approx(-154.8758862347)