OGS
transformData.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 "transformData.h"
5
6#include <algorithm>
7#include <array>
8#include <optional>
9#include <range/v3/action/push_back.hpp>
10#include <range/v3/view/transform.hpp>
11#include <string>
12
13#include "BaseLib/cpp23.h"
14#if defined(USE_PETSC)
15#include "BaseLib/MPI.h"
16#endif
17#include "InfoLib/GitInfo.h"
19#include "MeshLib/Mesh.h"
20#include "MeshLib/MeshEnums.h"
21#include "MeshLib/Node.h"
23#include "partition.h"
24
25using namespace BaseLib;
26namespace MeshLib::IO
27{
29{
30 // https://xdmf.org/index.php/XDMF_Model_and_Format#Topology, Section
31 // Arbitrary
33 unsigned int number_of_nodes;
34};
35
36static constexpr auto elemOGSTypeToXDMFType()
37{
39 elem_type{};
67 ParentDataType::WEDGE, 6}; // parallel triangle wedge
76 return elem_type;
77}
78
80
81constexpr auto cellTypeOGS2XDMF(MeshLib::CellType const& cell_type)
82{
83 return elem_type_ogs2xdmf[to_underlying(cell_type)];
84}
85
86std::optional<XdmfHdfData> transformAttribute(
87 std::pair<std::string, PropertyVectorBase*> const& property_pair,
88 unsigned int const n_files, unsigned int const chunk_size_bytes)
89{
90 // 3 data that will be captured and written by lambda f below
92 std::size_t num_of_tuples = 0;
93 void const* data_ptr = 0;
94
95 // lambda f : Collects properties from the PropertyVectorBase. It captures
96 // (and overwrites) data that can only be collected via the typed property.
97 // It has boolean return type to allow kind of pipe using || operator.
98 auto f = [&data_type, &num_of_tuples, &data_ptr,
99 &property_pair](auto basic_type) -> bool
100 {
101 auto const property_base = property_pair.second;
102 auto const typed_property =
103 dynamic_cast<PropertyVector<decltype(basic_type)> const*>(
104 property_base);
105 if (typed_property == nullptr)
106 {
107 return false;
108 }
109 // overwrite captured data
110 num_of_tuples = typed_property->getNumberOfTuples();
111 data_ptr = typed_property->data();
112
113 if constexpr (std::is_same_v<double, decltype(basic_type)>)
114 {
115 // The standard 64-bit IEEE 754 floating-point type
116 // (double-precision) has a 53 bit fractional part (52 bits written,
117 // one implied)
118 static_assert((std::numeric_limits<double>::digits == 53),
119 "Double has 52 bits fractional part");
121 }
122 else if constexpr (std::is_same_v<float, decltype(basic_type)>)
123 {
124 // The standard 32-bit IEEE 754 floating-point type
125 // (single-precision) has a 24 bit fractional part (23 bits written,
126 // one implied)
127 static_assert((std::numeric_limits<float>::digits == 24),
128 "Float has 23 bits fractional part");
130 }
131 else if constexpr (std::is_same_v<int32_t, decltype(basic_type)>)
132 {
133 data_type = MeshPropertyDataType::int32;
134 }
135 else if constexpr (std::is_same_v<uint32_t, decltype(basic_type)>)
136 {
138 }
139 else if constexpr (std::is_same_v<int64_t, decltype(basic_type)>)
140 {
141 data_type = MeshPropertyDataType::int64;
142 }
143 else if constexpr (std::is_same_v<uint64_t, decltype(basic_type)>)
144 {
146 }
147 else if constexpr (std::is_same_v<int8_t, decltype(basic_type)>)
148 {
149 data_type = MeshPropertyDataType::int8;
150 }
151 else if constexpr (std::is_same_v<uint8_t, decltype(basic_type)>)
152 {
153 data_type = MeshPropertyDataType::uint8;
154 }
155 else if constexpr (std::is_same_v<char, decltype(basic_type)>)
156 {
158 }
159 else if constexpr (std::is_same_v<unsigned char, decltype(basic_type)>)
160 {
161 static_assert((std::numeric_limits<unsigned char>::digits == 8),
162 "Unsigned char has 8 bits");
163 data_type = MeshPropertyDataType::uchar;
164 }
165 else if constexpr (std::is_same_v<unsigned long, decltype(basic_type)>)
166 {
167 if (sizeof(unsigned long) == 8 &&
168 std::numeric_limits<unsigned long>::digits == 64)
169 {
171 }
172 else if (sizeof(unsigned long) == 4 &&
173 std::numeric_limits<unsigned long>::digits == 32)
174 {
176 }
177 else
178 {
179 return false;
180 }
181 }
182 else if constexpr (std::is_same_v<std::size_t, decltype(basic_type)>)
183 {
184 if (sizeof(std::size_t) == 8 &&
185 std::numeric_limits<std::size_t>::digits == 64)
186 {
188 }
189 else if (sizeof(std::size_t) == 4 &&
190 std::numeric_limits<std::size_t>::digits == 32)
191 {
193 }
194 else
195 {
196 return false;
197 }
198 }
199 else
200 {
201 return false;
202 }
203 return true;
204 };
205
206 f(double{}) || f(float{}) || f(int{}) || f(long{}) || f(unsigned{}) ||
207 f(long{}) || f(static_cast<unsigned long>(0)) || f(std::size_t{}) ||
208 f(char{}) || f(static_cast<unsigned char>(0));
209
210 if (data_type == MeshPropertyDataType::unknown)
211 {
212 return std::nullopt;
213 }
214
215 auto const& property_base = property_pair.second;
216 auto const& global_components =
217 property_base->getNumberOfGlobalComponents();
218 // TODO (tm) property_pair vector::getNumberOfGlobalComponents should return
219 // unsigned value. Then explicit cast from signed to unsigned int and
220 // assert can be removed here. Implicit cast to long long is fine and
221 // can be kept
222 assert(global_components >= 0);
223 auto const ui_global_components =
224 static_cast<unsigned int>(global_components);
225
226 MeshLib::MeshItemType const mesh_item_type =
227 property_base->getMeshItemType();
228
229 std::string const& name = property_base->getPropertyName();
230
231 HdfData hdf = {data_ptr, num_of_tuples, ui_global_components, name,
232 data_type, n_files, chunk_size_bytes};
233
234 XdmfData xdmf{num_of_tuples, ui_global_components, data_type,
235 name, mesh_item_type, n_files,
236 std::nullopt};
237
238 return XdmfHdfData{std::move(hdf), std::move(xdmf)};
239}
240
241std::vector<XdmfHdfData> transformAttributes(
242 MeshLib::Mesh const& mesh, unsigned int const n_files,
243 unsigned int const chunk_size_bytes)
244{
245 MeshLib::Properties const& properties = mesh.getProperties();
246
247 // \TODO (tm) use c++20 ranges
248 // a = p | filter (first!=OGS_VERSION) | filter null_opt | transformAttr |
249 std::vector<XdmfHdfData> attributes;
250 for (auto const& [name, property_base] : properties)
251 {
253 {
254 continue;
255 }
256
257 if (!property_base->is_for_output)
258 {
259 continue;
260 }
261
262 if (auto const attribute = transformAttribute(
263
264 std::pair(std::string(name), property_base), n_files,
265 chunk_size_bytes))
266
267 {
268 attributes.push_back(attribute.value());
269 }
270 else
271 {
272 WARN("Could not create attribute meta of {:s}.", name);
273 }
274 }
275 return attributes;
276}
277
278std::vector<double> transformToXDMFGeometry(MeshLib::Mesh const& mesh)
279{
280 std::vector<MeshLib::Node*> const& nodes = mesh.getNodes();
281
282 int const point_size = 3;
283 std::vector<double> values;
284 values.reserve(nodes.size() * point_size);
285 for (auto const& n : nodes)
286 {
287 const double* x = n->data();
288 values.insert(values.cend(), x, x + point_size);
289 }
290
291 return values;
292}
293
294XdmfHdfData transformGeometry(MeshLib::Mesh const& mesh, double const* data_ptr,
295 unsigned int const n_files,
296 unsigned int const chunk_size_bytes)
297{
298 std::string const name = "geometry";
299 std::vector<MeshLib::Node*> const& nodes = mesh.getNodes();
300
301 int const point_size = 3;
302 auto const& partition_dim = nodes.size();
303
304 HdfData const hdf = {data_ptr,
305 partition_dim,
306 point_size,
307 name,
309 n_files,
310 chunk_size_bytes};
311 XdmfData const xdmf = {
312 partition_dim, point_size, MeshPropertyDataType::float64,
313 name, std::nullopt, n_files,
314 std::nullopt};
315
316 return XdmfHdfData{std::move(hdf), std::move(xdmf)};
317}
318
320{
321 auto const& elements = mesh.getElements();
322
323 if (elements.empty())
324 {
326 }
327 auto const ogs_cell_type = elements[0]->getCellType();
328
329 if (std::any_of(elements.begin(), elements.end(),
330 [&](auto const& cell)
331 { return cell->getCellType() != ogs_cell_type; }))
332 {
334 }
335
336 return cellTypeOGS2XDMF(ogs_cell_type).id;
337}
338
339#if !defined(USE_PETSC)
341{
342 return getLocalTopologyType(mesh);
343}
344#else
346{
347 auto const local_topology_type = getLocalTopologyType(mesh);
348 // get the topology_type from other partitions
349 BaseLib::MPI::Mpi const mpi;
350
351 std::vector<int> const topology_types =
352 BaseLib::MPI::allgather(static_cast<int>(local_topology_type), mpi);
353
354 auto const common_topology_type =
355 std::all_of(topology_types.begin(), topology_types.end(),
356 [&local_topology_type](int other)
357 { return other == static_cast<int>(local_topology_type); })
358 ? local_topology_type
360 return common_topology_type;
361}
362#endif // USE_PETSC
363
364std::pair<std::vector<std::size_t>, ParentDataType> transformToXDMFTopology(
365 MeshLib::Mesh const& mesh, std::size_t const offset)
366{
367 std::vector<MeshLib::Element*> const& elements = mesh.getElements();
368 std::vector<std::size_t> values;
369
370 auto const push_cellnode_ids_to_vector =
371 [&values, &offset](auto const& cell)
372 {
373 values |= ranges::actions::push_back(
374 cell->nodes() | MeshLib::views::ids |
375 ranges::views::transform([&offset](auto const node_id)
376 { return node_id + offset; }));
377 };
378
379 auto const topology_type = getTopologyType(mesh);
380 if (topology_type == ParentDataType::MIXED)
381 {
382 values.reserve(elements.size() * 2); // each cell has at least two
383 // numbers
384 for (auto const& cell : elements)
385 {
386 auto const ogs_cell_type = cell->getCellType();
387 auto const xdmf_cell_id = cellTypeOGS2XDMF(ogs_cell_type).id;
388 values.push_back(to_underlying(xdmf_cell_id));
389 if (ogs_cell_type == MeshLib::CellType::LINE2)
390 {
391 values.push_back(2);
392 }
393 push_cellnode_ids_to_vector(cell);
394 }
395 }
396 else
397 {
398 values.reserve(elements.size() * elements[0]->getNumberOfNodes());
399 for (auto const& cell : elements)
400 {
401 push_cellnode_ids_to_vector(cell);
402 }
403 }
404
405 return {values, topology_type};
406}
407
408XdmfHdfData transformTopology(std::vector<std::size_t> const& values,
409 ParentDataType const parent_data_type,
410 unsigned int const n_files,
411 unsigned int const chunk_size_bytes)
412{
413 std::string const name = "topology";
414 auto const tuple_size = ParentDataType2String(parent_data_type).second;
415 HdfData const hdf = {values.data(),
416 values.size() / tuple_size,
417 tuple_size,
418 name,
420 n_files,
421 chunk_size_bytes};
422 XdmfData const xdmf{values.size() / tuple_size,
423 tuple_size,
425 name,
426 std::nullopt,
427 n_files,
428 parent_data_type};
429
430 return XdmfHdfData{std::move(hdf), std::move(xdmf)};
431}
432} // namespace MeshLib::IO
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
std::pair< std::string, std::size_t > ParentDataType2String(ParentDataType p)
MeshPropertyDataType
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
Properties & getProperties()
Definition Mesh.h:125
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
constexpr std::size_t getNumberOfTuples() const
static std::vector< T > allgather(T const &value, Mpi const &mpi)
Definition MPI.h:98
constexpr auto to_underlying(E e) noexcept
Converts an enumeration to its underlying type.
Definition cpp23.h:22
const std::string OGS_VERSION
Definition GitInfo.cpp:11
ParentDataType getLocalTopologyType(MeshLib::Mesh const &mesh)
constexpr auto cellTypeOGS2XDMF(MeshLib::CellType const &cell_type)
ParentDataType getTopologyType(MeshLib::Mesh const &mesh)
std::optional< XdmfHdfData > transformAttribute(std::pair< std::string, PropertyVectorBase * > const &property_pair, unsigned int const n_files, unsigned int const chunk_size_bytes)
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.
static constexpr auto elemOGSTypeToXDMFType()
std::vector< XdmfHdfData > transformAttributes(MeshLib::Mesh const &mesh, 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 auto elem_type_ogs2xdmf
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216
CellType
Types of mesh elements supported by OpenGeoSys.
Definition MeshEnums.h:53