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

Minimal Lennard-Jones simulation in the NVT (canonical) ensemble with output of observables and final phase position in GRO format.

#include <CLI/App.hpp>
#include <CLI/Config.hpp>
#include <CLI/Formatter.hpp>
#include <Kokkos_Core.hpp>
#include <algorithm>
#include <format>
#include <fstream>
#include <iomanip>
#include <iostream>
#include "Cabana_NeighborList.hpp"
#include "data/Atoms.hpp"
#include "datatypes.hpp"
#include "io/DumpGRO.hpp"
using namespace mrmd;
Top-level namespace of MRMD.

Configuration for the Argon NVT example simulation.

struct Config
{
// time parameters
idx_t nsteps = 400001;
static constexpr real_t dt = 0.002;
// 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 maxVelocity =
1_r;
static constexpr real_t r_cut = 2.5_r * sigma;
static constexpr real_t r_cap = 0.7_r * sigma;
// neighbor list parameters
static constexpr real_t skin = 0.1_r * sigma;
static constexpr real_t neighborCutoff = r_cut + skin;
static constexpr real_t cell_ratio =
1_r;
static constexpr idx_t estimatedMaxNeighbors =
60;
// system parameters
static constexpr idx_t numAtoms = 16 * 16 * 16;
real_t Lx = 20_r * sigma;
// thermostat parameters
real_t temperature = 1.5_r;
static constexpr real_t friction =
0.04_r / dt;
// output parameters
bool bOutput = true;
idx_t outputInterval = -1;
const std::string resName = "Argon";
const std::vector<std::string> typeNames = {"Ar"};
std::string fileOut = "lennardJonesNVT";
std::string fileOutFinalGRO = format("{0}_final.gro", fileOut);
};
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99

Simulation logic goes here

