Use SimulatorModules parameter.
[gromacs.git] / src / gromacs / mdrun / simulatorbuilder.h
blobf94f7904e9eebe6824151b2e7002367f3d27d1a4
1 /*
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.
35 /*! \internal
36 * \brief Declares the simulator builder for mdrun
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_mdrun
41 #ifndef GMX_MDRUN_SIMULATORBUILDER_H
42 #define GMX_MDRUN_SIMULATORBUILDER_H
44 #include <memory>
46 #include "gromacs/mdlib/vsite.h"
47 #include "gromacs/utility/gmxassert.h"
48 #include "gromacs/utility/mdmodulenotification.h"
50 #include "replicaexchange.h"
52 class energyhistory_t;
53 struct gmx_ekindata_t;
54 struct gmx_enerdata_t;
55 struct gmx_enfrot;
56 struct gmx_mtop_t;
57 struct gmx_multisim_t;
58 struct gmx_output_env_t;
59 struct gmx_wallcycle;
60 struct gmx_walltime_accounting;
61 struct ObservablesHistory;
62 struct pull_t;
63 struct t_commrec;
64 struct t_forcerec;
65 struct t_filenm;
66 struct t_inputrec;
67 struct t_nrnb;
68 struct t_swap;
69 class t_state;
71 namespace gmx
73 enum class StartingBehavior;
74 class BoxDeformation;
75 class Constraints;
76 class MdrunScheduleWorkload;
77 class MembedHolder;
78 class IMDOutputProvider;
79 class ImdSession;
80 class MDLogger;
81 class MDAtoms;
82 class ISimulator;
83 class StopHandlerBuilder;
84 struct MdrunOptions;
86 struct SimulatorConfig
88 public:
89 SimulatorConfig(const MdrunOptions& mdrunOptions,
90 StartingBehavior startingBehavior,
91 MdrunScheduleWorkload* runScheduleWork) :
92 mdrunOptions_(mdrunOptions),
93 startingBehavior_(startingBehavior),
94 runScheduleWork_(runScheduleWork)
97 // TODO: Specify copy and move semantics.
99 const MdrunOptions& mdrunOptions_;
100 StartingBehavior startingBehavior_;
101 MdrunScheduleWorkload* runScheduleWork_;
105 // TODO: Reconsider the name.
106 struct SimulatorStateData
108 t_state* globalState_p;
109 ObservablesHistory* observablesHistory_p;
110 gmx_enerdata_t* enerdata_p;
111 gmx_ekindata_t* ekindata_p;
113 SimulatorStateData(t_state* globalState,
114 ObservablesHistory* observablesHistory,
115 gmx_enerdata_t* enerdata,
116 gmx_ekindata_t* ekindata) :
117 globalState_p(globalState),
118 observablesHistory_p(observablesHistory),
119 enerdata_p(enerdata),
120 ekindata_p(ekindata)
124 SimulatorStateData(const SimulatorStateData& simulatorStateData) = default;
127 class SimulatorEnv
129 public:
130 SimulatorEnv(FILE* fplog,
131 t_commrec* commRec,
132 gmx_multisim_t* multisimCommRec,
133 const MDLogger& logger,
134 gmx_output_env_t* outputEnv) :
135 fplog_{ fplog },
136 commRec_{ commRec },
137 multisimCommRec_{ multisimCommRec },
138 logger_{ logger },
139 outputEnv_{ outputEnv }
143 FILE* fplog_;
144 t_commrec* commRec_;
145 const gmx_multisim_t* multisimCommRec_;
146 const MDLogger& logger_;
147 const gmx_output_env_t* outputEnv_;
150 class Profiling
152 public:
153 Profiling(t_nrnb* nrnb, gmx_walltime_accounting* walltimeAccounting, gmx_wallcycle* wallCycle) :
154 nrnb(nrnb),
155 wallCycle(wallCycle),
156 walltimeAccounting(walltimeAccounting)
160 t_nrnb* nrnb;
161 gmx_wallcycle* wallCycle;
162 gmx_walltime_accounting* walltimeAccounting;
165 class ConstraintsParam
167 public:
168 ConstraintsParam(Constraints* constraints, gmx_enfrot* enforcedRotation, VirtualSitesHandler* vSite) :
169 constr(constraints),
170 enforcedRotation(enforcedRotation),
171 vsite(vSite)
175 Constraints* constr;
176 gmx_enfrot* enforcedRotation;
177 VirtualSitesHandler* vsite;
180 class LegacyInput
182 public:
183 LegacyInput(int filenamesSize,
184 const t_filenm* filenamesData,
185 t_inputrec* inputRec,
186 t_forcerec* forceRec) :
187 numFile(filenamesSize),
188 filenames(filenamesData),
189 inputrec(inputRec),
190 forceRec(forceRec)
193 int numFile;
194 const t_filenm* filenames;
195 t_inputrec* inputrec;
196 t_forcerec* forceRec;
199 /*! \brief SimulatorBuilder parameter type for InteractiveMD.
201 * Conveys a non-owning pointer to implementation details.
203 class InteractiveMD
205 public:
206 explicit InteractiveMD(ImdSession* imdSession) : imdSession(imdSession) {}
208 ImdSession* imdSession;
211 class SimulatorModules
213 public:
214 SimulatorModules(IMDOutputProvider* mdOutputProvider, const MdModulesNotifier& notifier) :
215 outputProvider(mdOutputProvider),
216 mdModulesNotifier(notifier)
220 IMDOutputProvider* outputProvider;
221 const MdModulesNotifier& mdModulesNotifier;
224 class CenterOfMassPulling
226 public:
227 explicit CenterOfMassPulling(pull_t gmx_unused* pPull) {}
230 class IonSwapping
232 public:
233 IonSwapping(t_swap gmx_unused* pSwap) {}
236 class TopologyData
238 public:
239 TopologyData(gmx_mtop_t gmx_unused* pMtop, MDAtoms gmx_unused* pAtoms) {}
242 /*! \libinternal
243 * \brief Class preparing the creation of Simulator objects
245 * Objects of this class build Simulator objects, which in turn are used to
246 * run molecular simulations.
248 class SimulatorBuilder
250 public:
251 void add(MembedHolder&& membedHolder);
253 void add(std::unique_ptr<StopHandlerBuilder> stopHandlerBuilder)
255 stopHandlerBuilder_ = std::move(stopHandlerBuilder);
258 void add(SimulatorStateData&& simulatorStateData)
260 simulatorStateData_ = std::make_unique<SimulatorStateData>(simulatorStateData);
263 void add(SimulatorConfig&& simulatorConfig)
265 // Note: SimulatorConfig appears to the compiler to be trivially copyable,
266 // but this may not be safe and may change in the future.
267 simulatorConfig_ = std::make_unique<SimulatorConfig>(simulatorConfig);
270 void add(SimulatorEnv&& simulatorEnv)
272 simulatorEnv_ = std::make_unique<SimulatorEnv>(simulatorEnv);
275 void add(Profiling&& profiling) { profiling_ = std::make_unique<Profiling>(profiling); }
277 void add(ConstraintsParam&& constraintsParam)
279 constraintsParam_ = std::make_unique<ConstraintsParam>(constraintsParam);
282 void add(LegacyInput&& legacyInput)
284 legacyInput_ = std::make_unique<LegacyInput>(legacyInput);
287 void add(ReplicaExchangeParameters&& replicaExchangeParameters)
289 replicaExchangeParameters_ =
290 std::make_unique<ReplicaExchangeParameters>(replicaExchangeParameters);
293 void add(InteractiveMD&& interactiveMd)
295 interactiveMD_ = std::make_unique<InteractiveMD>(interactiveMd);
298 void add(SimulatorModules&& simulatorModules)
300 simulatorModules_ = std::make_unique<SimulatorModules>(simulatorModules);
303 void add(CenterOfMassPulling&& centerOfMassPulling)
305 centerOfMassPulling_ = std::make_unique<CenterOfMassPulling>(centerOfMassPulling);
308 void add(IonSwapping&& ionSwapping)
310 ionSwapping_ = std::make_unique<IonSwapping>(ionSwapping);
313 void add(TopologyData&& topologyData)
315 topologyData_ = std::make_unique<TopologyData>(topologyData);
318 /*! \brief Build a Simulator object based on input data
320 * Return a pointer to a simulation object. The use of a parameter
321 * pack insulates the builder from changes to the arguments of the
322 * Simulator objects.
324 * \throws gmx::APIError if expected set-up methods have not been called before build()
326 * \return Unique pointer to a Simulator object
328 std::unique_ptr<ISimulator> build(bool useModularSimulator,
329 BoxDeformation* deform,
330 pull_t* pull_work,
331 t_swap* swap,
332 gmx_mtop_t* top_global,
333 MDAtoms* mdAtoms,
334 const ReplicaExchangeParameters& replExParams);
336 private:
337 // Note: we use std::unique_ptr instead of std::optional because we want to
338 // allow for opaque types at the discretion of the module developer.
339 std::unique_ptr<SimulatorConfig> simulatorConfig_;
340 std::unique_ptr<MembedHolder> membedHolder_;
341 std::unique_ptr<StopHandlerBuilder> stopHandlerBuilder_;
342 std::unique_ptr<SimulatorStateData> simulatorStateData_;
343 std::unique_ptr<SimulatorEnv> simulatorEnv_;
344 std::unique_ptr<Profiling> profiling_;
345 std::unique_ptr<ConstraintsParam> constraintsParam_;
346 std::unique_ptr<LegacyInput> legacyInput_;
347 std::unique_ptr<ReplicaExchangeParameters> replicaExchangeParameters_;
348 std::unique_ptr<InteractiveMD> interactiveMD_;
349 std::unique_ptr<SimulatorModules> simulatorModules_;
350 std::unique_ptr<CenterOfMassPulling> centerOfMassPulling_;
351 std::unique_ptr<IonSwapping> ionSwapping_;
352 std::unique_ptr<TopologyData> topologyData_;
355 } // namespace gmx
357 #endif // GMX_MDRUN_SIMULATORBUILDER_SIMULATORBUILDER_H