preCICE
Loading...
Searching...
No Matches
Mesh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4#include <deque>
5#include <iosfwd>
6#include <list>
7#include <map>
8#include <string>
9#include <string_view>
10#include <vector>
11
12#include "logging/Logger.hpp"
13#include "mesh/BoundingBox.hpp"
14#include "mesh/Data.hpp"
15#include "mesh/Edge.hpp"
17#include "mesh/Tetrahedron.hpp"
18#include "mesh/Triangle.hpp"
19#include "mesh/Vertex.hpp"
21#include "query/Index.hpp"
23#include "utils/assertion.hpp"
24
25namespace precice::mesh {
26
38class Mesh {
39public:
40 using VertexContainer = std::deque<Vertex>;
41 using EdgeContainer = std::deque<Edge>;
42 using TriangleContainer = std::deque<Triangle>;
43 using TetraContainer = std::deque<Tetrahedron>;
44 using DataContainer = std::vector<PtrData>;
45 using BoundingBoxMap = std::map<int, BoundingBox>;
46
48 using VertexDistribution = std::map<Rank, std::vector<VertexID>>;
49
51 using CommunicationMap = std::map<Rank, std::vector<VertexID>>;
52 using ConnectionMap = CommunicationMap; // until we decide on a name
53
54 using VertexOffsets = std::vector<int>;
55
57 static constexpr MeshID MESH_ID_UNDEFINED{-1};
58
66 Mesh(
67 std::string name,
68 int dimensions,
69 MeshID id,
70 bool isJustInTime = false);
71
74
76 const Vertex &vertex(VertexID id) const;
77
80
82 const VertexContainer &vertices() const;
83
85 std::size_t nVertices() const;
86
88 bool empty() const
89 {
90 return _vertices.empty();
91 }
92
95
97 const EdgeContainer &edges() const;
98
99 bool hasEdges() const
100 {
101 return !_edges.empty();
102 }
103
106
108 const TriangleContainer &triangles() const;
109
110 bool hasTriangles() const
111 {
112 return !_triangles.empty();
113 }
114
117
119 const TetraContainer &tetrahedra() const;
120
121 bool hasTetrahedra() const
122 {
123 return !_tetrahedra.empty();
124 }
125
126 bool hasConnectivity() const
127 {
128 return hasEdges() || hasTriangles() || hasTetrahedra();
129 }
130
131 int getDimensions() const;
132
134 Vertex &createVertex(const Eigen::Ref<const Eigen::VectorXd> &coords);
135
143 Vertex &vertexOne,
144 Vertex &vertexTwo);
145
154 Edge &edgeOne,
155 Edge &edgeTwo,
156 Edge &edgeThree);
157
166 Vertex &vertexOne,
167 Vertex &vertexTwo,
168 Vertex &vertexThree);
169
179 Vertex &vertexOne,
180 Vertex &vertexTwo,
181 Vertex &vertexThree,
182 Vertex &vertexFour);
183
185 PtrData &createData(const std::string &name,
186 int dimension,
187 DataID id,
188 int waveformDegree = time::Time::DEFAULT_WAVEFORM_DEGREE);
189
191 PtrData &createData(const std::string &name,
192 int dimension,
193 DataID id,
194 int waveformDegree,
195 std::vector<std::optional<double>> lowerBound,
196 std::vector<std::optional<double>> upperBound);
197
199 const DataContainer &data() const;
200
202 bool hasDataID(DataID dataID) const;
203
205 const PtrData &data(DataID dataID) const;
206
208 bool hasDataName(std::string_view dataName) const;
209
211 std::vector<std::string> availableData() const;
212
214 const PtrData &data(std::string_view dataName) const;
215
217 const std::string &getName() const;
218
220 MeshID getID() const;
221
223 bool isValidVertexID(VertexID vertexID) const;
224
226 void allocateDataValues(); //@todo Redesign mapping and remove this function. See https://github.com/precice/precice/issues/1651.
227
229 void computeBoundingBox();
230
239 void clear();
240
242 void clearPartitioning();
243
245 void clearDataStamples();
246
248 {
249 PRECICE_ASSERT(std::all_of(vd.begin(), vd.end(), [](const auto &p) { return std::is_sorted(p.second.begin(), p.second.end()); }));
250 _vertexDistribution = std::move(vd);
251 }
252
255 {
256 return _vertexDistribution;
257 }
258
260 {
261 return _vertexOffsets;
262 }
263
265 bool isPartitionEmpty(Rank rank) const;
266
268 void setVertexOffsets(VertexOffsets vertexOffsets)
269 {
270 _vertexOffsets = std::move(vertexOffsets);
271 }
272
274 {
276 }
277
279 {
281 }
282
283 // Get the data of owned vertices for given data ID
284 Eigen::VectorXd getOwnedVertexData(const Eigen::VectorXd &values);
285
286 // Tag all the vertices
287 void tagAll();
288
290 const std::vector<Rank> &getConnectedRanks() const
291 {
292 return _connectedRanks;
293 }
294
296 void setConnectedRanks(std::vector<Rank> ranks)
297 {
298 _connectedRanks = std::move(ranks);
299 }
300
306
307 void addMesh(Mesh &deltaMesh);
308
320 const BoundingBox &getBoundingBox() const;
321
322 void expandBoundingBox(const BoundingBox &bounding_box);
323
324 bool operator==(const Mesh &other) const;
325
326 bool operator!=(const Mesh &other) const;
327
329 const query::Index &index() const
330 {
331 return _index;
332 }
333
336 {
337 return _index;
338 }
339
340 bool isJustInTime() const
341 {
342 return _isJustInTime;
343 }
344
353 void preprocess();
354
355private:
356 mutable logging::Logger _log{"mesh::Mesh"};
357
359 std::string _name;
360
363
366
372
375
382
384
389
396
401 std::vector<Rank> _connectedRanks;
402
408
410 bool _isJustInTime = false;
411
413
415
417 void removeDuplicates();
418
424};
425
426std::ostream &operator<<(std::ostream &os, const Mesh &q);
427
428} // namespace precice::mesh
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
Mesh(std::string name, int dimensions, MeshID id, bool isJustInTime=false)
Constructor.
Definition Mesh.cpp:26
This class provides a lightweight logger.
Definition Logger.hpp:17
An axis-aligned bounding box around a (partition of a) mesh.
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:15
Container and creator for meshes.
Definition Mesh.hpp:38
void expandBoundingBox(const BoundingBox &bounding_box)
Definition Mesh.cpp:415
Triangle & createTriangle(Edge &edgeOne, Edge &edgeTwo, Edge &edgeThree)
Creates and initializes a Triangle object.
Definition Mesh.cpp:120
void setGlobalNumberOfVertices(int num)
Definition Mesh.hpp:278
const VertexDistribution & getVertexDistribution() const
Returns a mapping from rank to used (not necessarily owned) vertex IDs.
Definition Mesh.hpp:254
MeshID _id
The ID of this mesh.
Definition Mesh.hpp:365
MeshID getID() const
Returns the base ID of the mesh.
Definition Mesh.cpp:247
int getGlobalNumberOfVertices() const
Definition Mesh.hpp:273
std::string _name
Name of the mesh.
Definition Mesh.hpp:359
std::map< Rank, std::vector< VertexID > > CommunicationMap
A mapping from remote local ranks to the IDs that must be communicated.
Definition Mesh.hpp:51
BoundingBox _boundingBox
Definition Mesh.hpp:412
std::deque< Triangle > TriangleContainer
Definition Mesh.hpp:42
bool hasTetrahedra() const
Definition Mesh.hpp:121
void setVertexDistribution(VertexDistribution vd)
Definition Mesh.hpp:247
int _globalNumberOfVertices
Number of unique vertices for complete distributed mesh.
Definition Mesh.hpp:395
int getDimensions() const
Definition Mesh.cpp:99
VertexContainer & vertices()
Returns modifieable container holding all vertices.
Definition Mesh.cpp:54
void clearDataStamples()
Clears all data stamples.
Definition Mesh.cpp:304
std::vector< PtrData > DataContainer
Definition Mesh.hpp:44
bool hasDataID(DataID dataID) const
Returns whether Mesh has Data with the matchingID.
Definition Mesh.cpp:199
Eigen::VectorXd getOwnedVertexData(const Eigen::VectorXd &values)
Definition Mesh.cpp:324
void clear()
Removes all mesh elements and data values (does not remove data or the bounding boxes).
Definition Mesh.cpp:280
DataContainer _data
Data hold by the vertices of the mesh.
Definition Mesh.hpp:374
bool hasEdges() const
Definition Mesh.hpp:99
void addMesh(Mesh &deltaMesh)
Definition Mesh.cpp:354
bool operator!=(const Mesh &other) const
Definition Mesh.cpp:554
std::vector< std::string > availableData() const
Returns the names of all available data.
Definition Mesh.cpp:224
const std::string & getName() const
Returns the name of the mesh, as set in the config file.
Definition Mesh.cpp:242
void removeDuplicates()
Removes all duplicate connectivity.
Definition Mesh.cpp:426
static constexpr MeshID MESH_ID_UNDEFINED
Use if the id of the mesh is not necessary.
Definition Mesh.hpp:57
std::map< Rank, std::vector< VertexID > > VertexDistribution
A mapping from rank to used (not necessarily owned) vertex IDs.
Definition Mesh.hpp:48
std::size_t nVertices() const
Returns the number of vertices.
Definition Mesh.cpp:64
bool hasConnectivity() const
Definition Mesh.hpp:126
VertexDistribution _vertexDistribution
Vertex distribution for the primary rank, holding for each secondary rank all vertex IDs it owns.
Definition Mesh.hpp:381
CommunicationMap & getCommunicationMap()
Returns a mapping from remote local connected ranks to the corresponding vertex IDs.
Definition Mesh.hpp:302
TetraContainer & tetrahedra()
Returns modifiable container holding all tetrahedra.
Definition Mesh.cpp:94
std::deque< Tetrahedron > TetraContainer
Definition Mesh.hpp:43
bool operator==(const Mesh &other) const
Definition Mesh.cpp:542
CommunicationMap ConnectionMap
Definition Mesh.hpp:52
logging::Logger _log
Definition Mesh.hpp:356
std::vector< Rank > _connectedRanks
each rank stores list of connected remote ranks. In the m2n package, this is used to create the initi...
Definition Mesh.hpp:401
bool _isJustInTime
for just-in-time mapping, we need an artificial mesh, which we can use
Definition Mesh.hpp:410
TetraContainer _tetrahedra
Definition Mesh.hpp:371
Vertex & vertex(VertexID id)
Mutable access to a vertex by VertexID.
Definition Mesh.cpp:42
bool isJustInTime() const
Definition Mesh.hpp:340
Mesh(std::string name, int dimensions, MeshID id, bool isJustInTime=false)
Constructor.
Definition Mesh.cpp:26
const query::Index & index() const
Call preprocess() before index() to ensure correct projection handling.
Definition Mesh.hpp:329
query::Index _index
Definition Mesh.hpp:414
VertexContainer _vertices
Holds vertices, edges, triangles and tetrahedra.
Definition Mesh.hpp:368
bool hasDataName(std::string_view dataName) const
Returns whether Mesh has Data with the dataName.
Definition Mesh.cpp:216
std::deque< Edge > EdgeContainer
Definition Mesh.hpp:41
PtrData & createData(const std::string &name, int dimension, DataID id, int waveformDegree=time::Time::DEFAULT_WAVEFORM_DEGREE)
Create only data for vertex.
Definition Mesh.cpp:152
std::map< int, BoundingBox > BoundingBoxMap
Definition Mesh.hpp:45
CommunicationMap _communicationMap
each rank stores list of connected ranks and corresponding vertex IDs here. In the m2n package,...
Definition Mesh.hpp:407
bool empty() const
Does the mesh contain any vertices?
Definition Mesh.hpp:88
void generateImplictPrimitives()
Definition Mesh.cpp:477
const std::vector< Rank > & getConnectedRanks() const
Returns a vector of connected ranks.
Definition Mesh.hpp:290
const VertexOffsets & getVertexOffsets() const
Definition Mesh.hpp:259
void clearPartitioning()
Clears the partitioning information.
Definition Mesh.cpp:295
void computeBoundingBox()
Computes the boundingBox for the vertices.
Definition Mesh.cpp:266
TriangleContainer _triangles
Definition Mesh.hpp:370
bool isPartitionEmpty(Rank rank) const
checks if the given ranks partition is empty
Definition Mesh.cpp:311
std::vector< int > VertexOffsets
Definition Mesh.hpp:54
Tetrahedron & createTetrahedron(Vertex &vertexOne, Vertex &vertexTwo, Vertex &vertexThree, Vertex &vertexFour)
Creates and initializes a Tetrahedron object.
Definition Mesh.cpp:142
VertexOffsets _vertexOffsets
Holds the index of the last vertex for each rank.
Definition Mesh.hpp:388
void setVertexOffsets(VertexOffsets vertexOffsets)
Only used for tests.
Definition Mesh.hpp:268
bool isValidVertexID(VertexID vertexID) const
Returns true if the given vertexID is valid.
Definition Mesh.cpp:252
EdgeContainer _edges
Definition Mesh.hpp:369
void setConnectedRanks(std::vector< Rank > ranks)
Returns a vector of connected ranks.
Definition Mesh.hpp:296
const DataContainer & data() const
Allows access to all data.
Definition Mesh.cpp:194
TriangleContainer & triangles()
Returns modifiable container holding all triangles.
Definition Mesh.cpp:79
const BoundingBox & getBoundingBox() const
Returns the bounding box of the mesh.
Definition Mesh.cpp:410
query::Index & index()
Call preprocess() before index() to ensure correct projection handling.
Definition Mesh.hpp:335
std::deque< Vertex > VertexContainer
Definition Mesh.hpp:40
Edge & createEdge(Vertex &vertexOne, Vertex &vertexTwo)
Creates and initializes an Edge object.
Definition Mesh.cpp:112
Vertex & createVertex(const Eigen::Ref< const Eigen::VectorXd > &coords)
Creates and initializes a Vertex object.
Definition Mesh.cpp:104
EdgeContainer & edges()
Returns modifiable container holding all edges.
Definition Mesh.cpp:69
void allocateDataValues()
Allocates memory for the vertex data values and corresponding gradient values.
Definition Mesh.cpp:257
bool hasTriangles() const
Definition Mesh.hpp:110
int _dimensions
Dimension of mesh.
Definition Mesh.hpp:362
Tetrahedron of a mesh, defined by 4 vertices.
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:24
Vertex of a mesh.
Definition Vertex.hpp:16
Class to query the index trees of the mesh.
Definition Index.hpp:64
static const int DEFAULT_WAVEFORM_DEGREE
To be used, when the interpolation degree is not defined.
Definition Time.hpp:8
provides Mesh, Data and primitives.
std::shared_ptr< Data > PtrData
std::ostream & operator<<(std::ostream &os, const BoundingBox &bb)
int MeshID
Definition Types.hpp:30
int VertexID
Definition Types.hpp:13
int Rank
Definition Types.hpp:37
int DataID
Definition Types.hpp:25