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

Equilibration simulation of a Lennard-Jones system using the Berendsen thermostat as a first step towards running an AdResS simulation. The particles are initialized with random positions and velocities inside a rectangular simulation box with user-defined dimensions and number of particles. The final phase position is dumped into both GRO and H5MD output files.

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

struct Config
{
// time parameters
idx_t nsteps = 100001;
real_t dt = 0.001_r;
// 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_r;
// pressure parameters
real_t pressure_averaging_coefficient =
0.02_r;
// thermostatting parameters
real_t temperature = 1.5_r;
real_t friction = 1_r;
real_t temperature_averaging_coefficient =
0.2_r;
// system parameters
real_t Lx = 30.0_r * sigma;
real_t Ly = 30.0_r * sigma;
real_t Lz = 30.0_r * sigma;
idx_t numAtoms = 9990;
// 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 = "equilibrateBerendsen";
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 equilibrateBerendsen(Config& config)
{
// initialize
auto subdomain =
data::Subdomain({0_r, 0_r, 0_r}, {config.Lx, config.Ly, config.Lz}, config.neighborCutoff);
const auto volume = subdomain.getVolume();
auto atoms =
util::fillDomainWithAtoms(subdomain, config.numAtoms, config.maxVelocity, config.mass);
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;
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);
currentTemperature << analysis::getMeanKineticEnergy(atoms) * 2_r / 3_r;
// ouput 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");
}
for (auto step = 0; step < config.nsteps; ++step)
{
maxAtomDisplacement += action::VelocityVerlet::preForceIntegrate(atoms, config.dt);
atoms, currentTemperature, config.temperature, config.friction);
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);
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);
}
}
if (config.bOutput)
{
// 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
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
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
Calculates the exponential moving average (EMA) for values added with append.
Definition ExponentialMovingAverage.hpp:32
void apply(data::Atoms &atoms, const real_t &currentTemperature, const real_t &targetTemperature, const real_t &gamma)
Berendsen thermostat.
Definition BerendsenThermostat.cpp:27
real_t getKineticEnergy(data::Atoms &atoms)
Definition KineticEnergy.hpp:26
real_t getMeanKineticEnergy(data::Atoms &atoms)
Definition KineticEnergy.hpp:44
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
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{"preliminary NVT equilibration of Lennard-Jones fluid with Berendsen thermostat"};
app.add_option("-n,--nsteps", config.nsteps, "number of simulation steps");
app.add_option("-x,--xlength", config.Lx, "x length of the box");
app.add_option("-y,--ylength", config.Ly, "y length of the box");
app.add_option("-z,--zlength", config.Lz, "z length of the box");
app.add_option("-N,--numAtoms", config.numAtoms, "number of atoms");
app.add_option("-T,--temperature", config.temperature, "thermostat target temperature");
app.add_option("-o,--outint", config.outputInterval, "output interval");
app.add_option("-f,--outfile", config.fileOut, "output file name");
CLI11_PARSE(app, argc, argv);
config.fileOutFinalGRO = format("{0}_final.gro", config.fileOut);
config.fileOutFinalH5MD = format("{0}_final.h5md", config.fileOut);
if (config.outputInterval < 0) config.bOutput = false;
equilibrateBerendsen(config);
return EXIT_SUCCESS;
}