| 1 |
/* |
| 2 |
* This file is part of the GROMACS molecular simulation package. |
| 3 |
* |
| 4 |
* Copyright 2025- The GROMACS Authors |
| 5 |
* and the project initiators Erik Lindahl, Berk Hess and David van der Spoel. |
| 6 |
* Consult the AUTHORS/COPYING files and https://www.gromacs.org for details. |
| 7 |
* |
| 8 |
* GROMACS is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public License |
| 10 |
* as published by the Free Software Foundation; either version 2.1 |
| 11 |
* of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* GROMACS is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with GROMACS; if not, see |
| 20 |
* https://www.gnu.org/licenses, or write to the Free Software Foundation, |
| 21 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 |
* |
| 23 |
* If you want to redistribute modifications to GROMACS, please |
| 24 |
* consider that scientific software is very special. Version |
| 25 |
* control is crucial - bugs must be traceable. We will be happy to |
| 26 |
* consider code for inclusion in the official distribution, but |
| 27 |
* derived work must not be called official GROMACS. Details are found |
| 28 |
* in the README & COPYING files - if they are missing, get the |
| 29 |
* official version at https://www.gromacs.org. |
| 30 |
* |
| 31 |
* To help us fund GROMACS development, we humbly ask that you cite |
| 32 |
* the research papers on the package. Check out https://www.gromacs.org. |
| 33 |
*/ |
| 34 |
|
| 35 |
/*! \brief Declarations of H5md particle data block routines. |
| 36 |
* |
| 37 |
* \author Petter Johansson <[email protected]> |
| 38 |
*/ |
| 39 |
|
| 40 |
#ifndef GMX_FILEIO_H5MD_PARTICLEBLOCK_H |
| 41 |
#define GMX_FILEIO_H5MD_PARTICLEBLOCK_H |
| 42 |
|
| 43 |
#include <hdf5.h> |
| 44 |
|
| 45 |
#include <optional> |
| 46 |
|
| 47 |
#include "gromacs/fileio/h5md/h5md_timedatablock.h" |
| 48 |
#include "gromacs/utility/vectypes.h" |
| 49 |
|
| 50 |
namespace gmx |
| 51 |
{ |
| 52 |
|
| 53 |
class H5mdParticleBlockBuilder; |
| 54 |
|
| 55 |
/*! \brief Class which manages trajectory data inside of a H5md particle group. |
| 56 |
* |
| 57 |
* Per the H5md specification, trajectory data for subgroups is stored in |
| 58 |
* the tree hierarchy as: |
| 59 |
* |
| 60 |
* \-- /particles/group1 |
| 61 |
* \-- box |
| 62 |
* \-- edges |
| 63 |
* \-- value: real[numFrames][3][3] |
| 64 |
* \-- position |
| 65 |
* \-- value: RVec[numFrames][numAtoms] |
| 66 |
* \-- step: int64_t[numFrames] |
| 67 |
* \-- (time): double[numFrames] (optional) |
| 68 |
* \-- velocity |
| 69 |
* \-- value: RVec[numFrames][numAtoms] |
| 70 |
* \-- step: int64_t[numFrames] |
| 71 |
* \-- (time): double[numFrames] (optional) |
| 72 |
* \-- force |
| 73 |
* \-- value: RVec[numFrames][numAtoms] |
| 74 |
* \-- step: int64_t[numFrames] |
| 75 |
* \-- (time): double[numFrames] (optional) |
| 76 |
* |
| 77 |
* This class collects data sets for a single group into one object, providing |
| 78 |
* access to \c H5mdTimeDataBlocks<RVec> for position, velocity and force using getters, |
| 79 |
* and a \c H5mdFrameDataSet<real> for the simulation box. |
| 80 |
* |
| 81 |
* Use the \c H5mdParticleBlockBuilder builder class to construct this object |
| 82 |
* by adding selected data sets at will. |
| 83 |
*/ |
| 84 |
class H5mdParticleBlock |
| 85 |
{ |
| 86 |
public: |
| 87 |
// Only make constructors available to our builder class. |
| 88 |
friend class H5mdParticleBlockBuilder; |
| 89 |
|
| 90 |
//! \brief Return whether the block stores a per-position-frame box value, else false. |
| 91 |
bool hasBox() const { return box_.has_value(); } |
| 92 |
|
| 93 |
//! \brief Return whether the block stores a position block. |
| 94 |
bool hasPosition() const { return position_.has_value(); } |
| 95 |
|
| 96 |
//! \brief Return whether the block stores a velocity block. |
| 97 |
bool hasVelocity() const { return velocity_.has_value(); } |
| 98 |
|
| 99 |
//! \brief Return whether the block stores a force block. |
| 100 |
bool hasForce() const { return force_.has_value(); } |
| 101 |
|
| 102 |
//! \brief Return whether time is stored in any of the managed data block sets. |
| 103 |
bool hasTime() const { return hasTime_; } |
| 104 |
|
| 105 |
//! \brief Return the number of particles for the managed blocks (0 if none are managed). |
| 106 |
int64_t numParticles() const { return numParticles_; } |
| 107 |
|
| 108 |
//! \brief Get the simulation box data set. |
| 109 |
std::optional<H5mdFrameDataSet<real>>& box() { return box_; } |
| 110 |
|
| 111 |
//! \brief Get the position data set. |
| 112 |
std::optional<H5mdTimeDataBlock<RVec>>& position() { return position_; } |
| 113 |
|
| 114 |
//! \brief Get the velocity data set. |
| 115 |
std::optional<H5mdTimeDataBlock<RVec>>& velocity() { return velocity_; } |
| 116 |
|
| 117 |
//! \brief Get the force data set. |
| 118 |
std::optional<H5mdTimeDataBlock<RVec>>& force() { return force_; } |
| 119 |
|
| 120 |
//! \brief Trim managed data sets to a maximum of \p maxStep - 1 steps. |
| 121 |
void trimDataSetsFromStep(int64_t maxStep); |
| 122 |
|
| 123 |
private: |
| 124 |
//! \brief Constructor. |
| 125 |
H5mdParticleBlock(std::optional<H5mdTimeDataBlock<RVec>>&& position, |
| 126 |
std::optional<H5mdTimeDataBlock<RVec>>&& velocity, |
| 127 |
std::optional<H5mdTimeDataBlock<RVec>>&& force, |
| 128 |
std::optional<H5mdFrameDataSet<real>>&& box, |
| 129 |
int64_t numParticles); |
| 130 |
|
| 131 |
//! \brief Positions of particles in block (if written to disk) |
| 132 |
std::optional<H5mdTimeDataBlock<RVec>> position_; |
| 133 |
|
| 134 |
//! \brief Velocities of particles in block (if written to disk) |
| 135 |
std::optional<H5mdTimeDataBlock<RVec>> velocity_; |
| 136 |
|
| 137 |
//! \brief Forces of particles in block (if written to disk) |
| 138 |
std::optional<H5mdTimeDataBlock<RVec>> force_; |
| 139 |
|
| 140 |
//! \brief Simulation box (if non-constant over the simulation) |
| 141 |
std::optional<H5mdFrameDataSet<real>> box_; |
| 142 |
|
| 143 |
//! \brief Whether or not any managed data set stores times. |
| 144 |
bool hasTime_; |
| 145 |
|
| 146 |
//! \brief Number of particles for managed data sets. |
| 147 |
int64_t numParticles_; |
| 148 |
}; |
| 149 |
|
| 150 |
/*! \brief Builder class for \c H5mdParticleBlock objects. |
| 151 |
* |
| 152 |
* Asserts that all added data sets are consistent with our expectation |
| 153 |
* for trajectory data. Trajectory data sets must have a 1-dimensional |
| 154 |
* frame dimension (i.e. each frame has shape real[numParticles][3]) |
| 155 |
* and created for the same number of particles. Adding a \c H5mdTimeDataBlock |
| 156 |
* which has inconsistent frame dimensions and number of particles results |
| 157 |
* in a throw. |
| 158 |
* |
| 159 |
* If no data sets are added, the constructed \c H5mdParticleBlock will |
| 160 |
* have numParticles() = 0. In this case no data sets are managed. |
| 161 |
*/ |
| 162 |
class H5mdParticleBlockBuilder |
| 163 |
{ |
| 164 |
public: |
| 165 |
//! Construct a new builder. |
| 166 |
H5mdParticleBlockBuilder() = default; |
| 167 |
|
| 168 |
/*! \brief Assign a per-frame box data set. |
| 169 |
* |
| 170 |
* \throws gmx::FileIOError if the data set frame dimensions are not { 3, 3 }. |
| 171 |
*/ |
| 172 |
H5mdParticleBlockBuilder& setBox(H5mdFrameDataSet<real>&& box); |
| 173 |
|
| 174 |
/*! \brief Assign a position data set. |
| 175 |
* |
| 176 |
* \throws gmx::FileIOError if the value frame dimensions are not 1-dimensional |
| 177 |
* or do not exactly match the frame dimensions of other already added data sets. |
| 178 |
*/ |
| 179 |
H5mdParticleBlockBuilder& setPosition(H5mdTimeDataBlock<RVec>&& position); |
| 180 |
|
| 181 |
/*! \brief Assign a velocity data set. |
| 182 |
* |
| 183 |
* \throws gmx::FileIOError if the value frame dimensions are not 1-dimensional |
| 184 |
* or do not exactly match the frame dimensions of other already added data sets. |
| 185 |
*/ |
| 186 |
H5mdParticleBlockBuilder& setVelocity(H5mdTimeDataBlock<RVec>&& velocity); |
| 187 |
|
| 188 |
/*! \brief Assign a force data set. |
| 189 |
* |
| 190 |
* \throws gmx::FileIOError if the value frame dimensions are not 1-dimensional |
| 191 |
* or do not exactly match the frame dimensions of other already added data sets. |
| 192 |
*/ |
| 193 |
H5mdParticleBlockBuilder& setForce(H5mdTimeDataBlock<RVec>&& force); |
| 194 |
|
| 195 |
//! \brief Build and return the \c H5mdParticleBlock. |
| 196 |
H5mdParticleBlock build(); |
| 197 |
|
| 198 |
private: |
| 199 |
/*! \brief Assert that an \c H5mdTimeDataBlock set \p dataBlock matches others. |
| 200 |
* |
| 201 |
* \note On first invocation this updates numParticles_ to the number of particles |
| 202 |
* in the added \p dataBlock. On subsequent invocations the number of particles |
| 203 |
* in \p dataBlock is compared against this value. Any inconsistency results |
| 204 |
* in a throw. |
| 205 |
* |
| 206 |
* \throws gmx::FileIOError if an inconsistency is found. |
| 207 |
*/ |
| 208 |
void ensureParticleCountConsistency(const H5mdTimeDataBlock<RVec>& dataBlock); |
| 209 |
|
| 210 |
//! \brief Positions of particles in block (if written to disk) |
| 211 |
std::optional<H5mdTimeDataBlock<RVec>> position_; |
| 212 |
|
| 213 |
//! \brief Velocities of particles in block (if written to disk) |
| 214 |
std::optional<H5mdTimeDataBlock<RVec>> velocity_; |
| 215 |
|
| 216 |
//! \brief Forces of particles in block (if written to disk) |
| 217 |
std::optional<H5mdTimeDataBlock<RVec>> force_; |
| 218 |
|
| 219 |
//! \brief Simulation box (if non-constant over the simulation) |
| 220 |
std::optional<H5mdFrameDataSet<real>> box_; |
| 221 |
|
| 222 |
//! \brief Number of particles for managed data sets. |
| 223 |
std::optional<int64_t> numParticles_ = std::nullopt; |
| 224 |
|
| 225 |
GMX_DISALLOW_COPY_MOVE_AND_ASSIGN(H5mdParticleBlockBuilder); |
| 226 |
}; |
| 227 |
|
| 228 |
} // namespace gmx |
| 229 |
|
| 230 |
#endif // GMX_FILEIO_H5MD_PARTICLEBLOCK_H |