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