OGS
MeshLib::IO::XdmfHdfWriter Class Referencefinal

Detailed Description

Definition at line 14 of file XdmfHdfWriter.h.

#include <XdmfHdfWriter.h>

Public Member Functions

 XdmfHdfWriter (std::vector< std::reference_wrapper< const MeshLib::Mesh > > const &meshes, std::filesystem::path const &filepath, unsigned long long time_step, double initial_time, std::set< std::string > const &output_variable_names, std::set< std::string > const &static_attribute_names, bool use_compression, unsigned int n_files, unsigned int chunk_size_bytes, bool store_static_data_separately)
 Write xdmf and h5 file with geometry and topology data.
void writeStep (double time)
 Adds data for either lazy (xdmf) or eager (hdf) writing algorithm.

Private Attributes

std::unique_ptr< HdfWriter_static_hdf_writer
std::unique_ptr< HdfWriter_hdf_writer
std::vector< std::unique_ptr< XdmfWriter > > _xdmf_writer

Constructor & Destructor Documentation

◆ XdmfHdfWriter()

MeshLib::IO::XdmfHdfWriter::XdmfHdfWriter ( std::vector< std::reference_wrapper< const MeshLib::Mesh > > const & meshes,
std::filesystem::path const & filepath,
unsigned long long time_step,
double initial_time,
std::set< std::string > const & output_variable_names,
std::set< std::string > const & static_attribute_names,
bool use_compression,
unsigned int n_files,
unsigned int chunk_size_bytes,
bool store_static_data_separately )

Write xdmf and h5 file with geometry and topology data.

Nomenclature:

OGSXDMF
process variabledynamic_attribute / variable attribute (legacy)
secondary variable, derived variabledynamic attribute / variable attribute (legacy)
general time-dependent data in PropertyVectordynamic attribute / variable attribute (legacy)
mesh nodes / geometrystatic attribute
mesh elements topologystatic attribute
MaterialIDs, bulk IDs, global IDs static attribute
time-independent data in PropertyVector static attribute
Parameters
meshesMeshes or NodePartitionedMeshes to be written to file(s)
filepathabsolute or relative filepath to the hdf5 file
time_stepnumber of the step (temporal collection)
initial_timetime in seconds of the first time step
output_variable_namesnames of PropertyVectors that should be outputted
static_attribute_namessubset of output_variable_name that should be written as static attributes (written once, without a time series) in addition to the always-static built-in names:
  • MaterialIDs
  • bulk_node_ids
  • bulk_element_ids
  • bulk_edge_ids
  • bulk_face_ids
  • global_node_ids
  • global_element_ids
use_compressionif true, zlib compression in HDFWriter component is used
n_filesnumber of hdf5 output files
chunk_size_bytesData will be split into chunks. The parameter specifies the size (in bytes) of the largest chunk.
store_static_data_separatelyIf true, geometry, topology, and constant attributes are written to a separate static HDF5 file (mesh_static.h5). If false, the single-file layout is used.

Definition at line 76 of file XdmfHdfWriter.cpp.

