Esempio n. 1
0
    def test_two_particle(self):
        """Test the LJ potential against analytical calculation of two particles"""
        params = {"displacement": 1.2345}
        with open("two.xyz", "w") as myfile:
            myfile.write("""2
-1 8 8 8
0 0 0 0
1 0 0 {displacement}""".format(**params))
        run_fst({
            "config_params":
            "particle_type0 /feasst/forcefield/lj.fstprt xyz_file two.xyz"
        })
        mc = fst.MonteCarlo().deserialize(
            pyfeasst.read_checkpoint("checkpoint.fst"))
        self.assertEqual(mc.configuration().num_particles(), 2)

        # compute the expected analytical LJ and LRC energies
        enlj = 8 * (params["displacement"]**(-12) -
                    params["displacement"]**(-6))
        rcut = mc.system().configuration().model_params().select(
            "cutoff").value(0)
        enlrc = (8./3.)*fst.PI*mc.system().configuration().num_particles()**2/ \
            mc.configuration().domain().volume()*((1./3.)*rcut**(-9) - rcut**(-3))

        # Compare the analytical results with the FEASST computed energies.
        # The energies of the individual potentials (e.g., LJ and LRC) are stored as profiles with
        # indices based on the order that the potentials were initialized.
        # Thus, profile index 0 refers to LJ while 1 refers to LRC.
        # In addition, the last computed value of the energy of all potentials is also stored.
        self.assertAlmostEqual(enlj,
                               mc.system().stored_energy_profile()[0], 15)
        self.assertAlmostEqual(enlrc,
                               mc.system().stored_energy_profile()[1], 15)
        self.assertAlmostEqual(enlj + enlrc, mc.system().stored_energy(), 15)
Esempio n. 2
0
 def test_srsw_ref_config(self):
     """Test the LJ potential against a configuration of 30 particles.
     In particular, the 4th configuration of the LJ SRSW reference:
     https://www.nist.gov/mml/csd/chemical-informatics-research-group/lennard-jones-fluid-reference-calculations
     """
     run_fst({
         "config_params":
         "cubic_box_length 8 particle_type0 /feasst/forcefield/lj.fstprt \
               xyz_file /feasst/plugin/configuration/test/data/lj_sample_config_periodic4.xyz"
     })
     mc = fst.MonteCarlo().deserialize(
         pyfeasst.read_checkpoint("checkpoint.fst"))
     self.assertEqual(mc.configuration().num_particles(), 30)
     enlj = -16.790321304625856
     enlrc = -0.5451660014945704
     self.assertEqual(enlj, mc.system().stored_energy_profile()[0], 15)
     self.assertAlmostEqual(enlrc,
                            mc.system().stored_energy_profile()[1], 15)
     self.assertAlmostEqual(enlj + enlrc, mc.system().energy(), 15)
Esempio n. 3
0
 def test_srsw_ref_config_triclinic(self):
     """Test the LJ potential against a configuration of 300 particles in a trinclinic cell.
     In particular, the 3th configuration of the triclinic LJ SRSW reference:
     https://www.nist.gov/mml/csd/chemical-informatics-group/lennard-jones-fluid-reference-calculations-non-cuboid-cell
     """
     run_fst({
         "config_params":
         "side_length0 10.0 side_length1 9.84807753012208 side_length2 9.64974312607518 \
         xy 1.7364817766693041 xz 2.5881904510252074 yz 0.42863479791864567 \
         particle_type0 /feasst/forcefield/lj.fstprt \
         xyz_file /feasst/plugin/configuration/test/data/lj_triclinic_sample_config_periodic3.xyz"
     })
     mc = fst.MonteCarlo().deserialize(
         pyfeasst.read_checkpoint("checkpoint.fst"))
     self.assertEqual(mc.configuration().num_particles(), 300)
     enlj = -505.78567945268367
     enlrc = -29.37186430697248
     self.assertEqual(enlj, mc.system().stored_energy_profile()[0], 15)
     self.assertAlmostEqual(enlrc,
                            mc.system().stored_energy_profile()[1], 15)
     self.assertAlmostEqual(enlj + enlrc, mc.system().energy(), 15)
