Multi Resolution Molecular Dynamics 0.4.0
Multi Resolution Molecular Dynamics
Loading...
Searching...
No Matches
05_LennardJones_EquilibrateLangevin
Note
This page is generated from examples/05_LennardJones_Equilibration/05_LennardJones_EquilibrateLangevin.cpp. Build and run it with cmake --build mrmd-build --target 05_LennardJones_EquilibrateLangevin followed by ./mrmd-build/examples/05_LennardJones_Equilibration/05_LennardJones_EquilibrateLangevin. Pass --help to see the available command line options.

Equilibration run for the Lennard-Jones fluid using the Langevin thermostat as a second step towards running an AdResS simulation. The initial phase position is restored from an H5MD file that can be generated by a preceding Berendsen equilibration. The final phase position is dumped into both GRO and H5MD output files and the trajectory is stored in a separate H5MD file.

Configuration for the Lennard-Jones equilibration simulation using the Langevin thermostat.

struct Config
{
// time parameters
idx_t nsteps = 100001;
real_t dt = 0.002_r;
// input file parameters
std::string fileRestoreH5MD =
"equilibrateBerendsen_final.h5md";
// interaction parameters
static constexpr real_t sigma =
1_r;
static constexpr real_t epsilon = 1_r;
static constexpr real_t mass = 1_r;
static constexpr real_t r_cut = 2.5_r * sigma;
static constexpr real_t r_cap = 0_r;
static constexpr bool doShift =
true;
// pressure parameters
real_t pressure_averaging_coefficient =
0.02_r;
// thermostatting parameters
real_t temperature = 1.5_r;
real_t friction = 0.04_r / dt;
real_t temperature_averaging_coefficient =
0.2_r;
// neighbor-list parameters
static constexpr real_t skin = 0.3_r * sigma;
static constexpr real_t neighborCutoff = r_cut + skin;
static constexpr real_t cell_ratio =
1_r;
static constexpr idx_t estimatedMaxNeighbors =
60;
// output parameters
bool bOutput = true;
idx_t outputInterval = -1;
const std::string resName = "Argon";
const std::vector<std::string> typeNames = {"Ar"};
std::string fileOut = "equilibrateLangevin";
std::string fileOutH5MD;
std::string fileOutTF;
std::string fileOutFinalGRO;
std::string fileOutFinalH5MD;
};
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99

Simulation logic goes here

