ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
h5md_core.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo 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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "BoxGeometry.hpp"
25#include "ParticleRange.hpp"
27
28#include <utils/Vector.hpp>
29
30#include <boost/mpi/communicator.hpp>
31
32#include <cstddef>
33#include <filesystem>
34#include <memory>
35#include <stdexcept>
36#include <string>
37#include <type_traits>
38#include <unordered_map>
39#include <utility>
40#include <vector>
41
42namespace HighFive {
43class File;
44class DataSet;
45} // namespace HighFive
46
47namespace Writer {
48namespace H5md {
49
50/**
51 * @brief Constants which indicate what to output.
52 * To indicate the output of multiple fields, OR the
53 * corresponding values.
54 */
71
72/**
73 * @brief Class for writing H5MD files.
74 */
75class File {
76public:
77 /**
78 * @brief Constructor.
79 * @param file_path Name for the hdf5 file on disk.
80 * @param script_path Path to the simulation script.
81 * @param output_fields Properties to write to disk.
82 * @param mass_unit The unit for mass.
83 * @param length_unit The unit for length.
84 * @param time_unit The unit for time.
85 * @param force_unit The unit for force.
86 * @param velocity_unit The unit for velocity.
87 * @param charge_unit The unit for charge.
88 * @param chunk_size The chunk size for DataSet in hdf5 file
89 */
90 File(std::filesystem::path file_path, std::filesystem::path script_path,
91 std::vector<std::string> const &output_fields, std::string mass_unit,
92 std::string length_unit, std::string time_unit, std::string force_unit,
93 std::string velocity_unit, std::string charge_unit, int chunk_size);
95
96 /**
97 * @brief Method to perform the renaming of the temporary file from
98 * "filename" + ".bak" to "filename".
99 */
100 void close();
101
102 /**
103 * @brief Write data to the hdf5 file.
104 * @param particles Particle range for which to write data.
105 * @param time Simulation time.
106 * @param step Simulation step (monotonically increasing).
107 * @param geometry The box dimensions.
108 */
109 void write(const ParticleRange &particles, double time, int step,
110 BoxGeometry const &geometry);
111
112 /**
113 * @brief Retrieve the path to the hdf5 file.
114 * @return The path as a file system object.
115 */
116 auto const &file_path() const { return m_file_path; }
117
118 /**
119 * @brief Retrieve the path to the simulation script.
120 * @return The path as a file system object.
121 */
122 auto const &script_path() const { return m_script_path; }
123
124 /**
125 * @brief Retrieve the set mass unit.
126 * @return The unit as a string.
127 */
128 auto const &mass_unit() const { return m_mass_unit; }
129
130 /**
131 * @brief Retrieve the set length unit.
132 * @return The unit as a string.
133 */
134 auto const &length_unit() const { return m_length_unit; }
135
136 /**
137 * @brief Retrieve the set time unit.
138 * @return The unit as a string.
139 */
140 auto const &time_unit() const { return m_time_unit; }
141
142 /**
143 * @brief Retrieve the set force unit.
144 * @return The unit as a string.
145 */
146 auto const &force_unit() const { return m_force_unit; }
147
148 /**
149 * @brief Retrieve the set velocity unit.
150 * @return The unit as a string.
151 */
152 auto const &velocity_unit() const { return m_velocity_unit; }
153
154 /**
155 * @brief Retrieve the set charge unit.
156 * @return The unit as a string.
157 */
158 auto const &charge_unit() const { return m_charge_unit; }
159
160 /**
161 * @brief Retrieve the set chunk size.
162 */
163 auto const &chunk_size() const { return m_chunk_size; }
164
165 /**
166 * @brief Build the list of valid output fields.
167 * @return The list as a vector of strings.
168 */
169 std::vector<std::string> valid_fields() const;
170
171 /**
172 * @brief Method to enforce flushing the buffer to disk.
173 */
174 void flush();
175
176private:
177 /**
178 * @brief Initialize the File object.
179 */
180 void init_file();
181
182 /**
183 * @brief Creates a new H5MD file.
184 */
185 void create_file();
186
187 /**
188 * @brief Loads an existing H5MD file.
189 */
190 void load_file();
191
192 /**
193 * @brief Create the HDF5 groups according to the H5MD specification.
194 */
195 void create_groups();
196
197 /**
198 * @brief Creates the necessary HDF5 datasets according to the H5MD
199 * specification.
200 */
201 void create_datasets();
202
203 /**
204 * @brief Load datasets of the file.
205 */
206 void load_datasets();
207
208 /**
209 * @brief Write the particle bonds (currently only pairs).
210 * @param particles Particle range for which to write bonds.
211 */
212 void write_connectivity(const ParticleRange &particles);
213 /**
214 * @brief Write the unit attributes.
215 */
216 void write_units();
217 /**
218 * @brief Create hard links for the simulation time and simulation step
219 * entries of time-dependent datasets.
220 */
221 void create_hard_links();
222
223 std::filesystem::path m_file_path;
224 std::filesystem::path m_backup_path;
225 std::filesystem::path m_script_path;
226 std::filesystem::path m_absolute_script_path;
227 std::string m_mass_unit;
228 std::string m_length_unit;
229 std::string m_time_unit;
230 std::string m_force_unit;
231 std::string m_velocity_unit;
232 std::string m_charge_unit;
233 int m_chunk_size;
234 boost::mpi::communicator m_comm;
235 unsigned int m_fields;
236 std::unique_ptr<HighFive::File> m_h5md_file;
237 std::unique_ptr<std::unordered_map<std::string, HighFive::DataSet>>
238 m_datasets;
239 Specification m_h5md_specification;
240};
241
242struct incompatible_h5mdfile : public std::exception {
243 const char *what() const noexcept override {
244 return "The given .h5 file does not match the specifications in 'fields'.";
245 }
246};
247
248struct left_backupfile : public std::exception {
249 const char *what() const noexcept override {
250 return "A backup of the .h5 file exists. This usually means that either "
251 "you forgot to call the 'close' method or your simulation crashed.";
252 }
253};
254
255} /* namespace H5md */
256} /* namespace Writer */
Vector implementation and trait types for boost qvm interoperability.
A range of particles.
Class for writing H5MD files.
Definition h5md_core.hpp:75
void write(const ParticleRange &particles, double time, int step, BoxGeometry const &geometry)
Write data to the hdf5 file.
auto const & chunk_size() const
Retrieve the set chunk size.
auto const & length_unit() const
Retrieve the set length unit.
auto const & time_unit() const
Retrieve the set time unit.
void close()
Method to perform the renaming of the temporary file from "filename" + ".bak" to "filename".
auto const & file_path() const
Retrieve the path to the hdf5 file.
auto const & force_unit() const
Retrieve the set force unit.
auto const & mass_unit() const
Retrieve the set mass unit.
auto const & charge_unit() const
Retrieve the set charge unit.
auto const & velocity_unit() const
Retrieve the set velocity unit.
std::vector< std::string > valid_fields() const
Build the list of valid output fields.
auto const & script_path() const
Retrieve the path to the simulation script.
void flush()
Method to enforce flushing the buffer to disk.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
H5MDOutputFields
Constants which indicate what to output.
Definition h5md_core.hpp:55
Layout information for H5MD files.
const char * what() const noexcept override
const char * what() const noexcept override