Esempio n. 4
0
def monte_carlo(
        proc=0,  # processor number
        criteria=criteria_flathist(),  # flat histogram criteria
        steps_per=1e5,  # steps per analysis
        system=lj_system.system(
            lj_system.configuration(box_length=8, forcefield="data.lj")),
        run=True  # run the simulation
):
    """Create, run and return a flat histogram grand canonical Monte Carlo simulation"""
    monte_carlo0 = fst.MonteCarlo()
    monte_carlo0.set(system)

    # add the minimum number of particles
    monte_carlo0.set(
        fst.MakeMetropolis(fst.args({
            "beta": "0.1",
            "chemical_potential": "1"
        })))
    monte_carlo0.add(
        fst.MakeTrialTranslate(
            fst.args({
                "weight": "0.375",
                "tunable_param": "2."
            })))
    if monte_carlo0.system().configuration().particle_type(0).num_sites() > 1:
        monte_carlo0.add(
            fst.MakeTrialRotate(
                fst.args({
                    "weight": "0.375",
                    "tunable_param": "2."
                })))
    fst.add_trial_transfer(monte_carlo0,
                           fst.args({
                               "weight": "0.125",
                               "particle_type": "0"
                           }))

    min_macro = int(criteria.macrostate().histogram().center_of_bin(0))
    # print("seeking", min_macro)
    fst.SeekNumParticles(min_macro).run(monte_carlo0)

    # replace the acceptance criteria with flat histogram
    monte_carlo0.set(criteria)

    analyze.add(monte_carlo0,
                steps_per,
                proc=proc,
                log="log" + str(proc) + ".txt")

    # periodically write the status of the flat histogram criteria
    monte_carlo0.add(
        fst.MakeCriteriaUpdater(fst.args({"steps_per": str(steps_per)})))
    monte_carlo0.add(
        fst.MakeCriteriaWriter(
            fst.args({
                "steps_per": str(steps_per),
                "file_name": "crit" + str(proc) + ".txt"
            })))

    # periodically write a checkpoint file
    monte_carlo0.add(
        fst.MakeCheckpoint(
            fst.args({
                "file_name": "checkpoint" + str(proc) + ".txt",
                "num_hours": "0.01"
            })))

    # periodically write the energy of the macrostates
    monte_carlo0.add(
        fst.MakeEnergy(
            fst.args({
                "file_name": "energy" + str(proc) + ".txt",
                "steps_per_update": "1",
                "steps_per_write": str(steps_per),
                "multistate": "true"
            })))

    if run:
        monte_carlo0.run_until_complete()
    #print(monte_carlo0.criteria().write())
    return monte_carlo0
Esempio n. 5
0
                    type=int,
                    help="number of trials for equilibration",
                    default=int(5e7))
parser.add_argument(
    "--trials",
    type=int,
    help="total number of trials (equilibration and production)",
    default=int(3e8))
parser.add_argument("--num_hours",
                    type=float,
                    help="number of hours before restart",
                    default=1.)
args = parser.parse_args()
print("args:", args)

mc = fst.MonteCarlo()
if args.task > 0:
    mc = fst.MakeMonteCarlo("checkpoint.fst")
    mc.attempt(args.trials - mc.trials().num_attempts())
    quit()
mc.set(fst.MakeRandomMT19937(fst.args({"seed": args.seed})))
mc.add(
    fst.Configuration(
        fst.MakeDomain(
            fst.args({
                "cubic_box_length":
                str((args.num / args.density)**(1. / 3.))
            })), fst.args({"particle_type": args.data})))
mc.add(fst.MakePotential(fst.MakeLennardJones()))
mc.add(fst.MakePotential(fst.MakeLongRangeCorrections()))
mc.set(fst.MakeThermoParams(fst.args({"beta": str(args.beta)})))
Esempio n. 6
0
import feasst

monte_carlo = feasst.MonteCarlo()
monte_carlo.set(feasst.MakeRandomMT19937(feasst.args({"seed": "time"})))
monte_carlo.add(
    feasst.Configuration(
        feasst.MakeDomain(feasst.args({"cubic_box_length": "8"})),
        feasst.args(
            {"particle_type": feasst.install_dir() + "/forcefield/data.lj"})))
