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_growing_string_climbing(gs_kwargs_, opt_ref_cycle, tsopt_ref_cycle): calc = AnaPot() geoms = calc.get_path(num=2) gs_kwargs = { "perp_thresh": 0.5, "reparam_check": "rms", } gs_kwargs.update(gs_kwargs_) cos = GrowingString(geoms, lambda: AnaPot(), **gs_kwargs) opt_kwargs = { "gamma_mult": True, "max_step": 0.04, "rms_force": 0.1, "rms_force_only": True, } opt = StringOptimizer(cos, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == opt_ref_cycle # calc = AnaPot() # calc.anim_opt(opt, show=True) hei_geom = cos.images[cos.get_hei_index()] tsopt = RSIRFOptimizer(hei_geom, thresh="gau_vtight") tsopt.run() assert tsopt.is_converged assert tsopt.cur_cycle == tsopt_ref_cycle
def test_neb_springs(neb_kwargs, ref_cycle): calc = AnaPot() geoms = calc.get_path(15) neb = NEB(geoms, **neb_kwargs) opt = SteepestDescent(neb) opt.run() # calc.anim_opt(opt, show=True) assert (opt.is_converged) assert (opt.cur_cycle == ref_cycle)
def test_freeend_anapot(): calc = AnaPot() geoms = calc.get_path(15) geoms = geoms[3:11] fe_neb = FreeEndNEB(geoms) opt = SteepestDescent(fe_neb) opt.run() # calc.anim_opt(opt, show=True) assert opt.is_converged assert opt.cur_cycle == 31
def test_anapot_aneb(): calc = AnaPot() all_geoms = calc.get_path(10) aneb = AdaptiveNEB(all_geoms) opt_kwargs = { "rms_force": 3e-3, } opt = ConjugateGradient(aneb, **opt_kwargs) opt.run() # ap = calc.anim_opt(opt, show=True) assert opt.is_converged assert opt.cur_cycle == 21
def animate(opt): xlim = (-2, 2.5) ylim = (0, 5) levels = (-3, 4, 80) ap = AnimPlot(AnaPot(), opt, xlim=xlim, ylim=ylim, levels=levels) ap.animate() return ap
def get_geoms(coords=None): if coords is None: # left = np.array((0.188646, 1.45698, 0)) # right = np.array((0.950829, 1.54153, 0)) left = np.array((0.354902, 1.34229, 0)) right = np.array((0.881002, 1.71074, 0)) right = np.array((0.77, 1.97, 0)) # Very close # left = np.array((0.531642, 1.41899, 0)) # right = np.array((0.702108, 1.57077, 0)) coords = (right, left) # near_ts = np.array((0.553726, 1.45458, 0)) # coords = (near_ts, ) # left_far = np.array((-0.455116, 0.926978, 0)) # right_far = np.array((-0.185653, 1.02486, 0)) # coords = (left_far, right_far) atoms = ("H") geoms = [Geometry(atoms, c) for c in coords] for geom in geoms: geom.set_calculator(AnaPot()) return geoms
def test_anapot_growingstring_opt(): coords = ( (-1.05274, 1.02776, 0), (1.94101, 3.85427, 0), ) calc_getter = AnaPot eps = .05 damp = .05 images = get_geoms(coords, calc_getter) gs_kwargs = { "max_nodes": 10, "perp_thresh": 0.5, # "perp_thresh": 1, } gs = GrowingString(images, calc_getter, reparam_every=1) # from pysisyphus.optimizers.QuickMin import QuickMin # opt = QuickMin(gs) # self.coords = [c.reshape(-1, 3) for c in self.gs.coords_list] # self.tangents = self.gs.tangent_list # self.perp_forces = self.gs.perp_force_list from pysisyphus.optimizers.SteepestDescent import SteepestDescent # opt = SteepestDescent(gs, alpha=0.05, bt_disable=True, max_cycles=175) opt = SteepestDescent(gs, alpha=0.05, bt_disable=True, max_cycles=70) opt.run() xlim = (-2, 2.5) ylim = (0, 5) levels = (-3, 4, 80) ap = AnimPlot(AnaPot(), opt, xlim=xlim, ylim=ylim, levels=levels) ap.animate()
def test_optimizers(opt_cls, opt_kwargs_, ref_cycle): geom = AnaPot.get_geom((0.667, 1.609, 0.)) ref_coords = np.array((1.941, 3.8543, 0.)) opt_kwargs = { "thresh": "gau_tight", "dump": False, "overachieve_factor": 2., } opt_kwargs.update(opt_kwargs_) opt = opt_cls(geom, **opt_kwargs) opt.run() # 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 diff = ref_coords - geom.coords diff_norm = np.linalg.norm(diff) print(f"@\tnorm(diff)={diff_norm:.8f}") assert diff_norm < 6e-5
def test_sqnm(): # geom = AnaPot.get_geom((0, 4, 0)) geom = AnaPot.get_geom((-0.8, 1.73, 0)) opt_kwargs = { "max_cycles": 15, "eps": 1e-4, "E_thresh": 1e-4, "alpha": 0.5, "hist_max": 10, # "thresh": "gau_tight", "trust_radius": 0.1, # "bio": False, "dump": True, } opt = StabilizedQNMethod(geom, **opt_kwargs) # opt = SQNM3(geom, **opt_kwargs) opt.run() c = np.array(opt.coords) calc = geom.calculator calc.plot() ax = calc.ax ax.plot(c[:, 0], c[:, 1], "o-") plt.show()
def test_dimer(rotation_method, ref_cycle): coords = (-0.2, 1.1, 0) geom = Geometry(("X", ), coords) N_raw = np.array((0.83, 0.27, 0.)) # New implementation dimer_kwargs = { "rotation_method": rotation_method, "calculator": AnaPot(), "N_raw": N_raw, "rotation_remove_trans": False, } dimer = Dimer(**dimer_kwargs) geom.set_calculator(dimer) opt_kwargs = { "precon": False, "line_search": None, "max_step_element": 0.25, "thresh": "gau_tight", "max_cycles": 15, } opt = PreconLBFGS(geom, **opt_kwargs) opt.run() # AnaPot().plot_opt(opt) assert opt.is_converged assert opt.cur_cycle == ref_cycle assert geom.energy == pytest.approx(2.80910484)
def test_dump_neb(): # NEB geoms = AnaPot().get_path(10) cos = NEB(geoms) opt_kwargs = { "dump": True, # "rms_force": 1.0, "h5_group_name": "neb", } opt = SteepestDescent(cos, **opt_kwargs) # opt = SteepestDescent(geoms[7], **opt_kwargs) opt.run() # GrowingString geoms = AnaPot().get_path(10) gsm = GrowingString(geoms, calc_getter=AnaPot, perp_thresh=0.25) opt_kwargs = { "dump": True, "h5_group_name": "gsm", "gamma": 10., "stop_in_when_full": 0, } opt = StringOptimizer(gsm, **opt_kwargs) 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() return hei_coords, *_ = cos.get_splined_hei() ts_geom = geoms[0].copy() ts_geom.coords = hei_coords ts_geom.set_calculator(AnaPot()) ts_opt_kwargs = { # "dump": True, "thresh": "gau_tight", } ts_opt = RSIRFOptimizer(ts_geom, **ts_opt_kwargs) ts_opt.run()
def run_dimer(): x0 = (-0.5767, 1.6810, 0) geom0 = AnaPot.get_geom(x0) N = np.array((-0.9, 0.43, 0)) N /= np.linalg.norm(N) R = 0.125 coords = dimer_method(geom0, N, R, AnaPot, max_step=0.6, max_cycles=5) coords = np.array(coords) pot = AnaPot() pot.plot() ax = pot.ax for i, rot_cycle in enumerate(coords): ax.plot(*rot_cycle.T[:2], "o-", label=f"Cycle {i:02d}") ax.legend() plt.show()
def run_lanczos(): x0 = (-0.5767, 1.6810, 0) geom0 = AnaPot.get_geom(x0) w_min, v_min = lanczos(geom0, dx=1e-5) H = geom0.hessian w, v = np.linalg.eigh(H) w_min_ref = w[0] print(f"w_min_ref={w_min_ref:.6f}")
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_conjugate_gradient(): calc = AnaPot() geoms = calc.get_path(2) gs_kwargs = { "perp_thresh": 0.5, "reparam_check": "rms", } gs = GrowingString(geoms, lambda: AnaPot(), **gs_kwargs) opt_kwargs = { "keep_last": 0, "rms_force": 0.02, "rms_force_only": True, } opt = StringOptimizer(gs, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == 23
def test_anapot_lanczos(): geom = AnaPot.get_geom((-0.5767, 1.6810, 0.)) guess = (0.4, 0.3, 0.0) w_min, v_min = geom_lanczos(geom, guess=guess, dx=1e-5, dl=1e-5, logger=logger) H = geom.hessian w, v = np.linalg.eigh(H) w_ref = w[0] v_ref = v[:,0] assert w_min == pytest.approx(w_ref, abs=1e-4) assert any([np.allclose(v, v_ref, atol=5e-2) for v in (v_min, -v_min)])
def ase_md_playground(): geom = AnaPot.get_geom((0.52, 1.80, 0), atoms=("H", )) atoms = geom.as_ase_atoms() # ase_calc = FakeASE(geom.calculator) # from ase.optimize import BFGS # dyn = BFGS(atoms) # dyn.run(fmax=0.05) import ase from ase import units from ase.io.trajectory import Trajectory from ase.md.velocitydistribution import MaxwellBoltzmannDistribution from ase.md.verlet import VelocityVerlet MaxwellBoltzmannDistribution(atoms, 300 * units.kB) momenta = atoms.get_momenta() momenta[0, 2] = 0. # Zero 3rd dimension atoms.set_momenta(momenta) dyn = VelocityVerlet(atoms, .005 * units.fs) # 5 fs time step. def printenergy(a): """Function to print the potential, kinetic and total energy""" epot = a.get_potential_energy() / len(a) ekin = a.get_kinetic_energy() / len(a) print('Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) ' 'Etot = %.3feV' % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin)) # Now run the dynamics printenergy(atoms) traj_fn = 'asemd.traj' traj = Trajectory(traj_fn, 'w', atoms) dyn.attach(traj.write, interval=5) # dyn.attach(bumms().bimms, interval=1) dyn.run(10000) printenergy(atoms) traj.close() traj = ase.io.read(traj_fn+"@:")#, "r") pos = [a.get_positions() for a in traj] from pysisyphus.constants import BOHR2ANG pos = np.array(pos) / BOHR2ANG calc = geom.calculator calc.plot() ax = calc.ax ax.plot(*pos[:,0,:2].T) plt.show()
def test_tshessian_opts(opt_cls, ref_cur_cycle): geom = AnaPot.get_geom((-0.6, 2.2, 0.)) opt_kwargs = { "trust_radius": 0.2, "dump": False, } opt = opt_cls(geom, **opt_kwargs) opt.run() assert opt.is_converged assert opt.cur_cycle == ref_cur_cycle
def test_anapot_cbm_rot(): pot = AnaPot() geoms = (pot.get_geom((0.44, 1.54, 0)), ) N_init = (-0.2, 1, 0) calc_getter = AnaPot dimer_kwargs = { "ana_2dpot": True, "restrict_step": "max", "angle_tol": 0.5, "f_thresh": 1e-4, "rot_opt": "lbfgs", "trans_opt": "lbfgs", "trans_memory": 5, "f_tran_mod": False, "N_init": N_init, "rot_f_thresh": 1e-2, # "multiple_translations": True, } result = dimer_method(geoms, calc_getter, **dimer_kwargs) true_ts = (0.61173, 1.49297, 0.) # plot_dimer_cycles(result.dimer_cycles, pot=AnaPot(), true_ts=true_ts[:2]) return result
def test_anapot_irc(irc_cls, mod_kwargs, ref): geom = AnaPot().get_geom((0.61173, 1.49297, 0.)) kwargs = { "step_length": 0.1, "rms_grad_thresh": 1e-2, } kwargs.update(**mod_kwargs) irc = irc_cls(geom, **kwargs) irc.run() # geom.calculator.plot_irc(irc, show=True) assert_anapot_irc(irc)
def run_cos_opt(geoms, between, calc_cls, cos_cls, cos_kwargs, opt_cls, opt_kwargs): interpol = Interpolator(geoms, between=between) images = interpol.interpolate_all() for image in images: image.set_calculator(AnaPot()) cos = cos_cls(images, **cos_kwargs) opt = opt_cls(cos, **opt_kwargs) opt.run() return opt
def test_anapot_irc(irc_cls, mod_kwargs, ref): geom = AnaPot().get_geom((0.61173, 1.49297, 0.)) kwargs = { "step_length": 0.1, "rms_grad_thresh": 1e-2, } kwargs.update(**mod_kwargs) irc = irc_cls(geom, **kwargs) irc.run() # plot_irc(irc, irc.__class__.__name__) assert_anapot_irc(irc)
def test_anapot_growing_string(keep_last, ref_cycle): initial = AnaPot.get_geom((-1.05274, 1.02776, 0)) final = AnaPot.get_geom((1.94101, 3.85427, 0)) geoms = (initial, final) gs_kwargs = { "perp_thresh": 0.5, "reparam_check": "rms", } gs = GrowingString(geoms, lambda: AnaPot(), **gs_kwargs) opt_kwargs = { # "stop_in_when_full": 3, "stop_in_when_full": keep_last, "keep_last": keep_last, } opt = StringOptimizer(gs, **opt_kwargs) opt.run() # calc = AnaPot() # calc.anim_opt(opt, show=True) assert opt.is_converged assert opt.cur_cycle == ref_cycle
def test_climb_lanczos(): calc = AnaPot() geoms = calc.get_path(2) gs_kwargs = { "perp_thresh": 0.5, "reparam_check": "rms", "climb": True, "climb_rms": 0.2, "climb_lanczos": True, } gs = GrowingString(geoms, lambda: AnaPot(), **gs_kwargs) opt_kwargs = { "keep_last": 0, "rms_force": 0.02, "rms_force_only": True, } opt = StringOptimizer(gs, **opt_kwargs) opt.run() # calc.anim_opt(opt, show=True) assert opt.is_converged assert opt.cur_cycle == 23
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 run_dimer(): x0 = (-0.5767, 1.6810, 0) geom0 = AnaPot.get_geom(x0) N = np.array((-0.9, 0.43, 0)) N /= np.linalg.norm(N) R = 0.125 geom1, geom2 = get_dimer_ends(geom0, N, R, AnaPot) coords = list() for i in range(35): f0 = geom0.forces norm_f0 = np.linalg.norm(f0) print(f"{i:0d} {norm_f0:.6f}") if norm_f0 < 1e-3: print("Converged!") break # Rotation rot_result = rotate_dimer(geom0, geom1, geom2, N, R) geom1, geom2, N, C, _ = rot_result # Translation trans_result = translate_dimer(geom0, N, C) update_dimer_ends(geom0, geom1, geom2, N, R) # Backup for plotting cs = (geom1.coords, geom0.coords, geom2.coords) coords.append(cs) # geom0.calculator.plot_eigenvalue_structure(grid=100) coords = np.array(coords) pot = AnaPot() pot.plot() ax = pot.ax for i, rot_cycle in enumerate(coords): ax.plot(*rot_cycle.T[:2], "o-", label=f"Cycle {i:02d}") ax.legend() plt.show()
def test_eulerpc_scipy(scipy_method): geom = AnaPot().get_geom((0.61173, 1.49297, 0.)) kwargs = { "step_length": 0.2, "rms_grad_thresh": 1e-2, "corr_func": "scipy", "scipy_method": scipy_method, } irc = EulerPC(geom, **kwargs) irc.run() # plot_irc(irc, irc.__class__.__name__) assert_anapot_irc(irc)
def test_add_gaussian(): geom = AnaPot.get_geom((-0.2, 1.1, 0)) N_raw = np.array((0.3, 0.7, 0.)) calc = geom.calculator dimer_kwargs = { "calculator": calc, "N_raw": N_raw, } dimer = Dimer(**dimer_kwargs) geom.set_calculator(dimer) g0 = dimer.add_gaussian(geom.atoms, geom.coords, dimer.N) assert g0.height == pytest.approx(0.1708984)
def test_imk(step_length): geom = AnaPot().get_geom((0.61173, 1.49297, 0.)) irc_kwargs = { "step_length": step_length, "rms_grad_thresh": 1e-2, "corr_first": True, "corr_first_energy": True, "corr_bisec_size": 0.0025, "corr_bisec_energy": True, } irc = IMKMod(geom, **irc_kwargs) irc.run() # plot_irc(irc, irc.__class__.__name__) assert_anapot_irc(irc)