OGS
Mesh.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <cstdlib>
18#include <memory>
19#include <range/v3/view/transform.hpp>
20#include <string>
21#include <vector>
22
23#include "BaseLib/Error.h"
24#include "MeshEnums.h"
25#include "Properties.h"
26
27namespace ApplicationUtils
28{
29 class NodeWiseMeshPartitioner;
30}
31
32namespace MeshLib
33{
34 class Node;
35 class Element;
36
40class Mesh
41{
42 /* friend functions: */
43 friend void removeMeshNodes(Mesh& mesh,
44 const std::vector<std::size_t>& nodes);
45
47
48public:
54 Mesh(std::string name,
55 std::vector<Node*>
56 nodes,
57 std::vector<Element*>
58 elements,
59 Properties const& properties = Properties());
60
62 Mesh(const Mesh &mesh);
63
64 Mesh(Mesh&& mesh);
65
66 Mesh& operator=(const Mesh& mesh) = delete;
67 Mesh& operator=(Mesh&& mesh) = delete;
68
70 virtual ~Mesh();
71
77 void shallowClean();
78
80 void addElement(Element* elem);
81
83 unsigned getDimension() const { return _mesh_dimension; }
84
86 const Node* getNode(std::size_t idx) const { return _nodes[idx]; }
87
89 const Element* getElement(std::size_t idx) const { return _elements[idx]; }
90
92 std::size_t getNumberOfElements() const { return _elements.size(); }
93
95 std::size_t getNumberOfNodes() const { return _nodes.size(); }
96
98 const std::string getName() const { return _name; }
99
101 std::vector<Node*> const& getNodes() const { return _nodes; }
102
104 std::vector<Element*> const& getElements() const { return _elements; }
105
107 void resetElementIDs();
108
110 void resetNodeIDs();
111
113 void setName(const std::string &name) { this->_name = name; }
114
116 std::size_t getID() const {return _id; }
117
119 std::size_t computeNumberOfBaseNodes() const;
120
122 bool hasNonlinearElement() const;
123
124 std::vector<Element const*> const& getElementsConnectedToNode(
125 std::size_t node_id) const;
126 std::vector<Element const*> const& getElementsConnectedToNode(
127 Node const& node) const;
128
130 Properties const& getProperties() const { return _properties; }
131
133 void setAxiallySymmetric(bool is_axial_symmetric) {
134 _is_axially_symmetric = is_axial_symmetric;
135 }
136
137protected:
140
142 void setDimension();
143
146 void setElementNeighbors();
147
148 std::size_t const _id;
151 std::pair<double, double> _node_distance;
152 std::string _name;
153 std::vector<Node*> _nodes;
154 std::vector<Element*> _elements;
156
157 std::vector<std::vector<Element const*>> _elements_connected_to_nodes;
158
160}; /* class */
161
164std::vector<std::vector<Node*>> calculateNodesConnectedByElements(
165 Mesh const& mesh);
166
168inline bool operator==(Mesh const& a, Mesh const& b)
169{
170 return a.getID() == b.getID();
171}
172
173inline bool operator!=(Mesh const& a, Mesh const& b)
174{
175 return !(a == b);
176}
177
180void scaleMeshPropertyVector(Mesh& mesh,
181 std::string const& property_name,
182 double factor);
183
194template <typename T>
195void addPropertyToMesh(Mesh& mesh, std::string_view name,
196 MeshItemType item_type, std::size_t number_of_components,
197 std::vector<T> const& values)
198{
199 if (item_type == MeshItemType::Node)
200 {
201 if (mesh.getNumberOfNodes() != values.size() / number_of_components)
202 {
203 OGS_FATAL(
204 "Error number of nodes ({:d}) does not match the number of "
205 "tuples ({:d}).",
206 mesh.getNumberOfNodes(), values.size() / number_of_components);
207 }
208 }
209 if (item_type == MeshItemType::Cell)
210 {
211 if (mesh.getNumberOfElements() != values.size() / number_of_components)
212 {
213 OGS_FATAL(
214 "Error number of elements ({:d}) does not match the number of "
215 "tuples ({:d}).",
216 mesh.getNumberOfElements(),
217 values.size() / number_of_components);
218 }
219 }
220
221 auto* const property = mesh.getProperties().createNewPropertyVector<T>(
222 name, item_type, number_of_components);
223 if (!property)
224 {
225 OGS_FATAL("Error while creating PropertyVector '{:s}'.", name);
226 }
227 property->reserve(values.size());
228 std::copy(values.cbegin(), values.cend(), std::back_inserter(*property));
229}
230
235template <typename T>
237 std::string const& property_name,
238 MeshItemType const item_type,
239 int const number_of_components)
240{
241 if (property_name.empty())
242 {
243 OGS_FATAL(
244 "Trying to get or to create a mesh property with empty name.");
245 }
246
247 auto numberOfMeshItems = [&mesh, &item_type]() -> std::size_t {
248 switch (item_type)
249 {
251 return mesh.getNumberOfElements();
253 return mesh.getNumberOfNodes();
255 return 0; // For the integration point data the size is
256 // variable
257 default:
258 OGS_FATAL(
259 "getOrCreateMeshProperty cannot handle other "
260 "types than Node, Cell, or IntegrationPoint.");
261 }
262 return 0;
263 };
264
265 if (mesh.getProperties().existsPropertyVector<T>(property_name))
266 {
267 auto result =
268 mesh.getProperties().template getPropertyVector<T>(property_name);
269 assert(result);
270 if (item_type != MeshItemType::IntegrationPoint)
271 {
272 // Test the size if number of mesh items is known, which is not the
273 // case for the integration point data.
274 assert(result->size() ==
275 numberOfMeshItems() * number_of_components);
276 }
277 return result;
278 }
279
280 auto result = mesh.getProperties().template createNewPropertyVector<T>(
281 property_name, item_type, number_of_components);
282 assert(result);
283 result->resize(numberOfMeshItems() * number_of_components);
284 return result;
285}
286
292PropertyVector<int> const* materialIDs(Mesh const& mesh);
293
298std::unique_ptr<Mesh> createMeshFromElementSelection(
299 std::string mesh_name, std::vector<Element*> const& elements);
300
303bool isBaseNode(Node const& node,
304 std::vector<Element const*> const& elements_connected_to_node);
305
307std::pair<double, double> minMaxEdgeLength(
308 std::vector<Element*> const& elements);
309
311namespace views
312{
314inline constexpr ranges::views::view_closure ids =
315 ranges::views::transform([](auto const& a) { return a->getID(); });
316
318inline constexpr ranges::views::view_closure names =
319 ranges::views::transform([](auto const& a) { return a->getName(); });
320} // namespace views
321} // namespace MeshLib
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of mesh-related Enumerations.
Definition of the class Properties that implements a container of properties.
bool isAxiallySymmetric() const
Definition: Mesh.h:132
Properties _properties
Definition: Mesh.h:155
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:101
void calcEdgeLengthRange()
Set the minimum and maximum length over the edges of the mesh.
std::size_t const _id
Definition: Mesh.h:148
Mesh(Mesh &&mesh)
std::vector< std::vector< Element const * > > _elements_connected_to_nodes
Definition: Mesh.h:157
void setName(const std::string &name)
Changes the name of the mesh.
Definition: Mesh.h:113
void addElement(Element *elem)
Add an element to the mesh.
Definition: Mesh.cpp:145
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:104
unsigned _mesh_dimension
Definition: Mesh.h:149
std::size_t computeNumberOfBaseNodes() const
Get the number of base nodes.
Definition: Mesh.cpp:229
friend void removeMeshNodes(Mesh &mesh, const std::vector< std::size_t > &nodes)
std::string _name
Definition: Mesh.h:152
Mesh & operator=(Mesh &&mesh)=delete
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:83
std::size_t getID() const
Get id of the mesh.
Definition: Mesh.h:116
void resetNodeIDs()
Resets the IDs of all mesh-nodes to their position in the node vector.
Definition: Mesh.cpp:150
const Node * getNode(std::size_t idx) const
Get the node with the given index.
Definition: Mesh.h:86
Properties & getProperties()
Definition: Mesh.h:129
const Element * getElement(std::size_t idx) const
Get the element with the given index.
Definition: Mesh.h:89
std::vector< Element * > _elements
Definition: Mesh.h:154
void setDimension()
Sets the dimension of the mesh.
Definition: Mesh.cpp:168
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:98
void resetElementIDs()
Resets the IDs of all mesh-elements to their position in the element vector.
Definition: Mesh.cpp:159
void setElementNeighbors()
Definition: Mesh.cpp:195
virtual ~Mesh()
Destructor.
Definition: Mesh.cpp:130
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition: Mesh.h:95
std::vector< Element const * > const & getElementsConnectedToNode(std::size_t node_id) const
Definition: Mesh.cpp:247
void setAxiallySymmetric(bool is_axial_symmetric)
Definition: Mesh.h:133
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:151
Mesh & operator=(const Mesh &mesh)=delete
void shallowClean()
Definition: Mesh.cpp:124
Properties const & getProperties() const
Definition: Mesh.h:130
bool hasNonlinearElement() const
Check if the mesh contains any nonlinear element.
Definition: Mesh.cpp:239
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:92
std::vector< Node * > _nodes
Definition: Mesh.h:153
bool _is_axially_symmetric
Definition: Mesh.h:159
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition: Properties.h:36
bool existsPropertyVector(std::string_view name) const
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition: Mesh.h:314
constexpr ranges::views::view_closure names
For an element of a range view return its name.
Definition: Mesh.h:318
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
Definition: Mesh.h:236
std::unique_ptr< MeshLib::Mesh > createMeshFromElementSelection(std::string mesh_name, std::vector< MeshLib::Element * > const &elements)
Definition: Mesh.cpp:291
std::vector< std::vector< Node * > > calculateNodesConnectedByElements(Mesh const &mesh)
Definition: Mesh.cpp:355
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:273
MeshItemType
Definition: Location.h:21
std::pair< double, double > minMaxEdgeLength(std::vector< Element * > const &elements)
Returns the minimum and maximum edge length for given elements.
Definition: Mesh.cpp:180
bool operator==(Mesh const &a, Mesh const &b)
Meshes are equal if their id's are equal.
Definition: Mesh.h:168
bool operator!=(Mesh const &a, Mesh const &b)
Definition: Mesh.h:173
bool isBaseNode(Node const &node, std::vector< Element const * > const &elements_connected_to_node)
Definition: Mesh.cpp:393
void scaleMeshPropertyVector(MeshLib::Mesh &mesh, std::string const &property_name, double factor)
Definition: Mesh.cpp:259
void addPropertyToMesh(Mesh &mesh, std::string_view name, MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)
Definition: Mesh.h:195