void equilibrateLangevin(Config& config)
{
// initialize
data::Subdomain subdomain;
auto atoms = data::Atoms(0);
// load data from file
auto io = io::RestoreH5MD();
io.restore(config.fileRestoreH5MD, subdomain, atoms);
const auto volume = subdomain.getVolume();
auto rho = real_c(atoms.numLocalAtoms) / volume;
std::cout << "rho: " << rho << std::endl;
// output management
auto dump = io::DumpH5MD("J-Hizzle");
// technical setup
action::LennardJones LJ(config.r_cut, config.sigma, config.epsilon, 0.5_r * config.sigma);
HalfVerletList verletList;
action::VelocityVerletLangevinThermostat langevinIntegrator(config.friction,
config.temperature);
Kokkos::Timer timer;
real_t maxAtomDisplacement = std::numeric_limits<real_t>::max();
idx_t rebuildCounter = 0;
util::ExponentialMovingAverage currentPressure(config.pressure_averaging_coefficient);
util::ExponentialMovingAverage currentTemperature(config.temperature_averaging_coefficient);
// output management
auto dumpH5MD = io::DumpH5MD("J-Hizzle");
if (config.bOutput)
{
"step", "wall time", "T", "p", "V", "E_kin", "E_LJ", "E_total", "Nlocal", "Nghost");
"step", "wall time", "T", "p", "V", "E_kin", "E_LJ", "E_total", "Nlocal", "Nghost");
dumpH5MD.open(config.fileOutH5MD, subdomain, atoms);
}
// main integration loop
for (auto step = 0; step < config.nsteps; ++step)
{
maxAtomDisplacement += langevinIntegrator.preForceIntegrate(atoms, config.dt);
if (maxAtomDisplacement >= config.skin * 0.5_r)
{
// reset displacement
maxAtomDisplacement = 0_r;
ghostLayer.exchangeRealAtoms(atoms, subdomain);
ghostLayer.createGhostAtoms(atoms, subdomain);
verletList.build(atoms.getPos(),
0,
atoms.numLocalAtoms,
config.neighborCutoff,
config.cell_ratio,
subdomain.minGhostCorner.data(),
subdomain.maxGhostCorner.data(),
config.estimatedMaxNeighbors);
++rebuildCounter;
}
else
{
ghostLayer.updateGhostAtoms(atoms, subdomain);
}
auto force = atoms.getForce();
Cabana::deep_copy(force, 0_r);
LJ.apply(atoms, verletList);
auto Ek = analysis::getKineticEnergy(atoms);
currentPressure << 2_r * (Ek - LJ.getVirial()) / (3_r * volume);
Ek /= real_c(atoms.numLocalAtoms);
currentTemperature << (2_r / 3_r) * Ek;
ghostLayer.contributeBackGhostToReal(atoms);
langevinIntegrator.postForceIntegrate(atoms, config.dt);
if (config.bOutput && (step % config.outputInterval == 0))
{
timer.seconds(),
currentTemperature,
currentPressure,
volume,
Ek,
LJ.getEnergy() / real_c(atoms.numLocalAtoms),
Ek + LJ.getEnergy() / real_c(atoms.numLocalAtoms),
atoms.numLocalAtoms,
atoms.numGhostAtoms);
// phase point output
dumpH5MD.dumpStep(subdomain, atoms, step, config.dt);
}
}
if (config.bOutput)
{
dumpH5MD.close();
// final phase point output
dumpH5MD.dump(config.fileOutFinalH5MD, subdomain, atoms);
io::dumpGRO(config.fileOutFinalGRO,
atoms,
subdomain,
0,
config.resName,
config.resName,
config.typeNames,
false,
true);
}
// write performance data to file
auto time = timer.seconds();
std::cout << time << std::endl;
auto cores = util::getEnvironmentVariable("OMP_NUM_THREADS");
std::ofstream fout("ecab.perf", std::ofstream::app);
fout << cores << ", " << time << ", " << atoms.numLocalAtoms << ", " << config.nsteps
<< std::endl;
fout.close();
}
High-level interface for the application of the Lennard-Jones potential to given atoms.
Definition LennardJones.hpp:118
Integrator that updates the positions and velocities of atoms using the BAOAB Langevin dynamics algor...
Definition VelocityVerletLangevinThermostat.hpp:36
High-level interface for managing ghost atoms at the periodic boundaries.
Definition GhostLayer.hpp:33
void createGhostAtoms(data::Atoms &atoms, const data::Subdomain &subdomain)
Definition GhostLayer.hpp:44
void updateGhostAtoms(data::Atoms &atoms, const data::Subdomain &subdomain)
Definition GhostLayer.hpp:49
void exchangeRealAtoms(data::Atoms &atoms, const data::Subdomain &subdomain)
Definition GhostLayer.hpp:39
void contributeBackGhostToReal(data::Atoms &atoms)
Definition GhostLayer.hpp:56
Handles dumping trajectory data in H5MD format.
Definition DumpH5MD.hpp:41
Handles restoring trajectory data from H5MD format.
Definition RestoreH5MD.hpp:33
Calculates the exponential moving average (EMA) for values added with append.
Definition ExponentialMovingAverage.hpp:32
real_t getKineticEnergy(data::Atoms &atoms)
Definition KineticEnergy.hpp:26
DeviceAtoms Atoms
Definition Atoms.hpp:177
Save and load simulation data.
void dumpGRO(const std::string &filename, data::Atoms &atoms, const data::Subdomain &subdomain, const real_t &timestamp, const std::string &title, const std::string &resName, const std::vector< std::string > &typeNames, bool dumpGhosts, bool dumpVelocities)
format specification https://manual.gromacs.org/current/reference-manual/file-formats....
Definition DumpGRO.cpp:26
void printTableSep(HEAD)
Definition PrintTable.hpp:40
std::string getEnvironmentVariable(const std::string &variable)
Definition EnvironmentVariables.hpp:23
void printTable(HEAD head)
Definition PrintTable.hpp:27
Cabana::VerletList< Kokkos::DefaultExecutionSpace::memory_space, Cabana::HalfNeighborTag, Cabana::VerletLayout2D, Cabana::TeamOpTag > HalfVerletList
Definition datatypes.hpp:184
KOKKOS_INLINE_FUNCTION constexpr real_t real_c(T t)
Definition datatypes.hpp:114
Axis-aligned simulation box with a surrounding ghost layer.
Definition Subdomain.hpp:46
Point3D minGhostCorner
Definition Subdomain.hpp:97
Point3D maxGhostCorner
Definition Subdomain.hpp:100
real_t getVolume() const
Definition Subdomain.cpp:57

Main function for the simulation

int main(int argc, char* argv[])
{
// initialize Kokkos environment
Kokkos::ScopeGuard scope_guard(argc, argv);
std::cout << "execution space: " << typeid(Kokkos::DefaultExecutionSpace).name() << std::endl;
Config config;
CLI::App app{"follow-up NVT equilibration run of Lennard-Jones fluid with Langevin thermostat"};
app.add_option("-n,--nsteps", config.nsteps, "number of simulation steps");
app.add_option("-o,--outint", config.outputInterval, "output interval");
app.add_option("-i,--inpfile", config.fileRestoreH5MD, "input file name");
app.add_option("-T,--temperature", config.temperature, "thermostat target temperature");
app.add_option("--friction", config.friction, "friction coefficient for thermostat");
app.add_option("-f,--outfile", config.fileOut, "output file name");
CLI11_PARSE(app, argc, argv);
config.fileOutH5MD = format("{0}.h5md", config.fileOut);
config.fileOutTF = format("{0}_tf.txt", config.fileOut);
config.fileOutFinalH5MD = format("{0}_final.h5md", config.fileOut);
config.fileOutFinalGRO = format("{0}_final.gro", config.fileOut);
if (config.outputInterval < 0) config.bOutput = false;
equilibrateLangevin(config);
return EXIT_SUCCESS;
}