2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 * \brief Declares the free energy perturbation element for the modular simulator
38 * \author Pascal Merz <pascal.merz@me.com>
39 * \ingroup module_modularsimulator
41 * This header is only used within the modular simulator module
44 #ifndef GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H
45 #define GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H
47 #include "gromacs/mdtypes/md_enums.h"
48 #include "gromacs/utility/arrayref.h"
49 #include "gromacs/utility/real.h"
51 #include "modularsimulatorinterfaces.h"
58 enum class CheckpointDataOperation;
60 class GlobalCommunicationHelper;
61 class LegacySimulatorData;
63 class ModularSimulatorAlgorithmBuilderHelper;
64 class StatePropagatorData;
67 * \ingroup module_modularsimulator
68 * \brief The free energy perturbation data
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.
75 class FreeEnergyPerturbationData final
79 FreeEnergyPerturbationData(FILE* fplog, const t_inputrec* inputrec, MDAtoms* mdAtoms);
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)
90 //! The element taking part in the simulator loop
92 //! Get pointer to element (whose lifetime is managed by this)
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();
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);
111 std::unique_ptr<Element> element_;
113 //! The lambda vector
114 std::array<real, efptNR> lambda_;
115 //! The current free energy state
116 int currentFEPState_;
120 //! Contains user input mdp options.
121 const t_inputrec* inputrec_;
122 //! Atom parameters for this domain.
128 * \ingroup module_modularsimulator
129 * \brief The free energy perturbation data element
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
136 class FreeEnergyPerturbationData::Element final : public ISimulatorElement, public ICheckpointHelperClient
140 explicit Element(FreeEnergyPerturbationData* freeEnergyPerturbationElement, double deltaLambda);
142 //! Update lambda and mdatoms
143 void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
145 //! Update the MdAtoms object
146 void elementSetup() override;
148 //! No teardown needed
149 void elementTeardown() override{};
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;
158 /*! \brief Factory method implementation
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
167 * \return Pointer to the element to be added. Element needs to have been stored using \c storeElement
169 static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
170 ModularSimulatorAlgorithmBuilderHelper* builderHelper,
171 StatePropagatorData* statePropagatorData,
172 EnergyData* energyData,
173 FreeEnergyPerturbationData* freeEnergyPerturbationData,
174 GlobalCommunicationHelper* globalCommunicationHelper);
177 //! The free energy data
178 FreeEnergyPerturbationData* freeEnergyPerturbationData_;
179 //! Whether lambda values are non-static
180 const bool lambdasChange_;
185 #endif // GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H