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/gmxlib/network.h"
51 #include "gromacs/gmxlib/nrnb.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/mdlib/checkpointhandler.h"
54 #include "gromacs/mdlib/constr.h"
55 #include "gromacs/mdlib/energyoutput.h"
56 #include "gromacs/mdlib/forcerec.h"
57 #include "gromacs/mdlib/mdatoms.h"
58 #include "gromacs/mdlib/resethandler.h"
59 #include "gromacs/mdlib/stat.h"
60 #include "gromacs/mdlib/update.h"
61 #include "gromacs/mdrun/replicaexchange.h"
62 #include "gromacs/mdrun/shellfc.h"
63 #include "gromacs/mdrunutility/handlerestart.h"
64 #include "gromacs/mdrunutility/printtime.h"
65 #include "gromacs/mdtypes/commrec.h"
66 #include "gromacs/mdtypes/fcdata.h"
67 #include "gromacs/mdtypes/inputrec.h"
68 #include "gromacs/mdtypes/mdrunoptions.h"
69 #include "gromacs/mdtypes/observableshistory.h"
70 #include "gromacs/mdtypes/state.h"
71 #include "gromacs/nbnxm/nbnxm.h"
72 #include "gromacs/timing/walltime_accounting.h"
73 #include "gromacs/topology/mtop_util.h"
74 #include "gromacs/topology/topology.h"
75 #include "gromacs/utility/cstringutil.h"
76 #include "gromacs/utility/fatalerror.h"
78 #include "compositesimulatorelement.h"
79 #include "computeglobalselement.h"
80 #include "constraintelement.h"
81 #include "energyelement.h"
82 #include "forceelement.h"
83 #include "freeenergyperturbationelement.h"
84 #include "parrinellorahmanbarostat.h"
85 #include "propagator.h"
86 #include "shellfcelement.h"
87 #include "signallers.h"
88 #include "statepropagatordata.h"
89 #include "trajectoryelement.h"
90 #include "vrescalethermostat.h"
94 void ModularSimulator::run()
96 GMX_LOG(mdlog
.info
).asParagraph().appendText("Using the modular simulator.");
97 constructElementsAndSignallers();
99 for (auto& signaller
: signallerCallList_
)
101 signaller
->signallerSetup();
105 domDecHelper_
->setup();
108 for (auto& element
: elementsOwnershipList_
)
110 element
->elementSetup();
112 if (pmeLoadBalanceHelper_
)
114 // State must have been initialized so pmeLoadBalanceHelper_ gets a valid box
115 pmeLoadBalanceHelper_
->setup();
118 while (step_
<= signalHelper_
->lastStep_
)
122 while (!taskQueue_
.empty())
124 auto task
= std::move(taskQueue_
.front());
131 for (auto& element
: elementsOwnershipList_
)
133 element
->elementTeardown();
135 if (pmeLoadBalanceHelper_
)
137 pmeLoadBalanceHelper_
->teardown();
142 void ModularSimulator::simulatorSetup()
144 if (!mdrunOptions
.writeConfout
)
146 // This is on by default, and the main known use case for
147 // turning it off is for convenience in benchmarking, which is
148 // something that should not show up in the general user
153 "The -noconfout functionality is deprecated, and "
154 "may be removed in a future version.");
159 char sbuf
[STEPSTRSIZE
], sbuf2
[STEPSTRSIZE
];
160 std::string timeString
;
161 fprintf(stderr
, "starting mdrun '%s'\n", *(top_global
->name
));
162 if (inputrec
->nsteps
>= 0)
164 timeString
= formatString("%8.1f", static_cast<double>(inputrec
->init_step
+ inputrec
->nsteps
)
165 * inputrec
->delta_t
);
169 timeString
= "infinite";
171 if (inputrec
->init_step
> 0)
173 fprintf(stderr
, "%s steps, %s ps (continuing from step %s, %8.1f ps).\n",
174 gmx_step_str(inputrec
->init_step
+ inputrec
->nsteps
, sbuf
), timeString
.c_str(),
175 gmx_step_str(inputrec
->init_step
, sbuf2
), inputrec
->init_step
* inputrec
->delta_t
);
179 fprintf(stderr
, "%s steps, %s ps.\n", gmx_step_str(inputrec
->nsteps
, sbuf
),
182 fprintf(fplog
, "\n");
185 walltime_accounting_start_time(walltime_accounting
);
186 wallcycle_start(wcycle
, ewcRUN
);
187 print_start(fplog
, cr
, walltime_accounting
, "mdrun");
189 step_
= inputrec
->init_step
;
192 void ModularSimulator::preStep(Step step
, Time gmx_unused time
, bool isNeighborSearchingStep
)
194 if (stopHandler_
->stoppingAfterCurrentStep(isNeighborSearchingStep
) && step
!= signalHelper_
->lastStep_
)
197 * Stop handler wants to stop after the current step, which was
198 * not known when building the current task queue. This happens
199 * e.g. when a stop is signalled by OS. We therefore want to purge
200 * the task queue now, and re-schedule this step as last step.
203 std::queue
<SimulatorRunFunctionPtr
>().swap(taskQueue_
);
209 resetHandler_
->setSignal(walltime_accounting
);
210 // This is a hack to avoid having to rewrite StopHandler to be a NeighborSearchSignaller
211 // and accept the step as input. Eventually, we want to do that, but currently this would
212 // require introducing NeighborSearchSignaller in the legacy do_md or a lot of code
214 stophandlerIsNSStep_
= isNeighborSearchingStep
;
215 stophandlerCurrentStep_
= step
;
216 stopHandler_
->setSignal();
218 wallcycle_start(wcycle
, ewcSTEP
);
221 void ModularSimulator::postStep(Step step
, Time gmx_unused time
)
226 if (do_per_step(step
, inputrec
->nstlog
))
228 if (fflush(fplog
) != 0)
230 gmx_fatal(FARGS
, "Cannot flush logfile - maybe you are out of disk space?");
234 const bool do_verbose
= mdrunOptions
.verbose
235 && (step
% mdrunOptions
.verboseStepPrintInterval
== 0
236 || step
== inputrec
->init_step
|| step
== signalHelper_
->lastStep_
);
237 // Print the remaining wall clock time for the run
238 if (MASTER(cr
) && (do_verbose
|| gmx_got_usr_signal())
239 && !(pmeLoadBalanceHelper_
&& pmeLoadBalanceHelper_
->pmePrinting()))
241 print_time(stderr
, walltime_accounting
, step
, inputrec
, cr
);
244 double cycles
= wallcycle_stop(wcycle
, ewcSTEP
);
245 if (DOMAINDECOMP(cr
) && wcycle
)
247 dd_cycles_add(cr
->dd
, static_cast<float>(cycles
), ddCyclStep
);
250 resetHandler_
->resetCounters(
251 step
, step
- inputrec
->init_step
, mdlog
, fplog
, cr
, fr
->nbv
.get(), nrnb
, fr
->pmedata
,
252 pmeLoadBalanceHelper_
? pmeLoadBalanceHelper_
->loadBalancingObject() : nullptr, wcycle
,
253 walltime_accounting
);
256 void ModularSimulator::simulatorTeardown()
259 // Stop measuring walltime
260 walltime_accounting_end_time(walltime_accounting
);
262 if (!thisRankHasDuty(cr
, DUTY_PME
))
264 /* Tell the PME only node to finish */
265 gmx_pme_send_finish(cr
);
268 walltime_accounting_set_nsteps_done(walltime_accounting
, step_
- inputrec
->init_step
);
271 void ModularSimulator::populateTaskQueue()
273 auto registerRunFunction
= std::make_unique
<RegisterRunFunction
>(
274 [this](SimulatorRunFunctionPtr ptr
) { taskQueue_
.push(std::move(ptr
)); });
276 Time startTime
= inputrec
->init_t
;
277 Time timeStep
= inputrec
->delta_t
;
278 Time time
= startTime
+ step_
* timeStep
;
280 // Run an initial call to the signallers
281 for (auto& signaller
: signallerCallList_
)
283 signaller
->signal(step_
, time
);
286 if (checkpointHelper_
)
288 checkpointHelper_
->run(step_
, time
);
291 if (pmeLoadBalanceHelper_
)
293 pmeLoadBalanceHelper_
->run(step_
, time
);
297 domDecHelper_
->run(step_
, time
);
302 // local variables for lambda capturing
303 const int step
= step_
;
304 const bool isNSStep
= step
== signalHelper_
->nextNSStep_
;
307 (*registerRunFunction
)(std::make_unique
<SimulatorRunFunction
>(
308 [this, step
, time
, isNSStep
]() { preStep(step
, time
, isNSStep
); }));
309 // register elements for step
310 for (auto& element
: elementCallList_
)
312 element
->scheduleTask(step_
, time
, registerRunFunction
);
314 // register post-step
315 (*registerRunFunction
)(
316 std::make_unique
<SimulatorRunFunction
>([this, step
, time
]() { postStep(step
, time
); }));
320 time
= startTime
+ step_
* timeStep
;
321 for (auto& signaller
: signallerCallList_
)
323 signaller
->signal(step_
, time
);
325 } while (step_
!= signalHelper_
->nextNSStep_
&& step_
<= signalHelper_
->lastStep_
);
328 void ModularSimulator::constructElementsAndSignallers()
330 /* When restarting from a checkpoint, it can be appropriate to
331 * initialize ekind from quantities in the checkpoint. Otherwise,
332 * compute_globals must initialize ekind before the simulation
333 * starts/restarts. However, only the master rank knows what was
334 * found in the checkpoint file, so we have to communicate in
335 * order to coordinate the restart.
337 * TODO (modular) This should become obsolete when checkpoint reading
338 * happens within the modular simulator framework: The energy
339 * element should read its data from the checkpoint file pointer,
340 * and signal to the compute globals element if it needs anything
343 * TODO (legacy) Consider removing this communication if/when checkpoint
344 * reading directly follows .tpr reading, because all ranks can
345 * agree on hasReadEkinState at that time.
347 bool hasReadEkinState
= MASTER(cr
) ? state_global
->ekinstate
.hasReadEkinState
: false;
350 gmx_bcast(sizeof(hasReadEkinState
), &hasReadEkinState
, cr
);
352 if (hasReadEkinState
)
354 restore_ekinstate_from_state(cr
, ekind
, &state_global
->ekinstate
);
358 * Build data structures
361 std::make_unique
<TopologyHolder
>(*top_global
, cr
, inputrec
, fr
, mdAtoms
, constr
, vsite
);
363 std::unique_ptr
<FreeEnergyPerturbationElement
> freeEnergyPerturbationElement
= nullptr;
364 FreeEnergyPerturbationElement
* freeEnergyPerturbationElementPtr
= nullptr;
365 if (inputrec
->efep
!= efepNO
)
367 freeEnergyPerturbationElement
=
368 std::make_unique
<FreeEnergyPerturbationElement
>(fplog
, inputrec
, mdAtoms
);
369 freeEnergyPerturbationElementPtr
= freeEnergyPerturbationElement
.get();
372 auto statePropagatorData
= std::make_unique
<StatePropagatorData
>(
373 top_global
->natoms
, fplog
, cr
, state_global
, inputrec
->nstxout
, inputrec
->nstvout
,
374 inputrec
->nstfout
, inputrec
->nstxout_compressed
, fr
->nbv
->useGpu(),
375 freeEnergyPerturbationElementPtr
, topologyHolder_
.get(), fr
->bMolPBC
,
376 mdrunOptions
.writeConfout
, opt2fn("-c", nfile
, fnm
), inputrec
, mdAtoms
->mdatoms());
377 auto statePropagatorDataPtr
= compat::make_not_null(statePropagatorData
.get());
379 auto energyElement
= std::make_unique
<EnergyElement
>(
380 statePropagatorDataPtr
, freeEnergyPerturbationElementPtr
, top_global
, inputrec
, mdAtoms
,
381 enerd
, ekind
, constr
, fplog
, fcd
, mdModulesNotifier
, MASTER(cr
), observablesHistory
,
383 auto energyElementPtr
= compat::make_not_null(energyElement
.get());
388 const bool simulationsShareState
= false;
389 stopHandler_
= stopHandlerBuilder
->getStopHandlerMD(
390 compat::not_null
<SimulationSignal
*>(&signals_
[eglsSTOPCOND
]), simulationsShareState
,
391 MASTER(cr
), inputrec
->nstlist
, mdrunOptions
.reproducible
, nstglobalcomm_
,
392 mdrunOptions
.maximumHoursToRun
, inputrec
->nstlist
== 0, fplog
, stophandlerCurrentStep_
,
393 stophandlerIsNSStep_
, walltime_accounting
);
396 * Create simulator builders
398 SignallerBuilder
<NeighborSearchSignaller
> neighborSearchSignallerBuilder
;
399 SignallerBuilder
<LastStepSignaller
> lastStepSignallerBuilder
;
400 SignallerBuilder
<LoggingSignaller
> loggingSignallerBuilder
;
401 SignallerBuilder
<EnergySignaller
> energySignallerBuilder
;
402 TrajectoryElementBuilder trajectoryElementBuilder
;
405 * Register data structures to signallers
407 trajectoryElementBuilder
.registerWriterClient(statePropagatorDataPtr
);
408 trajectoryElementBuilder
.registerSignallerClient(statePropagatorDataPtr
);
409 lastStepSignallerBuilder
.registerSignallerClient(statePropagatorDataPtr
);
411 trajectoryElementBuilder
.registerWriterClient(energyElementPtr
);
412 trajectoryElementBuilder
.registerSignallerClient(energyElementPtr
);
413 energySignallerBuilder
.registerSignallerClient(energyElementPtr
);
415 // Register the simulator itself to the neighbor search / last step signaller
416 neighborSearchSignallerBuilder
.registerSignallerClient(compat::make_not_null(signalHelper_
.get()));
417 lastStepSignallerBuilder
.registerSignallerClient(compat::make_not_null(signalHelper_
.get()));
420 * Build integrator - this takes care of force calculation, propagation,
421 * constraining, and of the place the statePropagatorData and the energy element
422 * have a full timestep state.
424 // TODO: Make a CheckpointHelperBuilder
425 std::vector
<ICheckpointHelperClient
*> checkpointClients
= { statePropagatorDataPtr
, energyElementPtr
,
426 freeEnergyPerturbationElementPtr
};
427 CheckBondedInteractionsCallbackPtr checkBondedInteractionsCallback
= nullptr;
429 buildIntegrator(&neighborSearchSignallerBuilder
, &energySignallerBuilder
,
430 &loggingSignallerBuilder
, &trajectoryElementBuilder
, &checkpointClients
,
431 &checkBondedInteractionsCallback
, statePropagatorDataPtr
,
432 energyElementPtr
, freeEnergyPerturbationElementPtr
, hasReadEkinState
);
435 * Build infrastructure elements
438 if (PmeLoadBalanceHelper::doPmeLoadBalancing(mdrunOptions
, inputrec
, fr
))
440 pmeLoadBalanceHelper_
= std::make_unique
<PmeLoadBalanceHelper
>(
441 mdrunOptions
.verbose
, statePropagatorDataPtr
, fplog
, cr
, mdlog
, inputrec
, wcycle
, fr
);
442 neighborSearchSignallerBuilder
.registerSignallerClient(
443 compat::make_not_null(pmeLoadBalanceHelper_
.get()));
446 if (DOMAINDECOMP(cr
))
448 GMX_ASSERT(checkBondedInteractionsCallback
,
449 "Domain decomposition needs a callback for check the number of bonded "
451 domDecHelper_
= std::make_unique
<DomDecHelper
>(
452 mdrunOptions
.verbose
, mdrunOptions
.verboseStepPrintInterval
, statePropagatorDataPtr
,
453 freeEnergyPerturbationElementPtr
, topologyHolder_
.get(),
454 std::move(checkBondedInteractionsCallback
), nstglobalcomm_
, fplog
, cr
, mdlog
,
455 constr
, inputrec
, mdAtoms
, nrnb
, wcycle
, fr
, vsite
, imdSession
, pull_work
);
456 neighborSearchSignallerBuilder
.registerSignallerClient(compat::make_not_null(domDecHelper_
.get()));
459 const bool simulationsShareResetCounters
= false;
460 resetHandler_
= std::make_unique
<ResetHandler
>(
461 compat::make_not_null
<SimulationSignal
*>(&signals_
[eglsRESETCOUNTERS
]),
462 simulationsShareResetCounters
, inputrec
->nsteps
, MASTER(cr
),
463 mdrunOptions
.timingOptions
.resetHalfway
, mdrunOptions
.maximumHoursToRun
, mdlog
, wcycle
,
464 walltime_accounting
);
467 * Build signaller list
469 * Note that as signallers depend on each others, the order of calling the signallers
470 * matters. It is the responsibility of this builder to ensure that the order is
473 auto energySignaller
= energySignallerBuilder
.build(
474 inputrec
->nstcalcenergy
, inputrec
->fepvals
->nstdhdl
, inputrec
->nstpcouple
);
475 trajectoryElementBuilder
.registerSignallerClient(compat::make_not_null(energySignaller
.get()));
476 loggingSignallerBuilder
.registerSignallerClient(compat::make_not_null(energySignaller
.get()));
477 auto trajectoryElement
= trajectoryElementBuilder
.build(
478 fplog
, nfile
, fnm
, mdrunOptions
, cr
, outputProvider
, mdModulesNotifier
, inputrec
,
479 top_global
, oenv
, wcycle
, startingBehavior
, simulationsShareState
);
480 loggingSignallerBuilder
.registerSignallerClient(compat::make_not_null(trajectoryElement
.get()));
482 // Add checkpoint helper here since we need a pointer to the trajectory element and
483 // need to register it with the lastStepSignallerBuilder
484 auto checkpointHandler
= std::make_unique
<CheckpointHandler
>(
485 compat::make_not_null
<SimulationSignal
*>(&signals_
[eglsCHKPT
]), simulationsShareState
,
486 inputrec
->nstlist
== 0, MASTER(cr
), mdrunOptions
.writeConfout
,
487 mdrunOptions
.checkpointOptions
.period
);
488 checkpointHelper_
= std::make_unique
<CheckpointHelper
>(
489 std::move(checkpointClients
), std::move(checkpointHandler
), inputrec
->init_step
,
490 trajectoryElement
.get(), top_global
->natoms
, fplog
, cr
, observablesHistory
,
491 walltime_accounting
, state_global
, mdrunOptions
.writeConfout
);
492 lastStepSignallerBuilder
.registerSignallerClient(compat::make_not_null(checkpointHelper_
.get()));
494 lastStepSignallerBuilder
.registerSignallerClient(compat::make_not_null(trajectoryElement
.get()));
495 auto loggingSignaller
=
496 loggingSignallerBuilder
.build(inputrec
->nstlog
, inputrec
->init_step
, inputrec
->init_t
);
497 lastStepSignallerBuilder
.registerSignallerClient(compat::make_not_null(loggingSignaller
.get()));
498 auto lastStepSignaller
=
499 lastStepSignallerBuilder
.build(inputrec
->nsteps
, inputrec
->init_step
, stopHandler_
.get());
500 neighborSearchSignallerBuilder
.registerSignallerClient(compat::make_not_null(lastStepSignaller
.get()));
501 auto neighborSearchSignaller
= neighborSearchSignallerBuilder
.build(
502 inputrec
->nstlist
, inputrec
->init_step
, inputrec
->init_t
);
504 addToCallListAndMove(std::move(neighborSearchSignaller
), signallerCallList_
, signallersOwnershipList_
);
505 addToCallListAndMove(std::move(lastStepSignaller
), signallerCallList_
, signallersOwnershipList_
);
506 addToCallListAndMove(std::move(loggingSignaller
), signallerCallList_
, signallersOwnershipList_
);
507 addToCallList(trajectoryElement
, signallerCallList_
);
508 addToCallListAndMove(std::move(energySignaller
), signallerCallList_
, signallersOwnershipList_
);
511 * Build the element list
513 * This is the actual sequence of (non-infrastructure) elements to be run.
514 * For NVE, the trajectory element is used outside of the integrator
515 * (composite) element, as well as the checkpoint helper. The checkpoint
516 * helper should be on top of the loop, and is only part of the simulator
517 * call list to be able to react to the last step being signalled.
519 addToCallList(checkpointHelper_
, elementCallList_
);
520 if (freeEnergyPerturbationElement
)
522 addToCallListAndMove(std::move(freeEnergyPerturbationElement
), elementCallList_
,
523 elementsOwnershipList_
);
525 addToCallListAndMove(std::move(integrator
), elementCallList_
, elementsOwnershipList_
);
526 addToCallListAndMove(std::move(trajectoryElement
), elementCallList_
, elementsOwnershipList_
);
527 // for vv, we need to setup statePropagatorData after the compute
528 // globals so that we reset the right velocities
529 // TODO: Avoid this by getting rid of the need of resetting velocities in vv
530 elementsOwnershipList_
.emplace_back(std::move(statePropagatorData
));
531 elementsOwnershipList_
.emplace_back(std::move(energyElement
));
534 std::unique_ptr
<ISimulatorElement
>
535 ModularSimulator::buildForces(SignallerBuilder
<NeighborSearchSignaller
>* neighborSearchSignallerBuilder
,
536 SignallerBuilder
<EnergySignaller
>* energySignallerBuilder
,
537 StatePropagatorData
* statePropagatorDataPtr
,
538 EnergyElement
* energyElementPtr
,
539 FreeEnergyPerturbationElement
* freeEnergyPerturbationElement
)
541 const bool isVerbose
= mdrunOptions
.verbose
;
542 const bool isDynamicBox
= inputrecDynamicBox(inputrec
);
543 // Check for polarizable models and flexible constraints
544 if (ShellFCElement::doShellsOrFlexConstraints(topologyHolder_
->globalTopology(),
545 constr
? constr
->numFlexibleConstraints() : 0))
547 auto shellFCElement
= std::make_unique
<ShellFCElement
>(
548 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElement
, isVerbose
,
549 isDynamicBox
, fplog
, cr
, inputrec
, mdAtoms
, nrnb
, fr
, fcd
, wcycle
, runScheduleWork
, vsite
,
550 imdSession
, pull_work
, constr
, &topologyHolder_
->globalTopology(), enforcedRotation
);
551 topologyHolder_
->registerClient(shellFCElement
.get());
552 neighborSearchSignallerBuilder
->registerSignallerClient(
553 compat::make_not_null(shellFCElement
.get()));
554 energySignallerBuilder
->registerSignallerClient(compat::make_not_null(shellFCElement
.get()));
556 // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
557 return std::move(shellFCElement
);
561 auto forceElement
= std::make_unique
<ForceElement
>(
562 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElement
,
563 isDynamicBox
, fplog
, cr
, inputrec
, mdAtoms
, nrnb
, fr
, fcd
, wcycle
, runScheduleWork
,
564 vsite
, imdSession
, pull_work
, enforcedRotation
);
565 topologyHolder_
->registerClient(forceElement
.get());
566 neighborSearchSignallerBuilder
->registerSignallerClient(compat::make_not_null(forceElement
.get()));
567 energySignallerBuilder
->registerSignallerClient(compat::make_not_null(forceElement
.get()));
569 // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
570 return std::move(forceElement
);
574 std::unique_ptr
<ISimulatorElement
> ModularSimulator::buildIntegrator(
575 SignallerBuilder
<NeighborSearchSignaller
>* neighborSearchSignallerBuilder
,
576 SignallerBuilder
<EnergySignaller
>* energySignallerBuilder
,
577 SignallerBuilder
<LoggingSignaller
>* loggingSignallerBuilder
,
578 TrajectoryElementBuilder
* trajectoryElementBuilder
,
579 std::vector
<ICheckpointHelperClient
*>* checkpointClients
,
580 CheckBondedInteractionsCallbackPtr
* checkBondedInteractionsCallback
,
581 compat::not_null
<StatePropagatorData
*> statePropagatorDataPtr
,
582 compat::not_null
<EnergyElement
*> energyElementPtr
,
583 FreeEnergyPerturbationElement
* freeEnergyPerturbationElementPtr
,
584 bool hasReadEkinState
)
587 buildForces(neighborSearchSignallerBuilder
, energySignallerBuilder
,
588 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
);
590 // list of elements owned by the simulator composite object
591 std::vector
<std::unique_ptr
<ISimulatorElement
>> elementsOwnershipList
;
592 // call list of the simulator composite object
593 std::vector
<compat::not_null
<ISimulatorElement
*>> elementCallList
;
595 std::function
<void()> needToCheckNumberOfBondedInteractions
;
596 if (inputrec
->eI
== eiMD
)
598 auto computeGlobalsElement
=
599 std::make_unique
<ComputeGlobalsElement
<ComputeGlobalsAlgorithm::LeapFrog
>>(
600 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
601 &signals_
, nstglobalcomm_
, fplog
, mdlog
, cr
, inputrec
, mdAtoms
, nrnb
,
602 wcycle
, fr
, &topologyHolder_
->globalTopology(), constr
, hasReadEkinState
);
603 topologyHolder_
->registerClient(computeGlobalsElement
.get());
604 energySignallerBuilder
->registerSignallerClient(compat::make_not_null(computeGlobalsElement
.get()));
605 trajectoryElementBuilder
->registerSignallerClient(
606 compat::make_not_null(computeGlobalsElement
.get()));
608 *checkBondedInteractionsCallback
=
609 computeGlobalsElement
->getCheckNumberOfBondedInteractionsCallback();
611 auto propagator
= std::make_unique
<Propagator
<IntegrationStep::LeapFrog
>>(
612 inputrec
->delta_t
, statePropagatorDataPtr
, mdAtoms
, wcycle
);
614 addToCallListAndMove(std::move(forceElement
), elementCallList
, elementsOwnershipList
);
615 addToCallList(statePropagatorDataPtr
, elementCallList
); // we have a full microstate at time t here!
616 if (inputrec
->etc
== etcVRESCALE
)
618 // TODO: With increased complexity of the propagator, this will need further development,
619 // e.g. using propagators templated for velocity propagation policies and a builder
620 propagator
->setNumVelocityScalingVariables(inputrec
->opts
.ngtc
);
621 auto thermostat
= std::make_unique
<VRescaleThermostat
>(
622 inputrec
->nsttcouple
, -1, false, inputrec
->ld_seed
, inputrec
->opts
.ngtc
,
623 inputrec
->delta_t
* inputrec
->nsttcouple
, inputrec
->opts
.ref_t
, inputrec
->opts
.tau_t
,
624 inputrec
->opts
.nrdf
, energyElementPtr
, propagator
->viewOnVelocityScaling(),
625 propagator
->velocityScalingCallback(), state_global
, cr
, inputrec
->bContinuation
);
626 checkpointClients
->emplace_back(thermostat
.get());
627 energyElementPtr
->setVRescaleThermostat(thermostat
.get());
628 addToCallListAndMove(std::move(thermostat
), elementCallList
, elementsOwnershipList
);
631 std::unique_ptr
<ParrinelloRahmanBarostat
> prBarostat
= nullptr;
632 if (inputrec
->epc
== epcPARRINELLORAHMAN
)
634 // Building the PR barostat here since it needs access to the propagator
635 // and we want to be able to move the propagator object
636 prBarostat
= std::make_unique
<ParrinelloRahmanBarostat
>(
637 inputrec
->nstpcouple
, -1, inputrec
->delta_t
* inputrec
->nstpcouple
,
638 inputrec
->init_step
, propagator
->viewOnPRScalingMatrix(),
639 propagator
->prScalingCallback(), statePropagatorDataPtr
, energyElementPtr
,
640 fplog
, inputrec
, mdAtoms
, state_global
, cr
, inputrec
->bContinuation
);
641 energyElementPtr
->setParrinelloRahamnBarostat(prBarostat
.get());
642 checkpointClients
->emplace_back(prBarostat
.get());
644 addToCallListAndMove(std::move(propagator
), elementCallList
, elementsOwnershipList
);
647 auto constraintElement
= std::make_unique
<ConstraintsElement
<ConstraintVariable::Positions
>>(
648 constr
, statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
649 MASTER(cr
), fplog
, inputrec
, mdAtoms
->mdatoms());
650 auto constraintElementPtr
= compat::make_not_null(constraintElement
.get());
651 energySignallerBuilder
->registerSignallerClient(constraintElementPtr
);
652 trajectoryElementBuilder
->registerSignallerClient(constraintElementPtr
);
653 loggingSignallerBuilder
->registerSignallerClient(constraintElementPtr
);
655 addToCallListAndMove(std::move(constraintElement
), elementCallList
, elementsOwnershipList
);
658 addToCallListAndMove(std::move(computeGlobalsElement
), elementCallList
, elementsOwnershipList
);
659 addToCallList(energyElementPtr
, elementCallList
); // we have the energies at time t here!
662 addToCallListAndMove(std::move(prBarostat
), elementCallList
, elementsOwnershipList
);
665 else if (inputrec
->eI
== eiVV
)
667 auto computeGlobalsElementAtFullTimeStep
=
668 std::make_unique
<ComputeGlobalsElement
<ComputeGlobalsAlgorithm::VelocityVerletAtFullTimeStep
>>(
669 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
670 &signals_
, nstglobalcomm_
, fplog
, mdlog
, cr
, inputrec
, mdAtoms
, nrnb
,
671 wcycle
, fr
, &topologyHolder_
->globalTopology(), constr
, hasReadEkinState
);
672 topologyHolder_
->registerClient(computeGlobalsElementAtFullTimeStep
.get());
673 energySignallerBuilder
->registerSignallerClient(
674 compat::make_not_null(computeGlobalsElementAtFullTimeStep
.get()));
675 trajectoryElementBuilder
->registerSignallerClient(
676 compat::make_not_null(computeGlobalsElementAtFullTimeStep
.get()));
678 auto computeGlobalsElementAfterCoordinateUpdate
=
679 std::make_unique
<ComputeGlobalsElement
<ComputeGlobalsAlgorithm::VelocityVerletAfterCoordinateUpdate
>>(
680 statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
681 &signals_
, nstglobalcomm_
, fplog
, mdlog
, cr
, inputrec
, mdAtoms
, nrnb
,
682 wcycle
, fr
, &topologyHolder_
->globalTopology(), constr
, hasReadEkinState
);
683 topologyHolder_
->registerClient(computeGlobalsElementAfterCoordinateUpdate
.get());
684 energySignallerBuilder
->registerSignallerClient(
685 compat::make_not_null(computeGlobalsElementAfterCoordinateUpdate
.get()));
686 trajectoryElementBuilder
->registerSignallerClient(
687 compat::make_not_null(computeGlobalsElementAfterCoordinateUpdate
.get()));
689 *checkBondedInteractionsCallback
=
690 computeGlobalsElementAfterCoordinateUpdate
->getCheckNumberOfBondedInteractionsCallback();
692 auto propagatorVelocities
= std::make_unique
<Propagator
<IntegrationStep::VelocitiesOnly
>>(
693 inputrec
->delta_t
* 0.5, statePropagatorDataPtr
, mdAtoms
, wcycle
);
694 auto propagatorVelocitiesAndPositions
=
695 std::make_unique
<Propagator
<IntegrationStep::VelocityVerletPositionsAndVelocities
>>(
696 inputrec
->delta_t
, statePropagatorDataPtr
, mdAtoms
, wcycle
);
698 addToCallListAndMove(std::move(forceElement
), elementCallList
, elementsOwnershipList
);
700 std::unique_ptr
<ParrinelloRahmanBarostat
> prBarostat
= nullptr;
701 if (inputrec
->epc
== epcPARRINELLORAHMAN
)
703 // Building the PR barostat here since it needs access to the propagator
704 // and we want to be able to move the propagator object
705 prBarostat
= std::make_unique
<ParrinelloRahmanBarostat
>(
706 inputrec
->nstpcouple
, -1, inputrec
->delta_t
* inputrec
->nstpcouple
,
707 inputrec
->init_step
, propagatorVelocities
->viewOnPRScalingMatrix(),
708 propagatorVelocities
->prScalingCallback(), statePropagatorDataPtr
, energyElementPtr
,
709 fplog
, inputrec
, mdAtoms
, state_global
, cr
, inputrec
->bContinuation
);
710 energyElementPtr
->setParrinelloRahamnBarostat(prBarostat
.get());
711 checkpointClients
->emplace_back(prBarostat
.get());
713 addToCallListAndMove(std::move(propagatorVelocities
), elementCallList
, elementsOwnershipList
);
716 auto constraintElement
= std::make_unique
<ConstraintsElement
<ConstraintVariable::Velocities
>>(
717 constr
, statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
718 MASTER(cr
), fplog
, inputrec
, mdAtoms
->mdatoms());
719 energySignallerBuilder
->registerSignallerClient(compat::make_not_null(constraintElement
.get()));
720 trajectoryElementBuilder
->registerSignallerClient(
721 compat::make_not_null(constraintElement
.get()));
722 loggingSignallerBuilder
->registerSignallerClient(
723 compat::make_not_null(constraintElement
.get()));
725 addToCallListAndMove(std::move(constraintElement
), elementCallList
, elementsOwnershipList
);
727 addToCallListAndMove(std::move(computeGlobalsElementAtFullTimeStep
), elementCallList
,
728 elementsOwnershipList
);
729 addToCallList(statePropagatorDataPtr
, elementCallList
); // we have a full microstate at time t here!
730 if (inputrec
->etc
== etcVRESCALE
)
732 // TODO: With increased complexity of the propagator, this will need further development,
733 // e.g. using propagators templated for velocity propagation policies and a builder
734 propagatorVelocitiesAndPositions
->setNumVelocityScalingVariables(inputrec
->opts
.ngtc
);
735 auto thermostat
= std::make_unique
<VRescaleThermostat
>(
736 inputrec
->nsttcouple
, 0, true, inputrec
->ld_seed
, inputrec
->opts
.ngtc
,
737 inputrec
->delta_t
* inputrec
->nsttcouple
, inputrec
->opts
.ref_t
,
738 inputrec
->opts
.tau_t
, inputrec
->opts
.nrdf
, energyElementPtr
,
739 propagatorVelocitiesAndPositions
->viewOnVelocityScaling(),
740 propagatorVelocitiesAndPositions
->velocityScalingCallback(), state_global
, cr
,
741 inputrec
->bContinuation
);
742 checkpointClients
->emplace_back(thermostat
.get());
743 energyElementPtr
->setVRescaleThermostat(thermostat
.get());
744 addToCallListAndMove(std::move(thermostat
), elementCallList
, elementsOwnershipList
);
746 addToCallListAndMove(std::move(propagatorVelocitiesAndPositions
), elementCallList
,
747 elementsOwnershipList
);
750 auto constraintElement
= std::make_unique
<ConstraintsElement
<ConstraintVariable::Positions
>>(
751 constr
, statePropagatorDataPtr
, energyElementPtr
, freeEnergyPerturbationElementPtr
,
752 MASTER(cr
), fplog
, inputrec
, mdAtoms
->mdatoms());
753 energySignallerBuilder
->registerSignallerClient(compat::make_not_null(constraintElement
.get()));
754 trajectoryElementBuilder
->registerSignallerClient(
755 compat::make_not_null(constraintElement
.get()));
756 loggingSignallerBuilder
->registerSignallerClient(
757 compat::make_not_null(constraintElement
.get()));
759 addToCallListAndMove(std::move(constraintElement
), elementCallList
, elementsOwnershipList
);
761 addToCallListAndMove(std::move(computeGlobalsElementAfterCoordinateUpdate
), elementCallList
,
762 elementsOwnershipList
);
763 addToCallList(energyElementPtr
, elementCallList
); // we have the energies at time t here!
766 addToCallListAndMove(std::move(prBarostat
), elementCallList
, elementsOwnershipList
);
771 gmx_fatal(FARGS
, "Integrator not implemented for the modular simulator.");
774 auto integrator
= std::make_unique
<CompositeSimulatorElement
>(std::move(elementCallList
),
775 std::move(elementsOwnershipList
));
776 // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
777 return std::move(integrator
);
780 bool ModularSimulator::isInputCompatible(bool exitOnFailure
,
781 const t_inputrec
* inputrec
,
783 const gmx_mtop_t
& globalTopology
,
784 const gmx_multisim_t
* ms
,
785 const ReplicaExchangeParameters
& replExParams
,
787 bool doEssentialDynamics
,
790 auto conditionalAssert
= [exitOnFailure
](bool condition
, const char* message
) {
793 GMX_RELEASE_ASSERT(condition
, message
);
798 bool isInputCompatible
= true;
800 // GMX_USE_MODULAR_SIMULATOR allows to use modular simulator also for non-standard uses,
801 // such as the leap-frog integrator
802 const auto modularSimulatorExplicitlyTurnedOn
= (getenv("GMX_USE_MODULAR_SIMULATOR") != nullptr);
803 // GMX_USE_MODULAR_SIMULATOR allows to use disable modular simulator for all uses,
804 // including the velocity-verlet integrator used by default
805 const auto modularSimulatorExplicitlyTurnedOff
= (getenv("GMX_DISABLE_MODULAR_SIMULATOR") != nullptr);
808 !(modularSimulatorExplicitlyTurnedOn
&& modularSimulatorExplicitlyTurnedOff
),
809 "Cannot have both GMX_USE_MODULAR_SIMULATOR=ON and GMX_DISABLE_MODULAR_SIMULATOR=ON. "
810 "Unset one of the two environment variables to explicitly chose which simulator to "
812 "or unset both to recover default behavior.");
815 !(modularSimulatorExplicitlyTurnedOff
&& inputrec
->eI
== eiVV
816 && inputrec
->epc
== epcPARRINELLORAHMAN
),
817 "Cannot use a Parrinello-Rahman barostat with md-vv and "
818 "GMX_DISABLE_MODULAR_SIMULATOR=ON, "
819 "as the Parrinello-Rahman barostat is not implemented in the legacy simulator. Unset "
820 "GMX_DISABLE_MODULAR_SIMULATOR or use a different pressure control algorithm.");
824 && conditionalAssert(
825 inputrec
->eI
== eiMD
|| inputrec
->eI
== eiVV
,
826 "Only integrators md and md-vv are supported by the modular simulator.");
827 isInputCompatible
= isInputCompatible
828 && conditionalAssert(inputrec
->eI
!= eiMD
|| modularSimulatorExplicitlyTurnedOn
,
829 "Set GMX_USE_MODULAR_SIMULATOR=ON to use the modular "
830 "simulator with integrator md.");
833 && conditionalAssert(!doRerun
, "Rerun is not supported by the modular simulator.");
836 && conditionalAssert(
837 inputrec
->etc
== etcNO
|| inputrec
->etc
== etcVRESCALE
,
838 "Only v-rescale thermostat is supported by the modular simulator.");
841 && conditionalAssert(
842 inputrec
->epc
== epcNO
|| inputrec
->epc
== epcPARRINELLORAHMAN
,
843 "Only Parrinello-Rahman barostat is supported by the modular simulator.");
846 && conditionalAssert(
847 !(inputrecNptTrotter(inputrec
) || inputrecNphTrotter(inputrec
)
848 || inputrecNvtTrotter(inputrec
)),
849 "Legacy Trotter decomposition is not supported by the modular simulator.");
850 isInputCompatible
= isInputCompatible
851 && conditionalAssert(inputrec
->efep
== efepNO
|| inputrec
->efep
== efepYES
852 || inputrec
->efep
== efepSLOWGROWTH
,
853 "Expanded ensemble free energy calculation is not "
854 "supported by the modular simulator.");
855 isInputCompatible
= isInputCompatible
856 && conditionalAssert(!inputrec
->bPull
,
857 "Pulling is not supported by the modular simulator.");
860 && conditionalAssert(inputrec
->opts
.ngacc
== 1 && inputrec
->opts
.acc
[0][XX
] == 0.0
861 && inputrec
->opts
.acc
[0][YY
] == 0.0
862 && inputrec
->opts
.acc
[0][ZZ
] == 0.0 && inputrec
->cos_accel
== 0.0,
863 "Acceleration is not supported by the modular simulator.");
866 && conditionalAssert(inputrec
->opts
.ngfrz
== 1 && inputrec
->opts
.nFreeze
[0][XX
] == 0
867 && inputrec
->opts
.nFreeze
[0][YY
] == 0
868 && inputrec
->opts
.nFreeze
[0][ZZ
] == 0,
869 "Freeze groups are not supported by the modular simulator.");
872 && conditionalAssert(
873 inputrec
->deform
[XX
][XX
] == 0.0 && inputrec
->deform
[XX
][YY
] == 0.0
874 && inputrec
->deform
[XX
][ZZ
] == 0.0 && inputrec
->deform
[YY
][XX
] == 0.0
875 && inputrec
->deform
[YY
][YY
] == 0.0 && inputrec
->deform
[YY
][ZZ
] == 0.0
876 && inputrec
->deform
[ZZ
][XX
] == 0.0 && inputrec
->deform
[ZZ
][YY
] == 0.0
877 && inputrec
->deform
[ZZ
][ZZ
] == 0.0,
878 "Deformation is not supported by the modular simulator.");
881 && conditionalAssert(gmx_mtop_interaction_count(globalTopology
, IF_VSITE
) == 0,
882 "Virtual sites are not supported by the modular simulator.");
883 isInputCompatible
= isInputCompatible
884 && conditionalAssert(!inputrec
->bDoAwh
,
885 "AWH is not supported by the modular simulator.");
888 && conditionalAssert(gmx_mtop_ftype_count(globalTopology
, F_DISRES
) == 0,
889 "Distance restraints are not supported by the modular simulator.");
892 && conditionalAssert(
893 gmx_mtop_ftype_count(globalTopology
, F_ORIRES
) == 0,
894 "Orientation restraints are not supported by the modular simulator.");
897 && conditionalAssert(ms
== nullptr,
898 "Multi-sim are not supported by the modular simulator.");
901 && conditionalAssert(replExParams
.exchangeInterval
== 0,
902 "Replica exchange is not supported by the modular simulator.");
904 int numEnsembleRestraintSystems
;
907 numEnsembleRestraintSystems
= fcd
->disres
.nsystems
;
911 auto distantRestraintEnsembleEnvVar
= getenv("GMX_DISRE_ENSEMBLE_SIZE");
912 numEnsembleRestraintSystems
=
913 (ms
!= nullptr && distantRestraintEnsembleEnvVar
!= nullptr)
914 ? static_cast<int>(strtol(distantRestraintEnsembleEnvVar
, nullptr, 10))
919 && conditionalAssert(numEnsembleRestraintSystems
<= 1,
920 "Ensemble restraints are not supported by the modular simulator.");
923 && conditionalAssert(!doSimulatedAnnealing(inputrec
),
924 "Simulated annealing is not supported by the modular simulator.");
927 && conditionalAssert(!inputrec
->bSimTemp
,
928 "Simulated tempering is not supported by the modular simulator.");
929 isInputCompatible
= isInputCompatible
930 && conditionalAssert(!inputrec
->bExpanded
,
931 "Expanded ensemble simulations are not supported by "
932 "the modular simulator.");
935 && conditionalAssert(!doEssentialDynamics
,
936 "Essential dynamics is not supported by the modular simulator.");
937 isInputCompatible
= isInputCompatible
938 && conditionalAssert(inputrec
->eSwapCoords
== eswapNO
,
939 "Ion / water position swapping is not supported by "
940 "the modular simulator.");
943 && conditionalAssert(!inputrec
->bIMD
,
944 "Interactive MD is not supported by the modular simulator.");
947 && conditionalAssert(!doMembed
,
948 "Membrane embedding is not supported by the modular simulator.");
949 const bool useGraph
= !areMoleculesDistributedOverPbc(*inputrec
, globalTopology
, MDLogger());
952 && conditionalAssert(!useGraph
, "Graph is not supported by the modular simulator.");
953 // TODO: Change this to the boolean passed when we merge the user interface change for the GPU update.
956 && conditionalAssert(
957 getenv("GMX_FORCE_UPDATE_DEFAULT_GPU") == nullptr,
958 "Integration on the GPU is not supported by the modular simulator.");
959 // Modular simulator is centered around NS updates
960 // TODO: think how to handle nstlist == 0
961 isInputCompatible
= isInputCompatible
962 && conditionalAssert(inputrec
->nstlist
!= 0,
963 "Simulations without neighbor list update are not "
964 "supported by the modular simulator.");
965 isInputCompatible
= isInputCompatible
966 && conditionalAssert(!GMX_FAHCORE
,
967 "GMX_FAHCORE not supported by the modular simulator.");
969 return isInputCompatible
;
972 void ModularSimulator::checkInputForDisabledFunctionality()
974 isInputCompatible(true, inputrec
, doRerun
, *top_global
, ms
, replExParams
, fcd
,
975 opt2bSet("-ei", nfile
, fnm
), membed
!= nullptr);
976 if (observablesHistory
->edsamHistory
)
979 "The checkpoint is from a run with essential dynamics sampling, "
980 "but the current run did not specify the -ei option. "
981 "Either specify the -ei option to mdrun, or do not use this checkpoint file.");
985 SignallerCallbackPtr
ModularSimulator::SignalHelper::registerLastStepCallback()
987 return std::make_unique
<SignallerCallback
>(
988 [this](Step step
, Time gmx_unused time
) { this->lastStep_
= step
; });
991 SignallerCallbackPtr
ModularSimulator::SignalHelper::registerNSCallback()
993 return std::make_unique
<SignallerCallback
>(
994 [this](Step step
, Time gmx_unused time
) { this->nextNSStep_
= step
; });