OGS 6.1.0-1721-g6382411ad
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 
191 
195  std::string const& property_name,
196  double factor);
197 
208 template <typename T>
209 void addPropertyToMesh(MeshLib::Mesh& mesh, std::string const& name,
210  MeshLib::MeshItemType item_type,
211  std::size_t number_of_components,
212  std::vector<T> const& values)
213 {
214  if (item_type == MeshLib::MeshItemType::Node)
215  if (mesh.getNumberOfNodes() != values.size() / number_of_components)
216  OGS_FATAL(
217  "Error number of nodes (%u) does not match the number of "
218  "tuples (%u).",
219  mesh.getNumberOfNodes(), values.size() / number_of_components);
220  if (item_type == MeshLib::MeshItemType::Cell)
221  if (mesh.getNumberOfElements() != values.size() / number_of_components)
222  OGS_FATAL(
223  "Error number of elements (%u) does not match the number of "
224  "tuples (%u).",
225  mesh.getNumberOfElements(),
226  values.size() / number_of_components);
227 
228  auto* const property = mesh.getProperties().createNewPropertyVector<T>(
229  name, item_type, number_of_components);
230  if (!property)
231  {
232  OGS_FATAL("Error while creating PropertyVector '%s'.", name.c_str());
233  }
234  property->reserve(values.size());
235  std::copy(values.cbegin(), values.cend(), std::back_inserter(*property));
236 }
237 
242 template <typename T>
244  std::string const& property_name,
245  MeshLib::MeshItemType const item_type,
246  int const number_of_components)
247 {
248  if (property_name.empty())
249  OGS_FATAL(
250  "Trying to get or to create a mesh property with empty name.");
251 
252  auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t {
253  switch (item_type)
254  {
256  return mesh.getNumberOfElements();
258  return mesh.getNumberOfNodes();
260  return 0; // For the integration point data the size is
261  // variable
262  default:
263  OGS_FATAL(
264  "MeshLib::getOrCreateMeshProperty cannot handle other "
265  "types than Node, Cell, or IntegrationPoint.");
266  }
267  return 0;
268  };
269 
270  if (mesh.getProperties().existsPropertyVector<T>(property_name))
271  {
272  auto result =
273  mesh.getProperties().template getPropertyVector<T>(property_name);
274  assert(result);
275  if (item_type != MeshItemType::IntegrationPoint)
276  {
277  // Test the size if number of mesh items is known, which is not the
278  // case for the integration point data.
279  assert(result->size() ==
280  numberOfMeshItems() * number_of_components);
281  }
282  return result;
283  }
284 
285  auto result = mesh.getProperties().template createNewPropertyVector<T>(
286  property_name, item_type, number_of_components);
287  assert(result);
288  result->resize(numberOfMeshItems() * number_of_components);
289  return result;
290 }
291 
297 PropertyVector<int> const* materialIDs(Mesh const& mesh);
298 
303 std::unique_ptr<MeshLib::Mesh> createMeshFromElementSelection(
304  std::string mesh_name, std::vector<MeshLib::Element*> const& elements);
305 
306 } /* namespace */
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
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 isAxiallySymmetric() const
Definition: Mesh.h:137
void scaleMeshPropertyVector(MeshLib::Mesh &mesh, std::string const &property_name, double factor)
Definition: Mesh.cpp:337
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:362
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:28
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:243
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:352
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:209
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:71
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