OGS 6.2.0-405-gb717f6088
Mesh.h
Go to the documentation of this file.
1 
15 #pragma once
16 
17 #include <cstdlib>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "BaseLib/Counter.h"
23 #include "BaseLib/Error.h"
24 
25 #include "MeshEnums.h"
26 #include "Properties.h"
27 
28 namespace ApplicationUtils
29 {
30  class NodeWiseMeshPartitioner;
31 }
32 
33 namespace MeshLib
34 {
35  class Node;
36  class Element;
37 
41 class Mesh : BaseLib::Counter<Mesh>
42 {
43  /* friend functions: */
44  friend void removeMeshNodes(MeshLib::Mesh &mesh, const std::vector<std::size_t> &nodes);
45 
47 
48 public:
60  Mesh(std::string name,
61  std::vector<Node*>
62  nodes,
63  std::vector<Element*>
64  elements,
65  Properties const& properties = Properties(),
66  const std::size_t n_base_nodes = 0);
67 
69  Mesh(const Mesh &mesh);
70 
72  virtual ~Mesh();
73 
75  void addNode(Node* node);
76 
78  void addElement(Element* elem);
79 
81  unsigned getDimension() const { return _mesh_dimension; }
82 
84  const Node* getNode(std::size_t idx) const { return _nodes[idx]; }
85 
87  const Element* getElement(std::size_t idx) const { return _elements[idx]; }
88 
90  double getMinEdgeLength() const { return _edge_length.first; }
91 
93  double getMaxEdgeLength() const { return _edge_length.second; }
94 
96  std::size_t getNumberOfElements() const { return _elements.size(); }
97 
99  std::size_t getNumberOfNodes() const { return _nodes.size(); }
100 
102  const std::string getName() const { return _name; }
103 
105  std::vector<Node*> const& getNodes() const { return _nodes; }
106 
108  std::vector<Element*> const& getElements() const { return _elements; }
109 
111  void resetElementIDs();
112 
114  void resetNodeIDs();
115 
117  void recalculateMaxBaseNodeId();
118 
120  void setName(const std::string &name) { this->_name = name; }
121 
123  std::size_t getID() const {return _id; }
124 
126  std::size_t getNumberOfBaseNodes() const { return _n_base_nodes; }
127 
129  bool isBaseNode(std::size_t node_idx) const {return node_idx < _n_base_nodes; }
130 
132  bool isNonlinear() const { return (getNumberOfNodes() != getNumberOfBaseNodes()); }
133 
134  MeshLib::Properties & getProperties() { return _properties; }
135  MeshLib::Properties const& getProperties() const { return _properties; }
136 
137  bool isAxiallySymmetric() const { return _is_axially_symmetric; }
138  void setAxiallySymmetric(bool is_axial_symmetric) {
139  _is_axially_symmetric = is_axial_symmetric;
140  }
141 
142 protected:
144  void calcEdgeLengthRange();
145 
151  void resetElementsConnectedToNodes();
152 
154  void setDimension();
155 
157  void setElementsConnectedToNodes();
158 
161  void setElementNeighbors();
162 
163  void setNodesConnectedByEdges();
164 
167  void setNodesConnectedByElements();
168 
170  void checkNonlinearNodeIDs() const;
171 
173  bool hasNonlinearElement() const;
174 
175  std::size_t const _id;
176  unsigned _mesh_dimension;
178  std::pair<double, double> _edge_length;
180  std::pair<double, double> _node_distance;
181  std::string _name;
182  std::vector<Node*> _nodes;
183  std::vector<Element*> _elements;
184  std::size_t _n_base_nodes;
186 
187  bool _is_axially_symmetric = false;
188 }; /* class */
189 
190 
192 inline bool operator==(Mesh const& a, Mesh const& b)
193 {
194  return a.getID() == b.getID();
195 }
196 
197 inline bool operator!=(Mesh const& a, Mesh const& b)
198 {
199  return !(a == b);
200 }
201 
205  std::string const& property_name,
206  double factor);
207 
218 template <typename T>
219 void addPropertyToMesh(MeshLib::Mesh& mesh, std::string const& name,
220  MeshLib::MeshItemType item_type,
221  std::size_t number_of_components,
222  std::vector<T> const& values)
223 {
224  if (item_type == MeshLib::MeshItemType::Node)
225  {
226  if (mesh.getNumberOfNodes() != values.size() / number_of_components)
227  {
228  OGS_FATAL(
229  "Error number of nodes (%u) does not match the number of "
230  "tuples (%u).",
231  mesh.getNumberOfNodes(), values.size() / number_of_components);
232  }
233  }
234  if (item_type == MeshLib::MeshItemType::Cell)
235  {
236  if (mesh.getNumberOfElements() != values.size() / number_of_components)
237  {
238  OGS_FATAL(
239  "Error number of elements (%u) does not match the number of "
240  "tuples (%u).",
241  mesh.getNumberOfElements(),
242  values.size() / number_of_components);
243  }
244  }
245 
246  auto* const property = mesh.getProperties().createNewPropertyVector<T>(
247  name, item_type, number_of_components);
248  if (!property)
249  {
250  OGS_FATAL("Error while creating PropertyVector '%s'.", name.c_str());
251  }
252  property->reserve(values.size());
253  std::copy(values.cbegin(), values.cend(), std::back_inserter(*property));
254 }
255 
260 template <typename T>
262  std::string const& property_name,
263  MeshLib::MeshItemType const item_type,
264  int const number_of_components)
265 {
266  if (property_name.empty())
267  {
268  OGS_FATAL(
269  "Trying to get or to create a mesh property with empty name.");
270  }
271 
272  auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t {
273  switch (item_type)
274  {
276  return mesh.getNumberOfElements();
278  return mesh.getNumberOfNodes();
280  return 0; // For the integration point data the size is
281  // variable
282  default:
283  OGS_FATAL(
284  "MeshLib::getOrCreateMeshProperty cannot handle other "
285  "types than Node, Cell, or IntegrationPoint.");
286  }
287  return 0;
288  };
289 
290  if (mesh.getProperties().existsPropertyVector<T>(property_name))
291  {
292  auto result =
293  mesh.getProperties().template getPropertyVector<T>(property_name);
294  assert(result);
295  if (item_type != MeshItemType::IntegrationPoint)
296  {
297  // Test the size if number of mesh items is known, which is not the
298  // case for the integration point data.
299  assert(result->size() ==
300  numberOfMeshItems() * number_of_components);
301  }
302  return result;
303  }
304 
305  auto result = mesh.getProperties().template createNewPropertyVector<T>(
306  property_name, item_type, number_of_components);
307  assert(result);
308  result->resize(numberOfMeshItems() * number_of_components);
309  return result;
310 }
311 
317 PropertyVector<int> const* materialIDs(Mesh const& mesh);
318 
323 std::unique_ptr<MeshLib::Mesh> createMeshFromElementSelection(
324  std::string mesh_name, std::vector<MeshLib::Element*> const& elements);
325 
326 } // namespace MeshLib
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition: Mesh.h:99
std::pair< double, double > _edge_length
The minimal and maximal edge length over all elements in the mesh.
Definition: Mesh.h:178
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition: Mesh.h:84
std::string _name
Definition: Mesh.h:181
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition: Mesh.h:87
std::vector< Element * > _elements
Definition: Mesh.h:183
std::size_t getNumberOfBaseNodes() const
Get the number of base nodes.
Definition: Mesh.h:126
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
Definition: Properties.h:15
std::size_t getID() const
Get id of the mesh.
Definition: Mesh.h:123
bool operator==(Mesh const &a, Mesh const &b)
Meshes are equal if their id&#39;s are equal.
Definition: Mesh.h:192
std::vector< Node * > _nodes
Definition: Mesh.h:182
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:105
double getMinEdgeLength() const
Get the minimum edge length over all elements of the mesh.
Definition: Mesh.h:90
MeshLib::Properties & getProperties()
Definition: Mesh.h:134
bool operator!=(Mesh const &a, Mesh const &b)
Definition: Mesh.h:197
bool isAxiallySymmetric() const
Definition: Mesh.h:137
void scaleMeshPropertyVector(MeshLib::Mesh &mesh, std::string const &property_name, double factor)
Definition: Mesh.cpp:386
bool isBaseNode(std::size_t node_idx) const
Return true if the given node is a basic one (i.e. linear order node)
Definition: Mesh.h:129
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties...
Definition: Properties.h:37
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:102
std::pair< double, double > _node_distance
The minimal and maximal distance of nodes within an element over all elements in the mesh...
Definition: Mesh.h:180
std::unique_ptr< MeshLib::Mesh > createMeshFromElementSelection(std::string mesh_name, std::vector< MeshLib::Element *> const &elements)
Definition: Mesh.cpp:413
MeshItemType
Definition: Location.h:21
std::size_t _n_base_nodes
Definition: Mesh.h:184
std::size_t const _id
Definition: Mesh.h:175
unsigned _mesh_dimension
Definition: Mesh.h:176
Interface for heuristic search length strategy.
Definition: ProjectData.h:30
Properties _properties
Definition: Mesh.h:185
MeshLib::Properties const & getProperties() const
Definition: Mesh.h:135
bool isNonlinear() const
Return true if the mesh has any nonlinear nodes.
Definition: Mesh.h:132
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshLib::MeshItemType const item_type, int const number_of_components)
Definition: Mesh.h:261
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:403
void setAxiallySymmetric(bool is_axial_symmetric)
Definition: Mesh.h:138
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:108
bool existsPropertyVector(std::string const &name) const
Definition: Properties.h:79
void addPropertyToMesh(MeshLib::Mesh &mesh, std::string const &name, MeshLib::MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)
Definition: Mesh.h:219
Definition of mesh-related Enumerations.
Definition of the class Properties that implements a container of properties.
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:81
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:96
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
void setName(const std::string &name)
Changes the name of the mesh.
Definition: Mesh.h:120
double getMaxEdgeLength() const
Get the maximum edge length over all elements of the mesh.
Definition: Mesh.h:93