Introduce plumbing for ObservablesReducer
[alexxy/gromacs.git] / src / gromacs / modularsimulator / firstorderpressurecoupling.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 the element performing first-order pressure coupling for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  *
41  * This header is only used within the modular simulator module
42  */
43
44 #ifndef GMX_MODULARSIMULATOR_FIRSTORDERPRESSURECOUPLING_H
45 #define GMX_MODULARSIMULATOR_FIRSTORDERPRESSURECOUPLING_H
46
47 #include "modularsimulatorinterfaces.h"
48
49 struct t_inputrec;
50 struct t_nrnb;
51
52 enum class PressureCoupling;
53
54 namespace gmx
55 {
56 class EnergyData;
57 class FreeEnergyPerturbationData;
58 class GlobalCommunicationHelper;
59 class LegacySimulatorData;
60 class MDAtoms;
61 class ModularSimulatorAlgorithmBuilderHelper;
62 class ObservablesReducer;
63 class StatePropagatorData;
64
65 /*! \internal
66  * \brief Element performing first-order pressure coupling
67  *
68  * This element implements the first-order pressure coupling algorithms
69  * (Berendesen, c-rescale).
70  */
71 class FirstOrderPressureCoupling final : public ISimulatorElement, public ICheckpointHelperClient
72 {
73 public:
74     //! Constructor
75     FirstOrderPressureCoupling(int                               couplingFrequency,
76                                int                               couplingOffset,
77                                real                              couplingTimeStep,
78                                StatePropagatorData*              statePropagatorData,
79                                EnergyData*                       energyData,
80                                FILE*                             fplog,
81                                const t_inputrec*                 inputrec,
82                                const MDAtoms*                    mdAtoms,
83                                t_nrnb*                           nrnb,
84                                ReportPreviousStepConservedEnergy reportPreviousStepConservedEnergy);
85
86     void scheduleTask(Step step, Time time, const RegisterRunFunction& function) override;
87     //! Setup - initialize relative box matrix
88     void elementSetup() override;
89     //! No teardown needed
90     void elementTeardown() override{};
91
92     //! ICheckpointHelperClient write checkpoint implementation
93     void saveCheckpointState(std::optional<WriteCheckpointData> checkpointData, const t_commrec* cr) override;
94     //! ICheckpointHelperClient read checkpoint implementation
95     void restoreCheckpointState(std::optional<ReadCheckpointData> checkpointData, const t_commrec* cr) override;
96     //! ICheckpointHelperClient key implementation
97     const std::string& clientID() override;
98
99     /*! \brief Factory method implementation
100      *
101      * \param legacySimulatorData  Pointer allowing access to simulator level data
102      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
103      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
104      * \param energyData  Pointer to the \c EnergyData object
105      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
106      * \param globalCommunicationHelper   Pointer to the \c GlobalCommunicationHelper object
107      * \param observablesReducer          Pointer to the \c ObservablesReducer object
108      * \param offset  The step offset at which the barostat is applied
109      * \param reportPreviousStepConservedEnergy  Report the previous or the current step conserved energy
110      *
111      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
112      */
113     static ISimulatorElement*
114     getElementPointerImpl(LegacySimulatorData*                    legacySimulatorData,
115                           ModularSimulatorAlgorithmBuilderHelper* builderHelper,
116                           StatePropagatorData*                    statePropagatorData,
117                           EnergyData*                             energyData,
118                           FreeEnergyPerturbationData gmx_unused* freeEnergyPerturbationData,
119                           GlobalCommunicationHelper gmx_unused* globalCommunicationHelper,
120                           ObservablesReducer*                   observablesReducer,
121                           int                                   offset,
122                           ReportPreviousStepConservedEnergy     reportPreviousStepConservedEnergy);
123
124 private:
125     //! Calculate the scaling matrix
126     template<PressureCoupling pressureCouplingType>
127     void calculateScalingMatrix(Step step);
128     //! Scale the box and coordinates according to the current scaling matrix
129     template<PressureCoupling pressureCouplingType>
130     void scaleBoxAndCoordinates();
131     //! Helper function returning the conserved energy contribution
132     real conservedEnergyContribution(Step step);
133
134     //! The pressure coupling type required
135     const PressureCoupling pressureCouplingType_;
136     //! The coupling time step (simulation time step x coupling frequency)
137     const real couplingTimeStep_;
138     //! The frequency at which pressure coupling is applied
139     const int couplingFrequency_;
140     //! The offset at which pressure coupling is applied
141     const int couplingOffset_;
142
143     //! The current box scaling matrix
144     tensor boxScalingMatrix_;
145     //! Relative box shape
146     tensor boxRel_;
147     //! Contribution to the conserved energy
148     double conservedEnergyContribution_;
149     //! Contribution to the conserved energy
150     double previousStepConservedEnergyContribution_;
151     //! Current step of the conserved energy contribution
152     Step conservedEnergyContributionStep_;
153     //! Whether we're reporting current step or previous step conserved energy
154     const ReportPreviousStepConservedEnergy reportPreviousStepConservedEnergy_;
155
156     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
157     //! Pointer to the micro state
158     StatePropagatorData* statePropagatorData_;
159     //! Pointer to the energy data
160     EnergyData* energyData_;
161
162     // Access to ISimulator data
163     //! Handles logging.
164     FILE* fplog_;
165     //! Contains user input mdp options.
166     const t_inputrec* inputrec_;
167     //! Atom parameters for this domain.
168     const MDAtoms* mdAtoms_;
169     //! Manages flop accounting.
170     t_nrnb* nrnb_;
171
172     //! CheckpointHelper identifier
173     const std::string identifier_;
174     //! Helper function to read from / write to CheckpointData
175     template<CheckpointDataOperation operation>
176     void doCheckpointData(CheckpointData<operation>* checkpointData);
177 };
178
179 } // namespace gmx
180
181 #endif // GMX_MODULARSIMULATOR_FIRSTORDERPRESSURECOUPLING_H