OGS
OGSMesh Class Reference

Detailed Description

Definition at line 12 of file OGSMesh.h.

#include <OGSMesh.h>

Collaboration diagram for OGSMesh:
[legend]

Public Member Functions

 OGSMesh (MeshLib::Mesh &mesh)
std::vector< double > getPointCoordinates () const
std::pair< std::vector< int >, std::vector< int > > getCells () const
std::vector< std::string > getDataArrayNames () const
MeshLib::MeshItemType meshItemType (std::string_view const name) const
template<typename T>
pybind11::array & dataArray (std::string const &name)
pybind11::object dataArray_dispatch (std::string const &name, std::string const &dtype)

Private Attributes

MeshLib::Mesh_mesh
std::map< std::string, pybind11::array > data_array_mapping

Constructor & Destructor Documentation

◆ OGSMesh()

OGSMesh::OGSMesh ( MeshLib::Mesh & mesh)
explicit

Definition at line 21 of file OGSMesh.cpp.

21: _mesh(mesh) {}
MeshLib::Mesh & _mesh
Definition OGSMesh.h:141

References _mesh.

Member Function Documentation

◆ dataArray()

template<typename T>
pybind11::array & OGSMesh::dataArray ( std::string const & name)
inline

Definition at line 25 of file OGSMesh.h.

26 {
27 auto const data_array_it = data_array_mapping.find(name);
28 if (data_array_it != data_array_mapping.end())
29 {
30 INFO("found data array '{}' with address: {}", name,
31 fmt::ptr(&(data_array_it->second)));
32 return data_array_it->second;
33 }
34
35 auto& mesh_properties = _mesh.getProperties();
36
37 // check if PropertyVector with specified data type T exists
38 auto* property = mesh_properties.getPropertyVector<T>(name);
39 if (property == nullptr)
40 {
41 OGS_FATAL("Couldn't access data array '{}'.", name);
42 }
43
44 // Capsule ties lifetime of the numpy array to the PropertyVector
45 auto capsule = pybind11::capsule(property, [](void* /*ignored*/) {});
46
47 auto const n_components = property->getNumberOfGlobalComponents();
48 if (n_components == 1)
49 {
50 auto const& [it, success] = data_array_mapping.insert(
51 {name,
52 pybind11::array(
53 pybind11::buffer_info(
54 property->data(), // data pointer
55 sizeof(T), // item size
56 pybind11::format_descriptor<T>::format(), // dtype
57 // format
58 // string
59 1, // ndim
60 {property->size()}, // shape
61 {sizeof(T)} // strides
62 ),
63 capsule)});
64 if (!success)
65 {
66 OGS_FATAL("Could not insert data array '{}' into internal map.",
67 name);
68 }
69 INFO("insert data array '{}' with address: {}", name,
70 fmt::ptr(&(it->second)));
71 return it->second;
72 }
73 else
74 {
75 // 2D case: (n_items, n_components)
76 auto const& [it, success] = data_array_mapping.insert(
77 {name,
78 pybind11::array(
79 pybind11::buffer_info(
80 property->data(), // data pointer
81 sizeof(T), // item size
82 pybind11::format_descriptor<T>::format(), // dtype
83 // format
84 // string
85 2, // ndim
86 std::vector<pybind11::ssize_t>{
87 static_cast<pybind11::ssize_t>(property->size() /
88 n_components),
89 static_cast<pybind11::ssize_t>(
90 n_components)}, // shape
91 std::vector<pybind11::ssize_t>{
92 static_cast<pybind11::ssize_t>(sizeof(T) *
93 n_components),
94 static_cast<pybind11::ssize_t>(sizeof(T))}
95 // strides
96 ),
97 capsule)});
98 if (!success)
99 {
100 OGS_FATAL("Could not insert data array '{}' into internal map.",
101 name);
102 }
103 INFO("insert data array '{}' with address: {}", name,
104 fmt::ptr(&(it->second)));
105 return it->second;
106 }
107 }
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
std::map< std::string, pybind11::array > data_array_mapping
Definition OGSMesh.h:142

