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/network.h"
52 #include "gromacs/gmxlib/nrnb.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/mdlib/checkpointhandler.h"
55 #include "gromacs/mdlib/constr.h"
56 #include "gromacs/mdlib/coupling.h"
57 #include "gromacs/mdlib/energyoutput.h"
58 #include "gromacs/mdlib/forcerec.h"
59 #include "gromacs/mdlib/mdatoms.h"
60 #include "gromacs/mdlib/resethandler.h"
61 #include "gromacs/mdlib/stat.h"
62 #include "gromacs/mdlib/update.h"
63 #include "gromacs/mdrun/replicaexchange.h"
64 #include "gromacs/mdrun/shellfc.h"
65 #include "gromacs/mdrunutility/handlerestart.h"
66 #include "gromacs/mdrunutility/printtime.h"
67 #include "gromacs/mdtypes/commrec.h"
68 #include "gromacs/mdtypes/fcdata.h"
69 #include "gromacs/mdtypes/forcerec.h"
70 #include "gromacs/mdtypes/inputrec.h"
71 #include "gromacs/mdtypes/mdatom.h"
72 #include "gromacs/mdtypes/mdrunoptions.h"
73 #include "gromacs/mdtypes/observableshistory.h"
74 #include "gromacs/mdtypes/state.h"
75 #include "gromacs/nbnxm/nbnxm.h"
76 #include "gromacs/timing/walltime_accounting.h"
77 #include "gromacs/topology/mtop_util.h"
78 #include "gromacs/topology/topology.h"
79 #include "gromacs/utility/cstringutil.h"
80 #include "gromacs/utility/fatalerror.h"
82 #include "compositesimulatorelement.h"
83 #include "computeglobalselement.h"
84 #include "constraintelement.h"
85 #include "energyelement.h"
86 #include "forceelement.h"
87 #include "freeenergyperturbationelement.h"
88 #include "parrinellorahmanbarostat.h"
89 #include "propagator.h"
90 #include "signallers.h"
91 #include "statepropagatordata.h"
92 #include "trajectoryelement.h"
93 #include "vrescalethermostat.h"
97 void ModularSimulator::run()
99 GMX_LOG(mdlog.info).asParagraph().appendText("Using the modular simulator.");
100 constructElementsAndSignallers();
102 for (auto& signaller : signallerList_)
108 domDecHelper_->setup();
111 for (auto& element : elementsOwnershipList_)
113 element->elementSetup();
115 if (pmeLoadBalanceHelper_)
117 // State must have been initialized so pmeLoadBalanceHelper_ gets a valid box
118 pmeLoadBalanceHelper_->setup();
121 while (step_ <= signalHelper_->lastStep_)
125 while (!taskQueue_.empty())
127 auto task = std::move(taskQueue_.front());
134 for (auto& element : elementsOwnershipList_)
136 element->elementTeardown();
138 if (pmeLoadBalanceHelper_)
140 pmeLoadBalanceHelper_->teardown();
145 void ModularSimulator::simulatorSetup()
147 if (!mdrunOptions.writeConfout)
149 // This is on by default, and the main known use case for
150 // turning it off is for convenience in benchmarking, which is
151 // something that should not show up in the general user
156 "The -noconfout functionality is deprecated, and "
157 "may be removed in a future version.");
162 char sbuf[STEPSTRSIZE], sbuf2[STEPSTRSIZE];
163 std::string timeString;
164 fprintf(stderr, "starting mdrun '%s'\n", *(top_global->name));
165 if (inputrec->nsteps >= 0)
167 timeString = formatString("%8.1f", static_cast<double>(inputrec->init_step + inputrec->nsteps)
168 * inputrec->delta_t);
172 timeString = "infinite";
174 if (inputrec->init_step > 0)
176 fprintf(stderr, "%s steps, %s ps (continuing from step %s, %8.1f ps).\n",
177 gmx_step_str(inputrec->init_step + inputrec->nsteps, sbuf), timeString.c_str(),
178 gmx_step_str(inputrec->init_step, sbuf2), inputrec->init_step * inputrec->delta_t);
182 fprintf(stderr, "%s steps, %s ps.\n", gmx_step_str(inputrec->nsteps, sbuf),
185 fprintf(fplog, "\n");
188 walltime_accounting_start_time(walltime_accounting);
189 wallcycle_start(wcycle, ewcRUN);
190 print_start(fplog, cr, walltime_accounting, "mdrun");
192 step_ = inputrec->init_step;
195 void ModularSimulator::preStep(Step step, Time gmx_unused time, bool isNeighborSearchingStep)
197 if (stopHandler_->stoppingAfterCurrentStep(isNeighborSearchingStep) && step != signalHelper_->lastStep_)
200 * Stop handler wants to stop after the current step, which was
201 * not known when building the current task queue. This happens
202 * e.g. when a stop is signalled by OS. We therefore want to purge
203 * the task queue now, and re-schedule this step as last step.
206 std::queue<SimulatorRunFunctionPtr>().swap(taskQueue_);
212 resetHandler_->setSignal(walltime_accounting);
213 // This is a hack to avoid having to rewrite StopHandler to be a NeighborSearchSignaller
214 // and accept the step as input. Eventually, we want to do that, but currently this would
215 // require introducing NeighborSearchSignaller in the legacy do_md or a lot of code
217 stophandlerIsNSStep_ = isNeighborSearchingStep;
218 stophandlerCurrentStep_ = step;
219 stopHandler_->setSignal();
221 wallcycle_start(wcycle, ewcSTEP);
224 void ModularSimulator::postStep(Step step, Time gmx_unused time)
229 if (do_per_step(step, inputrec->nstlog))
231 if (fflush(fplog) != 0)
233 gmx_fatal(FARGS, "Cannot flush logfile - maybe you are out of disk space?");
237 const bool do_verbose = mdrunOptions.verbose
238 && (step % mdrunOptions.verboseStepPrintInterval == 0
239 || step == inputrec->init_step || step == signalHelper_->lastStep_);
240 // Print the remaining wall clock time for the run
241 if (MASTER(cr) && (do_verbose || gmx_got_usr_signal())
242 && !(pmeLoadBalanceHelper_ && pmeLoadBalanceHelper_->pmePrinting()))
244 print_time(stderr, walltime_accounting, step, inputrec, cr);
247 double cycles = wallcycle_stop(wcycle, ewcSTEP);
248 if (DOMAINDECOMP(cr) && wcycle)
250 dd_cycles_add(cr->dd, static_cast<float>(cycles), ddCyclStep);
253 resetHandler_->resetCounters(
254 step, step - inputrec->init_step, mdlog, fplog, cr, fr->nbv.get(), nrnb, fr->pmedata,
255 pmeLoadBalanceHelper_ ? pmeLoadBalanceHelper_->loadBalancingObject() : nullptr, wcycle,
256 walltime_accounting);
259 void ModularSimulator::simulatorTeardown()
262 // Stop measuring walltime
263 walltime_accounting_end_time(walltime_accounting);
265 if (!thisRankHasDuty(cr, DUTY_PME))
267 /* Tell the PME only node to finish */
268 gmx_pme_send_finish(cr);
271 walltime_accounting_set_nsteps_done(walltime_accounting, step_ - inputrec->init_step);
274 void ModularSimulator::populateTaskQueue()
276 auto registerRunFunction = std::make_unique<RegisterRunFunction>(
277 [this](SimulatorRunFunctionPtr ptr) { taskQueue_.push(std::move(ptr)); });
279 Time startTime = inputrec->init_t;
280 Time timeStep = inputrec->delta_t;
281 Time time = startTime + step_ * timeStep;
283 // Run an initial call to the signallers
284 for (auto& signaller : signallerList_)
286 signaller->signal(step_, time);
289 if (checkpointHelper_)
291 checkpointHelper_->run(step_, time);
294 if (pmeLoadBalanceHelper_)
296 pmeLoadBalanceHelper_->run(step_, time);
300 domDecHelper_->run(step_, time);
305 // local variables for lambda capturing
306 const int step = step_;
307 const bool isNSStep = step == signalHelper_->nextNSStep_;
310 (*registerRunFunction)(std::make_unique<SimulatorRunFunction>(
311 [this, step, time, isNSStep]() { preStep(step, time, isNSStep); }));
312 // register elements for step
313 for (auto& element : elementCallList_)
315 element->scheduleTask(step_, time, registerRunFunction);
317 // register post-step
318 (*registerRunFunction)(
319 std::make_unique<SimulatorRunFunction>([this, step, time]() { postStep(step, time); }));
323 time = startTime + step_ * timeStep;
324 for (auto& signaller : signallerList_)
326 signaller->signal(step_, time);
328 } while (step_ != signalHelper_->nextNSStep_ && step_ <= signalHelper_->lastStep_);
331 void ModularSimulator::constructElementsAndSignallers()
333 /* When restarting from a checkpoint, it can be appropriate to
334 * initialize ekind from quantities in the checkpoint. Otherwise,
335 * compute_globals must initialize ekind before the simulation
336 * starts/restarts. However, only the master rank knows what was
337 * found in the checkpoint file, so we have to communicate in
338 * order to coordinate the restart.
340 * TODO (modular) This should become obsolete when checkpoint reading
341 * happens within the modular simulator framework: The energy
342 * element should read its data from the checkpoint file pointer,
343 * and signal to the compute globals element if it needs anything
346 * TODO (legacy) Consider removing this communication if/when checkpoint
347 * reading directly follows .tpr reading, because all ranks can
348 * agree on hasReadEkinState at that time.
350 bool hasReadEkinState = MASTER(cr) ? state_global->ekinstate.hasReadEkinState : false;
353 gmx_bcast(sizeof(hasReadEkinState), &hasReadEkinState, cr->mpi_comm_mygroup);
355 if (hasReadEkinState)
357 restore_ekinstate_from_state(cr, ekind, &state_global->ekinstate);
361 * Build data structures
364 std::make_unique<TopologyHolder>(*top_global, cr, inputrec, fr, mdAtoms, constr, vsite);
366 std::unique_ptr<FreeEnergyPerturbationElement> freeEnergyPerturbationElement = nullptr;
367 FreeEnergyPerturbationElement* freeEnergyPerturbationElementPtr = nullptr;
368 if (inputrec->efep != efepNO)
370 freeEnergyPerturbationElement =
371 std::make_unique<FreeEnergyPerturbationElement>(fplog, inputrec, mdAtoms);
372 freeEnergyPerturbationElementPtr = freeEnergyPerturbationElement.get();
375 auto statePropagatorData = std::make_unique<StatePropagatorData>(
376 top_global->natoms, fplog, cr, state_global, inputrec->nstxout, inputrec->nstvout,
377 inputrec->nstfout, inputrec->nstxout_compressed, fr->nbv->useGpu(),
378 freeEnergyPerturbationElementPtr, topologyHolder_.get(), fr->bMolPBC,
379 mdrunOptions.writeConfout, opt2fn("-c", nfile, fnm), inputrec, mdAtoms->mdatoms());
380 auto statePropagatorDataPtr = compat::make_not_null(statePropagatorData.get());
382 auto energyElement = std::make_unique<EnergyElement>(
383 statePropagatorDataPtr, freeEnergyPerturbationElementPtr, top_global, inputrec, mdAtoms,
384 enerd, ekind, constr, fplog, fcd, mdModulesNotifier, MASTER(cr), observablesHistory,
386 auto energyElementPtr = compat::make_not_null(energyElement.get());
391 const bool simulationsShareState = false;
392 stopHandler_ = stopHandlerBuilder->getStopHandlerMD(
393 compat::not_null<SimulationSignal*>(&signals_[eglsSTOPCOND]), simulationsShareState,
394 MASTER(cr), inputrec->nstlist, mdrunOptions.reproducible, nstglobalcomm_,
395 mdrunOptions.maximumHoursToRun, inputrec->nstlist == 0, fplog, stophandlerCurrentStep_,
396 stophandlerIsNSStep_, walltime_accounting);
399 * Create simulator builders
401 SignallerBuilder<NeighborSearchSignaller> neighborSearchSignallerBuilder;
402 SignallerBuilder<LastStepSignaller> lastStepSignallerBuilder;
403 SignallerBuilder<LoggingSignaller> loggingSignallerBuilder;
404 SignallerBuilder<EnergySignaller> energySignallerBuilder;
405 SignallerBuilder<TrajectorySignaller> trajectorySignallerBuilder;
406 TrajectoryElementBuilder trajectoryElementBuilder;
409 * Register data structures to signallers
411 trajectoryElementBuilder.registerWriterClient(statePropagatorDataPtr);
412 trajectorySignallerBuilder.registerSignallerClient(statePropagatorDataPtr);
413 lastStepSignallerBuilder.registerSignallerClient(statePropagatorDataPtr);
415 trajectoryElementBuilder.registerWriterClient(energyElementPtr);
416 trajectorySignallerBuilder.registerSignallerClient(energyElementPtr);
417 energySignallerBuilder.registerSignallerClient(energyElementPtr);
419 // Register the simulator itself to the neighbor search / last step signaller
420 neighborSearchSignallerBuilder.registerSignallerClient(compat::make_not_null(signalHelper_.get()));
421 lastStepSignallerBuilder.registerSignallerClient(compat::make_not_null(signalHelper_.get()));
424 * Build integrator - this takes care of force calculation, propagation,
425 * constraining, and of the place the statePropagatorData and the energy element
426 * have a full timestep state.
428 // TODO: Make a CheckpointHelperBuilder
429 std::vector<ICheckpointHelperClient*> checkpointClients = { statePropagatorDataPtr, energyElementPtr,
430 freeEnergyPerturbationElementPtr };
431 CheckBondedInteractionsCallbackPtr checkBondedInteractionsCallback = nullptr;
432 auto integrator = buildIntegrator(&neighborSearchSignallerBuilder, &energySignallerBuilder,
433 &loggingSignallerBuilder, &trajectorySignallerBuilder,
434 &checkpointClients, &checkBondedInteractionsCallback,
435 statePropagatorDataPtr, energyElementPtr,
436 freeEnergyPerturbationElementPtr, hasReadEkinState);
439 * Build infrastructure elements
442 if (PmeLoadBalanceHelper::doPmeLoadBalancing(mdrunOptions, inputrec, fr))
444 pmeLoadBalanceHelper_ = std::make_unique<PmeLoadBalanceHelper>(
445 mdrunOptions.verbose, statePropagatorDataPtr, fplog, cr, mdlog, inputrec, wcycle, fr);
446 neighborSearchSignallerBuilder.registerSignallerClient(
447 compat::make_not_null(pmeLoadBalanceHelper_.get()));
450 if (DOMAINDECOMP(cr))
452 GMX_ASSERT(checkBondedInteractionsCallback,
453 "Domain decomposition needs a callback for check the number of bonded "
455 domDecHelper_ = std::make_unique<DomDecHelper>(
456 mdrunOptions.verbose, mdrunOptions.verboseStepPrintInterval, statePropagatorDataPtr,
457 topologyHolder_.get(), std::move(checkBondedInteractionsCallback), nstglobalcomm_, fplog,
458 cr, mdlog, constr, inputrec, mdAtoms, nrnb, wcycle, fr, vsite, imdSession, pull_work);
459 neighborSearchSignallerBuilder.registerSignallerClient(compat::make_not_null(domDecHelper_.get()));
462 const bool simulationsShareResetCounters = false;
463 resetHandler_ = std::make_unique<ResetHandler>(
464 compat::make_not_null<SimulationSignal*>(&signals_[eglsRESETCOUNTERS]),
465 simulationsShareResetCounters, inputrec->nsteps, MASTER(cr),
466 mdrunOptions.timingOptions.resetHalfway, mdrunOptions.maximumHoursToRun, mdlog, wcycle,
467 walltime_accounting);
470 * Build signaller list
472 * Note that as signallers depend on each others, the order of calling the signallers
473 * matters. It is the responsibility of this builder to ensure that the order is
476 auto energySignaller = energySignallerBuilder.build(
477 inputrec->nstcalcenergy, inputrec->fepvals->nstdhdl, inputrec->nstpcouple);
478 trajectorySignallerBuilder.registerSignallerClient(compat::make_not_null(energySignaller.get()));
479 loggingSignallerBuilder.registerSignallerClient(compat::make_not_null(energySignaller.get()));
480 auto trajectoryElement = trajectoryElementBuilder.build(
481 fplog, nfile, fnm, mdrunOptions, cr, outputProvider, mdModulesNotifier, inputrec,
482 top_global, oenv, wcycle, startingBehavior, simulationsShareState);
483 loggingSignallerBuilder.registerSignallerClient(compat::make_not_null(trajectoryElement.get()));
484 trajectorySignallerBuilder.registerSignallerClient(compat::make_not_null(trajectoryElement.get()));
485 auto trajectorySignaller = trajectorySignallerBuilder.build(
486 inputrec->nstxout, inputrec->nstvout, inputrec->nstfout, inputrec->nstxout_compressed,
487 trajectoryElement->tngBoxOut(), trajectoryElement->tngLambdaOut(),
488 trajectoryElement->tngBoxOutCompressed(), trajectoryElement->tngLambdaOutCompressed(),
489 inputrec->nstenergy);
491 // Add checkpoint helper here since we need a pointer to the trajectory element and
492 // need to register it with the lastStepSignallerBuilder
493 auto checkpointHandler = std::make_unique<CheckpointHandler>(
494 compat::make_not_null<SimulationSignal*>(&signals_[eglsCHKPT]), simulationsShareState,
495 inputrec->nstlist == 0, MASTER(cr), mdrunOptions.writeConfout,
496 mdrunOptions.checkpointOptions.period);
497 checkpointHelper_ = std::make_unique<CheckpointHelper>(
498 std::move(checkpointClients), std::move(checkpointHandler), inputrec->init_step,
499 trajectoryElement.get(), top_global->natoms, fplog, cr, observablesHistory,
500 walltime_accounting, state_global, mdrunOptions.writeConfout);
501 lastStepSignallerBuilder.registerSignallerClient(compat::make_not_null(checkpointHelper_.get()));
503 lastStepSignallerBuilder.registerSignallerClient(compat::make_not_null(trajectorySignaller.get()));
504 auto loggingSignaller =
505 loggingSignallerBuilder.build(inputrec->nstlog, inputrec->init_step, inputrec->init_t);
506 lastStepSignallerBuilder.registerSignallerClient(compat::make_not_null(loggingSignaller.get()));
507 auto lastStepSignaller =
508 lastStepSignallerBuilder.build(inputrec->nsteps, inputrec->init_step, stopHandler_.get());
509 neighborSearchSignallerBuilder.registerSignallerClient(compat::make_not_null(lastStepSignaller.get()));
510 auto neighborSearchSignaller = neighborSearchSignallerBuilder.build(
511 inputrec->nstlist, inputrec->init_step, inputrec->init_t);
513 signallerList_.emplace_back(std::move(neighborSearchSignaller));
514 signallerList_.emplace_back(std::move(lastStepSignaller));
515 signallerList_.emplace_back(std::move(loggingSignaller));
516 signallerList_.emplace_back(std::move(trajectorySignaller));
517 signallerList_.emplace_back(std::move(energySignaller));
520 * Build the element list
522 * This is the actual sequence of (non-infrastructure) elements to be run.
523 * For NVE, the trajectory element is used outside of the integrator
524 * (composite) element, as well as the checkpoint helper. The checkpoint
525 * helper should be on top of the loop, and is only part of the simulator
526 * call list to be able to react to the last step being signalled.
528 addToCallList(checkpointHelper_, elementCallList_);
529 if (freeEnergyPerturbationElement)
531 addToCallListAndMove(std::move(freeEnergyPerturbationElement), elementCallList_,
532 elementsOwnershipList_);
534 addToCallListAndMove(std::move(integrator), elementCallList_, elementsOwnershipList_);
535 addToCallListAndMove(std::move(trajectoryElement), elementCallList_, elementsOwnershipList_);
536 // for vv, we need to setup statePropagatorData after the compute
537 // globals so that we reset the right velocities
538 // TODO: Avoid this by getting rid of the need of resetting velocities in vv
539 elementsOwnershipList_.emplace_back(std::move(statePropagatorData));
540 elementsOwnershipList_.emplace_back(std::move(energyElement));
543 std::unique_ptr<ISimulatorElement>
544 ModularSimulator::buildForces(SignallerBuilder<NeighborSearchSignaller>* neighborSearchSignallerBuilder,
545 SignallerBuilder<EnergySignaller>* energySignallerBuilder,
546 StatePropagatorData* statePropagatorDataPtr,
547 EnergyElement* energyElementPtr,
548 FreeEnergyPerturbationElement* freeEnergyPerturbationElement)
550 const bool isVerbose = mdrunOptions.verbose;
551 const bool isDynamicBox = inputrecDynamicBox(inputrec);
553 auto forceElement = std::make_unique<ForceElement>(
554 statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElement, isVerbose,
555 isDynamicBox, fplog, cr, inputrec, mdAtoms, nrnb, fr, fcd, wcycle, runScheduleWork, vsite,
556 imdSession, pull_work, constr, &topologyHolder_->globalTopology(), enforcedRotation);
557 topologyHolder_->registerClient(forceElement.get());
558 neighborSearchSignallerBuilder->registerSignallerClient(compat::make_not_null(forceElement.get()));
559 energySignallerBuilder->registerSignallerClient(compat::make_not_null(forceElement.get()));
561 // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
562 return std::move(forceElement);
565 std::unique_ptr<ISimulatorElement> ModularSimulator::buildIntegrator(
566 SignallerBuilder<NeighborSearchSignaller>* neighborSearchSignallerBuilder,
567 SignallerBuilder<EnergySignaller>* energySignallerBuilder,
568 SignallerBuilder<LoggingSignaller>* loggingSignallerBuilder,
569 SignallerBuilder<TrajectorySignaller>* trajectorySignallerBuilder,
570 std::vector<ICheckpointHelperClient*>* checkpointClients,
571 CheckBondedInteractionsCallbackPtr* checkBondedInteractionsCallback,
572 compat::not_null<StatePropagatorData*> statePropagatorDataPtr,
573 compat::not_null<EnergyElement*> energyElementPtr,
574 FreeEnergyPerturbationElement* freeEnergyPerturbationElementPtr,
575 bool hasReadEkinState)
578 buildForces(neighborSearchSignallerBuilder, energySignallerBuilder,
579 statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr);
581 // list of elements owned by the simulator composite object
582 std::vector<std::unique_ptr<ISimulatorElement>> elementsOwnershipList;
583 // call list of the simulator composite object
584 std::vector<compat::not_null<ISimulatorElement*>> elementCallList;
586 std::function<void()> needToCheckNumberOfBondedInteractions;
587 if (inputrec->eI == eiMD)
589 auto computeGlobalsElement =
590 std::make_unique<ComputeGlobalsElement<ComputeGlobalsAlgorithm::LeapFrog>>(
591 statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr,
592 &signals_, nstglobalcomm_, fplog, mdlog, cr, inputrec, mdAtoms, nrnb,
593 wcycle, fr, &topologyHolder_->globalTopology(), constr, hasReadEkinState);
594 topologyHolder_->registerClient(computeGlobalsElement.get());
595 energySignallerBuilder->registerSignallerClient(compat::make_not_null(computeGlobalsElement.get()));
596 trajectorySignallerBuilder->registerSignallerClient(
597 compat::make_not_null(computeGlobalsElement.get()));
599 *checkBondedInteractionsCallback =
600 computeGlobalsElement->getCheckNumberOfBondedInteractionsCallback();
602 auto propagator = std::make_unique<Propagator<IntegrationStep::LeapFrog>>(
603 inputrec->delta_t, statePropagatorDataPtr, mdAtoms, wcycle);
605 addToCallListAndMove(std::move(forceElement), elementCallList, elementsOwnershipList);
606 addToCallList(statePropagatorDataPtr, elementCallList); // we have a full microstate at time t here!
607 if (inputrec->etc == etcVRESCALE)
609 // TODO: With increased complexity of the propagator, this will need further development,
610 // e.g. using propagators templated for velocity propagation policies and a builder
611 propagator->setNumVelocityScalingVariables(inputrec->opts.ngtc);
612 auto thermostat = std::make_unique<VRescaleThermostat>(
613 inputrec->nsttcouple, -1, false, inputrec->ld_seed, inputrec->opts.ngtc,
614 inputrec->delta_t * inputrec->nsttcouple, inputrec->opts.ref_t, inputrec->opts.tau_t,
615 inputrec->opts.nrdf, energyElementPtr, propagator->viewOnVelocityScaling(),
616 propagator->velocityScalingCallback(), state_global, cr, inputrec->bContinuation);
617 checkpointClients->emplace_back(thermostat.get());
618 energyElementPtr->setVRescaleThermostat(thermostat.get());
619 addToCallListAndMove(std::move(thermostat), elementCallList, elementsOwnershipList);
622 std::unique_ptr<ParrinelloRahmanBarostat> prBarostat = nullptr;
623 if (inputrec->epc == epcPARRINELLORAHMAN)
625 // Building the PR barostat here since it needs access to the propagator
626 // and we want to be able to move the propagator object
627 prBarostat = std::make_unique<ParrinelloRahmanBarostat>(
628 inputrec->nstpcouple, -1, inputrec->delta_t * inputrec->nstpcouple,
629 inputrec->init_step, propagator->viewOnPRScalingMatrix(),
630 propagator->prScalingCallback(), statePropagatorDataPtr, energyElementPtr,
631 fplog, inputrec, mdAtoms, state_global, cr, inputrec->bContinuation);
632 energyElementPtr->setParrinelloRahamnBarostat(prBarostat.get());
633 checkpointClients->emplace_back(prBarostat.get());
635 addToCallListAndMove(std::move(propagator), elementCallList, elementsOwnershipList);
638 auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Positions>>(
639 constr, statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr,
640 MASTER(cr), fplog, inputrec, mdAtoms->mdatoms());
641 auto constraintElementPtr = compat::make_not_null(constraintElement.get());
642 energySignallerBuilder->registerSignallerClient(constraintElementPtr);
643 trajectorySignallerBuilder->registerSignallerClient(constraintElementPtr);
644 loggingSignallerBuilder->registerSignallerClient(constraintElementPtr);
646 addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
649 addToCallListAndMove(std::move(computeGlobalsElement), elementCallList, elementsOwnershipList);
650 addToCallList(energyElementPtr, elementCallList); // we have the energies at time t here!
653 addToCallListAndMove(std::move(prBarostat), elementCallList, elementsOwnershipList);
656 else if (inputrec->eI == eiVV)
658 auto computeGlobalsElement =
659 std::make_unique<ComputeGlobalsElement<ComputeGlobalsAlgorithm::VelocityVerlet>>(
660 statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr,
661 &signals_, nstglobalcomm_, fplog, mdlog, cr, inputrec, mdAtoms, nrnb,
662 wcycle, fr, &topologyHolder_->globalTopology(), constr, hasReadEkinState);
663 topologyHolder_->registerClient(computeGlobalsElement.get());
664 energySignallerBuilder->registerSignallerClient(compat::make_not_null(computeGlobalsElement.get()));
665 trajectorySignallerBuilder->registerSignallerClient(
666 compat::make_not_null(computeGlobalsElement.get()));
668 *checkBondedInteractionsCallback =
669 computeGlobalsElement->getCheckNumberOfBondedInteractionsCallback();
671 auto propagatorVelocities = std::make_unique<Propagator<IntegrationStep::VelocitiesOnly>>(
672 inputrec->delta_t * 0.5, statePropagatorDataPtr, mdAtoms, wcycle);
673 auto propagatorVelocitiesAndPositions =
674 std::make_unique<Propagator<IntegrationStep::VelocityVerletPositionsAndVelocities>>(
675 inputrec->delta_t, statePropagatorDataPtr, mdAtoms, wcycle);
677 addToCallListAndMove(std::move(forceElement), elementCallList, elementsOwnershipList);
679 std::unique_ptr<ParrinelloRahmanBarostat> prBarostat = nullptr;
680 if (inputrec->epc == epcPARRINELLORAHMAN)
682 // Building the PR barostat here since it needs access to the propagator
683 // and we want to be able to move the propagator object
684 prBarostat = std::make_unique<ParrinelloRahmanBarostat>(
685 inputrec->nstpcouple, -1, inputrec->delta_t * inputrec->nstpcouple,
686 inputrec->init_step, propagatorVelocities->viewOnPRScalingMatrix(),
687 propagatorVelocities->prScalingCallback(), statePropagatorDataPtr, energyElementPtr,
688 fplog, inputrec, mdAtoms, state_global, cr, inputrec->bContinuation);
689 energyElementPtr->setParrinelloRahamnBarostat(prBarostat.get());
690 checkpointClients->emplace_back(prBarostat.get());
692 addToCallListAndMove(std::move(propagatorVelocities), elementCallList, elementsOwnershipList);
695 auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Velocities>>(
696 constr, statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr,
697 MASTER(cr), fplog, inputrec, mdAtoms->mdatoms());
698 energySignallerBuilder->registerSignallerClient(compat::make_not_null(constraintElement.get()));
699 trajectorySignallerBuilder->registerSignallerClient(
700 compat::make_not_null(constraintElement.get()));
701 loggingSignallerBuilder->registerSignallerClient(
702 compat::make_not_null(constraintElement.get()));
704 addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
706 addToCallList(compat::make_not_null(computeGlobalsElement.get()), elementCallList);
707 addToCallList(statePropagatorDataPtr, elementCallList); // we have a full microstate at time t here!
708 if (inputrec->etc == etcVRESCALE)
710 // TODO: With increased complexity of the propagator, this will need further development,
711 // e.g. using propagators templated for velocity propagation policies and a builder
712 propagatorVelocitiesAndPositions->setNumVelocityScalingVariables(inputrec->opts.ngtc);
713 auto thermostat = std::make_unique<VRescaleThermostat>(
714 inputrec->nsttcouple, 0, true, inputrec->ld_seed, inputrec->opts.ngtc,
715 inputrec->delta_t * inputrec->nsttcouple, inputrec->opts.ref_t,
716 inputrec->opts.tau_t, inputrec->opts.nrdf, energyElementPtr,
717 propagatorVelocitiesAndPositions->viewOnVelocityScaling(),
718 propagatorVelocitiesAndPositions->velocityScalingCallback(), state_global, cr,
719 inputrec->bContinuation);
720 checkpointClients->emplace_back(thermostat.get());
721 energyElementPtr->setVRescaleThermostat(thermostat.get());
722 addToCallListAndMove(std::move(thermostat), elementCallList, elementsOwnershipList);
724 addToCallListAndMove(std::move(propagatorVelocitiesAndPositions), elementCallList,
725 elementsOwnershipList);
728 auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Positions>>(
729 constr, statePropagatorDataPtr, energyElementPtr, freeEnergyPerturbationElementPtr,
730 MASTER(cr), fplog, inputrec, mdAtoms->mdatoms());
731 energySignallerBuilder->registerSignallerClient(compat::make_not_null(constraintElement.get()));
732 trajectorySignallerBuilder->registerSignallerClient(
733 compat::make_not_null(constraintElement.get()));
734 loggingSignallerBuilder->registerSignallerClient(
735 compat::make_not_null(constraintElement.get()));
737 addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
739 addToCallListAndMove(std::move(computeGlobalsElement), elementCallList, elementsOwnershipList);
740 addToCallList(energyElementPtr, elementCallList); // we have the energies at time t here!
743 addToCallListAndMove(std::move(prBarostat), elementCallList, elementsOwnershipList);
748 gmx_fatal(FARGS, "Integrator not implemented for the modular simulator.");
751 auto integrator = std::make_unique<CompositeSimulatorElement>(std::move(elementCallList),
752 std::move(elementsOwnershipList));
753 // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
754 return std::move(integrator);
757 bool ModularSimulator::isInputCompatible(bool exitOnFailure,
758 const t_inputrec* inputrec,
760 const gmx_mtop_t& globalTopology,
761 const gmx_multisim_t* ms,
762 const ReplicaExchangeParameters& replExParams,
764 bool doEssentialDynamics,
767 auto conditionalAssert = [exitOnFailure](bool condition, const char* message) {
770 GMX_RELEASE_ASSERT(condition, message);
775 bool isInputCompatible = true;
777 // GMX_USE_MODULAR_SIMULATOR allows to use modular simulator also for non-standard uses,
778 // such as the leap-frog integrator
779 const auto modularSimulatorExplicitlyTurnedOn = (getenv("GMX_USE_MODULAR_SIMULATOR") != nullptr);
780 // GMX_USE_MODULAR_SIMULATOR allows to use disable modular simulator for all uses,
781 // including the velocity-verlet integrator used by default
782 const auto modularSimulatorExplicitlyTurnedOff = (getenv("GMX_DISABLE_MODULAR_SIMULATOR") != nullptr);
785 !(modularSimulatorExplicitlyTurnedOn && modularSimulatorExplicitlyTurnedOff),
786 "Cannot have both GMX_USE_MODULAR_SIMULATOR=ON and GMX_DISABLE_MODULAR_SIMULATOR=ON. "
787 "Unset one of the two environment variables to explicitly chose which simulator to "
789 "or unset both to recover default behavior.");
792 !(modularSimulatorExplicitlyTurnedOff && inputrec->eI == eiVV
793 && inputrec->epc == epcPARRINELLORAHMAN),
794 "Cannot use a Parrinello-Rahman barostat with md-vv and "
795 "GMX_DISABLE_MODULAR_SIMULATOR=ON, "
796 "as the Parrinello-Rahman barostat is not implemented in the legacy simulator. Unset "
797 "GMX_DISABLE_MODULAR_SIMULATOR or use a different pressure control algorithm.");
801 && conditionalAssert(
802 inputrec->eI == eiMD || inputrec->eI == eiVV,
803 "Only integrators md and md-vv are supported by the modular simulator.");
804 isInputCompatible = isInputCompatible
805 && conditionalAssert(inputrec->eI != eiMD || modularSimulatorExplicitlyTurnedOn,
806 "Set GMX_USE_MODULAR_SIMULATOR=ON to use the modular "
807 "simulator with integrator md.");
810 && conditionalAssert(!doRerun, "Rerun is not supported by the modular simulator.");
813 && conditionalAssert(
814 inputrec->etc == etcNO || inputrec->etc == etcVRESCALE,
815 "Only v-rescale thermostat is supported by the modular simulator.");
818 && conditionalAssert(
819 inputrec->epc == epcNO || inputrec->epc == epcPARRINELLORAHMAN,
820 "Only Parrinello-Rahman barostat is supported by the modular simulator.");
823 && conditionalAssert(
824 !(inputrecNptTrotter(inputrec) || inputrecNphTrotter(inputrec)
825 || inputrecNvtTrotter(inputrec)),
826 "Legacy Trotter decomposition is not supported by the modular simulator.");
827 isInputCompatible = isInputCompatible
828 && conditionalAssert(inputrec->efep == efepNO || inputrec->efep == efepYES
829 || inputrec->efep == efepSLOWGROWTH,
830 "Expanded ensemble free energy calculation is not "
831 "supported by the modular simulator.");
832 isInputCompatible = isInputCompatible
833 && conditionalAssert(!inputrec->bPull,
834 "Pulling is not supported by the modular simulator.");
837 && conditionalAssert(inputrec->opts.ngacc == 1 && inputrec->opts.acc[0][XX] == 0.0
838 && inputrec->opts.acc[0][YY] == 0.0
839 && inputrec->opts.acc[0][ZZ] == 0.0 && inputrec->cos_accel == 0.0,
840 "Acceleration is not supported by the modular simulator.");
843 && conditionalAssert(inputrec->opts.ngfrz == 1 && inputrec->opts.nFreeze[0][XX] == 0
844 && inputrec->opts.nFreeze[0][YY] == 0
845 && inputrec->opts.nFreeze[0][ZZ] == 0,
846 "Freeze groups are not supported by the modular simulator.");
849 && conditionalAssert(
850 inputrec->deform[XX][XX] == 0.0 && inputrec->deform[XX][YY] == 0.0
851 && inputrec->deform[XX][ZZ] == 0.0 && inputrec->deform[YY][XX] == 0.0
852 && inputrec->deform[YY][YY] == 0.0 && inputrec->deform[YY][ZZ] == 0.0
853 && inputrec->deform[ZZ][XX] == 0.0 && inputrec->deform[ZZ][YY] == 0.0
854 && inputrec->deform[ZZ][ZZ] == 0.0,
855 "Deformation is not supported by the modular simulator.");
858 && conditionalAssert(gmx_mtop_interaction_count(globalTopology, IF_VSITE) == 0,
859 "Virtual sites are not supported by the modular simulator.");
860 isInputCompatible = isInputCompatible
861 && conditionalAssert(!inputrec->bDoAwh,
862 "AWH is not supported by the modular simulator.");
865 && conditionalAssert(gmx_mtop_ftype_count(globalTopology, F_DISRES) == 0,
866 "Distance restraints are not supported by the modular simulator.");
869 && conditionalAssert(
870 gmx_mtop_ftype_count(globalTopology, F_ORIRES) == 0,
871 "Orientation restraints are not supported by the modular simulator.");
874 && conditionalAssert(ms == nullptr,
875 "Multi-sim are not supported by the modular simulator.");
878 && conditionalAssert(replExParams.exchangeInterval == 0,
879 "Replica exchange is not supported by the modular simulator.");
881 int numEnsembleRestraintSystems;
884 numEnsembleRestraintSystems = fcd->disres.nsystems;
888 auto distantRestraintEnsembleEnvVar = getenv("GMX_DISRE_ENSEMBLE_SIZE");
889 numEnsembleRestraintSystems =
890 (ms != nullptr && distantRestraintEnsembleEnvVar != nullptr)
891 ? static_cast<int>(strtol(distantRestraintEnsembleEnvVar, nullptr, 10))
896 && conditionalAssert(numEnsembleRestraintSystems <= 1,
897 "Ensemble restraints are not supported by the modular simulator.");
900 && conditionalAssert(!doSimulatedAnnealing(inputrec),
901 "Simulated annealing is not supported by the modular simulator.");
904 && conditionalAssert(!inputrec->bSimTemp,
905 "Simulated tempering is not supported by the modular simulator.");
906 isInputCompatible = isInputCompatible
907 && conditionalAssert(!inputrec->bExpanded,
908 "Expanded ensemble simulations are not supported by "
909 "the modular simulator.");
912 && conditionalAssert(!doEssentialDynamics,
913 "Essential dynamics is not supported by the modular simulator.");
914 isInputCompatible = isInputCompatible
915 && conditionalAssert(inputrec->eSwapCoords == eswapNO,
916 "Ion / water position swapping is not supported by "
917 "the modular simulator.");
920 && conditionalAssert(!inputrec->bIMD,
921 "Interactive MD is not supported by the modular simulator.");
924 && conditionalAssert(!doMembed,
925 "Membrane embedding is not supported by the modular simulator.");
926 // TODO: Change this to the boolean passed when we merge the user interface change for the GPU update.
929 && conditionalAssert(
930 getenv("GMX_FORCE_UPDATE_DEFAULT_GPU") == nullptr,
931 "Integration on the GPU is not supported by the modular simulator.");
932 // Modular simulator is centered around NS updates
933 // TODO: think how to handle nstlist == 0
934 isInputCompatible = isInputCompatible
935 && conditionalAssert(inputrec->nstlist != 0,
936 "Simulations without neighbor list update are not "
937 "supported by the modular simulator.");
938 isInputCompatible = isInputCompatible
939 && conditionalAssert(!GMX_FAHCORE,
940 "GMX_FAHCORE not supported by the modular simulator.");
942 return isInputCompatible;
945 void ModularSimulator::checkInputForDisabledFunctionality()
947 isInputCompatible(true, inputrec, doRerun, *top_global, ms, replExParams, fcd,
948 opt2bSet("-ei", nfile, fnm), membed != nullptr);
949 if (observablesHistory->edsamHistory)
952 "The checkpoint is from a run with essential dynamics sampling, "
953 "but the current run did not specify the -ei option. "
954 "Either specify the -ei option to mdrun, or do not use this checkpoint file.");
958 SignallerCallbackPtr ModularSimulator::SignalHelper::registerLastStepCallback()
960 return std::make_unique<SignallerCallback>(
961 [this](Step step, Time gmx_unused time) { this->lastStep_ = step; });
964 SignallerCallbackPtr ModularSimulator::SignalHelper::registerNSCallback()
966 return std::make_unique<SignallerCallback>(
967 [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; });