85{
86 // ogs meshes to vector of Xdmf/HDF meshes (we keep Xdmf and HDF together
87 // because XDMF depends on HDF) to meta
88
89 // if no output name is specified, all data will be assumened to be
90 // variable over the timesteps. The xdmfhdfwriter is an alternative to
91 // other writers, that do not consider the constantness of data Callers
92 // of xdmfwriter (e.g. ogs tools) do not provide these information yet
93 // and indicate with empty list
94
95 // Transform the data to be written into a format conforming with the rules
96 // of xdmf topology and geometry
97 auto const transform_ogs_mesh_data_to_xdmf_conforming_data =
98 [&n_files, &chunk_size_bytes](auto const& mesh)
99 {
100 auto flattened_geometry_values = transformToXDMFGeometry(mesh);
101 // actually this line is only needed to calculate the offset
102 XdmfHdfData const& geometry = transformGeometry(
103 mesh, flattened_geometry_values.data(), n_files, chunk_size_bytes);
104 auto const [flattened_topology_values, parent_data_type] =
105 transformToXDMFTopology(mesh, geometry.hdf.offsets[0]);
106 return std::make_unique<TransformedMeshData>(TransformedMeshData{
107 std::move(flattened_geometry_values),
108 std::move(flattened_topology_values), parent_data_type});
109 };
110
111 // create metadata for transformed data and original ogs mesh data
112 auto const transform_to_meta_data =
113 [&output_variable_names,
114 &transform_ogs_mesh_data_to_xdmf_conforming_data, &n_files,
115 &chunk_size_bytes](auto const& mesh)
116 {
117 // important: transformed data must survive and be unique, raw pointer
118 // to its memory!
119 std::unique_ptr<TransformedMeshData> xdmf_conforming_data =
120 transform_ogs_mesh_data_to_xdmf_conforming_data(mesh);
121 auto const geometry = transformGeometry(
122 mesh, xdmf_conforming_data->flattened_geometry_values.data(),
123 n_files, chunk_size_bytes);
124 auto const topology = transformTopology(
125 xdmf_conforming_data->flattened_topology_values,
126 xdmf_conforming_data->parent_data_type, n_files, chunk_size_bytes);
127 auto const attributes = transformAttributes(mesh, output_variable_names,
128 n_files, chunk_size_bytes);
129 return XdmfHdfMesh{std::move(geometry), std::move(topology),
130 std::move(attributes), mesh.get().getName(),
131 std::move(xdmf_conforming_data)};
132 };
133 auto isVariableHdfAttribute = isVariableAttribute<HdfData>(
134 output_variable_names, static_attribute_names);
135
136 // extract meta data relevant for HDFWriter
137 auto const transform_metamesh_to_hdf =
138 [&isVariableHdfAttribute](auto const& metamesh)
139 {
140 // topology and geometry can be treated as any other attribute
141 std::vector<HdfData> hdf_data_attributes = {metamesh.geometry.hdf,
142 metamesh.topology.hdf};
143
144 hdf_data_attributes.reserve(hdf_data_attributes.size() +
145 metamesh.attributes.size());
146 std::transform(metamesh.attributes.begin(), metamesh.attributes.end(),
147 std::back_inserter(hdf_data_attributes),
148 [](XdmfHdfData att) -> HdfData { return att.hdf; });
149
150 HDFAttributes constant_attributes;
151 std::copy_if(hdf_data_attributes.begin(), hdf_data_attributes.end(),
152 back_inserter(constant_attributes),
153 std::not_fn(isVariableHdfAttribute));
154 HDFAttributes variable_attributes;
155 std::copy_if(hdf_data_attributes.begin(), hdf_data_attributes.end(),
156 back_inserter(variable_attributes),
157 isVariableHdfAttribute);
158
159 return MeshHdfData{
160 .constant_attributes = std::move(constant_attributes),
161 .variable_attributes = std::move(variable_attributes),
162 .name = std::move(metamesh.name)};
163 };
164
165 // --------------- XDMF + HDF ---------------------
166 std::vector<XdmfHdfMesh> xdmf_hdf_meshes;
167 xdmf_hdf_meshes.reserve(meshes.size());
168 std::transform(meshes.begin(), meshes.end(),
169 std::back_inserter(xdmf_hdf_meshes), transform_to_meta_data);
170
171 std::vector<MeshHdfData> hdf_meshes;
172 hdf_meshes.reserve(xdmf_hdf_meshes.size());
173 std::transform(xdmf_hdf_meshes.begin(), xdmf_hdf_meshes.end(),
174 std::back_inserter(hdf_meshes), transform_metamesh_to_hdf);
175
176 // --------------- HDF ---------------------
177 std::filesystem::path const hdf_filepath =
178 filepath.parent_path() / (filepath.stem().string() + ".h5");
179 std::filesystem::path const static_hdf_filepath =
180 filepath.parent_path() / (filepath.stem().string() + "_static.h5");
181
182 auto const is_file_manager = isFileManager();
183
184 if (store_static_data_separately)
185 {
186 // Split hdf meshes into static (constant attributes only) and dynamic
187 // (variable attributes only).
188 std::vector<MeshHdfData> static_hdf_meshes;
189 std::vector<MeshHdfData> dynamic_hdf_meshes;
190 for (auto& m : hdf_meshes) // move data out of m
191 {
192 static_hdf_meshes.push_back(MeshHdfData{
193 .constant_attributes = std::move(m.constant_attributes),
194 .variable_attributes = {},
195 .name = m.name});
196 dynamic_hdf_meshes.push_back(MeshHdfData{
197 .constant_attributes = {},
198 .variable_attributes = std::move(m.variable_attributes),
199 .name = std::move(m.name)});
200 }
201
202 _static_hdf_writer = std::make_unique<HdfWriter>(
203 std::move(static_hdf_meshes), time_step, initial_time,
204 static_hdf_filepath, use_compression, is_file_manager, n_files,
205 true);
206 _hdf_writer = std::make_unique<HdfWriter>(
207 std::move(dynamic_hdf_meshes), time_step, initial_time,
208 hdf_filepath, use_compression, is_file_manager, n_files, false);
209 }
210 else
211 {
212 // Backward-compatible single-file mode: all data goes to one file with
213 // time dimension.
214 _hdf_writer = std::make_unique<HdfWriter>(
215 std::move(hdf_meshes), time_step, initial_time, hdf_filepath,
216 use_compression, is_file_manager, n_files, false);
217 }
218
219 // --------------- XDMF ---------------------
220 // The light data is only written by just one process
221 if (!is_file_manager)
222 {
223 return;
224 }
225
226 auto isVariableXdmfAttribute = isVariableAttribute<XdmfData>(
227 output_variable_names, static_attribute_names);
228 // xdmf section
229 // extract meta data relevant for XDMFWriter
230 auto const transform_metamesh_to_xdmf =
231 [&isVariableXdmfAttribute, &filepath, &hdf_filepath,
232 &static_hdf_filepath, &initial_time,
233 store_static_data_separately](XdmfHdfMesh& metamesh)
234 {
235 std::string const xdmf_name = metamesh.name;
236 std::filesystem::path const xdmf_filepath =
237 filepath.parent_path() /
238 (filepath.stem().string() + "_" + xdmf_name + ".xdmf");
239
240 std::vector<XdmfData> xdmf_attributes;
241 std::transform(metamesh.attributes.begin(), metamesh.attributes.end(),
242 std::back_inserter(xdmf_attributes),
243 [](XdmfHdfData const& att) -> XdmfData
244 { return att.xdmf; });
245
246 std::vector<XdmfData> xdmf_variable_attributes;
247 std::copy_if(xdmf_attributes.begin(), xdmf_attributes.end(),
248 back_inserter(xdmf_variable_attributes),
249 isVariableXdmfAttribute);
250 std::vector<XdmfData> xdmf_constant_attributes;
251 std::copy_if(xdmf_attributes.begin(), xdmf_attributes.end(),
252 back_inserter(xdmf_constant_attributes),
253 std::not_fn(isVariableXdmfAttribute));
254
255 // In backward compat mode, constant data also has a time dimension and
256 // shares the dynamic file.
257 auto const static_fn = store_static_data_separately
258 ? static_hdf_filepath.filename().string()
259 : hdf_filepath.filename().string();
260 auto const xdmf_writer_fn =
261 write_xdmf(metamesh.geometry.xdmf, metamesh.topology.xdmf,
262 xdmf_constant_attributes, xdmf_variable_attributes,
263 static_fn, hdf_filepath.filename().string(),
265 auto xdmf_writer = std::make_unique<XdmfWriter>(xdmf_filepath.string(),
266 xdmf_writer_fn);
267 xdmf_writer->addTimeStep(initial_time);
268 return xdmf_writer;
269 };
270
271 std::transform(xdmf_hdf_meshes.begin(), xdmf_hdf_meshes.end(),
272 std::back_inserter(_xdmf_writer),
273 transform_metamesh_to_xdmf);
274}
std::unique_ptr< HdfWriter > _hdf_writer
std::vector< std::unique_ptr< XdmfWriter > > _xdmf_writer
std::unique_ptr< HdfWriter > _static_hdf_writer
GITINFOLIB_EXPORT const std::string ogs_version
std::function< std::string(std::vector< double >)> write_xdmf(XdmfData const &geometry, XdmfData const &topology, std::vector< XdmfData > const &constant_attributes, std::vector< XdmfData > const &variable_attributes, std::string const &static_h5filename, std::string const &dynamic_h5filename, std::string const &ogs_version, std::string const &mesh_name)
Generator function that creates a function capturing the spatial data of a mesh Temporal data can lat...
std::function< bool(Data)> isVariableAttribute(std::set< std::string > const &output_variable_names, std::set< std::string > const &static_attribute_names)
std::vector< HdfData > HDFAttributes
Definition HdfWriter.h:16
std::pair< std::vector< std::size_t >, ParentDataType > transformToXDMFTopology(MeshLib::Mesh const &mesh, std::size_t const offset)
Copies all cells into a new vector. Contiguous data used for writing. The topology is specific to xdm...
std::vector< double > transformToXDMFGeometry(MeshLib::Mesh const &mesh)
Copies all node points into a new vector. Contiguous data used for writing. Conform with XDMF standar...
XdmfHdfData transformGeometry(MeshLib::Mesh const &mesh, double const *data_ptr, unsigned int const n_files, unsigned int const chunk_size_bytes)
Create meta data for geometry used for hdf5 and xdmf.
std::vector< XdmfHdfData > transformAttributes(MeshLib::Mesh const &mesh, std::set< std::string > const &output_variable_names, unsigned int const n_files, unsigned int const chunk_size_bytes)
Create meta data for attributes used for hdf5 and xdmf.
XdmfHdfData transformTopology(std::vector< std::size_t > const &values, ParentDataType const parent_data_type, unsigned int const n_files, unsigned int const chunk_size_bytes)
Create meta data for topology used for HDF5 and XDMF.
bool isFileManager()

References _hdf_writer, _static_hdf_writer, _xdmf_writer, MeshLib::IO::XdmfHdfData::hdf, MeshLib::IO::isFileManager(), MeshLib::IO::isVariableAttribute(), MeshLib::IO::HdfData::offsets, GitInfoLib::GitInfo::ogs_version, MeshLib::IO::transformAttributes(), MeshLib::IO::transformGeometry(), MeshLib::IO::transformTopology(), MeshLib::IO::transformToXDMFGeometry(), MeshLib::IO::transformToXDMFTopology(), and MeshLib::IO::write_xdmf().

Member Function Documentation

◆ writeStep()

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

Adds data for either lazy (xdmf) or eager (hdf) writing algorithm.

Parameters
timetime value of the current time_step

Definition at line 276 of file XdmfHdfWriter.cpp.

277{
278 // ToDo (tm) time_step will be used for simulation continuation (restart)
279 _hdf_writer->writeStep(time);
280 // The light data is only written by just one process
281 if (isFileManager())
282 {
283 for (auto const& xdmf_writer : _xdmf_writer)
284 {
285 xdmf_writer->addTimeStep(time);
286 }
287 }
288}

References _hdf_writer, _xdmf_writer, and MeshLib::IO::isFileManager().

Member Data Documentation

◆ _hdf_writer

std::unique_ptr<HdfWriter> MeshLib::IO::XdmfHdfWriter::_hdf_writer
private

Definition at line 78 of file XdmfHdfWriter.h.

Referenced by XdmfHdfWriter(), and writeStep().

◆ _static_hdf_writer

std::unique_ptr<HdfWriter> MeshLib::IO::XdmfHdfWriter::_static_hdf_writer
private

Definition at line 76 of file XdmfHdfWriter.h.

Referenced by XdmfHdfWriter().

◆ _xdmf_writer

std::vector<std::unique_ptr<XdmfWriter> > MeshLib::IO::XdmfHdfWriter::_xdmf_writer
private

Definition at line 79 of file XdmfHdfWriter.h.

Referenced by XdmfHdfWriter(), and writeStep().


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