OGS
MeshLib::IO::HdfWriter Class Referencefinal

Detailed Description

Definition at line 33 of file HdfWriter.h.

#include <HdfWriter.h>

Classes

struct  HdfMesh
 

Public Member Functions

 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 class. The writer assumes the data holder to not change during writing.
 
void writeStep (double time)
 Writes attributes. The data itself is hold by a structure outside of this class. The writer assumes the data holder to not change during writing and HdfData given to constructor to be still valid.
 
 ~HdfWriter ()
 

Private Attributes

std::filesystem::path const _hdf5_filepath
 
hid_t const _file
 
hid_t const _meshes_group
 
std::vector< std::unique_ptr< HdfMesh > > _hdf_meshes
 
std::vector< double > _step_times
 
bool const _use_compression
 
bool const _is_file_manager
 

Constructor & Destructor Documentation

◆ HdfWriter()

MeshLib::IO::HdfWriter::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 class. The writer assumes the data holder to not change during writing.

Parameters
meshesmeta data of meshes to be written
initial_stepnumber of the step (temporal collection), usually 0, greater 0 with continuation of simulation
filepathabsolute or relative filepath to the hdf5 file
use_compressionif true gzip compression is enabled
is_file_managerTrue if process (in parallel execution) is
n_filesNumber of output files

Definition at line 209 of file HdfWriter.cpp.

215 : _hdf5_filepath(filepath),
216 _file(createFile(filepath, n_files)),
218 H5Gcreate2(_file, "/meshes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)),
219 _step_times{0}, // ToDo need to be initial time
220 _use_compression(checkCompression() && use_compression),
221 _is_file_manager(is_file_manager)
222{
223 for (auto const& mesh : meshes)
224 {
225 hid_t const group = H5Gcreate2(_meshes_group, mesh.name.c_str(),
226 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
227
228 auto const createAndWriteDataSet = [&](auto const& attribute) -> hid_t
229 {
230 hid_t const dataset = createDataSet(
231 attribute.data_type, attribute.data_space, attribute.file_space,
232 attribute.chunk_space, _use_compression, group, attribute.name);
233
234 checkHdfStatus(dataset, "Creating HDF5 Dataset: {:s} failed.",
235 attribute.name);
236
237 writeDataSet(attribute.data_start, attribute.data_type,
238 attribute.data_space, attribute.offsets,
239 attribute.file_space, attribute.chunk_space,
240 attribute.name, initial_step, dataset);
241 return dataset;
242 };
243
244 for (auto const& attribute : mesh.constant_attributes)
245 {
246 hid_t const dataset = createAndWriteDataSet(attribute);
247 H5Dclose(dataset);
248 }
249
250 std::map<std::string, hid_t> datasets;
251 for (auto const& attribute : mesh.variable_attributes)
252 {
253 hid_t const dataset = createAndWriteDataSet(attribute);
254 // datasets are kept open
255 datasets.insert({attribute.name, dataset});
256 }
257
258 _hdf_meshes.push_back(std::make_unique<HdfMesh>(
259 HdfMesh{group, datasets, mesh.variable_attributes}));
260 }
261}
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.
void checkHdfStatus(const hid_t status, fmt::format_string< Args... > formatting, Args &&... args)
Definition HdfWriter.cpp:22
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)
Definition HdfWriter.cpp:63
static bool checkCompression()
Definition HdfWriter.cpp:35
bool const _use_compression
Definition HdfWriter.h:73
std::filesystem::path const _hdf5_filepath
Definition HdfWriter.h:68
std::vector< std::unique_ptr< HdfMesh > > _hdf_meshes
Definition HdfWriter.h:71
bool const _is_file_manager
Definition HdfWriter.h:74
hid_t const _meshes_group
Definition HdfWriter.h:70
std::vector< double > _step_times
Definition HdfWriter.h:72
int64_t createFile(std::filesystem::path const &filepath, unsigned int n_files)
Definition fileIO.cpp:38

References _hdf_meshes, _meshes_group, _use_compression, checkHdfStatus(), createDataSet(), MeshLib::IO::HdfWriter::HdfMesh::variable_attributes, and writeDataSet().

◆ ~HdfWriter()

MeshLib::IO::HdfWriter::~HdfWriter ( )

Definition at line 263 of file HdfWriter.cpp.

264{
266
267 for (auto const& mesh : _hdf_meshes)
268 {
269 for (auto const& dataset : mesh->datasets)
270 {
271 H5Dclose(dataset.second);
272 }
273 H5Gclose(mesh->group);
274 }
275 H5Gclose(_meshes_group);
276 H5Fclose(_file);
277}
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.

References _file, _hdf_meshes, _is_file_manager, _meshes_group, _step_times, and writeTimeSeries().

Member Function Documentation

◆ writeStep()

void MeshLib::IO::HdfWriter::writeStep ( double time)

Writes attributes. The data itself is hold by a structure outside of this class. The writer assumes the data holder to not change during writing and HdfData given to constructor to be still valid.

Parameters
timetime_value of step to be written to temporal collection

Definition at line 279 of file HdfWriter.cpp.

280{
281 auto const output_step = _step_times.size();
282 _step_times.push_back(time);
283
284 for (auto const& mesh : _hdf_meshes)
285 {
286 for (auto const& attribute : mesh->variable_attributes)
287 {
288 auto const& dataset_hid = mesh->datasets.find(attribute.name);
289 if (dataset_hid == mesh->datasets.end())
290 {
291 OGS_FATAL("Writing HDF5 Dataset: {:s} failed.", attribute.name);
292 }
293
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));
298 }
299 }
300}
#define OGS_FATAL(...)
Definition Error.h:26

References _hdf_meshes, _step_times, OGS_FATAL, and writeDataSet().

Member Data Documentation

◆ _file

hid_t const MeshLib::IO::HdfWriter::_file
private

Definition at line 69 of file HdfWriter.h.

Referenced by ~HdfWriter().

◆ _hdf5_filepath

std::filesystem::path const MeshLib::IO::HdfWriter::_hdf5_filepath
private

Definition at line 68 of file HdfWriter.h.

◆ _hdf_meshes

std::vector<std::unique_ptr<HdfMesh> > MeshLib::IO::HdfWriter::_hdf_meshes
private

Definition at line 71 of file HdfWriter.h.

Referenced by HdfWriter(), ~HdfWriter(), and writeStep().

◆ _is_file_manager

bool const MeshLib::IO::HdfWriter::_is_file_manager
private

Definition at line 74 of file HdfWriter.h.

Referenced by ~HdfWriter().

◆ _meshes_group

hid_t const MeshLib::IO::HdfWriter::_meshes_group
private

Definition at line 70 of file HdfWriter.h.

Referenced by HdfWriter(), and ~HdfWriter().

◆ _step_times

std::vector<double> MeshLib::IO::HdfWriter::_step_times
private

Definition at line 72 of file HdfWriter.h.

Referenced by ~HdfWriter(), and writeStep().

◆ _use_compression

bool const MeshLib::IO::HdfWriter::_use_compression
private

Definition at line 73 of file HdfWriter.h.

Referenced by HdfWriter().


The documentation for this class was generated from the following files: