OGS
XdmfHdfWriter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "XdmfHdfWriter.h"
5
6#include <algorithm>
7#include <functional>
8#include <range/v3/algorithm/contains.hpp>
9
10#include "BaseLib/Algorithm.h"
11#include "InfoLib/GitInfo.h"
12#include "partition.h"
13#include "transformData.h"
14#include "writeXdmf.h"
15
16using namespace std::literals;
17
18namespace MeshLib::IO
19{
21{
22 std::vector<double> flattened_geometry_values;
23 std::vector<std::size_t> flattened_topology_values;
25};
26struct XdmfHdfMesh final
27{
30 std::vector<XdmfHdfData> attributes;
31 std::string name;
32 // TransformedMeshData may be large, ensure it is never copied
33 std::unique_ptr<TransformedMeshData> transformed_data;
34};
35
47
48bool isStaticAttribute(std::string const& attribute_name,
49 std::set<std::string> const& static_attribute_names)
50{
51 return ranges::contains(constant_output_names, attribute_name) ||
52 static_attribute_names.contains(attribute_name);
53}
54
55template <typename Data>
56std::function<bool(Data)> isVariableAttribute(
57 std::set<std::string> const& output_variable_names,
58 std::set<std::string> const& static_attribute_names)
59{
60 if (output_variable_names.empty())
61 {
62 return [&static_attribute_names](Data const& data) -> bool
63 { return !isStaticAttribute(data.name, static_attribute_names); };
64 }
65 return [&output_variable_names,
66 &static_attribute_names](Data const& data) -> bool
67 {
68 if (isStaticAttribute(data.name, static_attribute_names))
69 {
70 return false;
71 }
72 return output_variable_names.contains(data.name);
73 };
74}
75
77 std::vector<std::reference_wrapper<const MeshLib::Mesh>> const& meshes,
78 std::filesystem::path const& filepath, unsigned long long const time_step,
79 double const initial_time,
80 std::set<std::string> const& output_variable_names,
81 std::set<std::string> const& static_attribute_names,
82 bool const use_compression, unsigned int const n_files,
83 unsigned int const chunk_size_bytes,
84 bool const store_static_data_separately)
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}
275
276void XdmfHdfWriter::writeStep(double const time)
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}
289
290} // namespace MeshLib::IO
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.
std::unique_ptr< HdfWriter > _hdf_writer
std::vector< std::unique_ptr< XdmfWriter > > _xdmf_writer
std::unique_ptr< HdfWriter > _static_hdf_writer
void writeStep(double time)
Adds data for either lazy (xdmf) or eager (hdf) writing algorithm.
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.
bool isStaticAttribute(std::string const &attribute_name, std::set< std::string > const &static_attribute_names)
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.
constexpr std::array constant_output_names
bool isFileManager()
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
constexpr std::string_view globalIDString(MeshLib::MeshItemType const mesh_item_type)
std::vector< Hdf5DimType > offsets
Definition HdfData.h:25
std::vector< std::size_t > flattened_topology_values
std::vector< double > flattened_geometry_values
std::vector< XdmfHdfData > attributes
std::unique_ptr< TransformedMeshData > transformed_data