OGS 6.2.2-330-gf48c72f61.dirty.20200225212913
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 addElement(Element* elem);
76 
78  unsigned getDimension() const { return _mesh_dimension; }
79 
81  const Node* getNode(std::size_t idx) const { return _nodes[idx]; }
82 
84  const Element* getElement(std::size_t idx) const { return _elements[idx]; }
85 
87  double getMinEdgeLength() const { return _edge_length.first; }
88 
90  double getMaxEdgeLength() const { return _edge_length.second; }
91 
93  std::size_t getNumberOfElements() const { return _elements.size(); }
94 
96  std::size_t getNumberOfNodes() const { return _nodes.size(); }
97 
99  const std::string getName() const { return _name; }
100 
102  std::vector<Node*> const& getNodes() const { return _nodes; }
103 
105  std::vector<Element*> const& getElements() const { return _elements; }
106 
108  void resetElementIDs();
109 
111  void resetNodeIDs();
112 
114  void recalculateMaxBaseNodeId();
115 
117  void setName(const std::string &name) { this->_name = name; }
118 
120  std::size_t getID() const {return _id; }
121 
123  std::size_t getNumberOfBaseNodes() const { return _n_base_nodes; }
124 
126  bool isBaseNode(std::size_t node_idx) const {return node_idx < _n_base_nodes; }
127 
129  bool isNonlinear() const { return (getNumberOfNodes() != getNumberOfBaseNodes()); }
130 
131  MeshLib::Properties & getProperties() { return _properties; }
132  MeshLib::Properties const& getProperties() const { return _properties; }
133 
134  bool isAxiallySymmetric() const { return _is_axially_symmetric; }
135  void setAxiallySymmetric(bool is_axial_symmetric) {
136  _is_axially_symmetric = is_axial_symmetric;
137  }
138 
139 protected:
141  void calcEdgeLengthRange();
142 
144  void setDimension();
145 
147  void setElementsConnectedToNodes();
148 
151  void setElementNeighbors();
152 
155  void setNodesConnectedByElements();
156 
158  void checkNonlinearNodeIDs() const;
159 
161  bool hasNonlinearElement() const;
162 
163  std::size_t const _id;
164  unsigned _mesh_dimension;
166  std::pair<double, double> _edge_length;
168  std::pair<double, double> _node_distance;
169  std::string _name;
170  std::vector<Node*> _nodes;
171  std::vector<Element*> _elements;
172  std::size_t _n_base_nodes;
174 
175  bool _is_axially_symmetric = false;
176 }; /* class */
177 
178 
180 inline bool operator==(Mesh const& a, Mesh const& b)
181 {
182  return a.getID() == b.getID();
183 }
184 
185 inline bool operator!=(Mesh const& a, Mesh const& b)
186 {
187  return !(a == b);
188 }
189 
193  std::string const& property_name,
194  double factor);
195 
206 template <typename T>
207 void addPropertyToMesh(MeshLib::Mesh& mesh, std::string const& name,
208  MeshLib::MeshItemType item_type,
209  std::size_t number_of_components,
210  std::vector<T> const& values)
211 {
212  if (item_type == MeshLib::MeshItemType::Node)
213  {
214  if (mesh.getNumberOfNodes() != values.size() / number_of_components)
215  {
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  }
221  }
222  if (item_type == MeshLib::MeshItemType::Cell)
223  {
224  if (mesh.getNumberOfElements() != values.size() / number_of_components)
225  {
226  OGS_FATAL(
227  "Error number of elements (%u) does not match the number of "
228  "tuples (%u).",
229  mesh.getNumberOfElements(),
230  values.size() / number_of_components);
231  }
232  }
233 
234  auto* const property = mesh.getProperties().createNewPropertyVector<T>(
235  name, item_type, number_of_components);
236  if (!property)
237  {
238  OGS_FATAL("Error while creating PropertyVector '%s'.", name.c_str());
239  }
240  property->reserve(values.size());
241  std::copy(values.cbegin(), values.cend(), std::back_inserter(*property));
242 }
243 
248 template <typename T>
250  std::string const& property_name,
251  MeshLib::MeshItemType const item_type,
252  int const number_of_components)
253 {
254  if (property_name.empty())
255  {
256  OGS_FATAL(
257  "Trying to get or to create a mesh property with empty name.");
258  }
259 
260  auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t {
261  switch (item_type)
262  {
264  return mesh.getNumberOfElements();
266  return mesh.getNumberOfNodes();
268  return 0; // For the integration point data the size is
269  // variable
270  default:
271  OGS_FATAL(
272  "MeshLib::getOrCreateMeshProperty cannot handle other "
273  "types than Node, Cell, or IntegrationPoint.");
274  }
275  return 0;
276  };
277 
278  if (mesh.getProperties().existsPropertyVector<T>(property_name))
279  {
280  auto result =
281  mesh.getProperties().template getPropertyVector<T>(property_name);
282  assert(result);
283  if (item_type != MeshItemType::IntegrationPoint)
284  {
285  // Test the size if number of mesh items is known, which is not the
286  // case for the integration point data.
287  assert(result->size() ==
288  numberOfMeshItems() * number_of_components);
289  }
290  return result;
291  }
292 
293  auto result = mesh.getProperties().template createNewPropertyVector<T>(
294  property_name, item_type, number_of_components);
295  assert(result);
296  result->resize(numberOfMeshItems() * number_of_components);
297  return result;
298 }
299 
305 PropertyVector<int> const* materialIDs(Mesh const& mesh);
306 
311 std::unique_ptr<MeshLib::Mesh> createMeshFromElementSelection(
312  std::string mesh_name, std::vector<MeshLib::Element*> const& elements);
313 
314 } // namespace MeshLib
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition: Mesh.h:96
std::pair< double, double > _edge_length
The minimal and maximal edge length over all elements in the mesh.
Definition: Mesh.h:166
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition: Mesh.h:81
std::string _name
Definition: Mesh.h:169
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition: Mesh.h:84
std::vector< Element * > _elements
Definition: Mesh.h:171
std::size_t getNumberOfBaseNodes() const
Get the number of base nodes.
Definition: Mesh.h:123
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:120
bool operator==(Mesh const &a, Mesh const &b)
Meshes are equal if their id&#39;s are equal.
Definition: Mesh.h:180
std::vector< Node * > _nodes
Definition: Mesh.h:170
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:102
double getMinEdgeLength() const
Get the minimum edge length over all elements of the mesh.
Definition: Mesh.h:87
MeshLib::Properties & getProperties()
Definition: Mesh.h:131
bool operator!=(Mesh const &a, Mesh const &b)
Definition: Mesh.h:185
bool isAxiallySymmetric() const
Definition: Mesh.h:134
void scaleMeshPropertyVector(MeshLib::Mesh &mesh, std::string const &property_name, double factor)
Definition: Mesh.cpp:293
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:126
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties...
Definition: Properties.h:36
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:99
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:168
std::unique_ptr< MeshLib::Mesh > createMeshFromElementSelection(std::string mesh_name, std::vector< MeshLib::Element *> const &elements)
Definition: Mesh.cpp:320
MeshItemType
Definition: Location.h:21
std::size_t _n_base_nodes
Definition: Mesh.h:172
std::size_t const _id
Definition: Mesh.h:163
unsigned _mesh_dimension
Definition: Mesh.h:164
Interface for heuristic search length strategy.
Definition: ProjectData.h:30
Properties _properties
Definition: Mesh.h:173
MeshLib::Properties const & getProperties() const
Definition: Mesh.h:132
bool isNonlinear() const
Return true if the mesh has any nonlinear nodes.
Definition: Mesh.h:129
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshLib::MeshItemType const item_type, int const number_of_components)
Definition: Mesh.h:249
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:310
void setAxiallySymmetric(bool is_axial_symmetric)
Definition: Mesh.h:135
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:105
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:207
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:78
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:93
#define OGS_FATAL(fmt,...)
Definition: Error.h:64
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:37
void setName(const std::string &name)
Changes the name of the mesh.
Definition: Mesh.h:117
double getMaxEdgeLength() const
Get the maximum edge length over all elements of the mesh.
Definition: Mesh.h:90