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 state 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_STATEPROPAGATORDATA_H
45 #define GMX_MODULARSIMULATOR_STATEPROPAGATORDATA_H
47 #include "gromacs/gpu_utils/hostallocator.h"
48 #include "gromacs/math/paddedvector.h"
49 #include "gromacs/math/vectypes.h"
51 #include "modularsimulatorinterfaces.h"
52 #include "topologyholder.h"
55 enum class PbcType
: int;
63 enum class ConstraintVariable
;
64 class FreeEnergyPerturbationElement
;
67 * \ingroup module_modularsimulator
68 * \brief StatePropagatorData and associated data
70 * The `StatePropagatorData` contains a little more than the pure
71 * statistical-physical micro state, namely the positions,
72 * velocities, forces, and box matrix, as well as a backup of
73 * the positions and box of the last time step. While it takes
74 * part in the simulator loop to be able to backup positions /
75 * boxes and save the current state if needed, it's main purpose
76 * is to offer access to its data via getter methods. All elements
77 * reading or writing to this data need a pointer to the
78 * `StatePropagatorData` and need to request their data explicitly. This
79 * will later simplify the understanding of data dependencies
82 * The `StatePropagatorData` takes part in the simulator run, as it might
83 * have to save a valid state at the right moment during the
84 * integration. Placing the StatePropagatorData correctly is for now the
85 * duty of the simulator builder - this might be automatized later
86 * if we have enough meta-data of the variables (i.e., if
87 * `StatePropagatorData` knows at which time the variables currently are,
88 * and can decide when a valid state (full-time step of all
89 * variables) is reached. The `StatePropagatorData` is also a client of
90 * both the trajectory signaller and writer - it will save a
91 * state for later writeout during the simulator step if it
92 * knows that trajectory writing will occur later in the step,
93 * and it knows how to write to file given a file pointer by
94 * the `TrajectoryElement`.
96 * Note that the `StatePropagatorData` can be converted to and from the
97 * legacy `t_state` object. This is useful when dealing with
98 * functionality which has not yet been adapted to use the new
99 * data approach - of the elements currently implemented, only
100 * domain decomposition, PME load balancing, and the initial
101 * constraining are using this.
103 class StatePropagatorData final
:
104 public ISimulatorElement
,
105 public ITrajectoryWriterClient
,
106 public ITrajectorySignallerClient
,
107 public ICheckpointHelperClient
,
108 public ILastStepSignallerClient
112 StatePropagatorData(int numAtoms
,
115 t_state
* globalState
,
119 int nstxout_compressed
,
121 FreeEnergyPerturbationElement
* freeEnergyPerturbationElement
,
122 const TopologyHolder
* topologyHolder
,
123 bool canMoleculesBeDistributedOverPBC
,
124 bool writeFinalConfiguration
,
125 std::string finalConfigurationFilename
,
126 const t_inputrec
* inputrec
,
127 const t_mdatoms
* mdatoms
);
129 // Allow access to state
130 //! Get write access to position vector
131 ArrayRefWithPadding
<RVec
> positionsView();
132 //! Get read access to position vector
133 ArrayRefWithPadding
<const RVec
> constPositionsView() const;
134 //! Get write access to previous position vector
135 ArrayRefWithPadding
<RVec
> previousPositionsView();
136 //! Get read access to previous position vector
137 ArrayRefWithPadding
<const RVec
> constPreviousPositionsView() const;
138 //! Get write access to velocity vector
139 ArrayRefWithPadding
<RVec
> velocitiesView();
140 //! Get read access to velocity vector
141 ArrayRefWithPadding
<const RVec
> constVelocitiesView() const;
142 //! Get write access to force vector
143 ArrayRefWithPadding
<RVec
> forcesView();
144 //! Get read access to force vector
145 ArrayRefWithPadding
<const RVec
> constForcesView() const;
146 //! Get pointer to box
148 //! Get const pointer to box
149 const rvec
* constBox() const;
150 //! Get pointer to previous box
152 //! Get const pointer to previous box
153 const rvec
* constPreviousBox() const;
154 //! Get the local number of atoms
156 //! Get the total number of atoms
159 /*! \brief Register run function for step / time
161 * This needs to be called during the integration part of the simulator,
162 * at the moment at which the state is at a full time step. Positioning
163 * this element is the responsibility of the programmer writing the
164 * integration algorithm! If the current step is a trajectory writing
165 * step, StatePropagatorData will save a backup for later writeout.
167 * This is also the place at which the current state becomes the previous
170 * @param step The step number
171 * @param time The time
172 * @param registerRunFunction Function allowing to register a run function
174 void scheduleTask(Step step
, Time time
, const RegisterRunFunctionPtr
& registerRunFunction
) override
;
176 /*! \brief Backup starting velocities
178 * This is only needed for vv, where the first (velocity) half step is only
179 * used to compute the constraint virial, but the velocities need to be reset
181 * TODO: There must be a more elegant solution to this!
183 void elementSetup() override
;
185 //! No element teardown needed
186 void elementTeardown() override
{}
189 // (doxygen doesn't like these)
190 // Classes which need access to legacy state
191 friend class DomDecHelper
;
195 //! The total number of atoms in the system
197 //! The position writeout frequency
199 //! The velocity writeout frequency
201 //! The force writeout frequency
203 //! The compressed position writeout frequency
204 int nstxout_compressed_
;
206 //! The local number of atoms
208 //! The position vector
209 PaddedHostVector
<RVec
> x_
;
210 //! The position vector of the previous step
211 PaddedHostVector
<RVec
> previousX_
;
212 //! The velocity vector
213 PaddedHostVector
<RVec
> v_
;
215 PaddedHostVector
<RVec
> f_
;
218 //! The box matrix of the previous step
220 //! The DD partitioning count for legacy t_state compatibility
223 //! Move x_ to previousX_
225 //! OMP helper to move x_ to previousX_
226 void copyPosition(int start
, int end
);
228 // Access to legacy state
229 //! Get a deep copy of the current state in legacy format
230 std::unique_ptr
<t_state
> localState();
231 //! Update the current state with a state in legacy format
232 void setLocalState(std::unique_ptr
<t_state
> state
);
233 //! Get a pointer to the global state
234 t_state
* globalState();
235 //! Get a force pointer
236 PaddedHostVector
<gmx::RVec
>* forcePointer();
238 //! Pointer to keep a backup of the state for later writeout
239 std::unique_ptr
<t_state
> localStateBackup_
;
240 //! Step at which next writeout occurs
242 //! Backup current state
245 //! ITrajectorySignallerClient implementation
246 SignallerCallbackPtr
registerTrajectorySignallerCallback(TrajectoryEvent event
) override
;
248 //! ITrajectoryWriterClient implementation
249 ITrajectoryWriterCallbackPtr
registerTrajectoryWriterCallback(TrajectoryEvent event
) override
;
251 //! ICheckpointHelperClient implementation
252 void writeCheckpoint(t_state
* localState
, t_state
* globalState
) override
;
254 //! ILastStepSignallerClient implementation (used for final output only)
255 SignallerCallbackPtr
registerLastStepCallback() override
;
257 //! Callback writing the state to file
258 void write(gmx_mdoutf
* outf
, Step step
, Time time
);
260 //! Whether we're doing VV and need to reset velocities after the first half step
261 bool vvResetVelocities_
;
262 //! Velocities backup for VV
263 PaddedHostVector
<RVec
> velocityBackup_
;
264 //! Function resetting the velocities
265 void resetVelocities();
267 //! Pointer to the free energy perturbation element (for trajectory writing only)
268 FreeEnergyPerturbationElement
* freeEnergyPerturbationElement_
;
270 //! Whether planned total number of steps was reached (used for final output only)
271 bool isRegularSimulationEnd_
;
272 //! The signalled last step (used for final output only)
275 //! Whether system can have molecules distributed over PBC boundaries (used for final output only)
276 const bool canMoleculesBeDistributedOverPBC_
;
277 //! Whether system has molecules self-interacting through PBC (used for final output only)
278 const bool systemHasPeriodicMolecules_
;
279 //! The PBC type (used for final output only)
280 const PbcType pbcType_
;
281 //! Pointer to the topology (used for final output only)
282 const TopologyHolder
* topologyHolder_
;
283 //! The (planned) last step - determines whether final configuration is written (used for final output only)
284 const Step lastPlannedStep_
;
285 //! Whether final configuration was chosen in mdrun options (used for final output only)
286 const bool writeFinalConfiguration_
;
287 //! The filename of the final configuration file (used for final output only)
288 const std::string finalConfigurationFilename_
;
290 // Access to ISimulator data
293 //! Handles communication.
294 const t_commrec
* cr_
;
295 //! Full simulation state (only non-nullptr on master rank).
296 t_state
* globalState_
;
298 //! No trajectory writer setup needed
299 void trajectoryWriterSetup(gmx_mdoutf gmx_unused
* outf
) override
{}
300 //! Trajectory writer teardown - write final coordinates
301 void trajectoryWriterTeardown(gmx_mdoutf
* outf
) override
;
306 #endif // GMX_MODULARSIMULATOR_STATEPROPAGATORDATA_H