25c2a52916e6bfaa2b6430ccfa5205fdd10d1515
[alexxy/gromacs.git] / src / gromacs / modularsimulator / freeenergyperturbationdata.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 /*! \internal \file
36  * \brief Declares the free energy perturbation element 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_FREEENERGYPERTURBATIONELEMENT_H
45 #define GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H
46
47 #include "gromacs/mdtypes/md_enums.h"
48 #include "gromacs/utility/arrayref.h"
49 #include "gromacs/utility/real.h"
50
51 #include "modularsimulatorinterfaces.h"
52
53 struct t_inputrec;
54 struct t_trxframe;
55
56 namespace gmx
57 {
58 enum class CheckpointDataOperation;
59 class EnergyData;
60 class GlobalCommunicationHelper;
61 class LegacySimulatorData;
62 class MDAtoms;
63 class ModularSimulatorAlgorithmBuilderHelper;
64 class StatePropagatorData;
65
66 /*! \internal
67  * \ingroup module_modularsimulator
68  * \brief The free energy perturbation data
69  *
70  * The lambda vector and the current FEP state are held by the
71  * FreeEnergyPerturbationData, offering access to its values via getter
72  * functions. The FreeEnergyPerturbationData::Element is responsible for
73  * lambda update (if applicable) and checkpointing.
74  */
75 class FreeEnergyPerturbationData final
76 {
77 public:
78     //! Constructor
79     FreeEnergyPerturbationData(FILE* fplog, const t_inputrec* inputrec, MDAtoms* mdAtoms);
80
81     //! Get a view of the current lambda vector
82     ArrayRef<real> lambdaView();
83     //! Get a const view of the current lambda vector
84     ArrayRef<const real> constLambdaView();
85     //! Get the current FEP state
86     int currentFEPState();
87     //! Update MDAtoms (public because it's called by DomDec - see #3700)
88     void updateMDAtoms();
89
90     //! The element taking part in the simulator loop
91     class Element;
92     //! Get pointer to element (whose lifetime is managed by this)
93     Element* element();
94
95     //! Read everything that can be stored in t_trxframe from a checkpoint file
96     static void readCheckpointToTrxFrame(t_trxframe*                       trxFrame,
97                                          std::optional<ReadCheckpointData> readCheckpointData);
98     //! CheckpointHelper identifier
99     static const std::string& checkpointID();
100
101 private:
102     //! Default constructor - only used internally
103     FreeEnergyPerturbationData() = default;
104     //! Update the lambda values
105     void updateLambdas(Step step);
106     //! Helper function to read from / write to CheckpointData
107     template<CheckpointDataOperation operation>
108     void doCheckpointData(CheckpointData<operation>* checkpointData);
109
110     //! The element
111     std::unique_ptr<Element> element_;
112
113     //! The lambda vector
114     std::array<real, efptNR> lambda_;
115     //! The current free energy state
116     int currentFEPState_;
117
118     //! Handles logging.
119     FILE* fplog_;
120     //! Contains user input mdp options.
121     const t_inputrec* inputrec_;
122     //! Atom parameters for this domain.
123     MDAtoms* mdAtoms_;
124 };
125
126
127 /*! \internal
128  * \ingroup module_modularsimulator
129  * \brief The free energy perturbation data element
130  *
131  * The FreeEnergyPerturbationData::Element does update the lambda
132  * values during the simulation run if lambda is non-static. It does
133  * implement the checkpointing client interface to save its current
134  * state for restart.
135  */
136 class FreeEnergyPerturbationData::Element final : public ISimulatorElement, public ICheckpointHelperClient
137 {
138 public:
139     //! Constructor
140     explicit Element(FreeEnergyPerturbationData* freeEnergyPerturbationElement, double deltaLambda);
141
142     //! Update lambda and mdatoms
143     void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
144
145     //! Update the MdAtoms object
146     void elementSetup() override;
147
148     //! No teardown needed
149     void elementTeardown() override{};
150
151     //! ICheckpointHelperClient write checkpoint implementation
152     void saveCheckpointState(std::optional<WriteCheckpointData> checkpointData, const t_commrec* cr) override;
153     //! ICheckpointHelperClient read checkpoint implementation
154     void restoreCheckpointState(std::optional<ReadCheckpointData> checkpointData, const t_commrec* cr) override;
155     //! ICheckpointHelperClient key implementation
156     const std::string& clientID() override;
157
158     /*! \brief Factory method implementation
159      *
160      * \param legacySimulatorData  Pointer allowing access to simulator level data
161      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
162      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
163      * \param energyData  Pointer to the \c EnergyData object
164      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
165      * \param globalCommunicationHelper  Pointer to the \c GlobalCommunicationHelper object
166      *
167      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
168      */
169     static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
170                                                     ModularSimulatorAlgorithmBuilderHelper* builderHelper,
171                                                     StatePropagatorData*        statePropagatorData,
172                                                     EnergyData*                 energyData,
173                                                     FreeEnergyPerturbationData* freeEnergyPerturbationData,
174                                                     GlobalCommunicationHelper* globalCommunicationHelper);
175
176 private:
177     //! The free energy data
178     FreeEnergyPerturbationData* freeEnergyPerturbationData_;
179     //! Whether lambda values are non-static
180     const bool lambdasChange_;
181 };
182
183 } // namespace gmx
184
185 #endif // GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H