Improve modular simulator builders
[alexxy/gromacs.git] / src / gromacs / modularsimulator / modularsimulator.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Defines the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  */
41
42 #include "gmxpre.h"
43
44 #include "modularsimulator.h"
45
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/listed_forces/listed_forces.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/mdatoms.h"
59 #include "gromacs/mdlib/resethandler.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/forcerec.h"
68 #include "gromacs/mdtypes/inputrec.h"
69 #include "gromacs/mdtypes/mdatom.h"
70 #include "gromacs/mdtypes/mdrunoptions.h"
71 #include "gromacs/mdtypes/observableshistory.h"
72 #include "gromacs/nbnxm/nbnxm.h"
73 #include "gromacs/topology/mtop_util.h"
74 #include "gromacs/topology/topology.h"
75 #include "gromacs/utility/fatalerror.h"
76
77 #include "compositesimulatorelement.h"
78 #include "computeglobalselement.h"
79 #include "constraintelement.h"
80 #include "energydata.h"
81 #include "forceelement.h"
82 #include "freeenergyperturbationdata.h"
83 #include "parrinellorahmanbarostat.h"
84 #include "propagator.h"
85 #include "signallers.h"
86 #include "simulatoralgorithm.h"
87 #include "statepropagatordata.h"
88 #include "trajectoryelement.h"
89 #include "vrescalethermostat.h"
90
91 namespace gmx
92 {
93 void ModularSimulator::run()
94 {
95     GMX_LOG(legacySimulatorData_->mdlog.info)
96             .asParagraph()
97             .appendText("Using the modular simulator.");
98
99     ModularSimulatorAlgorithmBuilder algorithmBuilder(compat::make_not_null(legacySimulatorData_.get()));
100     auto                             algorithm = algorithmBuilder.build();
101
102     while (const auto* task = algorithm.getNextTask())
103     {
104         // execute task
105         (*task)();
106     }
107 }
108
109 std::unique_ptr<ISimulatorElement> ModularSimulatorAlgorithmBuilder::buildForces(
110         SignallerBuilder<NeighborSearchSignaller>* neighborSearchSignallerBuilder,
111         SignallerBuilder<EnergySignaller>*         energySignallerBuilder,
112         StatePropagatorData*                       statePropagatorDataPtr,
113         EnergyData*                                energyDataPtr,
114         FreeEnergyPerturbationData*                freeEnergyPerturbationDataPtr,
115         TopologyHolder::Builder*                   topologyHolderBuilder)
116 {
117     const bool isVerbose    = legacySimulatorData_->mdrunOptions.verbose;
118     const bool isDynamicBox = inputrecDynamicBox(legacySimulatorData_->inputrec);
119
120     auto forceElement = std::make_unique<ForceElement>(
121             statePropagatorDataPtr, energyDataPtr, freeEnergyPerturbationDataPtr, isVerbose, isDynamicBox,
122             legacySimulatorData_->fplog, legacySimulatorData_->cr, legacySimulatorData_->inputrec,
123             legacySimulatorData_->mdAtoms, legacySimulatorData_->nrnb, legacySimulatorData_->fr,
124             legacySimulatorData_->wcycle, legacySimulatorData_->runScheduleWork,
125             legacySimulatorData_->vsite, legacySimulatorData_->imdSession,
126             legacySimulatorData_->pull_work, legacySimulatorData_->constr,
127             legacySimulatorData_->top_global, legacySimulatorData_->enforcedRotation);
128     topologyHolderBuilder->registerClient(forceElement.get());
129     neighborSearchSignallerBuilder->registerSignallerClient(compat::make_not_null(forceElement.get()));
130     energySignallerBuilder->registerSignallerClient(compat::make_not_null(forceElement.get()));
131
132     // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
133     return std::move(forceElement);
134 }
135
136 std::unique_ptr<ISimulatorElement> ModularSimulatorAlgorithmBuilder::buildIntegrator(
137         SignallerBuilder<NeighborSearchSignaller>* neighborSearchSignallerBuilder,
138         SignallerBuilder<LastStepSignaller>*       lastStepSignallerBuilder,
139         SignallerBuilder<EnergySignaller>*         energySignallerBuilder,
140         SignallerBuilder<LoggingSignaller>*        loggingSignallerBuilder,
141         SignallerBuilder<TrajectorySignaller>*     trajectorySignallerBuilder,
142         TrajectoryElementBuilder*                  trajectoryElementBuilder,
143         std::vector<ICheckpointHelperClient*>*     checkpointClients,
144         CheckBondedInteractionsCallbackPtr*        checkBondedInteractionsCallback,
145         compat::not_null<StatePropagatorData*>     statePropagatorDataPtr,
146         compat::not_null<EnergyData*>              energyDataPtr,
147         FreeEnergyPerturbationData*                freeEnergyPerturbationDataPtr,
148         bool                                       hasReadEkinState,
149         TopologyHolder::Builder*                   topologyHolderBuilder,
150         SimulationSignals*                         signals)
151 {
152     auto forceElement = buildForces(neighborSearchSignallerBuilder, energySignallerBuilder,
153                                     statePropagatorDataPtr, energyDataPtr,
154                                     freeEnergyPerturbationDataPtr, topologyHolderBuilder);
155
156     // list of elements owned by the simulator composite object
157     std::vector<std::unique_ptr<ISimulatorElement>> elementsOwnershipList;
158     // call list of the simulator composite object
159     std::vector<compat::not_null<ISimulatorElement*>> elementCallList;
160
161     std::function<void()> needToCheckNumberOfBondedInteractions;
162     if (legacySimulatorData_->inputrec->eI == eiMD)
163     {
164         auto computeGlobalsElement = std::make_unique<ComputeGlobalsElement<ComputeGlobalsAlgorithm::LeapFrog>>(
165                 statePropagatorDataPtr, energyDataPtr, freeEnergyPerturbationDataPtr, signals,
166                 nstglobalcomm_, legacySimulatorData_->fplog, legacySimulatorData_->mdlog,
167                 legacySimulatorData_->cr, legacySimulatorData_->inputrec, legacySimulatorData_->mdAtoms,
168                 legacySimulatorData_->nrnb, legacySimulatorData_->wcycle, legacySimulatorData_->fr,
169                 legacySimulatorData_->top_global, legacySimulatorData_->constr, hasReadEkinState);
170         topologyHolderBuilder->registerClient(computeGlobalsElement.get());
171         energySignallerBuilder->registerSignallerClient(compat::make_not_null(computeGlobalsElement.get()));
172         trajectorySignallerBuilder->registerSignallerClient(
173                 compat::make_not_null(computeGlobalsElement.get()));
174
175         *checkBondedInteractionsCallback =
176                 computeGlobalsElement->getCheckNumberOfBondedInteractionsCallback();
177
178         auto propagator = std::make_unique<Propagator<IntegrationStep::LeapFrog>>(
179                 legacySimulatorData_->inputrec->delta_t, statePropagatorDataPtr,
180                 legacySimulatorData_->mdAtoms, legacySimulatorData_->wcycle);
181
182         addToCallListAndMove(std::move(forceElement), elementCallList, elementsOwnershipList);
183         auto stateElement = compat::make_not_null(statePropagatorDataPtr->element());
184         trajectoryElementBuilder->registerWriterClient(stateElement);
185         trajectorySignallerBuilder->registerSignallerClient(stateElement);
186         lastStepSignallerBuilder->registerSignallerClient(stateElement);
187         checkpointClients->emplace_back(stateElement);
188         // we have a full microstate at time t here!
189         addToCallList(stateElement, elementCallList);
190         if (legacySimulatorData_->inputrec->etc == etcVRESCALE)
191         {
192             // TODO: With increased complexity of the propagator, this will need further development,
193             //       e.g. using propagators templated for velocity propagation policies and a builder
194             propagator->setNumVelocityScalingVariables(legacySimulatorData_->inputrec->opts.ngtc);
195             auto thermostat = std::make_unique<VRescaleThermostat>(
196                     legacySimulatorData_->inputrec->nsttcouple, -1, false,
197                     legacySimulatorData_->inputrec->ld_seed, legacySimulatorData_->inputrec->opts.ngtc,
198                     legacySimulatorData_->inputrec->delta_t * legacySimulatorData_->inputrec->nsttcouple,
199                     legacySimulatorData_->inputrec->opts.ref_t,
200                     legacySimulatorData_->inputrec->opts.tau_t, legacySimulatorData_->inputrec->opts.nrdf,
201                     energyDataPtr, propagator->viewOnVelocityScaling(),
202                     propagator->velocityScalingCallback(), legacySimulatorData_->state_global,
203                     legacySimulatorData_->cr, legacySimulatorData_->inputrec->bContinuation);
204             checkpointClients->emplace_back(thermostat.get());
205             energyDataPtr->setVRescaleThermostat(thermostat.get());
206             addToCallListAndMove(std::move(thermostat), elementCallList, elementsOwnershipList);
207         }
208
209         std::unique_ptr<ParrinelloRahmanBarostat> prBarostat = nullptr;
210         if (legacySimulatorData_->inputrec->epc == epcPARRINELLORAHMAN)
211         {
212             // Building the PR barostat here since it needs access to the propagator
213             // and we want to be able to move the propagator object
214             prBarostat = std::make_unique<ParrinelloRahmanBarostat>(
215                     legacySimulatorData_->inputrec->nstpcouple, -1,
216                     legacySimulatorData_->inputrec->delta_t * legacySimulatorData_->inputrec->nstpcouple,
217                     legacySimulatorData_->inputrec->init_step, propagator->viewOnPRScalingMatrix(),
218                     propagator->prScalingCallback(), statePropagatorDataPtr, energyDataPtr,
219                     legacySimulatorData_->fplog, legacySimulatorData_->inputrec,
220                     legacySimulatorData_->mdAtoms, legacySimulatorData_->state_global,
221                     legacySimulatorData_->cr, legacySimulatorData_->inputrec->bContinuation);
222             energyDataPtr->setParrinelloRahamnBarostat(prBarostat.get());
223             checkpointClients->emplace_back(prBarostat.get());
224         }
225         addToCallListAndMove(std::move(propagator), elementCallList, elementsOwnershipList);
226         if (legacySimulatorData_->constr)
227         {
228             auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Positions>>(
229                     legacySimulatorData_->constr, statePropagatorDataPtr, energyDataPtr,
230                     freeEnergyPerturbationDataPtr, MASTER(legacySimulatorData_->cr),
231                     legacySimulatorData_->fplog, legacySimulatorData_->inputrec,
232                     legacySimulatorData_->mdAtoms->mdatoms());
233             auto constraintElementPtr = compat::make_not_null(constraintElement.get());
234             energySignallerBuilder->registerSignallerClient(constraintElementPtr);
235             trajectorySignallerBuilder->registerSignallerClient(constraintElementPtr);
236             loggingSignallerBuilder->registerSignallerClient(constraintElementPtr);
237
238             addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
239         }
240
241         addToCallListAndMove(std::move(computeGlobalsElement), elementCallList, elementsOwnershipList);
242         auto energyElement = compat::make_not_null(energyDataPtr->element());
243         trajectoryElementBuilder->registerWriterClient(energyElement);
244         trajectorySignallerBuilder->registerSignallerClient(energyElement);
245         energySignallerBuilder->registerSignallerClient(energyElement);
246         checkpointClients->emplace_back(energyElement);
247         // we have the energies at time t here!
248         addToCallList(energyElement, elementCallList);
249         if (prBarostat)
250         {
251             addToCallListAndMove(std::move(prBarostat), elementCallList, elementsOwnershipList);
252         }
253     }
254     else if (legacySimulatorData_->inputrec->eI == eiVV)
255     {
256         auto computeGlobalsElement =
257                 std::make_unique<ComputeGlobalsElement<ComputeGlobalsAlgorithm::VelocityVerlet>>(
258                         statePropagatorDataPtr, energyDataPtr, freeEnergyPerturbationDataPtr, signals,
259                         nstglobalcomm_, legacySimulatorData_->fplog, legacySimulatorData_->mdlog,
260                         legacySimulatorData_->cr, legacySimulatorData_->inputrec,
261                         legacySimulatorData_->mdAtoms, legacySimulatorData_->nrnb,
262                         legacySimulatorData_->wcycle, legacySimulatorData_->fr,
263                         legacySimulatorData_->top_global, legacySimulatorData_->constr, hasReadEkinState);
264         topologyHolderBuilder->registerClient(computeGlobalsElement.get());
265         energySignallerBuilder->registerSignallerClient(compat::make_not_null(computeGlobalsElement.get()));
266         trajectorySignallerBuilder->registerSignallerClient(
267                 compat::make_not_null(computeGlobalsElement.get()));
268
269         *checkBondedInteractionsCallback =
270                 computeGlobalsElement->getCheckNumberOfBondedInteractionsCallback();
271
272         auto propagatorVelocities = std::make_unique<Propagator<IntegrationStep::VelocitiesOnly>>(
273                 legacySimulatorData_->inputrec->delta_t * 0.5, statePropagatorDataPtr,
274                 legacySimulatorData_->mdAtoms, legacySimulatorData_->wcycle);
275         auto propagatorVelocitiesAndPositions =
276                 std::make_unique<Propagator<IntegrationStep::VelocityVerletPositionsAndVelocities>>(
277                         legacySimulatorData_->inputrec->delta_t, statePropagatorDataPtr,
278                         legacySimulatorData_->mdAtoms, legacySimulatorData_->wcycle);
279
280         addToCallListAndMove(std::move(forceElement), elementCallList, elementsOwnershipList);
281
282         std::unique_ptr<ParrinelloRahmanBarostat> prBarostat = nullptr;
283         if (legacySimulatorData_->inputrec->epc == epcPARRINELLORAHMAN)
284         {
285             // Building the PR barostat here since it needs access to the propagator
286             // and we want to be able to move the propagator object
287             prBarostat = std::make_unique<ParrinelloRahmanBarostat>(
288                     legacySimulatorData_->inputrec->nstpcouple, -1,
289                     legacySimulatorData_->inputrec->delta_t * legacySimulatorData_->inputrec->nstpcouple,
290                     legacySimulatorData_->inputrec->init_step,
291                     propagatorVelocities->viewOnPRScalingMatrix(),
292                     propagatorVelocities->prScalingCallback(), statePropagatorDataPtr,
293                     energyDataPtr, legacySimulatorData_->fplog, legacySimulatorData_->inputrec,
294                     legacySimulatorData_->mdAtoms, legacySimulatorData_->state_global,
295                     legacySimulatorData_->cr, legacySimulatorData_->inputrec->bContinuation);
296             energyDataPtr->setParrinelloRahamnBarostat(prBarostat.get());
297             checkpointClients->emplace_back(prBarostat.get());
298         }
299         addToCallListAndMove(std::move(propagatorVelocities), elementCallList, elementsOwnershipList);
300         if (legacySimulatorData_->constr)
301         {
302             auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Velocities>>(
303                     legacySimulatorData_->constr, statePropagatorDataPtr, energyDataPtr,
304                     freeEnergyPerturbationDataPtr, MASTER(legacySimulatorData_->cr),
305                     legacySimulatorData_->fplog, legacySimulatorData_->inputrec,
306                     legacySimulatorData_->mdAtoms->mdatoms());
307             energySignallerBuilder->registerSignallerClient(compat::make_not_null(constraintElement.get()));
308             trajectorySignallerBuilder->registerSignallerClient(
309                     compat::make_not_null(constraintElement.get()));
310             loggingSignallerBuilder->registerSignallerClient(
311                     compat::make_not_null(constraintElement.get()));
312
313             addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
314         }
315         addToCallList(compat::make_not_null(computeGlobalsElement.get()), elementCallList);
316         auto stateElement = compat::make_not_null(statePropagatorDataPtr->element());
317         trajectoryElementBuilder->registerWriterClient(stateElement);
318         trajectorySignallerBuilder->registerSignallerClient(stateElement);
319         lastStepSignallerBuilder->registerSignallerClient(stateElement);
320         checkpointClients->emplace_back(stateElement);
321         // we have a full microstate at time t here!
322         addToCallList(stateElement, elementCallList);
323         if (legacySimulatorData_->inputrec->etc == etcVRESCALE)
324         {
325             // TODO: With increased complexity of the propagator, this will need further development,
326             //       e.g. using propagators templated for velocity propagation policies and a builder
327             propagatorVelocitiesAndPositions->setNumVelocityScalingVariables(
328                     legacySimulatorData_->inputrec->opts.ngtc);
329             auto thermostat = std::make_unique<VRescaleThermostat>(
330                     legacySimulatorData_->inputrec->nsttcouple, 0, true,
331                     legacySimulatorData_->inputrec->ld_seed, legacySimulatorData_->inputrec->opts.ngtc,
332                     legacySimulatorData_->inputrec->delta_t * legacySimulatorData_->inputrec->nsttcouple,
333                     legacySimulatorData_->inputrec->opts.ref_t,
334                     legacySimulatorData_->inputrec->opts.tau_t, legacySimulatorData_->inputrec->opts.nrdf,
335                     energyDataPtr, propagatorVelocitiesAndPositions->viewOnVelocityScaling(),
336                     propagatorVelocitiesAndPositions->velocityScalingCallback(),
337                     legacySimulatorData_->state_global, legacySimulatorData_->cr,
338                     legacySimulatorData_->inputrec->bContinuation);
339             checkpointClients->emplace_back(thermostat.get());
340             energyDataPtr->setVRescaleThermostat(thermostat.get());
341             addToCallListAndMove(std::move(thermostat), elementCallList, elementsOwnershipList);
342         }
343         addToCallListAndMove(std::move(propagatorVelocitiesAndPositions), elementCallList,
344                              elementsOwnershipList);
345         if (legacySimulatorData_->constr)
346         {
347             auto constraintElement = std::make_unique<ConstraintsElement<ConstraintVariable::Positions>>(
348                     legacySimulatorData_->constr, statePropagatorDataPtr, energyDataPtr,
349                     freeEnergyPerturbationDataPtr, MASTER(legacySimulatorData_->cr),
350                     legacySimulatorData_->fplog, legacySimulatorData_->inputrec,
351                     legacySimulatorData_->mdAtoms->mdatoms());
352             energySignallerBuilder->registerSignallerClient(compat::make_not_null(constraintElement.get()));
353             trajectorySignallerBuilder->registerSignallerClient(
354                     compat::make_not_null(constraintElement.get()));
355             loggingSignallerBuilder->registerSignallerClient(
356                     compat::make_not_null(constraintElement.get()));
357
358             addToCallListAndMove(std::move(constraintElement), elementCallList, elementsOwnershipList);
359         }
360         addToCallListAndMove(std::move(computeGlobalsElement), elementCallList, elementsOwnershipList);
361         auto energyElement = compat::make_not_null(energyDataPtr->element());
362         trajectoryElementBuilder->registerWriterClient(energyElement);
363         trajectorySignallerBuilder->registerSignallerClient(energyElement);
364         energySignallerBuilder->registerSignallerClient(energyElement);
365         checkpointClients->emplace_back(energyElement);
366         // we have the energies at time t here!
367         addToCallList(energyElement, elementCallList);
368         if (prBarostat)
369         {
370             addToCallListAndMove(std::move(prBarostat), elementCallList, elementsOwnershipList);
371         }
372     }
373     else
374     {
375         gmx_fatal(FARGS, "Integrator not implemented for the modular simulator.");
376     }
377
378     auto integrator = std::make_unique<CompositeSimulatorElement>(std::move(elementCallList),
379                                                                   std::move(elementsOwnershipList));
380     // std::move *should* not be needed with c++-14, but clang-3.6 still requires it
381     return std::move(integrator);
382 }
383
384 bool ModularSimulator::isInputCompatible(bool                             exitOnFailure,
385                                          const t_inputrec*                inputrec,
386                                          bool                             doRerun,
387                                          const gmx_mtop_t&                globalTopology,
388                                          const gmx_multisim_t*            ms,
389                                          const ReplicaExchangeParameters& replExParams,
390                                          const t_fcdata*                  fcd,
391                                          bool                             doEssentialDynamics,
392                                          bool                             doMembed)
393 {
394     auto conditionalAssert = [exitOnFailure](bool condition, const char* message) {
395         if (exitOnFailure)
396         {
397             GMX_RELEASE_ASSERT(condition, message);
398         }
399         return condition;
400     };
401
402     bool isInputCompatible = true;
403
404     // GMX_USE_MODULAR_SIMULATOR allows to use modular simulator also for non-standard uses,
405     // such as the leap-frog integrator
406     const auto modularSimulatorExplicitlyTurnedOn = (getenv("GMX_USE_MODULAR_SIMULATOR") != nullptr);
407     // GMX_USE_MODULAR_SIMULATOR allows to use disable modular simulator for all uses,
408     // including the velocity-verlet integrator used by default
409     const auto modularSimulatorExplicitlyTurnedOff = (getenv("GMX_DISABLE_MODULAR_SIMULATOR") != nullptr);
410
411     GMX_RELEASE_ASSERT(
412             !(modularSimulatorExplicitlyTurnedOn && modularSimulatorExplicitlyTurnedOff),
413             "Cannot have both GMX_USE_MODULAR_SIMULATOR=ON and GMX_DISABLE_MODULAR_SIMULATOR=ON. "
414             "Unset one of the two environment variables to explicitly chose which simulator to "
415             "use, "
416             "or unset both to recover default behavior.");
417
418     GMX_RELEASE_ASSERT(
419             !(modularSimulatorExplicitlyTurnedOff && inputrec->eI == eiVV
420               && inputrec->epc == epcPARRINELLORAHMAN),
421             "Cannot use a Parrinello-Rahman barostat with md-vv and "
422             "GMX_DISABLE_MODULAR_SIMULATOR=ON, "
423             "as the Parrinello-Rahman barostat is not implemented in the legacy simulator. Unset "
424             "GMX_DISABLE_MODULAR_SIMULATOR or use a different pressure control algorithm.");
425
426     isInputCompatible =
427             isInputCompatible
428             && conditionalAssert(
429                        inputrec->eI == eiMD || inputrec->eI == eiVV,
430                        "Only integrators md and md-vv are supported by the modular simulator.");
431     isInputCompatible = isInputCompatible
432                         && conditionalAssert(inputrec->eI != eiMD || modularSimulatorExplicitlyTurnedOn,
433                                              "Set GMX_USE_MODULAR_SIMULATOR=ON to use the modular "
434                                              "simulator with integrator md.");
435     isInputCompatible =
436             isInputCompatible
437             && conditionalAssert(!doRerun, "Rerun is not supported by the modular simulator.");
438     isInputCompatible =
439             isInputCompatible
440             && conditionalAssert(
441                        inputrec->etc == etcNO || inputrec->etc == etcVRESCALE,
442                        "Only v-rescale thermostat is supported by the modular simulator.");
443     isInputCompatible =
444             isInputCompatible
445             && conditionalAssert(
446                        inputrec->epc == epcNO || inputrec->epc == epcPARRINELLORAHMAN,
447                        "Only Parrinello-Rahman barostat is supported by the modular simulator.");
448     isInputCompatible =
449             isInputCompatible
450             && conditionalAssert(
451                        !(inputrecNptTrotter(inputrec) || inputrecNphTrotter(inputrec)
452                          || inputrecNvtTrotter(inputrec)),
453                        "Legacy Trotter decomposition is not supported by the modular simulator.");
454     isInputCompatible = isInputCompatible
455                         && conditionalAssert(inputrec->efep == efepNO || inputrec->efep == efepYES
456                                                      || inputrec->efep == efepSLOWGROWTH,
457                                              "Expanded ensemble free energy calculation is not "
458                                              "supported by the modular simulator.");
459     isInputCompatible = isInputCompatible
460                         && conditionalAssert(!inputrec->bPull,
461                                              "Pulling is not supported by the modular simulator.");
462     isInputCompatible =
463             isInputCompatible
464             && conditionalAssert(inputrec->opts.ngacc == 1 && inputrec->opts.acc[0][XX] == 0.0
465                                          && inputrec->opts.acc[0][YY] == 0.0
466                                          && inputrec->opts.acc[0][ZZ] == 0.0 && inputrec->cos_accel == 0.0,
467                                  "Acceleration is not supported by the modular simulator.");
468     isInputCompatible =
469             isInputCompatible
470             && conditionalAssert(inputrec->opts.ngfrz == 1 && inputrec->opts.nFreeze[0][XX] == 0
471                                          && inputrec->opts.nFreeze[0][YY] == 0
472                                          && inputrec->opts.nFreeze[0][ZZ] == 0,
473                                  "Freeze groups are not supported by the modular simulator.");
474     isInputCompatible =
475             isInputCompatible
476             && conditionalAssert(
477                        inputrec->deform[XX][XX] == 0.0 && inputrec->deform[XX][YY] == 0.0
478                                && inputrec->deform[XX][ZZ] == 0.0 && inputrec->deform[YY][XX] == 0.0
479                                && inputrec->deform[YY][YY] == 0.0 && inputrec->deform[YY][ZZ] == 0.0
480                                && inputrec->deform[ZZ][XX] == 0.0 && inputrec->deform[ZZ][YY] == 0.0
481                                && inputrec->deform[ZZ][ZZ] == 0.0,
482                        "Deformation is not supported by the modular simulator.");
483     isInputCompatible =
484             isInputCompatible
485             && conditionalAssert(gmx_mtop_interaction_count(globalTopology, IF_VSITE) == 0,
486                                  "Virtual sites are not supported by the modular simulator.");
487     isInputCompatible = isInputCompatible
488                         && conditionalAssert(!inputrec->bDoAwh,
489                                              "AWH is not supported by the modular simulator.");
490     isInputCompatible =
491             isInputCompatible
492             && conditionalAssert(gmx_mtop_ftype_count(globalTopology, F_DISRES) == 0,
493                                  "Distance restraints are not supported by the modular simulator.");
494     isInputCompatible =
495             isInputCompatible
496             && conditionalAssert(
497                        gmx_mtop_ftype_count(globalTopology, F_ORIRES) == 0,
498                        "Orientation restraints are not supported by the modular simulator.");
499     isInputCompatible =
500             isInputCompatible
501             && conditionalAssert(ms == nullptr,
502                                  "Multi-sim are not supported by the modular simulator.");
503     isInputCompatible =
504             isInputCompatible
505             && conditionalAssert(replExParams.exchangeInterval == 0,
506                                  "Replica exchange is not supported by the modular simulator.");
507
508     int numEnsembleRestraintSystems;
509     if (fcd)
510     {
511         numEnsembleRestraintSystems = fcd->disres->nsystems;
512     }
513     else
514     {
515         auto distantRestraintEnsembleEnvVar = getenv("GMX_DISRE_ENSEMBLE_SIZE");
516         numEnsembleRestraintSystems =
517                 (ms != nullptr && distantRestraintEnsembleEnvVar != nullptr)
518                         ? static_cast<int>(strtol(distantRestraintEnsembleEnvVar, nullptr, 10))
519                         : 0;
520     }
521     isInputCompatible =
522             isInputCompatible
523             && conditionalAssert(numEnsembleRestraintSystems <= 1,
524                                  "Ensemble restraints are not supported by the modular simulator.");
525     isInputCompatible =
526             isInputCompatible
527             && conditionalAssert(!doSimulatedAnnealing(inputrec),
528                                  "Simulated annealing is not supported by the modular simulator.");
529     isInputCompatible =
530             isInputCompatible
531             && conditionalAssert(!inputrec->bSimTemp,
532                                  "Simulated tempering is not supported by the modular simulator.");
533     isInputCompatible = isInputCompatible
534                         && conditionalAssert(!inputrec->bExpanded,
535                                              "Expanded ensemble simulations are not supported by "
536                                              "the modular simulator.");
537     isInputCompatible =
538             isInputCompatible
539             && conditionalAssert(!doEssentialDynamics,
540                                  "Essential dynamics is not supported by the modular simulator.");
541     isInputCompatible = isInputCompatible
542                         && conditionalAssert(inputrec->eSwapCoords == eswapNO,
543                                              "Ion / water position swapping is not supported by "
544                                              "the modular simulator.");
545     isInputCompatible =
546             isInputCompatible
547             && conditionalAssert(!inputrec->bIMD,
548                                  "Interactive MD is not supported by the modular simulator.");
549     isInputCompatible =
550             isInputCompatible
551             && conditionalAssert(!doMembed,
552                                  "Membrane embedding is not supported by the modular simulator.");
553     // TODO: Change this to the boolean passed when we merge the user interface change for the GPU update.
554     isInputCompatible =
555             isInputCompatible
556             && conditionalAssert(
557                        getenv("GMX_FORCE_UPDATE_DEFAULT_GPU") == nullptr,
558                        "Integration on the GPU is not supported by the modular simulator.");
559     // Modular simulator is centered around NS updates
560     // TODO: think how to handle nstlist == 0
561     isInputCompatible = isInputCompatible
562                         && conditionalAssert(inputrec->nstlist != 0,
563                                              "Simulations without neighbor list update are not "
564                                              "supported by the modular simulator.");
565     isInputCompatible = isInputCompatible
566                         && conditionalAssert(!GMX_FAHCORE,
567                                              "GMX_FAHCORE not supported by the modular simulator.");
568
569     return isInputCompatible;
570 }
571
572 ModularSimulator::ModularSimulator(std::unique_ptr<LegacySimulatorData> legacySimulatorData) :
573     legacySimulatorData_(std::move(legacySimulatorData))
574 {
575     checkInputForDisabledFunctionality();
576 }
577
578 void ModularSimulator::checkInputForDisabledFunctionality()
579 {
580     isInputCompatible(true, legacySimulatorData_->inputrec,
581                       legacySimulatorData_->mdrunOptions.rerun, *legacySimulatorData_->top_global,
582                       legacySimulatorData_->ms, legacySimulatorData_->replExParams,
583                       &legacySimulatorData_->fr->listedForces->fcdata(),
584                       opt2bSet("-ei", legacySimulatorData_->nfile, legacySimulatorData_->fnm),
585                       legacySimulatorData_->membed != nullptr);
586     if (legacySimulatorData_->observablesHistory->edsamHistory)
587     {
588         gmx_fatal(FARGS,
589                   "The checkpoint is from a run with essential dynamics sampling, "
590                   "but the current run did not specify the -ei option. "
591                   "Either specify the -ei option to mdrun, or do not use this checkpoint file.");
592     }
593 }
594
595 } // namespace gmx