2 * This file is part of the GROMACS molecular simulation package.
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.
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/enumerationhelpers.h"
50 #include "gromacs/utility/real.h"
52 #include "modularsimulatorinterfaces.h"
59 enum class CheckpointDataOperation;
61 class GlobalCommunicationHelper;
62 class LegacySimulatorData;
64 class ModularSimulatorAlgorithmBuilderHelper;
65 class StatePropagatorData;
68 * \ingroup module_modularsimulator
69 * \brief The free energy perturbation data
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.
76 class FreeEnergyPerturbationData final
80 FreeEnergyPerturbationData(FILE* fplog, const t_inputrec& inputrec, MDAtoms* mdAtoms);
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)
91 //! The element taking part in the simulator loop
93 //! Get pointer to element (whose lifetime is managed by this)
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();
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);
110 std::unique_ptr<Element> element_;
112 //! The lambda vector
113 gmx::EnumerationArray<FreeEnergyPerturbationCouplingType, real> lambda_;
114 //! The current free energy state
115 int currentFEPState_;
119 //! Contains user input mdp options.
120 const t_inputrec& inputrec_;
121 //! Atom parameters for this domain.
127 * \ingroup module_modularsimulator
128 * \brief The free energy perturbation data element
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
135 class FreeEnergyPerturbationData::Element final : public ISimulatorElement, public ICheckpointHelperClient
139 explicit Element(FreeEnergyPerturbationData* freeEnergyPerturbationElement, double deltaLambda);
141 //! Update lambda and mdatoms
142 void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
144 //! Update the MdAtoms object
145 void elementSetup() override;
147 //! No teardown needed
148 void elementTeardown() override{};
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;
157 /*! \brief Factory method implementation
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
166 * \return Pointer to the element to be added. Element needs to have been stored using \c storeElement
168 static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
169 ModularSimulatorAlgorithmBuilderHelper* builderHelper,
170 StatePropagatorData* statePropagatorData,
171 EnergyData* energyData,
172 FreeEnergyPerturbationData* freeEnergyPerturbationData,
173 GlobalCommunicationHelper* globalCommunicationHelper);
176 //! The free energy data
177 FreeEnergyPerturbationData* freeEnergyPerturbationData_;
178 //! Whether lambda values are non-static
179 const bool lambdasChange_;
184 #endif // GMX_MODULARSIMULATOR_FREEENERGYPERTURBATIONELEMENT_H