Пример #1
0
    def test_restart_run_name_single_file(self):
        with temporary_directory() as tmp_dir:
            with temporary_cd(tmp_dir):
                Path("equil.inp").touch()
                restart_from, run_name = get_restart_name(None, None)
                assert run_name == "equil.rst.001"

        with temporary_directory() as tmp_dir:
            with temporary_cd(tmp_dir):
                Path("equil.rst.001.inp").touch()
                restart_from, run_name = get_restart_name(None, None)
                assert run_name == "equil.rst.002"
Пример #2
0
 def test_restart_run_name_multiple_invalid_files(self):
     with temporary_directory() as tmp_dir:
         with temporary_cd(tmp_dir):
             Path("prod.inp").touch()
             Path("equil.rst.001.inp").touch()
             Path("equil.rst.002.inp").touch()
             with pytest.raises(ValueError, match=r"Multiple"):
                 get_restart_name(None, None)
Пример #3
0
def main():
    # Create a water molecule with the spce geometry
    water = spce_water
    ff = foyer.Forcefield(get_ff("pore-spce-jc.xml"))
    water_typed = ff.apply(water)

    # Define conditions
    temperature = 298.0 * u.K
    # Define a range of (shifted) chemical potentials
    mus_adsorbate = np.arange(-58, -42, 2) * u.Unit("kJ/mol")

    # Define custom_args that are the same for all pure phase simulations
    custom_args = {
        "cutoff_style": "cut",
        "charge_style": "ewald",
        "rcut_min": 0.5 * u.angstrom,
        "vdw_cutoff": 9.0 * u.angstrom,
        "prop_freq": 10,
        "angle_style": ["fixed"],
    }

    for mu_adsorbate in mus_adsorbate:
        print(f"\nRun simulation: T = {temperature}, mu = {mu_adsorbate}\n")
        dirname = f"T_{temperature:0.1f}_mu_{mu_adsorbate:.1f}".replace(
            " ", "_"
        ).replace(
            "/", "-"
        )
        if not os.path.isdir(dirname):
            os.mkdir(dirname)
        else:
            pass
        with temporary_cd(dirname):
            # Box size depends on chemical potential
            # Test simulations show mu' = -48 kJ/mol; p ~ 0.01 bar
            # Employ IG law to estimate remaining box sizes; target 40 waters
            mu_0 = -48 * u.kJ/u.mol
            p_0 = 0.01 * u.bar
            n_water_target = 40
            p_ig = p_0 * np.exp((mu_adsorbate-mu_0)/(u.kb * temperature))
            vol = n_water_target * u.kb * temperature / p_ig
            boxl = (vol**(1./3.)).to_value("nm")
            custom_args["charge_cutoff"] = 0.25 * boxl * u.nm

            species_list = [water_typed]
            box_list = [mbuild.Box([boxl, boxl, boxl])]
            system = mc.System(
                box_list, species_list,
            )
            moveset = mc.MoveSet("gcmc", species_list)
            moveset.prob_regrow = 0.0
            moveset.prob_translate = 0.3
            moveset.prob_rotate = 0.3
            moveset.prob_insert = 0.2

            mc.run(
                system=system,
                moveset=moveset,
                run_type="equil",
                run_length=500000,
                temperature=temperature,
                run_name="equil",
                chemical_potentials=[mu_adsorbate],
                **custom_args,
            )

            mc.restart(
                system=system,
                moveset=moveset,
                run_type="prod",
                run_length=1000000,
                temperature=temperature,
                run_name="prod",
                restart_name="equil",
                chemical_potentials=[mu_adsorbate],
                **custom_args,
            )
