42d170e2e7ce0c2414b333f36f7c7ff8fc1dbb27
[alexxy/gromacs.git] / src / gromacs / modularsimulator / modularsimulator.h
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 /*! \libinternal \file
36  * \brief Provides the modular simulator.
37  *
38  * Defines the ModularSimulator class. Provides checkUseModularSimulator() utility function
39  * to determine whether the ModularSimulator should be used.
40  *
41  * \author Pascal Merz <pascal.merz@me.com>
42  * \ingroup module_modularsimulator
43  *
44  * This header is currently the only part of the modular simulator module which is exposed.
45  * Mdrunner creates an object of type ModularSimulator (via SimulatorBuilder), and calls its
46  * run() method. Mdrunner also calls checkUseModularSimulator(...), which in turns calls a
47  * static method of ModularSimulator. This could easily become a free function if this requires
48  * more exposure than otherwise necessary.
49  */
50 #ifndef GROMACS_MODULARSIMULATOR_MODULARSIMULATOR_H
51 #define GROMACS_MODULARSIMULATOR_MODULARSIMULATOR_H
52
53 #include "gromacs/mdrun/isimulator.h"
54
55 struct CheckpointHeaderContents;
56 struct t_fcdata;
57 struct t_trxframe;
58
59 namespace gmx
60 {
61 class ModularSimulatorAlgorithmBuilder;
62 class ReadCheckpointDataHolder;
63
64 /*! \libinternal
65  * \ingroup module_modularsimulator
66  * \brief The modular simulator
67  *
68  * Based on the input given, this simulator builds independent elements and
69  * signallers and stores them in a respective vector. The run function
70  * runs the simulation by, in turn, building a task list from the elements
71  * for a predefined number of steps, then running the task list, and repeating
72  * until the stop criterion is fulfilled.
73  */
74 class ModularSimulator final : public ISimulator
75 {
76 public:
77     //! Run the simulator
78     void run() override;
79
80     //! Check for disabled functionality
81     static bool isInputCompatible(bool                             exitOnFailure,
82                                   const t_inputrec*                inputrec,
83                                   bool                             doRerun,
84                                   const gmx_mtop_t&                globalTopology,
85                                   const gmx_multisim_t*            ms,
86                                   const ReplicaExchangeParameters& replExParams,
87                                   const t_fcdata*                  fcd,
88                                   bool                             doEssentialDynamics,
89                                   bool                             doMembed);
90
91     //! Read everything that can be stored in t_trxframe from a checkpoint file
92     static void readCheckpointToTrxFrame(t_trxframe*                     fr,
93                                          ReadCheckpointDataHolder*       readCheckpointDataHolder,
94                                          const CheckpointHeaderContents& checkpointHeaderContents);
95
96     // Only builder can construct
97     friend class SimulatorBuilder;
98
99 private:
100     //! Constructor
101     ModularSimulator(std::unique_ptr<LegacySimulatorData>      legacySimulatorData,
102                      std::unique_ptr<ReadCheckpointDataHolder> checkpointDataHolder);
103
104     //! Populate algorithm builder with elements
105     void addIntegrationElements(ModularSimulatorAlgorithmBuilder* builder);
106
107     //! Check for disabled functionality (during construction time)
108     void checkInputForDisabledFunctionality();
109
110     //! Pointer to legacy simulator data (TODO: Can we avoid using unique_ptr? #3628)
111     std::unique_ptr<LegacySimulatorData> legacySimulatorData_;
112     //! Input checkpoint data
113     std::unique_ptr<ReadCheckpointDataHolder> checkpointDataHolder_;
114 };
115
116 /*!
117  * \brief Whether or not to use the ModularSimulator
118  *
119  * GMX_DISABLE_MODULAR_SIMULATOR environment variable allows to disable modular simulator for
120  * all uses.
121  *
122  * See ModularSimulator::isInputCompatible() for function signature.
123  *
124  * \ingroup module_modularsimulator
125  */
126 template<typename... Ts>
127 auto checkUseModularSimulator(Ts&&... args)
128         -> decltype(ModularSimulator::isInputCompatible(std::forward<Ts>(args)...))
129 {
130     return ModularSimulator::isInputCompatible(std::forward<Ts>(args)...)
131            && getenv("GMX_DISABLE_MODULAR_SIMULATOR") == nullptr;
132 }
133
134 } // namespace gmx
135
136 #endif // GROMACS_MODULARSIMULATOR_MODULARSIMULATOR_H