OGS
Mesh2MeshPropertyInterpolation.cpp
Go to the documentation of this file.
1
16
17#include <fstream>
18#include <vector>
19
20#include "BaseLib/Logging.h"
21#include "GeoLib/AABB.h"
22#include "GeoLib/Grid.h"
24#include "MeshLib/Mesh.h"
25#include "MeshLib/MeshEnums.h"
26#include "MeshLib/Node.h"
27
28namespace MeshToolsLib
29{
31 MeshLib::Mesh const& src_mesh, std::string const& property_name)
32 : _src_mesh(src_mesh), _property_name(property_name)
33{
34}
35
37 MeshLib::Mesh& dest_mesh) const
38{
39 if (_src_mesh.getDimension() != dest_mesh.getDimension())
40 {
41 ERR("MeshLib::Mesh2MeshPropertyInterpolation::setPropertiesForMesh() "
42 "dimension of source (dim = {:d}) and destination (dim = {:d}) "
43 "mesh does not match.",
44 _src_mesh.getDimension(), dest_mesh.getDimension());
45 return false;
46 }
47
48 if (_src_mesh.getDimension() != 2)
49 {
50 WARN(
51 "MeshLib::Mesh2MeshPropertyInterpolation::setPropertiesForMesh() "
52 "implemented only for 2D case at the moment.");
53 return false;
54 }
55
56 MeshLib::PropertyVector<double>* dest_properties;
57 if (dest_mesh.getProperties().existsPropertyVector<double>(_property_name))
58 {
59 dest_properties =
61 }
62 else
63 {
64 INFO("Create new PropertyVector '{:s}' of type double.",
66 dest_properties =
67 dest_mesh.getProperties().createNewPropertyVector<double>(
69 if (!dest_properties)
70 {
71 WARN(
72 "Could not get or create a PropertyVector of type double using "
73 "the given name '{:s}'.",
75 return false;
76 }
77 }
78 if (dest_properties->size() != dest_mesh.getNumberOfElements())
79 {
80 dest_properties->resize(dest_mesh.getNumberOfElements());
81 }
82
83 interpolatePropertiesForMesh(dest_mesh, *dest_properties);
84
85 return true;
86}
87
89 MeshLib::Mesh const& dest_mesh,
90 MeshLib::PropertyVector<double>& dest_properties) const
91{
92 std::vector<double> interpolated_src_node_properties(
95 interpolated_src_node_properties);
96
97 // idea: looping over the destination elements and calculate properties
98 // from interpolated_src_node_properties to accelerate the (source) point
99 // search construct a grid
100 std::vector<MeshLib::Node*> const& src_nodes(_src_mesh.getNodes());
101 GeoLib::Grid<MeshLib::Node> src_grid(src_nodes.begin(), src_nodes.end(),
102 64);
103
104 auto const& dest_elements(dest_mesh.getElements());
105 const std::size_t n_dest_elements(dest_elements.size());
106 for (std::size_t k(0); k < n_dest_elements; k++)
107 {
108 MeshLib::Element& dest_element(*dest_elements[k]);
109 if (dest_element.getGeomType() == MeshLib::MeshElemType::LINE)
110 {
111 continue;
112 }
113
114 // compute axis aligned bounding box around the current element
115 const GeoLib::AABB elem_aabb(
116 dest_element.getNodes(),
117 dest_element.getNodes() + dest_element.getNumberOfBaseNodes());
118
119 // request "interesting" nodes from grid
120 std::vector<std::vector<MeshLib::Node*> const*> const nodes =
122 elem_aabb.getMinPoint(), elem_aabb.getMaxPoint());
123
124 std::size_t cnt(0);
125 double average_value(0.0);
126
127 for (auto const* nodes_vec : nodes)
128 {
129 for (auto const* node : *nodes_vec)
130 {
131 if (elem_aabb.containsPointXY(*node) &&
132 MeshLib::isPointInElementXY(*node, dest_element))
133 {
134 average_value +=
135 interpolated_src_node_properties[node->getID()];
136 cnt++;
137 }
138 }
139 }
140
141 if (cnt == 0)
142 {
143 OGS_FATAL(
144 "Mesh2MeshInterpolation: Could not find values in source mesh "
145 "for the element {:d}.",
146 k);
147 }
148 dest_properties[k] = average_value / cnt;
149 }
150}
151
154 std::vector<double>& interpolated_properties) const
155{
156 // fetch the source of property values
158 {
159 WARN("Did not find PropertyVector<double> '{:s}'.", _property_name);
160 return;
161 }
162 auto const* elem_props =
164
165 std::vector<MeshLib::Node*> const& src_nodes(_src_mesh.getNodes());
166 const std::size_t n_src_nodes(src_nodes.size());
167 for (std::size_t k(0); k < n_src_nodes; k++)
168 {
169 const std::size_t n_con_elems(
170 _src_mesh.getElementsConnectedToNode(*src_nodes[k]).size());
171 interpolated_properties[k] = (*elem_props)
172 [_src_mesh.getElementsConnectedToNode(*src_nodes[k])[0]->getID()];
173 for (std::size_t j(1); j < n_con_elems; j++)
174 {
175 interpolated_properties[k] +=
176 (*elem_props)[_src_mesh
177 .getElementsConnectedToNode(*src_nodes[k])[j]
178 ->getID()];
179 }
180 interpolated_properties[k] /= n_con_elems;
181 }
182}
183
184} // namespace MeshToolsLib
Definition of the AABB class.
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the Grid class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Implementation of the Mesh2MeshPropertyInterpolation class.
Definition of mesh-related Enumerations.
Definition of the Mesh class.
Definition of the Node class.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Eigen::Vector3d const & getMaxPoint() const
Definition AABB.h:187
Eigen::Vector3d const & getMinPoint() const
Definition AABB.h:180
bool containsPointXY(T const &pnt) const
Definition AABB.h:161
std::vector< std::vector< POINT * > const * > getPntVecsOfGridCellsIntersectingCuboid(Eigen::Vector3d const &min_pnt, Eigen::Vector3d const &max_pnt) const
Definition Grid.h:286
virtual MeshElemType getGeomType() const =0
virtual unsigned getNumberOfBaseNodes() const =0
virtual Node *const * getNodes() const =0
Get array of element nodes.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
Properties & getProperties()
Definition Mesh.h:134
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition Mesh.cpp:256
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
bool existsPropertyVector(std::string_view name) const
Definition Properties.h:74
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
Definition Properties.h:17
PropertyVector< T > const * getPropertyVector(std::string_view name) const
std::size_t size() const
void interpolatePropertiesForMesh(MeshLib::Mesh const &dest_mesh, MeshLib::PropertyVector< double > &dest_properties) const
void interpolateElementPropertiesToNodeProperties(std::vector< double > &interpolated_properties) const
Mesh2MeshPropertyInterpolation(MeshLib::Mesh const &src_mesh, std::string const &property_name)
bool isPointInElementXY(MathLib::Point3d const &p, Element const &e)
Definition Element.cpp:171