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