def test_oniom_md():
    calc_dict = {
        "high": {
            "type": "pypsi4",
            "method": "scf",
            "basis": "sto-3g",
        },
        "low": {
            "type": "pyxtb",
        },
    }
    high_inds = (4, 5, 6)
    from pysisyphus.calculators.ONIOM import ONIOM
    oniom = ONIOM(calc_dict, high_inds)

    geom = geom_loader("lib:acetaldehyd_oniom.xyz")
    geom.set_calculator(oniom)

    v0 = .005 * np.random.rand(*geom.coords.shape)
    md_kwargs = {
        "v0": v0,
        "t": 40,
        "dt": 0.5,
    }
    md_result = md(geom, **md_kwargs)
    from pysisyphus.xyzloader import make_trj_str

    coords = md_result.coords.reshape(-1, len(geom.atoms), 3) * BOHR2ANG
    trj_str = make_trj_str(geom.atoms, coords)
    with open("md.trj", "w") as handle:
        handle.write(trj_str)
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")
Exemple #3
0
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)
Exemple #4
0
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)
Exemple #5
0
def run_md(geom, dt, steps, v0=None, term_funcs=None, external=False):
    if external and hasattr(geom.calculator, "run_md"):
        t = dt * steps
        t_ps = t * 1e-3
        md_kwargs = {
            "atoms": geom.atoms,
            "coords": geom.coords,
            "t": t,
            "dt": dt,
            "velocities": v0,
            "dump": dt,
        }
        print("Running MD with external calculator implementation.")
        if term_funcs is not None:
            print("Termination functions are not supported in external MD!")
        geoms = geom.calculator.run_md(**md_kwargs)
        md_result = MDResult(
            coords=[geom.coords for geom in geoms],
            t_ps=t_ps,
            step=int(t / dt - 1),
            terminated=None,
            T=None,
            E_tot=None,
        )
    else:
        md_kwargs = {
            "v0": v0,
            "steps": steps,
            "dt": dt,
            "term_funcs": term_funcs,
            "verbose": False,
            "remove_com_v": False,
        }
        print("Running MD with internal implementation.")
        md_result = md(geom, **md_kwargs)

    return md_result
def test_velocity_verlet():
    geom = AnaPot.get_geom((0.52, 1.80, 0))
    x0 = geom.coords.copy()
    v0 = .1 * np.random.rand(*geom.coords.shape)
    t = 3
    dts = (.005, .01, .02, .04, .08)
    all_xs = list()
    for dt in dts:
        geom.coords = x0.copy()
        md_kwargs = {
            "v0": v0.copy(),
            "t": t,
            "dt": dt,
        }
        md_result = md(geom, **md_kwargs)
        all_xs.append(md_result.coords)
    calc = geom.calculator
    calc.plot()
    ax = calc.ax
    for dt, xs in zip(dts, all_xs):
        ax.plot(*xs.T[:2], "o-", label=f"dt={dt:.3f}")
        # ax.plot(*xs.T[:2], "-", label=f"dt={dt:.3f}")
    ax.legend()
    plt.show()