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