21template <
typename... Args>
22void checkHdfStatus(
const hid_t status, fmt::format_string<Args...> formatting,
27 OGS_FATAL(formatting, std::forward<Args>(args)...);
39 if (htri_t avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); !avail)
41 WARN(
"gzip filter not available.\n");
44 unsigned int filter_info;
45 H5Zget_filter_info(H5Z_FILTER_DEFLATE, &filter_info);
46 if (!(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) ||
47 !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED))
49 WARN(
"gzip filter not available for encoding and decoding.\n");
56 Hdf5DimType const prepend_value, std::vector<Hdf5DimType>
const& dimensions)
58 std::vector<Hdf5DimType> dims = {prepend_value};
59 dims.insert(dims.end(), dimensions.begin(), dimensions.end());
64 hid_t
const data_type, std::vector<Hdf5DimType>
const& data_dims,
65 std::vector<Hdf5DimType>
const& max_dims,
66 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
67 bool const use_compression, hid_t
const section,
68 std::string
const& dataset_name)
70 int const time_dim_local_size = data_dims.size() + 1;
72 std::vector<Hdf5DimType>
const time_max_dims =
74 std::vector<Hdf5DimType>
const time_data_global_dims =
77 std::vector<Hdf5DimType>
const time_data_chunk_dims =
81 H5Screate_simple(time_dim_local_size, time_data_global_dims.data(),
82 time_max_dims.data());
85 hid_t
const dcpl = H5Pcreate(H5P_DATASET_CREATE);
89 H5Pset_chunk(dcpl, chunk_dims.size() + 1, time_data_chunk_dims.data());
92 OGS_FATAL(
"H5Pset_layout failed for data set: {:s}.", dataset_name);
100 hid_t
const dataset = H5Dcreate2(section, dataset_name.c_str(), data_type,
101 fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT);
103 assert(dataset >= 0);
105 assert(H5Sclose(fspace) >= 0);
116 void const* nodes_data,
117 hid_t
const data_type,
118 std::vector<Hdf5DimType>
const& data_dims,
119 std::vector<Hdf5DimType>
const& offset_dims,
120 std::vector<Hdf5DimType>
const& max_dims,
121 [[maybe_unused]] std::vector<Hdf5DimType>
const& chunk_dims,
122 std::string
const& dataset_name,
Hdf5DimType const step,
127 std::vector<Hdf5DimType>
const time_data_local_dims = data_dims;
128 std::vector<Hdf5DimType>
const time_max_dims =
130 std::vector<Hdf5DimType>
const time_offsets =
132 std::vector<hsize_t>
const count =
137 hid_t
const mspace = H5Screate_simple(time_data_local_dims.size(),
138 time_data_local_dims.data(), NULL);
139 assert(H5Sselect_all(mspace) >= 0);
141 hid_t status = H5Dset_extent(dataset, time_max_dims.data());
144 OGS_FATAL(
"H5D set extent failed dataset '{:s}'.", dataset_name);
146 hid_t
const fspace = H5Dget_space(dataset);
148 H5Sselect_hyperslab(fspace, H5S_SELECT_SET, time_offsets.data(), NULL,
151 status = H5Dwrite(dataset, data_type, mspace, fspace, io_transfer_property,
155 OGS_FATAL(
"H5Dwrite failed in dataset '{:s}'.", dataset_name);
159 H5Pclose(io_transfer_property);
171 std::vector<double>
const& step_times,
172 bool const is_file_manager)
174 hsize_t
const size = step_times.size();
175 hid_t
const memspace = H5Screate_simple(1, &size, NULL);
176 hid_t
const filespace = H5Screate_simple(1, &size, NULL);
180 H5Sselect_all(memspace);
181 H5Sselect_all(filespace);
185 H5Sselect_none(memspace);
186 H5Sselect_none(filespace);
189 hid_t
const dataset =
190 H5Dcreate2(file,
"/times", H5T_NATIVE_DOUBLE, filespace, H5P_DEFAULT,
191 H5P_DEFAULT, H5P_DEFAULT);
193 H5Dwrite(dataset, H5T_NATIVE_DOUBLE, memspace, filespace, H5P_DEFAULT,
210 unsigned long long const initial_step,
211 std::filesystem::path
const& filepath,
212 bool const use_compression,
213 bool const is_file_manager,
214 unsigned int const n_files)
215 : _hdf5_filepath(filepath),
218 H5Gcreate2(_file,
"/meshes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)),
221 _is_file_manager(is_file_manager)
223 for (
auto const& mesh : meshes)
225 hid_t
const group = H5Gcreate2(
_meshes_group, mesh.name.c_str(),
226 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
228 auto const createAndWriteDataSet = [&](
auto const& attribute) -> hid_t
231 attribute.data_type, attribute.data_space, attribute.file_space,
238 attribute.data_space, attribute.offsets,
239 attribute.file_space, attribute.chunk_space,
240 attribute.name, initial_step, dataset);
244 for (
auto const& attribute : mesh.constant_attributes)
246 hid_t
const dataset = createAndWriteDataSet(attribute);
250 std::map<std::string, hid_t> datasets;
251 for (
auto const& attribute : mesh.variable_attributes)
253 hid_t
const dataset = createAndWriteDataSet(attribute);
255 datasets.insert({attribute.name, dataset});
259 HdfMesh{group, datasets, mesh.variable_attributes}));
269 for (
auto const& dataset : mesh->datasets)
271 H5Dclose(dataset.second);
273 H5Gclose(mesh->group);
286 for (
auto const& attribute : mesh->variable_attributes)
288 auto const& dataset_hid = mesh->datasets.find(attribute.name);
289 if (dataset_hid == mesh->datasets.end())
291 OGS_FATAL(
"Writing HDF5 Dataset: {:s} failed.", attribute.name);
295 attribute.data_start, attribute.data_type, attribute.data_space,
296 attribute.offsets, attribute.file_space, attribute.chunk_space,
297 attribute.name, output_step, mesh->datasets.at(attribute.name));
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)
Writes vectorized data to HDF File.
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
bool const _use_compression
HdfWriter(std::vector< MeshHdfData > const &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...
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