def tmp(geom, mode_ind, nu_thresh=-5.): org_coords = geom.coords.copy() hessian = geom.hessian mw_hess = geom.mw_hessian start_result = do_analysis(geom, nu_thresh) print(f"Distorting along mode #{mode_ind}") mode = start_result.v[:, mode_ind] # lengths = np.linspace(-2, 2, 41) lengths = np.linspace(-1.5, 1.5, 31) print(lengths) coords_list = list() M = np.sqrt(geom.masses_rep) results = list() mw_coords = geom.mw_coords.copy() for l in lengths: print(f"l={l:02f}") new_coords = (mw_coords + l * mode) / M geom.coords = new_coords # res = do_analysis(geom) # sys.stdout.flush() # results.append(res) coords_list.append(new_coords) coords_to_trj("displaced.trj", geom.atoms, coords_list) # with open("res", "wb") as handle: cloudpickle.dump(results, handle) with open("res", "rb") as handle: results = cloudpickle.load(handle) energies = np.array([r.energy for r in results]) energies -= energies.min() energies *= 2625.50 grad_norms = [np.linalg.norm(r.gradient) for r in results] neg_nus = np.array([r.nus[:2] for r in results]) fig, (ax, ax2, ax3) = plt.subplots(nrows=3, sharex=True) ax.plot(lengths, energies, "o--") ax.set_title("Energies") ax.set_ylabel("$\Delta$E / kJ mol⁻¹") ax2.plot(lengths, grad_norms, "o--") ax2.set_ylabel("Hartree Bohr⁻¹") ax2.set_title("norm(gradient)") width = .025 for i, modes in enumerate(neg_nus.T): ax3.bar(lengths + i * width, modes, width=width) ax3.set_xlabel("l") ax3.set_ylabel(r"$\nu$ / cm⁻¹") plt.tight_layout() plt.show() fig.savefig("imgkill.pdf")
def test_rattle_tip3p(this_dir): geom = geom_loader(this_dir / "output_10.xyz") # geom = geom_loader(this_dir / "output_2.xyz") # geom.jmol() T = 298.15 calc = TIP3P() potentials = [ { "type": "logfermi", "beta": 6, "T": T, "radius": 10, }, ] ext_calc = ExternalPotential(calc, potentials=potentials) geom.set_calculator(ext_calc) constraints = list( it.chain( *[get_water_constraints(i) for i in range(len(geom.atoms) // 3)])) seed = 20200626 v0 = get_mb_velocities_for_geom(geom, T, seed=seed).flatten() md_kwargs = { "v0": v0, # "steps": 100, # "dt": 1, "steps": 250, "dt": 1.5, # "steps": 500, # "dt": 0.25, "constraints": constraints, "constraint_kwargs": { # "remove_com_v": False, }, # "thermostat": "csvr", } # import pdb; pdb.set_trace() md_result = md(geom, **md_kwargs) from pysisyphus.xyzloader import coords_to_trj coords = md_result.coords trj_fn = "md.trj" atoms = geom.atoms coords_to_trj(trj_fn, atoms, coords)
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_freq_distort(): geom = geom_from_xyz_file("final_geometry.xyz") print(geom) # calc = ORCA("") # print(calc) # cwd = Path(".") # results = calc.parse_hessian(cwd) # print(results) # hess = results["hessian"] # np.save("hess", hess) # hess = np.load("hess.npy") hess = np.loadtxt("calculated_final_cart_hessian") # print("Non-mass-weighted") # w, v = np.linalg.eigh(hess) # nus = eigval_to_wavenumber(w) # neg_inds = w < 0 # print(w[neg_inds]) # print(nus[neg_inds]) # print("Mass-weighted") mw_hess = geom.mass_weigh_hessian(hess) # w, v = np.linalg.eigh(mw_hess) # nus = eigval_to_wavenumber(w) # neg_inds = w < 0 # print(w[neg_inds]) # print(nus[neg_inds]) # shaked = geom.copy() # shaked.coords = shake_coords(shaked.coords, scale=0.05, seed=25032018) # shaked.set_calculator(XTB(pal=4, base_name="shake")) # shess = shaked.hessian # sw, sv = np.linalg.eigh(shess) # with open("shaked.xyz", "w") as handle: # handle.write(shaked.as_xyz()) print("Eckart projected") proj_hess = geom.eckart_projection(mw_hess) w, v = np.linalg.eigh(proj_hess) nus = eigval_to_wavenumber(w) neg_inds = w < 0 print(w[neg_inds]) print(nus[neg_inds]) imag_mode = v[:, 0] print(imag_mode) lengths = np.linspace(-1, 1, 21) print(lengths) coords_list = list() M = np.sqrt(geom.masses_rep) for l in lengths: new_coords = (geom.mw_coords + l * imag_mode) / M coords_list.append(new_coords) coords_to_trj("displaced.trj", geom.atoms, coords_list) # calc = XTB(pal=4) # geom.set_calculator(calc) # energies = list() # for cs in coords_list: # geom.coords = cs # energy = geom.energy # energies.append(energy) # pass # print(energies) # energies = np.array(energies) # np.save("energies", energies) energies = np.load("energies.npy") energies -= energies.min() energies *= 2625.25 def func_harmonic(x, a, b, c): return a + b * (x + c)**2 fromto = 4 slice_ = slice(fromto, -fromto + 1) ydata = energies[slice_] xdata = np.arange(energies.size)[slice_] popt, pcov = curve_fit(func_harmonic, xdata, ydata) print("popt") print(popt) print("pcov") print(pcov) fig, ax = plt.subplots() ax.plot(energies, "o-", label="True") ax.plot(xdata, func_harmonic(xdata, *popt), "--", label="Harmonic") ax.legend() plt.show() pass
def freq_distort(geom, mode_ind): print(geom) hess = geom.hessian mw_hess = geom.mass_weigh_hessian(hess) # w, v = np.linalg.eigh(mw_hess) # nus = eigval_to_wavenumber(w) # neg_inds = w < 0 # print(w[neg_inds]) # print(nus[neg_inds]) # shaked = geom.copy() # shaked.coords = shake_coords(shaked.coords, scale=0.05, seed=25032018) # shaked.set_calculator(XTB(pal=4, base_name="shake")) # shess = shaked.hessian # sw, sv = np.linalg.eigh(shess) # with open("shaked.xyz", "w") as handle: # handle.write(shaked.as_xyz()) print("Eckart projected") proj_hess = geom.eckart_projection(mw_hess) w, v = np.linalg.eigh(proj_hess) nus = eigval_to_wavenumber(w) neg_inds = w < 0 print(w[neg_inds]) print(nus[neg_inds]) imag_mode = v[:, mode_ind] print(imag_mode) lengths = np.linspace(-1, 1, 21) print(lengths) coords_list = list() M = np.sqrt(geom.masses_rep) for l in lengths: new_coords = (geom.mw_coords + l * imag_mode) / M coords_list.append(new_coords) coords_to_trj("displaced.trj", geom.atoms, coords_list) energies = list() for cs in coords_list: geom.coords = cs energy = geom.energy energies.append(energy) pass print(energies) energies = np.array(energies) np.save("energies", energies) # energies -= energies.min() # energies *= 2625.25 def func_harmonic(x, a, b, c): return a + b * (x + c)**2 fromto = 4 slice_ = slice(fromto, -fromto + 1) ydata = energies[slice_] xdata = np.arange(energies.size)[slice_] popt, pcov = curve_fit(func_harmonic, xdata, ydata) print("popt") print(popt) print("pcov") print(pcov) fig, ax = plt.subplots() ax.plot(energies, "o-", label="True") ax.plot(xdata, func_harmonic(xdata, *popt), "--", label="Harmonic") ax.legend() plt.show() pass