References _mesh, data_array_mapping, INFO(), and OGS_FATAL.

Referenced by dataArray_dispatch().

◆ dataArray_dispatch()

pybind11::object OGSMesh::dataArray_dispatch ( std::string const & name,
std::string const & dtype )
inline

Definition at line 109 of file OGSMesh.h.

111 {
112 if (dtype == "double")
113 {
114 return dataArray<double>(name);
115 }
116 if (dtype == "float")
117 {
118 return dataArray<float>(name);
119 }
120 if (dtype == "int")
121 {
122 return dataArray<int>(name);
123 }
124 if (dtype == "int64")
125 {
126 return dataArray<int64_t>(name);
127 }
128 if (dtype == "std::size_t")
129 {
130 return dataArray<std::size_t>(name);
131 }
132 if (dtype == "char")
133 {
134 return dataArray<char>(name);
135 }
136
137 throw std::runtime_error("Unsupported dtype: " + dtype);
138 }
pybind11::array & dataArray(std::string const &name)
Definition OGSMesh.h:25

References dataArray().

Referenced by PYBIND11_MODULE().

◆ getCells()

std::pair< std::vector< int >, std::vector< int > > OGSMesh::getCells ( ) const

Definition at line 29 of file OGSMesh.cpp.

30{
31 auto const& elements = _mesh.getElements();
32 std::vector<int> cells;
33 std::vector<int> cell_types;
34 for (auto const* element : elements)
35 {
36 ranges::copy(element->nodes() | MeshLib::views::ids,
37 std::back_inserter(cells));
38 cell_types.push_back(OGSToVtkCellType(element->getCellType()));
39 }
40 return {cells, cell_types};
41}
int OGSToVtkCellType(MeshLib::CellType ogs)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:216

References _mesh, MeshLib::views::ids, and OGSToVtkCellType().

Referenced by PYBIND11_MODULE().

◆ getDataArrayNames()

std::vector< std::string > OGSMesh::getDataArrayNames ( ) const

Definition at line 43 of file OGSMesh.cpp.

44{
45 return _mesh.getProperties().getPropertyVectorNames();
46}

References _mesh.

Referenced by PYBIND11_MODULE().

◆ getPointCoordinates()

std::vector< double > OGSMesh::getPointCoordinates ( ) const

Definition at line 23 of file OGSMesh.cpp.

24{
25 return ranges::to<std::vector>(_mesh.getNodes() | MeshLib::views::coords |
26 ranges::views::join);
27}
constexpr ranges::views::view_closure coords
Definition Mesh.h:223

References _mesh, and MeshLib::views::coords.

Referenced by PYBIND11_MODULE().

◆ meshItemType()

MeshLib::MeshItemType OGSMesh::meshItemType ( std::string_view const name) const

Definition at line 48 of file OGSMesh.cpp.

49{
50 auto const& properties = _mesh.getProperties();
51
52 auto const& found = BaseLib::findElementOrError(
53 properties,
54 [&name](auto const& p) { return p.first == name; },
55 [&name]()
56 {
57 OGS_FATAL("A property with the name '{}' doesn't exist in the mesh",
58 name);
59 });
60 return found.second->getMeshItemType();
61}
ranges::range_reference_t< Range > findElementOrError(Range &range, std::predicate< ranges::range_reference_t< Range > > auto &&predicate, std::invocable auto error_callback)
Definition Algorithm.h:74

References _mesh, BaseLib::findElementOrError(), and OGS_FATAL.

Referenced by PYBIND11_MODULE().

Member Data Documentation

◆ _mesh

MeshLib::Mesh& OGSMesh::_mesh
private

◆ data_array_mapping

std::map<std::string, pybind11::array> OGSMesh::data_array_mapping
private

Definition at line 142 of file OGSMesh.h.

Referenced by dataArray().


The documentation for this class was generated from the following files: