2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2019,2020, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
36 * \brief Defines the modular simulator
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_modularsimulator
44 #include "modularsimulator.h"
46 #include "gromacs/commandline/filenm.h"
47 #include "gromacs/domdec/domdec.h"
48 #include "gromacs/ewald/pme.h"
49 #include "gromacs/ewald/pme_load_balancing.h"
50 #include "gromacs/ewald/pme_pp.h"
51 #include "gromacs/gmxlib/nrnb.h"
52 #include "gromacs/listed_forces/listed_forces.h"
53 #include "gromacs/mdlib/checkpointhandler.h"
54 #include "gromacs/mdlib/constr.h"
55 #include "gromacs/mdlib/coupling.h"
56 #include "gromacs/mdlib/energyoutput.h"
57 #include "gromacs/mdlib/mdatoms.h"
58 #include "gromacs/mdlib/resethandler.h"
59 #include "gromacs/mdlib/update.h"
60 #include "gromacs/mdrun/replicaexchange.h"
61 #include "gromacs/mdrun/shellfc.h"
62 #include "gromacs/mdrunutility/handlerestart.h"
63 #include "gromacs/mdrunutility/printtime.h"
64 #include "gromacs/mdtypes/commrec.h"
65 #include "gromacs/mdtypes/fcdata.h"
66 #include "gromacs/mdtypes/forcerec.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/mdatom.h"
69 #include "gromacs/mdtypes/mdrunoptions.h"
70 #include "gromacs/mdtypes/observableshistory.h"
71 #include "gromacs/nbnxm/nbnxm.h"
72 #include "gromacs/topology/mtop_util.h"
73 #include "gromacs/topology/topology.h"
74 #include "gromacs/utility/fatalerror.h"
76 #include "computeglobalselement.h"
77 #include "constraintelement.h"
78 #include "forceelement.h"
79 #include "parrinellorahmanbarostat.h"
80 #include "simulatoralgorithm.h"
81 #include "statepropagatordata.h"
82 #include "vrescalethermostat.h"
86 void ModularSimulator::run()
88 GMX_LOG(legacySimulatorData_->mdlog.info)
90 .appendText("Using the modular simulator.");
92 ModularSimulatorAlgorithmBuilder algorithmBuilder(compat::make_not_null(legacySimulatorData_),
93 std::move(checkpointDataHolder_));
94 addIntegrationElements(&algorithmBuilder);
95 auto algorithm = algorithmBuilder.build();
97 while (const auto* task = algorithm.getNextTask())
104 void ModularSimulator::addIntegrationElements(ModularSimulatorAlgorithmBuilder* builder)
106 if (legacySimulatorData_->inputrec->eI == eiMD)
108 // The leap frog integration algorithm
109 builder->add<ForceElement>();
110 builder->add<StatePropagatorData::Element>();
111 if (legacySimulatorData_->inputrec->etc == etcVRESCALE)
113 builder->add<VRescaleThermostat>(-1, UseFullStepKE::No, ReportPreviousStepConservedEnergy::No);
115 builder->add<Propagator<IntegrationStep::LeapFrog>>(legacySimulatorData_->inputrec->delta_t,
116 RegisterWithThermostat::True,
117 RegisterWithBarostat::True);
118 if (legacySimulatorData_->constr)
120 builder->add<ConstraintsElement<ConstraintVariable::Positions>>();
122 builder->add<ComputeGlobalsElement<ComputeGlobalsAlgorithm::LeapFrog>>();
123 builder->add<EnergyData::Element>();
124 if (legacySimulatorData_->inputrec->epc == epcPARRINELLORAHMAN)
126 builder->add<ParrinelloRahmanBarostat>(-1);
129 else if (legacySimulatorData_->inputrec->eI == eiVV)
131 // The velocity verlet integration algorithm
132 builder->add<ForceElement>();
133 builder->add<Propagator<IntegrationStep::VelocitiesOnly>>(
134 0.5 * legacySimulatorData_->inputrec->delta_t, RegisterWithThermostat::False,
135 RegisterWithBarostat::True);
136 if (legacySimulatorData_->constr)
138 builder->add<ConstraintsElement<ConstraintVariable::Velocities>>();
140 builder->add<ComputeGlobalsElement<ComputeGlobalsAlgorithm::VelocityVerlet>>();
141 builder->add<StatePropagatorData::Element>();
142 if (legacySimulatorData_->inputrec->etc == etcVRESCALE)
144 builder->add<VRescaleThermostat>(0, UseFullStepKE::Yes, ReportPreviousStepConservedEnergy::Yes);
146 builder->add<Propagator<IntegrationStep::VelocityVerletPositionsAndVelocities>>(
147 legacySimulatorData_->inputrec->delta_t, RegisterWithThermostat::True,
148 RegisterWithBarostat::False);
149 if (legacySimulatorData_->constr)
151 builder->add<ConstraintsElement<ConstraintVariable::Positions>>();
153 builder->add<ComputeGlobalsElement<ComputeGlobalsAlgorithm::VelocityVerlet>>();
154 builder->add<EnergyData::Element>();
155 if (legacySimulatorData_->inputrec->epc == epcPARRINELLORAHMAN)
157 builder->add<ParrinelloRahmanBarostat>(-1);
162 gmx_fatal(FARGS, "Integrator not implemented for the modular simulator.");
166 bool ModularSimulator::isInputCompatible(bool exitOnFailure,
167 const t_inputrec* inputrec,
169 const gmx_mtop_t& globalTopology,
170 const gmx_multisim_t* ms,
171 const ReplicaExchangeParameters& replExParams,
173 bool doEssentialDynamics,
176 auto conditionalAssert = [exitOnFailure](bool condition, const char* message) {
179 GMX_RELEASE_ASSERT(condition, message);
184 bool isInputCompatible = true;
186 // GMX_USE_MODULAR_SIMULATOR allows to use modular simulator also for non-standard uses,
187 // such as the leap-frog integrator
188 const auto modularSimulatorExplicitlyTurnedOn = (getenv("GMX_USE_MODULAR_SIMULATOR") != nullptr);
189 // GMX_USE_MODULAR_SIMULATOR allows to use disable modular simulator for all uses,
190 // including the velocity-verlet integrator used by default
191 const auto modularSimulatorExplicitlyTurnedOff = (getenv("GMX_DISABLE_MODULAR_SIMULATOR") != nullptr);
194 !(modularSimulatorExplicitlyTurnedOn && modularSimulatorExplicitlyTurnedOff),
195 "Cannot have both GMX_USE_MODULAR_SIMULATOR=ON and GMX_DISABLE_MODULAR_SIMULATOR=ON. "
196 "Unset one of the two environment variables to explicitly chose which simulator to "
198 "or unset both to recover default behavior.");
201 !(modularSimulatorExplicitlyTurnedOff && inputrec->eI == eiVV
202 && inputrec->epc == epcPARRINELLORAHMAN),
203 "Cannot use a Parrinello-Rahman barostat with md-vv and "
204 "GMX_DISABLE_MODULAR_SIMULATOR=ON, "
205 "as the Parrinello-Rahman barostat is not implemented in the legacy simulator. Unset "
206 "GMX_DISABLE_MODULAR_SIMULATOR or use a different pressure control algorithm.");
210 && conditionalAssert(
211 inputrec->eI == eiMD || inputrec->eI == eiVV,
212 "Only integrators md and md-vv are supported by the modular simulator.");
213 isInputCompatible = isInputCompatible
214 && conditionalAssert(inputrec->eI != eiMD || modularSimulatorExplicitlyTurnedOn,
215 "Set GMX_USE_MODULAR_SIMULATOR=ON to use the modular "
216 "simulator with integrator md.");
219 && conditionalAssert(!doRerun, "Rerun is not supported by the modular simulator.");
222 && conditionalAssert(
223 inputrec->etc == etcNO || inputrec->etc == etcVRESCALE,
224 "Only v-rescale thermostat is supported by the modular simulator.");
227 && conditionalAssert(
228 inputrec->epc == epcNO || inputrec->epc == epcPARRINELLORAHMAN,
229 "Only Parrinello-Rahman barostat is supported by the modular simulator.");
232 && conditionalAssert(
233 !(inputrecNptTrotter(inputrec) || inputrecNphTrotter(inputrec)
234 || inputrecNvtTrotter(inputrec)),
235 "Legacy Trotter decomposition is not supported by the modular simulator.");
236 isInputCompatible = isInputCompatible
237 && conditionalAssert(inputrec->efep == efepNO || inputrec->efep == efepYES
238 || inputrec->efep == efepSLOWGROWTH,
239 "Expanded ensemble free energy calculation is not "
240 "supported by the modular simulator.");
241 isInputCompatible = isInputCompatible
242 && conditionalAssert(!inputrec->bPull,
243 "Pulling is not supported by the modular simulator.");
246 && conditionalAssert(inputrec->opts.ngacc == 1 && inputrec->opts.acc[0][XX] == 0.0
247 && inputrec->opts.acc[0][YY] == 0.0
248 && inputrec->opts.acc[0][ZZ] == 0.0 && inputrec->cos_accel == 0.0,
249 "Acceleration is not supported by the modular simulator.");
252 && conditionalAssert(inputrec->opts.ngfrz == 1 && inputrec->opts.nFreeze[0][XX] == 0
253 && inputrec->opts.nFreeze[0][YY] == 0
254 && inputrec->opts.nFreeze[0][ZZ] == 0,
255 "Freeze groups are not supported by the modular simulator.");
258 && conditionalAssert(
259 inputrec->deform[XX][XX] == 0.0 && inputrec->deform[XX][YY] == 0.0
260 && inputrec->deform[XX][ZZ] == 0.0 && inputrec->deform[YY][XX] == 0.0
261 && inputrec->deform[YY][YY] == 0.0 && inputrec->deform[YY][ZZ] == 0.0
262 && inputrec->deform[ZZ][XX] == 0.0 && inputrec->deform[ZZ][YY] == 0.0
263 && inputrec->deform[ZZ][ZZ] == 0.0,
264 "Deformation is not supported by the modular simulator.");
267 && conditionalAssert(gmx_mtop_interaction_count(globalTopology, IF_VSITE) == 0,
268 "Virtual sites are not supported by the modular simulator.");
269 isInputCompatible = isInputCompatible
270 && conditionalAssert(!inputrec->bDoAwh,
271 "AWH is not supported by the modular simulator.");
274 && conditionalAssert(gmx_mtop_ftype_count(globalTopology, F_DISRES) == 0,
275 "Distance restraints are not supported by the modular simulator.");
278 && conditionalAssert(
279 gmx_mtop_ftype_count(globalTopology, F_ORIRES) == 0,
280 "Orientation restraints are not supported by the modular simulator.");
283 && conditionalAssert(ms == nullptr,
284 "Multi-sim are not supported by the modular simulator.");
287 && conditionalAssert(replExParams.exchangeInterval == 0,
288 "Replica exchange is not supported by the modular simulator.");
290 int numEnsembleRestraintSystems;
293 numEnsembleRestraintSystems = fcd->disres->nsystems;
297 auto distantRestraintEnsembleEnvVar = getenv("GMX_DISRE_ENSEMBLE_SIZE");
298 numEnsembleRestraintSystems =
299 (ms != nullptr && distantRestraintEnsembleEnvVar != nullptr)
300 ? static_cast<int>(strtol(distantRestraintEnsembleEnvVar, nullptr, 10))
305 && conditionalAssert(numEnsembleRestraintSystems <= 1,
306 "Ensemble restraints are not supported by the modular simulator.");
309 && conditionalAssert(!doSimulatedAnnealing(inputrec),
310 "Simulated annealing is not supported by the modular simulator.");
313 && conditionalAssert(!inputrec->bSimTemp,
314 "Simulated tempering is not supported by the modular simulator.");
315 isInputCompatible = isInputCompatible
316 && conditionalAssert(!inputrec->bExpanded,
317 "Expanded ensemble simulations are not supported by "
318 "the modular simulator.");
321 && conditionalAssert(!doEssentialDynamics,
322 "Essential dynamics is not supported by the modular simulator.");
323 isInputCompatible = isInputCompatible
324 && conditionalAssert(inputrec->eSwapCoords == eswapNO,
325 "Ion / water position swapping is not supported by "
326 "the modular simulator.");
329 && conditionalAssert(!inputrec->bIMD,
330 "Interactive MD is not supported by the modular simulator.");
333 && conditionalAssert(!doMembed,
334 "Membrane embedding is not supported by the modular simulator.");
335 // TODO: Change this to the boolean passed when we merge the user interface change for the GPU update.
338 && conditionalAssert(
339 getenv("GMX_FORCE_UPDATE_DEFAULT_GPU") == nullptr,
340 "Integration on the GPU is not supported by the modular simulator.");
341 // Modular simulator is centered around NS updates
342 // TODO: think how to handle nstlist == 0
343 isInputCompatible = isInputCompatible
344 && conditionalAssert(inputrec->nstlist != 0,
345 "Simulations without neighbor list update are not "
346 "supported by the modular simulator.");
347 isInputCompatible = isInputCompatible
348 && conditionalAssert(!GMX_FAHCORE,
349 "GMX_FAHCORE not supported by the modular simulator.");
351 return isInputCompatible;
354 ModularSimulator::ModularSimulator(std::unique_ptr<LegacySimulatorData> legacySimulatorData,
355 std::unique_ptr<ReadCheckpointDataHolder> checkpointDataHolder) :
356 legacySimulatorData_(std::move(legacySimulatorData)),
357 checkpointDataHolder_(std::move(checkpointDataHolder))
359 checkInputForDisabledFunctionality();
362 void ModularSimulator::checkInputForDisabledFunctionality()
364 isInputCompatible(true, legacySimulatorData_->inputrec, legacySimulatorData_->mdrunOptions.rerun,
365 *legacySimulatorData_->top_global, legacySimulatorData_->ms,
366 legacySimulatorData_->replExParams, legacySimulatorData_->fr->fcdata.get(),
367 opt2bSet("-ei", legacySimulatorData_->nfile, legacySimulatorData_->fnm),
368 legacySimulatorData_->membed != nullptr);
369 if (legacySimulatorData_->observablesHistory->edsamHistory)
372 "The checkpoint is from a run with essential dynamics sampling, "
373 "but the current run did not specify the -ei option. "
374 "Either specify the -ei option to mdrun, or do not use this checkpoint file.");