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 Parrinello-Rahman barostat 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_PARRINELLORAHMANBAROSTAT_H
45 #define GMX_MODULARSIMULATOR_PARRINELLORAHMANBAROSTAT_H
47 #include "gromacs/math/vectypes.h"
49 #include "modularsimulatorinterfaces.h"
50 #include "propagator.h"
57 enum class CheckpointDataOperation;
59 class LegacySimulatorData;
61 class ObservablesReducer;
62 class StatePropagatorData;
65 * \ingroup module_modularsimulator
66 * \brief Element implementing the Parrinello-Rahman barostat
69 * * integrates the Parrinello-Rahman box velocity equations,
70 * * takes a callback to the propagator to update the velocity
72 * * scales the box and the positions of the system.
74 class ParrinelloRahmanBarostat final : public ISimulatorElement, public ICheckpointHelperClient, public IEnergySignallerClient
78 ParrinelloRahmanBarostat(int nstpcouple,
80 real couplingTimeStep,
82 StatePropagatorData* statePropagatorData,
83 EnergyData* energyData,
85 const t_inputrec* inputrec,
86 const MDAtoms* mdAtoms);
88 /*! \brief Register run function for step / time
90 * \param step The step number
91 * \param time The time
92 * \param registerRunFunction Function allowing to register a run function
94 void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
96 //! Fix relative box shape
97 void elementSetup() override;
98 //! No element teardown needed
99 void elementTeardown() override {}
101 //! Getter for the box velocities
102 [[nodiscard]] const rvec* boxVelocities() const;
104 //! Connect this to propagator
105 void connectWithMatchingPropagator(const PropagatorConnection& connectionData,
106 const PropagatorTag& propagatorTag);
108 //! ICheckpointHelperClient write checkpoint implementation
109 void saveCheckpointState(std::optional<WriteCheckpointData> checkpointData, const t_commrec* cr) override;
110 //! ICheckpointHelperClient read checkpoint implementation
111 void restoreCheckpointState(std::optional<ReadCheckpointData> checkpointData, const t_commrec* cr) override;
112 //! ICheckpointHelperClient key implementation
113 const std::string& clientID() override;
115 /*! \brief Factory method implementation
117 * \param legacySimulatorData Pointer allowing access to simulator level data
118 * \param builderHelper ModularSimulatorAlgorithmBuilder helper object
119 * \param statePropagatorData Pointer to the \c StatePropagatorData object
120 * \param energyData Pointer to the \c EnergyData object
121 * \param freeEnergyPerturbationData Pointer to the \c FreeEnergyPerturbationData object
122 * \param globalCommunicationHelper Pointer to the \c GlobalCommunicationHelper object
123 * \param observablesReducer Pointer to the \c ObservablesReducer object
124 * \param propagatorTag Tag of the propagator to connect to
125 * \param offset The step offset at which the barostat is applied
127 * \return Pointer to the element to be added. Element needs to have been stored using \c storeElement
129 static ISimulatorElement*
130 getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
131 ModularSimulatorAlgorithmBuilderHelper* builderHelper,
132 StatePropagatorData* statePropagatorData,
133 EnergyData* energyData,
134 FreeEnergyPerturbationData gmx_unused* freeEnergyPerturbationData,
135 GlobalCommunicationHelper gmx_unused* globalCommunicationHelper,
136 ObservablesReducer* observablesReducer,
138 const PropagatorTag& propagatorTag);
141 //! The frequency at which the barostat is applied
142 const int nstpcouple_;
143 //! If != 0, offset the step at which the barostat is applied
145 //! The coupling time step - simulation time step x nstcouple_
146 const real couplingTimeStep_;
147 //! The first step of the simulation
148 const Step initStep_;
150 //! View on the velocity scaling tensor (owned by the propagator)
151 ArrayRef<rvec> scalingTensor_;
152 //! Callback to let propagator know that we updated lambda
153 PropagatorCallback propagatorCallback_;
155 //! Relative change in box before - after barostatting
157 //! Relative box shape
162 //! Current conserved energy contribution
163 real conservedEnergyContribution_;
164 //! Step of current conserved energy contribution
165 Step conservedEnergyContributionStep_;
167 // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
168 //! Pointer to the micro state
169 StatePropagatorData* statePropagatorData_;
170 //! Pointer to the energy data
171 EnergyData* energyData_;
173 //! Integrate the PR box vector equations of motion - does not alter state
174 void integrateBoxVelocityEquations(Step step);
175 //! Scale box and positions
176 void scaleBoxAndPositions();
178 //! CheckpointHelper identifier
179 const std::string identifier_ = "ParrinelloRahmanBarostat";
180 //! Helper function to read from / write to CheckpointData
181 template<CheckpointDataOperation operation>
182 void doCheckpointData(CheckpointData<operation>* checkpointData);
184 //! IEnergySignallerClient implementation
185 std::optional<SignallerCallback> registerEnergyCallback(EnergySignallerEvent event) override;
186 //! The next communicated energy calculation step
187 Step nextEnergyCalculationStep_;
189 //! Contribution to the conserved energy
190 [[nodiscard]] real conservedEnergyContribution() const;
192 // Access to ISimulator data
195 //! Contains user input mdp options.
196 const t_inputrec* inputrec_;
197 //! Atom parameters for this domain.
198 const MDAtoms* mdAtoms_;
203 #endif // GMX_MODULARSIMULATOR_PARRINELLORAHMANBAROSTAT_H