Пример #4
0
def main():

    # Load TON from a CIF file, replicate the cell
    # Use mbuild to create a zeolite supercell from CIF
    cif_path = resource_filename(
        "mc_examples",
        "realistic_workflows/zeolite_adsorption/resources/structures/TON.cif")
    lattice = mbuild.lattice.load_cif(cif_path)
    compound_dict = {
        "Si": mbuild.Compound(name="Si"),
        "O": mbuild.Compound(name="O"),
    }
    zeolite = lattice.populate(compound_dict, 2, 2, 6)

    # Create a CG methane, load and apply ff
    methane = mbuild.Compound(name="_CH4")
    ff_path = resource_filename(
        "mc_examples",
        "realistic_workflows/zeolite_adsorption/resources/ffxml/adsorbates.xml",
    )
    ff_ads = foyer.Forcefield(ff_path)
    methane_ff = ff_ads.apply(methane)

    # Define pure fluid temperatures and chemical potentials
    temperatures = [298 * u.K, 309 * u.K, 350 * u.K]
    mus_fluid = np.arange(-49, -30, 3) * u.Unit("kJ/mol")

    # Define the pressures at which we wish to study adsorption
    pressures = [
        0.01,
        0.1,
        0.25,
        0.5,
        0.75,
        1.0,
        2.0,
        3.0,
        5.0,
    ] * u.bar

    # Select the zeolite ff
    zeo_ff_names = ["june", "trappe"]

    # Define a few custom_args that will be
    # the same for all zeolite simulations
    custom_args = {
        "charge_style": "none",
        "vdw_cutoff": 14.0 * u.angstrom,
        "prop_freq": 10,
        "max_molecules": [1, 10000],
    }

    # Loop over different zeolite ff's
    for zeo_ff_name in zeo_ff_names:

        # Load and apply ff to the zeolite structure
        ff_path = resource_filename(
            "mc_examples",
            f"realistic_workflows/zeolite_adsorption/resources/ffxml/zeo_{zeo_ff_name}.xml",
        )
        ff_zeo = foyer.Forcefield(ff_path)
        zeolite_ff = ff_zeo.apply(zeolite)

        # Create the box_list, species_list, System, and MoveSet.
        # These are not dependent upon (T,P) condition
        box_list = [zeolite]
        species_list = [zeolite_ff, methane_ff]
        mols_in_boxes = [[1, 0]]

        system = mc.System(box_list, species_list, mols_in_boxes=mols_in_boxes)
        moveset = mc.MoveSet("gcmc", species_list)

        # Loop over each temperature to compute an isotherm
        for temperature in temperatures:

            # Before we begin we must determine the
            # chemical potentials required to achieve
            # the desired pressures
            fluid_pressures = []
            for mu_fluid in mus_fluid:
                dirname = f"fluid_T_{temperature:0.1f}_mu_{mu_fluid:.1f}".replace(
                    " ", "_").replace("/", "-")
                thermo = ThermoProps(dirname + "/prod.out.prp")
                fluid_pressures.append(np.mean(thermo.prop("Pressure")))
            fluid_pressures = u.unyt_array(fluid_pressures)

            # Fit a line to mu vs. P
            slope, intercept, r_value, p_value, stderr = linregress(
                np.log(fluid_pressures.to_value(u.bar)).flatten(),
                y=mus_fluid.to_value("kJ/mol").flatten(),
            )
            # Determine chemical potentials
            mus = (slope * np.log(pressures.in_units(u.bar)) +
                   intercept) * u.Unit("kJ/mol")

            # Loop over each pressure and run the MC simulation!
            for (pressure, mu) in zip(pressures, mus):
                print(f"\nRun simulation: T = {temperature}, P = {pressure}\n")
                dirname = f"zeo_ff_{zeo_ff_name}_T_{temperature:0.1f}_P_{pressure:0.2f}".replace(
                    " ", "_").replace("/", "-")
                if not os.path.isdir(dirname):
                    os.mkdir(dirname)
                else:
                    pass
                with temporary_cd(dirname):

                    mc.run(
                        system=system,
                        moveset=moveset,
                        run_type="equil",
                        run_length=50000,
                        temperature=temperature,
                        run_name="equil",
                        chemical_potentials=["none", mu],
                        **custom_args,
                    )

                    mc.restart(
                        restart_from="equil",
                        run_name="prod",
                        run_type="prod",
                        total_run_length=200000,
                    )
Пример #5
0
def main():
    # Create a CG methane, load and apply ff
    methane = mbuild.Compound(name="_CH4")
    ff_path = resource_filename(
        "mc_examples",
        "realistic_workflows/zeolite_adsorption/resources/ffxml/adsorbates.xml",
    )
    ff_ads = foyer.Forcefield(ff_path)
    methane_ff = ff_ads.apply(methane)

    # Define a few keyword args that will be the
    # same for all fluid phase simulations
    custom_args = {
        "charge_style": "none",
        "vdw_cutoff": 14.0 * u.angstrom,
        "prop_freq": 10,
    }

    # Define temperatures and chemical potential values
    # for the single phase GCMC simulations
    temperatures = [298 * u.K, 309 * u.K, 350 * u.K]
    mus = np.arange(-49, -30, 3) * u.Unit("kJ/mol")

    # Loop over temperatures and mus and run simulations
    for temperature in temperatures:
        for mu in mus:

            # For each simulation -- first estimate
            # the box volume to target ~40 molecules
            n_methane_target = 40
            beta = 1.0 / (u.kb * temperature)
            mass = 16.04 * u.amu
            debroglie = np.sqrt(2 * np.pi * u.hbar**2 * beta / mass)
            vol = n_methane_target * debroglie**3 * np.exp(
                -beta.to("mol/kJ") * mu)
            boxl = (vol**(1.0 / 3.0)).to_value("nm")
            if boxl < 2.0 * custom_args["vdw_cutoff"].to_value("nm"):
                boxl = 2.0 * custom_args["vdw_cutoff"].to_value("nm")

            # Define the species list, box list, system, moveset
            # We start with an empty box
            species_list = [methane_ff]
            box_list = [mbuild.Box([boxl, boxl, boxl])]
            system = mc.System(box_list, species_list)
            moveset = mc.MoveSet("gcmc", species_list)

            # Create a new directory, temporary cd, and run
            # the equilibration and production simulations!
            print(f"\nRun simulation: T = {temperature}, mu = {mu}\n")
            dirname = f"fluid_T_{temperature:0.1f}_mu_{mu:.1f}".replace(
                " ", "_").replace("/", "-")
            if not os.path.isdir(dirname):
                os.mkdir(dirname)
            else:
                pass
            with temporary_cd(dirname):
                mc.run(
                    system=system,
                    moveset=moveset,
                    run_type="equil",
                    run_length=50000,
                    temperature=temperature,
                    run_name="equil",
                    chemical_potentials=[mu],
                    **custom_args,
                )

                mc.restart(
                    restart_from="equil",
                    run_name="prod",
                    run_type="prod",
                    total_run_length=400000,
                )