Improve docs and naming for MdModulesNotifiers
[alexxy/gromacs.git] / src / gromacs / mdrun / simulatorbuilder.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020,2021, 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 simulator builder for mdrun
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_mdrun
40  */
41
42 #include "gmxpre.h"
43
44 #include "simulatorbuilder.h"
45
46 #include <memory>
47
48 #include "gromacs/mdlib/vsite.h"
49 #include "gromacs/mdtypes/checkpointdata.h"
50 #include "gromacs/mdtypes/mdrunoptions.h"
51 #include "gromacs/mdtypes/state.h"
52 #include "gromacs/modularsimulator/modularsimulator.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/utility/mdmodulenotification.h"
55
56 #include "legacysimulator.h"
57 #include "membedholder.h"
58 #include "replicaexchange.h"
59
60
61 namespace gmx
62 {
63
64 //! \brief Build a Simulator object
65 std::unique_ptr<ISimulator> SimulatorBuilder::build(bool useModularSimulator)
66 {
67     // TODO: Reduce protocol complexity.
68     //     Investigate individual paramters. Identify default-constructable parameters and clarify
69     //     usage requirements.
70     if (!stopHandlerBuilder_)
71     {
72         throw APIError("You must add a StopHandlerBuilder before calling build().");
73     }
74     if (!membedHolder_)
75     {
76         throw APIError("You must add a MembedHolder before calling build().");
77     }
78     if (!simulatorStateData_)
79     {
80         throw APIError("Simulator State Data has not been added to the builder");
81     }
82     if (!simulatorConfig_)
83     {
84         throw APIError("Simulator config should be set before building the simulator");
85     }
86     if (!simulatorEnv_)
87     {
88         throw APIError("You must add a SimulatorEnv before calling build().");
89     }
90     if (!profiling_)
91     {
92         throw APIError("You must add a Profiling before calling build().");
93     }
94     if (!constraintsParam_)
95     {
96         throw APIError("You must add a ConstraintsParam before calling build().");
97     }
98     if (!legacyInput_)
99     {
100         throw APIError("You must add a LegacyInput before calling build().");
101     }
102     if (!replicaExchangeParameters_)
103     {
104         throw APIError("You must add a ReplicaExchangeParameters before calling build().");
105     }
106     if (!interactiveMD_)
107     {
108         throw APIError("You must add a InteractiveMD before calling build().");
109     }
110     if (!simulatorModules_)
111     {
112         throw APIError("You must add a SimulatorModules before calling build().");
113     }
114     if (!centerOfMassPulling_)
115     {
116         throw APIError("You must add a CenterOfMassPulling before calling build().");
117     }
118     if (!ionSwapping_)
119     {
120         throw APIError("You must add a IonSwapping before calling build().");
121     }
122     if (!topologyData_)
123     {
124         throw APIError("You must add a TopologyData before calling build().");
125     }
126
127     if (useModularSimulator)
128     {
129         // NOLINTNEXTLINE(modernize-make-unique): make_unique does not work with private constructor
130         return std::unique_ptr<ModularSimulator>(new ModularSimulator(
131                 std::make_unique<LegacySimulatorData>(simulatorEnv_->fplog_,
132                                                       simulatorEnv_->commRec_,
133                                                       simulatorEnv_->multisimCommRec_,
134                                                       simulatorEnv_->logger_,
135                                                       legacyInput_->numFile,
136                                                       legacyInput_->filenames,
137                                                       simulatorEnv_->outputEnv_,
138                                                       simulatorConfig_->mdrunOptions_,
139                                                       simulatorConfig_->startingBehavior_,
140                                                       constraintsParam_->vsite,
141                                                       constraintsParam_->constr,
142                                                       constraintsParam_->enforcedRotation,
143                                                       boxDeformation_->deform,
144                                                       simulatorModules_->outputProvider,
145                                                       simulatorModules_->mdModulesNotifiers,
146                                                       legacyInput_->inputrec,
147                                                       interactiveMD_->imdSession,
148                                                       centerOfMassPulling_->pull_work,
149                                                       ionSwapping_->ionSwap,
150                                                       topologyData_->top_global,
151                                                       simulatorStateData_->globalState_p,
152                                                       simulatorStateData_->observablesHistory_p,
153                                                       topologyData_->mdAtoms,
154                                                       profiling_->nrnb,
155                                                       profiling_->wallCycle,
156                                                       legacyInput_->forceRec,
157                                                       simulatorStateData_->enerdata_p,
158                                                       simulatorStateData_->ekindata_p,
159                                                       simulatorConfig_->runScheduleWork_,
160                                                       *replicaExchangeParameters_,
161                                                       membedHolder_->membed(),
162                                                       profiling_->walltimeAccounting,
163                                                       std::move(stopHandlerBuilder_),
164                                                       simulatorConfig_->mdrunOptions_.rerun),
165                 std::move(modularSimulatorCheckpointData_)));
166     }
167     // NOLINTNEXTLINE(modernize-make-unique): make_unique does not work with private constructor
168     return std::unique_ptr<LegacySimulator>(new LegacySimulator(simulatorEnv_->fplog_,
169                                                                 simulatorEnv_->commRec_,
170                                                                 simulatorEnv_->multisimCommRec_,
171                                                                 simulatorEnv_->logger_,
172                                                                 legacyInput_->numFile,
173                                                                 legacyInput_->filenames,
174                                                                 simulatorEnv_->outputEnv_,
175                                                                 simulatorConfig_->mdrunOptions_,
176                                                                 simulatorConfig_->startingBehavior_,
177                                                                 constraintsParam_->vsite,
178                                                                 constraintsParam_->constr,
179                                                                 constraintsParam_->enforcedRotation,
180                                                                 boxDeformation_->deform,
181                                                                 simulatorModules_->outputProvider,
182                                                                 simulatorModules_->mdModulesNotifiers,
183                                                                 legacyInput_->inputrec,
184                                                                 interactiveMD_->imdSession,
185                                                                 centerOfMassPulling_->pull_work,
186                                                                 ionSwapping_->ionSwap,
187                                                                 topologyData_->top_global,
188                                                                 simulatorStateData_->globalState_p,
189                                                                 simulatorStateData_->observablesHistory_p,
190                                                                 topologyData_->mdAtoms,
191                                                                 profiling_->nrnb,
192                                                                 profiling_->wallCycle,
193                                                                 legacyInput_->forceRec,
194                                                                 simulatorStateData_->enerdata_p,
195                                                                 simulatorStateData_->ekindata_p,
196                                                                 simulatorConfig_->runScheduleWork_,
197                                                                 *replicaExchangeParameters_,
198                                                                 membedHolder_->membed(),
199                                                                 profiling_->walltimeAccounting,
200                                                                 std::move(stopHandlerBuilder_),
201                                                                 simulatorConfig_->mdrunOptions_.rerun));
202 }
203
204 void SimulatorBuilder::add(MembedHolder&& membedHolder)
205 {
206     membedHolder_ = std::make_unique<MembedHolder>(std::move(membedHolder));
207 }
208
209 void SimulatorBuilder::add(ReplicaExchangeParameters&& replicaExchangeParameters)
210 {
211     replicaExchangeParameters_ = std::make_unique<ReplicaExchangeParameters>(replicaExchangeParameters);
212 }
213
214 void SimulatorBuilder::add(std::unique_ptr<ReadCheckpointDataHolder> modularSimulatorCheckpointData)
215 {
216     modularSimulatorCheckpointData_ = std::move(modularSimulatorCheckpointData);
217 }
218
219
220 } // namespace gmx