21 template <
typename... Args>
27 OGS_FATAL(formatting, std::forward<Args>(args)...);
35 using namespace std::string_literals;
41 if (htri_t avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); !avail)
43 WARN(
"gzip filter not available.\n");
46 unsigned int filter_info;
47 H5Zget_filter_info(H5Z_FILTER_DEFLATE, &filter_info);
48 if (!(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) ||
49 !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED))
51 WARN(
"gzip filter not available for encoding and decoding.\n");
58 Hdf5DimType const prepend_value, std::vector<Hdf5DimType>
const& dimensions)
60 std::vector<Hdf5DimType> dims = {prepend_value};
61 dims.insert(dims.end(), dimensions.begin(), dimensions.end());
66 hid_t
const data_type, std::vector<Hdf5DimType>
const& data_dims,
67 std::vector<Hdf5DimType>
const& max_dims,
68 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
69 bool const use_compression, hid_t
const section,
70 std::string
const& dataset_name)
72 int const time_dim_local_size = data_dims.size() + 1;
74 std::vector<Hdf5DimType>
const time_max_dims =
76 std::vector<Hdf5DimType>
const time_data_global_dims =
79 std::vector<Hdf5DimType>
const time_data_chunk_dims =
83 H5Screate_simple(time_dim_local_size, time_data_global_dims.data(),
84 time_max_dims.data());
87 hid_t
const dcpl = H5Pcreate(H5P_DATASET_CREATE);
91 H5Pset_chunk(dcpl, chunk_dims.size() + 1, time_data_chunk_dims.data());
94 OGS_FATAL(
"H5Pset_layout failed for data set: {:s}.", dataset_name);
102 hid_t
const dataset = H5Dcreate2(section, dataset_name.c_str(), data_type,
103 fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT);
105 assert(dataset >= 0);
107 assert(H5Sclose(fspace) >= 0);
118 void const* nodes_data,
119 hid_t
const data_type,
120 std::vector<Hdf5DimType>
const& data_dims,
121 std::vector<Hdf5DimType>
const& offset_dims,
122 std::vector<Hdf5DimType>
const& max_dims,
123 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
124 std::string
const& dataset_name,
Hdf5DimType const step,
129 std::vector<Hdf5DimType>
const time_data_local_dims = data_dims;
130 std::vector<Hdf5DimType>
const time_max_dims =
132 std::vector<Hdf5DimType>
const time_offsets =
134 std::vector<hsize_t>
const count =
139 hid_t
const mspace = H5Screate_simple(time_data_local_dims.size(),
140 time_data_local_dims.data(), NULL);
141 assert(H5Sselect_all(mspace) >= 0);
143 hid_t status = H5Dset_extent(dataset, time_max_dims.data());
146 OGS_FATAL(
"H5D set extent failed dataset '{:s}'.", dataset_name);
148 hid_t
const fspace = H5Dget_space(dataset);
150 H5Sselect_hyperslab(fspace, H5S_SELECT_SET, time_offsets.data(), NULL,
153 status = H5Dwrite(dataset, data_type, mspace, fspace, io_transfer_property,
157 OGS_FATAL(
"H5Dwrite failed in dataset '{:s}'.", dataset_name);
161 H5Pclose(io_transfer_property);
173 std::vector<double>
const& step_times,
174 bool const is_file_manager)
176 hsize_t
const size = step_times.size();
177 hid_t
const memspace = H5Screate_simple(1, &size, NULL);
178 hid_t
const filespace = H5Screate_simple(1, &size, NULL);
182 H5Sselect_all(memspace);
183 H5Sselect_all(filespace);
187 H5Sselect_none(memspace);
188 H5Sselect_none(filespace);
191 hid_t
const dataset =
192 H5Dcreate2(file,
"/times", H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT,
193 H5P_DEFAULT, H5P_DEFAULT);
195 H5Dwrite(dataset, H5T_NATIVE_DOUBLE, memspace, filespace, H5P_DEFAULT,
213 unsigned long long const initial_step,
214 std::filesystem::path
const& filepath,
215 bool const use_compression,
216 bool const is_file_manager,
217 unsigned int const n_files)
218 : _hdf5_filepath(filepath),
221 H5Gcreate2(_file,
"/meshes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)),
224 _is_file_manager(is_file_manager)
226 for (
auto const& mesh : meshes)
228 hid_t
const group = H5Gcreate2(
_meshes_group, mesh.name.c_str(),
229 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
231 auto const createAndWriteDataSet = [&](
auto const& attribute) -> hid_t
234 attribute.data_type, attribute.data_space, attribute.file_space,
241 attribute.data_space, attribute.offsets,
242 attribute.file_space, attribute.chunk_space,
243 attribute.name, initial_step, dataset);
247 for (
auto const& attribute : mesh.constant_attributes)
249 hid_t
const dataset = createAndWriteDataSet(attribute);
253 std::map<std::string, hid_t> datasets;
254 for (
auto const& attribute : mesh.variable_attributes)
256 hid_t
const dataset = createAndWriteDataSet(attribute);
258 datasets.insert({attribute.name, dataset});
262 HdfMesh{group, mesh.
name, datasets, mesh.variable_attributes}));
272 for (
auto const& dataset : mesh->datasets)
274 H5Dclose(dataset.second);
276 H5Gclose(mesh->group);
289 for (
auto const& attribute : mesh->variable_attributes)
291 auto const& dataset_hid = mesh->datasets.find(attribute.name);
292 if (dataset_hid == mesh->datasets.end())
294 OGS_FATAL(
"Writing HDF5 Dataset: {:s} failed.", attribute.name);
298 attribute.data_start, attribute.data_type, attribute.data_space,
299 attribute.offsets, attribute.file_space, attribute.chunk_space,
300 attribute.name, output_step, mesh->datasets.at(attribute.name));
static std::vector< Hdf5DimType > prependDimension(Hdf5DimType const prepend_value, std::vector< Hdf5DimType > const &dimensions)
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.
static hid_t createDataSet(hid_t const data_type, std::vector< Hdf5DimType > const &data_dims, std::vector< Hdf5DimType > const &max_dims, [[maybe_unused]] 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
void checkHdfStatus(const hid_t status, std::string const &formatting, Args &&... args)
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, [[maybe_unused]] 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.
Writes vectorized data to HDF File.
void WARN(char const *fmt, Args const &... args)
bool const _use_compression
HdfWriter(std::vector< MeshHdfData > meshes, unsigned long long initial_step, 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< 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...
std::vector< double > _step_times
Dispatches HDF5 functions specific to execution platform (w/o MPI). There are multiple implementation...
unsigned long long Hdf5DimType
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