Introduce plumbing for ObservablesReducer
[alexxy/gromacs.git] / src / gromacs / modularsimulator / andersentemperaturecoupling.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 Declares Andersen temperature coupling for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  */
41
42 #ifndef GMX_MODULARSIMULATOR_ANDERSENTHERMOSTAT_H
43 #define GMX_MODULARSIMULATOR_ANDERSENTHERMOSTAT_H
44
45 #include "gromacs/utility/arrayref.h"
46
47 #include "energydata.h"
48 #include "modularsimulatorinterfaces.h"
49 #include "propagator.h"
50
51 struct t_commrec;
52 struct t_mdatoms;
53
54 namespace gmx
55 {
56
57 /*! \internal
58  * \ingroup module_modularsimulator
59  * \brief Element implementing the Andersen thermostat
60  *
61  */
62 class AndersenTemperatureCoupling final : public ISimulatorElement
63 {
64 public:
65     //! Constructor
66     AndersenTemperatureCoupling(double               simulationTimestep,
67                                 bool                 doMassive,
68                                 int64_t              seed,
69                                 ArrayRef<const real> referenceTemperature,
70                                 ArrayRef<const real> couplingTime,
71                                 StatePropagatorData* statePropagatorData,
72                                 const MDAtoms*       mdAtoms,
73                                 const t_commrec*     cr);
74
75     /*! \brief Register run function for step / time
76      *
77      * \param step                 The step number
78      * \param time                 The time
79      * \param registerRunFunction  Function allowing to register a run function
80      */
81     void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
82
83     //! Sanity check at setup time
84     void elementSetup() override;
85     //! No element teardown needed
86     void elementTeardown() override {}
87
88     /*! \brief Factory method implementation
89      *
90      * \param legacySimulatorData  Pointer allowing access to simulator level data
91      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
92      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
93      * \param energyData  Pointer to the \c EnergyData object
94      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
95      * \param globalCommunicationHelper   Pointer to the \c GlobalCommunicationHelper object
96      * \param observablesReducer          Pointer to the \c ObservablesReducer object
97      *
98      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
99      */
100     static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
101                                                     ModularSimulatorAlgorithmBuilderHelper* builderHelper,
102                                                     StatePropagatorData*        statePropagatorData,
103                                                     EnergyData*                 energyData,
104                                                     FreeEnergyPerturbationData* freeEnergyPerturbationData,
105                                                     GlobalCommunicationHelper* globalCommunicationHelper,
106                                                     ObservablesReducer*        observablesReducer);
107
108     //! Returns the frequency at which temperature coupling is performed
109     [[nodiscard]] int frequency() const;
110
111 private:
112     //! Update the reference temperature
113     static void updateReferenceTemperature(ArrayRef<const real>                temperatures,
114                                            ReferenceTemperatureChangeAlgorithm algorithm);
115
116     //! Whether we're doing massive Andersen thermostatting
117     const bool doMassive_;
118     //! The rate
119     const real randomizationRate_;
120     //! The frequency at which the thermostat is applied
121     const int couplingFrequency_;
122     //! The random seed
123     const int64_t seed_;
124     //! Coupling temperature per group
125     ArrayRef<const real> referenceTemperature_;
126     //! Coupling time per group
127     ArrayRef<const real> couplingTime_;
128
129     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
130     //! Pointer to the micro state
131     StatePropagatorData* statePropagatorData_;
132     //! Atom parameters for this domain.
133     const t_mdatoms* mdAtoms_;
134     //! Handles communication.
135     const t_commrec* cr_;
136
137     //! Apply the thermostat at step
138     void apply(Step step);
139 };
140
141 } // namespace gmx
142
143 #endif // GMX_MODULARSIMULATOR_ANDERSENTHERMOSTAT_H