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