15template <
typename... Args>
16void checkHdfStatus(
const hid_t status, fmt::format_string<Args...> formatting,
21 OGS_FATAL(formatting, std::forward<Args>(args)...);
33 if (htri_t avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); !avail)
35 WARN(
"gzip filter not available.\n");
38 unsigned int filter_info;
39 H5Zget_filter_info(H5Z_FILTER_DEFLATE, &filter_info);
40 if (!(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) ||
41 !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED))
43 WARN(
"gzip filter not available for encoding and decoding.\n");
50 Hdf5DimType const prepend_value, std::vector<Hdf5DimType>
const& dimensions)
52 std::vector<Hdf5DimType> dims = {prepend_value};
53 dims.insert(dims.end(), dimensions.begin(), dimensions.end());
58 hid_t
const data_type, std::vector<Hdf5DimType>
const& data_dims,
59 std::vector<Hdf5DimType>
const& max_dims,
60 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
61 bool const use_compression, hid_t
const section,
62 std::string
const& dataset_name)
64 int const time_dim_local_size = data_dims.size() + 1;
66 std::vector<Hdf5DimType>
const time_max_dims =
68 std::vector<Hdf5DimType>
const time_data_global_dims =
71 std::vector<Hdf5DimType>
const time_data_chunk_dims =
75 H5Screate_simple(time_dim_local_size, time_data_global_dims.data(),
76 time_max_dims.data());
79 hid_t
const dcpl = H5Pcreate(H5P_DATASET_CREATE);
83 H5Pset_chunk(dcpl, chunk_dims.size() + 1, time_data_chunk_dims.data());
86 OGS_FATAL(
"H5Pset_layout failed for data set: {:s}.", dataset_name);
94 hid_t
const dataset = H5Dcreate2(section, dataset_name.c_str(), data_type,
95 fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT);
99 assert(H5Sclose(fspace) >= 0);
110 void const* nodes_data,
111 hid_t
const data_type,
112 std::vector<Hdf5DimType>
const& data_dims,
113 std::vector<Hdf5DimType>
const& offset_dims,
114 std::vector<Hdf5DimType>
const& max_dims,
115 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
116 std::string
const& dataset_name,
Hdf5DimType const step,
121 std::vector<Hdf5DimType>
const time_data_local_dims = data_dims;
122 std::vector<Hdf5DimType>
const time_max_dims =
124 std::vector<Hdf5DimType>
const time_offsets =
126 std::vector<hsize_t>
const count =
131 hid_t
const mspace = H5Screate_simple(time_data_local_dims.size(),
132 time_data_local_dims.data(), NULL);
133 assert(H5Sselect_all(mspace) >= 0);
135 hid_t status = H5Dset_extent(dataset, time_max_dims.data());
138 OGS_FATAL(
"H5D set extent failed dataset '{:s}'.", dataset_name);
140 hid_t
const fspace = H5Dget_space(dataset);
142 H5Sselect_hyperslab(fspace, H5S_SELECT_SET, time_offsets.data(), NULL,
145 status = H5Dwrite(dataset, data_type, mspace, fspace, io_transfer_property,
149 OGS_FATAL(
"H5Dwrite failed in dataset '{:s}'.", dataset_name);
153 H5Pclose(io_transfer_property);
165 std::vector<double>
const& step_times,
166 bool const is_file_manager)
168 hsize_t
const size = step_times.size();
169 hid_t
const memspace = H5Screate_simple(1, &size, NULL);
170 hid_t
const filespace = H5Screate_simple(1, &size, NULL);
174 H5Sselect_all(memspace);
175 H5Sselect_all(filespace);
179 H5Sselect_none(memspace);
180 H5Sselect_none(filespace);
183 hid_t
const dataset =
184 H5Dcreate2(file,
"/times", H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT,
185 H5P_DEFAULT, H5P_DEFAULT);
187 H5Dwrite(dataset, H5T_NATIVE_DOUBLE, memspace, filespace, H5P_DEFAULT,
204 unsigned long long const initial_step,
205 double const initial_time,
206 std::filesystem::path
const& filepath,
207 bool const use_compression,
208 bool const is_file_manager,
209 unsigned int const n_files)
213 H5Gcreate2(
_file,
"/meshes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)),
218 for (
auto const& mesh : meshes)
220 hid_t
const group = H5Gcreate2(
_meshes_group, mesh.name.c_str(),
221 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
223 auto const createAndWriteDataSet = [&](
auto const& attribute) -> hid_t
226 attribute.data_type, attribute.data_space, attribute.file_space,
233 attribute.data_space, attribute.offsets,
234 attribute.file_space, attribute.chunk_space,
235 attribute.name, initial_step, dataset);
239 for (
auto const& attribute : mesh.constant_attributes)
241 hid_t
const dataset = createAndWriteDataSet(attribute);
245 std::map<std::string, hid_t> datasets;
246 for (
auto const& attribute : mesh.variable_attributes)
248 hid_t
const dataset = createAndWriteDataSet(attribute);
250 datasets.insert({attribute.name, dataset});
254 HdfMesh{group, datasets, mesh.variable_attributes}));
264 for (
auto const& dataset : mesh->datasets)
266 if (
auto const status = H5Dclose(dataset.second); status < 0)
268 ERR(
"Could not close dataset with id '{}' - status is '{}'.",
269 dataset.second, status);
272 if (
auto const err = H5Gclose(mesh->group); err < 0)
274 ERR(
"Could not close group with group id '{}' - status is '{}'.",
278 if (
auto const group_err = H5Gclose(
_meshes_group); group_err < 0)
280 ERR(
"Could not close group with group id '{}' - status is '{}'.",
283 if (
auto const status = H5Fflush(
_file, H5F_SCOPE_LOCAL); status < 0)
285 ERR(
"Could not flush data to file '{}' - status is '{}'.",
298 for (
auto const& attribute : mesh->variable_attributes)
300 auto const& dataset_hid = mesh->datasets.find(attribute.name);
301 if (dataset_hid == mesh->datasets.end())
303 OGS_FATAL(
"Writing HDF5 Dataset: '{:s}' to file '{}' failed.",
308 attribute.data_start, attribute.data_type, attribute.data_space,
309 attribute.offsets, attribute.file_space, attribute.chunk_space,
310 attribute.name, output_step, mesh->datasets.at(attribute.name));
311 if (
auto const flush_status = H5Fflush(
_file, H5F_SCOPE_LOCAL);
314 ERR(
"HdfWriter::writeStep(): Could not flush to file '{}' - "
static void writeDataSet(void const *nodes_data, hid_t const data_type, std::vector< Hdf5DimType > const &data_dims, std::vector< Hdf5DimType > const &offset_dims, std::vector< Hdf5DimType > const &max_dims, std::vector< Hdf5DimType > const &chunk_dims, std::string const &dataset_name, Hdf5DimType const step, hid_t const dataset)
Assumes a dataset is already opened by createDatasetFunction.
static void writeTimeSeries(hid_t const file, std::vector< double > const &step_times, bool const is_file_manager)
Write vector with time values into open hdf file.
void checkHdfStatus(const hid_t status, fmt::format_string< Args... > formatting, Args &&... args)
static hid_t createDataSet(hid_t const data_type, std::vector< Hdf5DimType > const &data_dims, std::vector< Hdf5DimType > const &max_dims, std::vector< Hdf5DimType > const &chunk_dims, bool const use_compression, hid_t const section, std::string const &dataset_name)
static bool checkCompression()
static unsigned short int const default_compression_factor
static std::vector< Hdf5DimType > prependDimension(Hdf5DimType const prepend_value, std::vector< Hdf5DimType > const &dimensions)
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
bool const _use_compression
std::filesystem::path const _hdf5_filepath
std::vector< std::unique_ptr< HdfMesh > > _hdf_meshes
bool const _is_file_manager
hid_t const _meshes_group
void writeStep(double time)
Writes attributes. The data itself is hold by a structure outside of this class. The writer assumes t...
HdfWriter(std::vector< MeshHdfData > const &meshes, unsigned long long initial_step, double initial_time, std::filesystem::path const &filepath, bool use_compression, bool is_file_manager, unsigned int n_files)
Write file with geometry and topology data. The data itself is held by a structure outside of this cl...
std::vector< double > _step_times
int64_t createHDF5TransferPolicy()
int64_t createFile(std::filesystem::path const &filepath, unsigned int n_files)
std::map< std::string, hid_t > const datasets
std::vector< HdfData > const variable_attributes