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

Minimal Lennard-Jones simulation in the NVE (microcanonical) ensemble starting from a phase position restored from a GRO file, with output of observables and the final phase position in H5MD 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/DumpH5MD.hpp"
using namespace mrmd;
Top-level namespace of MRMD.

Configuration for the Argon NVE example simulation.

struct Config
{
// input file parameters
std::string fileRestoreGRO = "lennardJonesNVT_final.gro";
// simulation 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;
// output parameters
bool bOutput = true;
idx_t outputInterval = -1;
const std::string resName = "Argon";
const std::vector<std::string> typeNames = {"Ar"};
std::string fileOut = "lennardJonesNVE";
std::string fileOutFinalH5MD = format("{0}_final.h5md", fileOut);
};
int64_t idx_t
Definition datatypes.hpp:91
double real_t
Definition datatypes.hpp:99

Simulation logic goes here

void runLennardJonesNVE(Config& config)
{
// initialize simulation domain
auto subdomain = data::Subdomain({0_r, 0_r, 0_r}, {0_r, 0_r, 0_r}, config.neighborCutoff);
// initialize atoms
auto atoms = data::Atoms(0);
// restore initial phase point from file
io::restoreGRO(config.fileRestoreGRO, subdomain, atoms);
// calculate volume of the simulation domain
const auto volume = subdomain.diameter[0] * subdomain.diameter[1] * subdomain.diameter[2];
// 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 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");
// set up H5MD output
auto dumpH5MD = io::DumpH5MD("J-Hizzle");
// main simulation loop
for (auto step = 0; step < config.nsteps; ++step)
{
// integrate equations of motion before force calculation
maxAtomDisplacement += action::VelocityVerlet::preForceIntegrate(atoms, config.dt);
// 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;
// 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);
// rebuild neighbor list
verletList.build(atoms.getPos(),
0,
atoms.numLocalAtoms,
config.neighborCutoff,
config.cell_ratio,
subdomain.minGhostCorner.data(),
subdomain.maxGhostCorner.data(),
config.estimatedMaxNeighbors);
++rebuildCounter;
}
else
{
// update ghost atom positions in the ghost layer according to periodic boundary
// conditions
ghostLayer.updateGhostAtoms(atoms, subdomain);
}
// 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
// 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 phase point output
dumpH5MD.dump(config.fileOutFinalH5MD, subdomain, atoms);
}
// 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
static void postForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerlet.cpp:69
static real_t preForceIntegrate(data::Atoms &atoms, const real_t dt)
Definition VelocityVerlet.cpp:26
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 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
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
DeviceAtoms Atoms
Definition Atoms.hpp:177
void restoreGRO(const std::string &filename, data::Subdomain &subdomain, data::Atoms &atoms, bool containsGhostAtoms)
Definition RestoreGRO.cpp:24
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

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 NVE benchmark application"};
app.add_option("-n,--nsteps", config.nsteps, "total number of simulation steps");
app.add_option("-o,--output", config.outputInterval, "output interval");
app.add_option("-i,--inpfile", config.fileRestoreGRO, "input file name");
CLI11_PARSE(app, argc, argv);
// reset output parameter if output interval is negative
if (config.outputInterval < 0) config.bOutput = false;
// run simulation in NVE
runLennardJonesNVE(config);
return EXIT_SUCCESS;
}