Introduce plumbing for ObservablesReducer
[alexxy/gromacs.git] / src / gromacs / modularsimulator / parrinellorahmanbarostat.h
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 Declares the Parrinello-Rahman barostat 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_PARRINELLORAHMANBAROSTAT_H
45 #define GMX_MODULARSIMULATOR_PARRINELLORAHMANBAROSTAT_H
46
47 #include "gromacs/math/vectypes.h"
48
49 #include "modularsimulatorinterfaces.h"
50 #include "propagator.h"
51
52 struct t_inputrec;
53 struct t_commrec;
54
55 namespace gmx
56 {
57 enum class CheckpointDataOperation;
58 class EnergyData;
59 class LegacySimulatorData;
60 class MDAtoms;
61 class ObservablesReducer;
62 class StatePropagatorData;
63
64 /*! \internal
65  * \ingroup module_modularsimulator
66  * \brief Element implementing the Parrinello-Rahman barostat
67  *
68  * This element
69  *   * integrates the Parrinello-Rahman box velocity equations,
70  *   * takes a callback to the propagator to update the velocity
71  *     scaling factor, and
72  *   * scales the box and the positions of the system.
73  */
74 class ParrinelloRahmanBarostat final : public ISimulatorElement, public ICheckpointHelperClient, public IEnergySignallerClient
75 {
76 public:
77     //! Constructor
78     ParrinelloRahmanBarostat(int                  nstpcouple,
79                              int                  offset,
80                              real                 couplingTimeStep,
81                              Step                 initStep,
82                              StatePropagatorData* statePropagatorData,
83                              EnergyData*          energyData,
84                              FILE*                fplog,
85                              const t_inputrec*    inputrec,
86                              const MDAtoms*       mdAtoms);
87
88     /*! \brief Register run function for step / time
89      *
90      * \param step                 The step number
91      * \param time                 The time
92      * \param registerRunFunction  Function allowing to register a run function
93      */
94     void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
95
96     //! Fix relative box shape
97     void elementSetup() override;
98     //! No element teardown needed
99     void elementTeardown() override {}
100
101     //! Getter for the box velocities
102     [[nodiscard]] const rvec* boxVelocities() const;
103
104     //! Connect this to propagator
105     void connectWithMatchingPropagator(const PropagatorConnection& connectionData,
106                                        const PropagatorTag&        propagatorTag);
107
108     //! ICheckpointHelperClient write checkpoint implementation
109     void saveCheckpointState(std::optional<WriteCheckpointData> checkpointData, const t_commrec* cr) override;
110     //! ICheckpointHelperClient read checkpoint implementation
111     void restoreCheckpointState(std::optional<ReadCheckpointData> checkpointData, const t_commrec* cr) override;
112     //! ICheckpointHelperClient key implementation
113     const std::string& clientID() override;
114
115     /*! \brief Factory method implementation
116      *
117      * \param legacySimulatorData  Pointer allowing access to simulator level data
118      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
119      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
120      * \param energyData  Pointer to the \c EnergyData object
121      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
122      * \param globalCommunicationHelper   Pointer to the \c GlobalCommunicationHelper object
123      * \param observablesReducer          Pointer to the \c ObservablesReducer object
124      * \param propagatorTag  Tag of the propagator to connect to
125      * \param offset  The step offset at which the barostat is applied
126      *
127      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
128      */
129     static ISimulatorElement*
130     getElementPointerImpl(LegacySimulatorData*                    legacySimulatorData,
131                           ModularSimulatorAlgorithmBuilderHelper* builderHelper,
132                           StatePropagatorData*                    statePropagatorData,
133                           EnergyData*                             energyData,
134                           FreeEnergyPerturbationData gmx_unused* freeEnergyPerturbationData,
135                           GlobalCommunicationHelper gmx_unused* globalCommunicationHelper,
136                           ObservablesReducer*                   observablesReducer,
137                           Offset                                offset,
138                           const PropagatorTag&                  propagatorTag);
139
140 private:
141     //! The frequency at which the barostat is applied
142     const int nstpcouple_;
143     //! If != 0, offset the step at which the barostat is applied
144     const int offset_;
145     //! The coupling time step - simulation time step x nstcouple_
146     const real couplingTimeStep_;
147     //! The first step of the simulation
148     const Step initStep_;
149
150     //! View on the velocity scaling tensor (owned by the propagator)
151     ArrayRef<rvec> scalingTensor_;
152     //! Callback to let propagator know that we updated lambda
153     PropagatorCallback propagatorCallback_;
154
155     //! Relative change in box before - after barostatting
156     matrix mu_;
157     //! Relative box shape
158     tensor boxRel_;
159     //! Box velocity
160     tensor boxVelocity_;
161
162     //! Current conserved energy contribution
163     real conservedEnergyContribution_;
164     //! Step of current conserved energy contribution
165     Step conservedEnergyContributionStep_;
166
167     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
168     //! Pointer to the micro state
169     StatePropagatorData* statePropagatorData_;
170     //! Pointer to the energy data
171     EnergyData* energyData_;
172
173     //! Integrate the PR box vector equations of motion - does not alter state
174     void integrateBoxVelocityEquations(Step step);
175     //! Scale box and positions
176     void scaleBoxAndPositions();
177
178     //! CheckpointHelper identifier
179     const std::string identifier_ = "ParrinelloRahmanBarostat";
180     //! Helper function to read from / write to CheckpointData
181     template<CheckpointDataOperation operation>
182     void doCheckpointData(CheckpointData<operation>* checkpointData);
183
184     //! IEnergySignallerClient implementation
185     std::optional<SignallerCallback> registerEnergyCallback(EnergySignallerEvent event) override;
186     //! The next communicated energy calculation step
187     Step nextEnergyCalculationStep_;
188
189     //! Contribution to the conserved energy
190     [[nodiscard]] real conservedEnergyContribution() const;
191
192     // Access to ISimulator data
193     //! Handles logging.
194     FILE* fplog_;
195     //! Contains user input mdp options.
196     const t_inputrec* inputrec_;
197     //! Atom parameters for this domain.
198     const MDAtoms* mdAtoms_;
199 };
200
201 } // namespace gmx
202
203 #endif // GMX_MODULARSIMULATOR_PARRINELLORAHMANBAROSTAT_H