void runLennardJonesNVT(Config& config)
{
// initialize simulation domain
auto subdomain =
data::Subdomain({0_r, 0_r, 0_r}, {config.Lx, config.Lx, config.Lx}, config.neighborCutoff);
// calculate volume of the simulation domain
const auto volume = subdomain.diameter[0] * subdomain.diameter[1] * subdomain.diameter[2];
// initialize atoms randomly in the domain
auto atoms =
util::fillDomainWithAtoms(subdomain, config.numAtoms, config.maxVelocity, config.mass);
// calculate and print initial density
auto rho = real_c(atoms.numLocalAtoms) / volume;
std::cout << "rho: " << rho << std::endl;
// set up ghost layer for periodic boundary conditions
// set up neighbor list
HalfVerletList verletList;
real_t maxAtomDisplacement = std::numeric_limits<real_t>::max();
idx_t rebuildCounter = 0;
// set up interaction potential and force calculation and application
action::LennardJones lennardJones(config.r_cut, config.sigma, config.epsilon, config.r_cap);
// set up thermostat for temperature control
action::VelocityVerletLangevinThermostat integrator(config.friction, config.temperature);
// set up timer for runtime measurement
Kokkos::Timer timer;
// set up mean square displacement analysis
analysis::MeanSquareDisplacement meanSquareDisplacement;
meanSquareDisplacement.reset(atoms);
auto msd = 0_r;
// print table header for simulation statistics
util::printTable("step", "time", "T", "Ek", "E0", "E", "p", "msd", "Nlocal", "Nghost");
util::printTableSep("step", "time", "T", "Ek", "E0", "E", "p", "msd", "Nlocal", "Nghost");
// open statistics file for writing simulation statistics
std::ofstream fStat("statistics.txt");
// main simulation loop
for (auto step = 0; step < config.nsteps; ++step)
{
// integrate equations of motion before force calculation
maxAtomDisplacement += integrator.preForceIntegrate(atoms, config.dt);
// reinsert atoms that left the domain according to periodic boundary conditions
ghostLayer.exchangeRealAtoms(atoms, subdomain);
// create ghost atoms in the ghost layer beyond the periodic boundaries
ghostLayer.createGhostAtoms(atoms, subdomain);
// check if neighbor list needs to be rebuilt
if (maxAtomDisplacement >=
config.skin *
0.5_r) // the condition is on half the skin thickness because in principle two
// atoms may both move half the skin thickness towards each other
{
// reset displacement
maxAtomDisplacement = 0_r;
// rebuild neighbor list
verletList.build(atoms.getPos(),
0,
atoms.numLocalAtoms,
config.neighborCutoff,
config.cell_ratio,
subdomain.minGhostCorner.data(),
subdomain.maxGhostCorner.data(),
config.estimatedMaxNeighbors);
++rebuildCounter;
}
// reset forces to zero
auto force = atoms.getForce();
Cabana::deep_copy(force, 0_r);
// compute and apply forces
lennardJones.apply(atoms, verletList);
// contribute forces calculated on ghost atoms back to real atoms
ghostLayer.contributeBackGhostToReal(atoms);
// integrate equations of motion after force calculation
integrator.postForceIntegrate(atoms, config.dt);
// handle output and statistics
if (config.bOutput && (step % config.outputInterval == 0))
{
// calculate statistics
auto E0 = lennardJones.getEnergy() / real_c(atoms.numLocalAtoms);
auto systemMomentum = analysis::getSystemMomentum(atoms);
auto T = (2_r / 3_r) * Ek;
auto p = analysis::getPressure(atoms, subdomain);
msd = meanSquareDisplacement.calc(atoms, subdomain) /
(real_c(config.outputInterval) * config.dt);
meanSquareDisplacement.reset(atoms);
// print statistics to console
timer.seconds(),
T,
Ek,
E0,
E0 + Ek,
p,
msd,
atoms.numLocalAtoms,
atoms.numGhostAtoms);
// dump statistics to file
fStat << step << " " << timer.seconds() << " " << T << " " << Ek << " " << E0 << " "
<< E0 + Ek << " " << p << " " << msd << " " << atoms.numLocalAtoms << " "
<< atoms.numGhostAtoms << " " << std::endl;
}
}
if (config.bOutput)
{
// final microstates output
io::dumpGRO(config.fileOutFinalGRO,
atoms,
subdomain,
real_c(config.nsteps) * config.dt,
config.fileOutFinalGRO,
config.resName,
config.typeNames,
false,
true);
}
// close statistics file
fStat.close();
// print performance data
auto time = timer.seconds();
std::cout << time << std::endl;
// write performance data to file
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
Calculates the mean square displacement of particles over time.
Definition MeanSquareDisplacement.hpp:31
void reset(data::Atoms &atoms)
Store current positions as a starting point.
Definition MeanSquareDisplacement.cpp:24
real_t calc(data::Atoms &atoms, const data::Subdomain &subdomain)
Compare current positions against the starting point.
Definition MeanSquareDisplacement.cpp:60
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 exchangeRealAtoms(data::Atoms &atoms, const data::Subdomain &subdomain)
Definition GhostLayer.hpp:39
void contributeBackGhostToReal(data::Atoms &atoms)
Definition GhostLayer.hpp:56
real_t getMeanKineticEnergy(data::Atoms &atoms)
Definition KineticEnergy.hpp:44
Vector3D getSystemMomentum(data::Atoms &atoms)
Definition SystemMomentum.cpp:21
real_t getPressure(data::Atoms &atoms, const data::Subdomain &subdomain)
Definition Pressure.cpp:23
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
data::Atoms fillDomainWithAtoms(const data::Subdomain &subdomain, const idx_t &numAtoms, const real_t &maxVelocity, const real_t &inputMass)
Helper function to provide an initial atoms ensamble.
Definition simulationSetup.cpp:22
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
Vector3D diameter
Definition Subdomain.hpp:111

Main function for the simulation

int main(int argc, char* argv[])
{
// initialize Kokkos environment
Kokkos::ScopeGuard scope_guard(argc, argv);
// print Kokkos execution space
std::cout << "execution space: " << typeid(Kokkos::DefaultExecutionSpace).name() << std::endl;
// initialize simulation configuration with command line interface
Config config;
CLI::App app{"Lennard Jones Fluid NVT benchmark application"};
app.add_option("-n,--nsteps", config.nsteps, "total number of simulation steps");
app.add_option("-L,--length", config.Lx, "simulation box diameter");
app.add_option("-T,--temperature", config.temperature, "thermostat target temperature");
app.add_option("-o,--outint", config.outputInterval, "output interval");
CLI11_PARSE(app, argc, argv);
// reset output parameter if output interval is negative
if (config.outputInterval < 0) config.bOutput = false;
// run simulation in NVT
runLennardJonesNVT(config);
return EXIT_SUCCESS;
}