OGS
writeXdmf.cpp
Go to the documentation of this file.
1
10#include "writeXdmf.h"
11
12#include <algorithm>
13#include <array>
14#include <fstream>
15#include <iostream>
16#include <iterator>
17#include <list>
18#include <map>
19#include <optional>
20#include <string_view>
21#include <vector>
22
23#include "BaseLib/Error.h"
24#include "BaseLib/cpp23.h"
25#include "MeshLib/Location.h"
26
27// naming convention for local function objects by postfix:
28// _transform: functions that take data (mostly XDMF meta data) and return
29// transformed data (XDMF string)
30// _fn: functions that take a function and return a new function
31
32using namespace fmt::literals;
33using namespace BaseLib;
34namespace MeshLib::IO
35{
36// similar definition in Location.h - XDMF uses capital letters
37// There is no correspondece for "IntegrationPoint" in XDMF Format
38// specification - add it as "Other"
39constexpr char const* mesh_item_type_strings[] = {"Node", "Edge", "Face",
40 "Cell", "Other"};
41
42// Transforms MeshItemType into string
43static std::string meshItemTypeString(
44 std::optional<MeshItemType> const& item_type)
45{
46 if (item_type)
47 {
48 return mesh_item_type_strings[static_cast<int>(item_type.value())];
49 }
50 OGS_FATAL("Cannot convert an empty optional mesh item type.");
51}
52
53// Transforms MeshPropertyDataType into string
54// constexpr with literal type std::string not yet supported
56{
57 std::array<std::string, to_underlying(MeshPropertyDataType::enum_length)>
58 ogs_to_xdmf_type = {};
59 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::float64)] = "Float";
60 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::float32)] = "Float";
61 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::int32)] = "Int";
62 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::int64)] = "Int";
63 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::uint32)] = "UInt";
64 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::uint64)] = "UInt";
65 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::int8)] = "Int";
66 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::uint8)] = "UInt";
67 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::char_native)] = "Char";
68 ogs_to_xdmf_type[to_underlying(MeshPropertyDataType::uchar)] = "UChar";
69 return ogs_to_xdmf_type;
70}
71
72// Transform MeshPropertyDatatype into string.
73static std::string getPropertyDataTypeString(
74 MeshPropertyDataType const& ogs_data_type)
75{
76 return meshPropertyDatatypeString()[to_underlying(ogs_data_type)];
77}
78
79// Returns MeshPropertyDataType-to-Size (in bytes) lookup-table
80static constexpr auto meshPropertyDatatypeSize()
81{
83 property_sizes{};
86 property_sizes[to_underlying(MeshPropertyDataType::int32)] = 4,
87 property_sizes[to_underlying(MeshPropertyDataType::int64)] = 8,
90 property_sizes[to_underlying(MeshPropertyDataType::int8)] = 1,
91 property_sizes[to_underlying(MeshPropertyDataType::uint8)] = 1;
92 return property_sizes;
93}
94
96
97// Transform MeshPropertyDataType into type_sizes (in bytes)
98static std::string getPropertyDataTypeSize(
99 MeshPropertyDataType const& ogs_data_type)
100{
101 return fmt::format("{}", ogs_to_xdmf_type_fn[to_underlying(ogs_data_type)]);
102}
103
104// Collects all known data and return a function that takes all unknown data
105// known: XdmfData of mesh to be written, unknown: timesteps
106std::function<std::string(std::vector<double>)> write_xdmf(
107 XdmfData const& geometry, XdmfData const& topology,
108 std::vector<XdmfData> const& constant_attributes,
109 std::vector<XdmfData> const& variable_attributes,
110 std::string const& h5filename, std::string const& ogs_version,
111 std::string const& mesh_name)
112{
113 // Generates function that writes <DataItem>. Late binding needed because
114 // maximum number of steps unknown. Time step and h5filename are captured to
115 // return a unary function
116 // _a suffix is a user defined literal for fmt named arguments
117 auto const time_dataitem_genfn =
118 [](unsigned long long const time_step, int const max_step,
119 std::string const& h5filename, std::string const& mesh_name)
120 {
121 return
122 [time_step, max_step, h5filename, mesh_name](auto const& xdmfdata)
123 {
124 return fmt::format(
125 fmt::runtime("\n\t\t<DataItem DataType=\"{datatype}\" "
126 "Dimensions=\"{local_dimensions}\" "
127 "Format=\"HDF\" "
128 "Precision=\"{precision}\">"
129 "{filename}:/meshes/{meshname}/{datasetname}|"
130 "{time_step} {starts}:1 {strides}:1 "
131 "{local_dimensions}:{max_step} "
132 "{global_dimensions}</"
133 "DataItem>"),
134 "datatype"_a = getPropertyDataTypeString(xdmfdata.data_type),
135 "local_dimensions"_a =
136 fmt::join(xdmfdata.global_block_dims, " "),
137 "precision"_a = getPropertyDataTypeSize(xdmfdata.data_type),
138 "filename"_a = h5filename,
139 "meshname"_a = mesh_name,
140 "datasetname"_a = xdmfdata.name,
141 "starts"_a = fmt::join(xdmfdata.starts, " "),
142 "strides"_a = fmt::join(xdmfdata.strides, " "),
143 "global_dimensions"_a =
144 fmt::join(xdmfdata.global_block_dims, " "),
145 "time_step"_a = fmt::format("{}", time_step),
146 "max_step"_a = fmt::format("{}", max_step));
147 };
148 };
149
150 // string_join could be moved to ogs lib if used more
151 auto const string_join_fn = [](auto const& collection)
152 { return fmt::to_string(fmt::join(collection, "")); };
153
154 // m_bind could be moved to ogs lib if used more
155 auto const m_bind_fn = [](auto const& transform, auto const& join)
156 {
157 return [join, transform](auto const& collection)
158 {
159 std::vector<std::string> temp;
160 temp.reserve(collection.size());
161 std::transform(collection.begin(), collection.end(),
162 std::back_inserter(temp), transform);
163 return join(temp);
164 };
165 };
166
167 // XDMF if part of the data that is already written in any previous step
168 // subsequent steps can refer to this data collection of elements navigates
169 // the xml tree (take first child, then in child take 2nd child ...)
170 auto const pointer_transfrom = [](auto const& elements)
171 {
172 return fmt::format(
173 fmt::runtime("\n\t<xi:include xpointer=\"element(/{elements})\"/>"),
174 "elements"_a = fmt::join(elements, "/"));
175 };
176
177 // Defines content of <Attribute> in XDMF, child of Attribute is a single
178 // <DataItem>
179 auto const attribute_transform =
180 [](XdmfData const& attribute, auto const& dataitem_transform)
181 {
182 return fmt::format(
183 "\n\t<Attribute Center=\"{center}\" ElementCell=\"\" "
184 "ElementDegree=\"0\" "
185 "ElementFamily=\"\" ItemType=\"\" Name=\"{name}\" "
186 "Type=\"None\">{dataitem}\n\t</Attribute>",
187 "center"_a = meshItemTypeString(attribute.attribute_center),
188 "name"_a = attribute.name,
189 "dataitem"_a = dataitem_transform(attribute));
190 };
191
192 // Define content of <Geometry> in XDMF, same as attribute_transform
193 auto const geometry_transform =
194 [](XdmfData const& geometry, auto const& dataitem_transform)
195 {
196 return fmt::format(
197 fmt::runtime("\n\t<Geometry Origin=\"\" "
198 "Type=\"XYZ\">{dataitem}\n\t</Geometry>"),
199 "dataitem"_a = dataitem_transform(geometry));
200 };
201
202 auto tag_string = [](auto const& topology, int nodes_per_element,
203 auto const& dataitem_transform)
204 {
205 return fmt::format(
206 fmt::runtime("\n\t<Topology Dimensions=\"{dimensions}\" "
207 "Type=\"{topology_type}\" "
208 "NodesPerElement=\"{nodes_per_element}\">{dataitem}"
209 "\n\t</Topology>"),
210 "topology_type"_a =
212 "dataitem"_a = dataitem_transform(topology),
213 "dimensions"_a = fmt::join(topology.global_block_dims, " "),
214 "nodes_per_element"_a = nodes_per_element);
215 };
216
217 // Define content of <Topology> in XDMF, same as attribute_transform
218 auto const topology_transform =
219 [&tag_string](XdmfData const& topology, auto const& dataitem_transform)
220 {
221 switch (*topology.parent_data_type)
222 {
224 return tag_string(topology, 1, dataitem_transform);
226 return tag_string(topology, 2, dataitem_transform);
244 return fmt::format(
245 fmt::runtime(
246 "\n\t<Topology Dimensions=\"{dimensions}\" "
247 "Type=\"{topology_type}\">{dataitem}\n\t</Topology>"),
248 "topology_type"_a =
250 "dataitem"_a = dataitem_transform(topology),
251 "dimensions"_a =
252 fmt::join(topology.global_block_dims, " "));
253 }
254 OGS_FATAL("Could not transform unknown XDMF topology type");
255 return std::string{};
256 };
257
258 // Defines content of <Grid> of a single time step, takes specific transform
259 // functions for all of its child elements
260 auto const grid_transform =
261 [](double const& time_value, auto const& geometry, auto const& topology,
262 auto const& constant_attributes, auto const& variable_attributes)
263 {
264 return fmt::format(
265 R"(
266<Grid Name="Grid" GridType="Uniform">
267 <Time Value="{time_value:.{precision}g}"/>
268{geometry}
269{topology}
270{fix_attributes}
271{variable_attributes}
272</Grid>)",
273 "time_value"_a = time_value,
274 // Output of "Time Value" with sufficient precision.
275 "precision"_a = std::numeric_limits<double>::max_digits10,
276 "geometry"_a = geometry,
277 "topology"_a = topology,
278 "fix_attributes"_a = constant_attributes,
279 "variable_attributes"_a = variable_attributes);
280 };
281
282 // An attribute may change over time (variable) or stay constant
283 enum class time_attribute
284 {
285 constant,
286 variable
287 };
288
289 // Generates a function that either writes the data or points to existing
290 // data
291 auto const time_step_fn = [time_dataitem_genfn, pointer_transfrom,
292 h5filename,
293 mesh_name](auto const& component_transform,
294 unsigned long long const time_step,
295 int const max_step,
296 time_attribute const variable)
297 {
298 return [component_transform, time_dataitem_genfn, pointer_transfrom,
299 variable, time_step, max_step, h5filename,
300 mesh_name](XdmfData const& attr)
301 {
302 // new data arrived
303 bool const changed =
304 ((time_step > 0 && (variable == time_attribute::variable)) ||
305 time_step == 0);
306 if (changed)
307 {
308 auto dataitem = time_dataitem_genfn(time_step, max_step,
309 h5filename, mesh_name);
310 return component_transform(attr, dataitem);
311 }
312 else
313 {
314 std::array<unsigned int, 5> position = {1, 1, 2, 1, attr.index};
315 return pointer_transfrom(position);
316 };
317 };
318 };
319
320 // Top-Level transform function (take spatial and temporal transform
321 // functions) and return the time depended grid transform function
322 // ToDo (tm) Remove capturing m_bind and string_join as helper function
323
324 auto const time_grid_transform =
325 [time_step_fn, m_bind_fn, string_join_fn, grid_transform,
326 geometry_transform, topology_transform, attribute_transform](
327 double const time, int const step, int const max_step,
328 auto const& geometry, auto const& topology,
329 auto const& constant_attributes, auto const& variable_attributes)
330 {
331 auto const time_step_geometry_transform = time_step_fn(
332 geometry_transform, step, max_step, time_attribute::constant);
333 auto const time_step_topology_transform = time_step_fn(
334 topology_transform, step, max_step, time_attribute::constant);
335 auto const time_step_const_attribute_fn = time_step_fn(
336 attribute_transform, step, max_step, time_attribute::constant);
337 auto const time_step_variable_attribute_fn = time_step_fn(
338 attribute_transform, step, max_step, time_attribute::variable);
339
340 // lift both functions from handling single attributes to work with
341 // collection of attributes
342 auto const variable_attributes_transform =
343 m_bind_fn(time_step_variable_attribute_fn, string_join_fn);
344 auto const constant_attributes_transform =
345 m_bind_fn(time_step_const_attribute_fn, string_join_fn);
346
347 return grid_transform(
348 time, time_step_geometry_transform(geometry),
349 time_step_topology_transform(topology),
350 constant_attributes_transform(constant_attributes),
351 variable_attributes_transform(variable_attributes));
352 };
353
354 // Function that combines all the data from spatial (geometry, topology,
355 // attribute) and temporal domain (time) with the time domain
356 // (time_step_fn). And writes <Grid CollectionType="Temporal">
357 auto const temporal_grid_collection_transform =
358 [time_grid_transform](
359 auto const& times, auto const& geometry, auto const& topology,
360 auto const& constant_attributes, auto const& variable_attributes)
361 {
362 // c++20 ranges zip missing
363 auto const max_step = times.size();
364 std::vector<std::string> grids;
365 grids.reserve(max_step);
366 for (size_t time_step = 0; time_step < max_step; ++time_step)
367 {
368 grids.push_back(time_grid_transform(
369 times[time_step], time_step, max_step, geometry, topology,
370 constant_attributes, variable_attributes));
371 }
372 return fmt::format(
373 fmt::runtime(
374 "\n<Grid CollectionType=\"Temporal\" GridType=\"Collection\" "
375 "Name=\"Collection\">{grids}\n</Grid>\n"),
376 "grids"_a = fmt::join(grids, ""));
377 };
378
379 // Generator function that return a function that represents complete xdmf
380 // structure
381 auto const xdmf_writer_fn = [temporal_grid_collection_transform](
382 auto ogs_version, auto geometry,
383 auto topology, auto constant_attributes,
384 auto variable_attributes)
385 {
386 // This function will be the return of the surrounding named function
387 // capture by copy - it is the function that will survive this scope!!
388 // Use generalized lambda capture to move for optimization
389 return [temporal_grid_collection_transform,
390 ogs_version = std::move(ogs_version),
391 geometry = std::move(geometry), topology = std::move(topology),
392 constant_attributes = std::move(constant_attributes),
393 variable_attributes = std::move(variable_attributes)](
394 std::vector<double> const& times)
395 {
396 return fmt::format(
397 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Xdmf "
398 "xmlns:xi=\"http://www.w3.org/2001/XInclude\" "
399 "Version=\"3.0\">\n<Domain>\n<Information Name=\"OGS_VERSION\" "
400 "Value=\"{ogs_version}\"/>{grid_collection}</Domain>\n</Xdmf>",
401 "ogs_version"_a = ogs_version,
402 "grid_collection"_a = temporal_grid_collection_transform(
403 times, geometry, topology, constant_attributes,
404 variable_attributes));
405 };
406 };
407
408 return xdmf_writer_fn(ogs_version, geometry, topology, constant_attributes,
409 variable_attributes);
410}
411} // namespace MeshLib::IO
#define OGS_FATAL(...)
Definition Error.h:26
std::string ParentDataType2String(ParentDataType p)
MeshPropertyDataType
constexpr auto to_underlying(E e) noexcept
Converts an enumeration to its underlying type.
Definition cpp23.h:29
static std::string getPropertyDataTypeString(MeshPropertyDataType const &ogs_data_type)
Definition writeXdmf.cpp:73
static auto meshPropertyDatatypeString()
Definition writeXdmf.cpp:55
static std::string meshItemTypeString(std::optional< MeshItemType > const &item_type)
Definition writeXdmf.cpp:43
constexpr char const * mesh_item_type_strings[]
Definition writeXdmf.cpp:39
static std::string getPropertyDataTypeSize(MeshPropertyDataType const &ogs_data_type)
Definition writeXdmf.cpp:98
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 &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...
auto ogs_to_xdmf_type_fn
Definition writeXdmf.cpp:95
static constexpr auto meshPropertyDatatypeSize()
Definition writeXdmf.cpp:80
std::optional< MeshLib::MeshItemType > attribute_center
Definition XdmfData.h:67
std::vector< XdmfDimType > global_block_dims
Definition XdmfData.h:64
std::string name
Definition XdmfData.h:66
std::optional< ParentDataType > parent_data_type
Definition XdmfData.h:69
write_xdmf generates a function based on spatial mesh data. The generated function finally generates ...