monte_carlo.add(feasst.Potential(feasst.MakeLennardJones()))
monte_carlo.add(feasst.Potential(feasst.MakeLongRangeCorrections()))
monte_carlo.add(feasst.MakeMetropolis(feasst.args({"beta": "1.5"})))
monte_carlo.add(
    feasst.MakeTrialTranslate(
        feasst.args({
            "tunable_param": "2.",
            "tunable_target_acceptance": "0.2"
        })))
steps_per = int(1e3)
monte_carlo.add(feasst.MakeTuner(feasst.args({"steps_per": str(steps_per)})))
feasst.SeekNumParticles(50)\
    .with_metropolis(feasst.args({"beta": "0.1", "chemical_potential": "10"}))\
    .with_trial_add().run(monte_carlo)
monte_carlo.add(feasst.MakeLog(feasst.args({"steps_per": str(steps_per)})))
monte_carlo.add(
    feasst.MakeMovie(
        feasst.args({
            "steps_per": str(steps_per),
            "file_name": "movie.xyz"
        })))
monte_carlo.add(
Esempio n. 7
0
def mc(thread, mn, mx):
    steps_per = str(int(1e5))
    mc = fst.MakeMonteCarlo()
    #mc.set(fst.MakeRandomMT19937(fst.args({"seed": "12345"})))
    #mc.set(fst.MakeRandomMT19937(fst.args({"seed": "1634926410"})))
    chi = 0.7  # patch coverage
    patch_angle = 2 * math.asin(math.sqrt(chi / 2)) * 180 / math.pi
    print('patch_angle', patch_angle)
    mc = fst.MonteCarlo()
    config = fst.MakeConfiguration(
        fst.args({
            "cubic_box_length":
            "8",
            "particle_type0":
            fst.install_dir() +
            "/plugin/patch/forcefield/two_patch_linear.fstprt"
        }))
    config.add(fst.MakeGroup(fst.args({"site_type0": "0"})))
    mc.add(config)
    mc.add(
        fst.MakePotential(
            fst.MakeHardSphere(),
            fst.MakeVisitModelCell(
                fst.args({
                    "min_length": "1",
                    "cell_group": "1"
                })), fst.args({"group_index": "1"})))
    patch = fst.MakeVisitModelInnerPatch(
        fst.args({"patch_degrees_of_type1": str(patch_angle)}))
    mc.add(
        fst.MakePotential(
            fst.MakeSquareWell(),
            fst.MakeVisitModel(patch),
            #fst.MakeVisitModelCell(patch, fst.args({"min_length": "1.5", "cell_group": "1"})),
            fst.args({"group_index": "1"})))
    mc.set(
        fst.MakeThermoParams(
            fst.args({
                "beta": str(1. / args.temperature),
                "chemical_potential": str(args.mu)
            })))
    mc.set(
        fst.MakeFlatHistogram(
            fst.MakeMacrostateNumParticles(
                fst.Histogram(
                    fst.args({
                        "width": "1",
                        "max": str(mx),
                        "min": str(mn)
                    }))),
            # fst.MakeTransitionMatrix(fst.args({"min_sweeps": str(args.min_sweeps)})),
            fst.MakeWLTM(
                fst.args({
                    "collect_flatness": "18",
                    "min_flatness": "22",
                    "min_sweeps": str(args.min_sweeps)
                }))))
    mc.add(
        fst.MakeTrialTranslate(
            fst.args({
                "weight": "0.5",
                "tunable_param": "0.1"
            })))
    mc.add(
        fst.MakeTrialRotate(fst.args({
            "weight": "0.5",
            "tunable_param": "20."
        })))
    mc.add(
        fst.MakeTrialTransfer(fst.args({
            "weight": "4",
            "particle_type": "0"
        })))
    #    mc.add(fst.MakeTrialGrow(fst.ArgsVector([{"translate": "true", "site": "0", "tunable_param": "1"}]),
    #        {"reference_index": ref, "num_steps": num_steps}))
    #    mc.add(fst.MakeTrialGrow(fst.ArgsVector([{"transfer": "true", "site": "0", "weight": "4"}]),
    #        {"reference_index": ref, "num_steps": num_steps}))
    mc.add(
        fst.MakeCheckEnergy(
            fst.args({
                "steps_per": steps_per,
                "tolerance": "0.0001"
            })))
    mc.add(
        fst.MakeTune(
            fst.args({
                "steps_per": steps_per,
                "stop_after_phase": "0"
            })))
    mc.add(
        fst.MakeLog(
            fst.args({
                "steps_per": steps_per,
                "file_name": "clones" + str(thread) + ".txt",
                "file_name_append_phase": "True"
            })))
    mc.add(
        fst.MakeMoviePatch(
            fst.args({
                "steps_per": steps_per,
                "file_name": "clones" + str(thread) + ".xyz",
                "file_name_append_phase": "True"
            })))
    mc.add(
        fst.MakeEnergy(
            fst.args({
                "steps_per_write": steps_per,
                "file_name": "en" + str(thread) + ".txt",
                "file_name_append_phase": "True",
                "start_after_phase": "0",
                "multistate": "True"
            })))
    mc.add(fst.MakeCriteriaUpdater(fst.args({"steps_per": steps_per})))
    mc.add(
        fst.MakeCriteriaWriter(
            fst.args({
                "steps_per": steps_per,
                "file_name": "clones" + str(thread) + "_crit.txt",
                "file_name_append_phase": "True"
            })))
    mc.set(
        fst.MakeCheckpoint(
            fst.args({
                "file_name":
                "checkpoint" + str(thread) + ".fst",
                "num_hours":
                str(0.1 * args.num_procs * args.num_hours),
                "num_hours_terminate":
                str(0.9 * args.num_procs * args.num_hours)
            })))
    return mc
Esempio n. 8
0
    def test_srsw_alt(self,
                      num_particles=500,
                      density=0.001,
                      steps_per=1e5,
                      beta=1. / 0.9,
                      fstprt=fst.install_dir() + "/forcefield/lj.fstprt",
                      num_equil=1e7,
                      num_prod=1e7):
        """Compare with the reported average energy from the NIST SRSW.
        https://mmlapps.nist.gov/srs/LJ_PURE/mc.htm
        https://www.nist.gov/programs-projects/nist-standard-reference-simulation-website

        num_particles -- number of LJ particles
        density -- number density
        steps_per -- steps between each Anaylze/Modify
        """
        params = {"box_length": (num_particles / density)**(1. / 3.)}
        params = dict(locals(), **params)
        with open('tutorial_1_lj_nvt.txt', 'w') as fsttxt:
            fsttxt.write("""
Checkpoint file_name checkpoint.fst
RandomMT19937 seed time
Configuration cubic_box_length {box_length} particle_type {fstprt}
Potential Model LennardJones
Potential VisitModel LongRangeCorrections
ThermoParams beta 0.1 chemical_potential 10
Metropolis
TrialTranslate tunable_param 2. tunable_target_acceptance 0.2
TrialAdd particle_type 0
Run until_num_particles {num_particles}
RemoveTrial name TrialAdd
ThermoParams beta {beta}
Tune steps_per {steps_per}
CheckEnergy steps_per {steps_per} tolerance 1e-8

# equilibrate
Run num_attempts {num_equil}
RemoveModify name Tune

# production analysis and output
Log steps_per {steps_per} file_name lj.txt
Energy steps_per_write {steps_per} file_name en.txt
# Run num_attempts {num_prod} # optionally run production here instead of python
WriteCheckpoint
""".format(**params))
        import pyfeasst
        syscode = subprocess.call(
            fst.install_dir() +
            "/build/bin/fst < tutorial_1_lj_nvt.txt >> launch.log",
            shell=True,
            executable='/bin/bash')
        if syscode > 0: sys.exit(1)
        mc = fst.MonteCarlo().deserialize(
            pyfeasst.read_checkpoint('checkpoint.fst'))

        # run production trials with an alternative running average of the energy
        # this demonstrates custom on-the-fly analysis in python scripts.
        energy_alt = fst.Accumulator()
        for _ in range(int(params["num_prod"])):
            mc.attempt(1)
            energy_alt.accumulate(mc.criteria().current_energy())
        energy = fst.SeekAnalyze().reference("Energy", mc).accumulator()

        # test that the two methods for computing the energy give the same result
        self.assertAlmostEqual(energy.average(),
                               energy_alt.average(),
                               delta=1e-6)

        # test the average against the NIST SRSW
        stdev = (energy.block_stdev()**2 + (1.89E-05)**2)**(1. / 2.)
        self.assertAlmostEqual(-9.9165E-03 * num_particles,
                               energy.average(),
                               delta=2.576 * stdev)