OGS
OGSMesh.h
Go to the documentation of this file.
1
12#include <pybind11/numpy.h>
13#include <pybind11/pybind11.h>
14
15#include "MeshLib/Mesh.h"
16#include "MeshLib/MeshEnums.h"
17
18// Needs to be exported, see
19// https://pybind11.readthedocs.io/en/stable/advanced/misc.html#partitioning-code-over-multiple-extension-modules
20class PYBIND11_EXPORT OGSMesh
21{
22public:
23 explicit OGSMesh(MeshLib::Mesh& mesh);
24
25 std::vector<double> getPointCoordinates() const;
26 std::pair<std::vector<int>, std::vector<int>> getCells() const;
27
28 std::vector<std::string> getDataArrayNames() const;
29
30 MeshLib::MeshItemType meshItemType(std::string_view const name) const;
31
32 template <typename T>
33 pybind11::array& dataArray(std::string const& name)
34 {
35 auto const data_array_it = data_array_mapping.find(name);
36 if (data_array_it != data_array_mapping.end())
37 {
38 INFO("found data array '{}' with address: {}", name,
39 fmt::ptr(&(data_array_it->second)));
40 return data_array_it->second;
41 }
42
43 auto& mesh_properties = _mesh.getProperties();
44
45 // check if PropertyVector with specified data type T exists
46 auto* property = mesh_properties.getPropertyVector<T>(name);
47 if (property == nullptr)
48 {
49 OGS_FATAL("Couldn't access data array '{}'.", name);
50 }
51
52 // Capsule ties lifetime of the numpy array to the PropertyVector
53 auto capsule = pybind11::capsule(property, [](void* /*ignored*/) {});
54
55 auto const n_components = property->getNumberOfGlobalComponents();
56 if (n_components == 1)
57 {
58 auto const& [it, success] = data_array_mapping.insert(
59 {name,
60 pybind11::array(
61 pybind11::buffer_info(
62 property->data(), // data pointer
63 sizeof(T), // item size
64 pybind11::format_descriptor<T>::format(), // dtype
65 // format
66 // string
67 1, // ndim
68 {property->size()}, // shape
69 {sizeof(T)} // strides
70 ),
71 capsule)});
72 if (!success)
73 {
74 OGS_FATAL("Could not insert data array '{}' into internal map.",
75 name);
76 }
77 INFO("insert data array '{}' with address: {}", name,
78 fmt::ptr(&(it->second)));
79 return it->second;
80 }
81 else
82 {
83 // 2D case: (n_items, n_components)
84 auto const& [it, success] = data_array_mapping.insert(
85 {name,
86 pybind11::array(
87 pybind11::buffer_info(
88 property->data(), // data pointer
89 sizeof(T), // item size
90 pybind11::format_descriptor<T>::format(), // dtype
91 // format
92 // string
93 2, // ndim
94 std::vector<pybind11::ssize_t>{
95 static_cast<pybind11::ssize_t>(property->size() /
96 n_components),
97 static_cast<pybind11::ssize_t>(
98 n_components)}, // shape
99 std::vector<pybind11::ssize_t>{
100 static_cast<pybind11::ssize_t>(sizeof(T) *
101 n_components),
102 static_cast<pybind11::ssize_t>(sizeof(T))}
103 // strides
104 ),
105 capsule)});
106 if (!success)
107 {
108 OGS_FATAL("Could not insert data array '{}' into internal map.",
109 name);
110 }
111 INFO("insert data array '{}' with address: {}", name,
112 fmt::ptr(&(it->second)));
113 return it->second;
114 }
115 }
116
117 pybind11::object dataArray_dispatch(std::string const& name,
118 std::string const& dtype)
119 {
120 if (dtype == "double")
121 {
122 return dataArray<double>(name);
123 }
124 if (dtype == "float")
125 {
126 return dataArray<float>(name);
127 }
128 if (dtype == "int")
129 {
130 return dataArray<int>(name);
131 }
132 if (dtype == "int64")
133 {
134 return dataArray<int64_t>(name);
135 }
136 if (dtype == "std::size_t")
137 {
138 return dataArray<std::size_t>(name);
139 }
140 if (dtype == "char")
141 {
142 return dataArray<char>(name);
143 }
144
145 throw std::runtime_error("Unsupported dtype: " + dtype);
146 }
147
148private:
150 std::map<std::string, pybind11::array> data_array_mapping;
151};
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
Definition of mesh-related Enumerations.
Definition of the Mesh class.
pybind11::array & dataArray(std::string const &name)
Definition OGSMesh.h:33
MeshLib::Mesh & _mesh
Definition OGSMesh.h:149
pybind11::object dataArray_dispatch(std::string const &name, std::string const &dtype)
Definition OGSMesh.h:117
std::map< std::string, pybind11::array > data_array_mapping
Definition OGSMesh.h:150