hp-FEM
Adaptive hp-FEM suite for nano-optics
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1#pragma once
5
6#include <array>
7#include <cstddef>
8#include <cstdint>
9#include <map>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14#include <vector>
15
16#include "hpfem/core/error.hpp"
17#include "hpfem/core/types.hpp"
19
20namespace hpfem::mesh {
21
23using Tag = std::int32_t;
24inline constexpr Tag kNoTag = 0;
25
60template <int Dim>
61class Mesh {
62 static_assert(Dim == 2 || Dim == 3, "Mesh is implemented for Dim = 2 and Dim = 3");
63
64 public:
67 using CellVertices = std::array<Index, static_cast<std::size_t>(Dim + 1)>;
68 using EdgeVertices = std::array<Index, 2>;
69 using FaceVertices = std::array<Index, 3>;
70 using FacetVertices = std::array<Index, static_cast<std::size_t>(Dim)>;
71
72 static constexpr LocalIndex kVerticesPerCell = Dim + 1;
73 static constexpr LocalIndex kEdgesPerCell = Topology::kNumEdges;
74 static constexpr LocalIndex kFacesPerCell = 4;
75 static constexpr LocalIndex kFacetsPerCell = Dim + 1;
76
77 using CellEdges = std::array<Index, static_cast<std::size_t>(kEdgesPerCell)>;
78 using CellEdgeFlags = std::array<bool, static_cast<std::size_t>(kEdgesPerCell)>;
79 using CellFaces = std::array<Index, static_cast<std::size_t>(kFacesPerCell)>;
80 using CellFacePermutations = std::array<std::uint8_t, static_cast<std::size_t>(kFacesPerCell)>;
81 using CellFacets = std::array<Index, static_cast<std::size_t>(kFacetsPerCell)>;
83 using FacetCells = std::array<Index, 2>;
84 using FacetLocalIndices = std::array<LocalIndex, 2>;
85
89 Mesh(std::vector<Vertex> vertices, std::vector<CellVertices> cells);
92 Mesh(std::vector<Vertex> vertices, std::vector<CellVertices> cells, std::vector<Tag> cell_tags);
93
94 // --- counts ----------------------------------------------------------------------------
95
96 [[nodiscard]] Index num_vertices() const noexcept { return static_cast<Index>(vertices_.size()); }
97 [[nodiscard]] Index num_edges() const noexcept { return static_cast<Index>(edges_.size()); }
98 [[nodiscard]] Index num_faces() const noexcept
99 requires(Dim == 3)
100 {
101 return static_cast<Index>(faces_.size());
102 }
104 [[nodiscard]] Index num_facets() const noexcept {
105 if constexpr (Dim == 2) {
106 return num_edges();
107 } else {
108 return num_faces();
109 }
110 }
111 [[nodiscard]] Index num_cells() const noexcept { return static_cast<Index>(cells_.size()); }
112
113 // --- vertices --------------------------------------------------------------------------
114
115 [[nodiscard]] const Vertex& vertex(Index v) const {
116 HPFEM_ASSERT(v >= 0 && v < num_vertices(), "vertex index out of range");
117 return vertices_[as_size(v)];
118 }
119 [[nodiscard]] std::span<const Vertex> vertices() const noexcept { return vertices_; }
124 void set_vertex(Index v, const Vertex& x);
125
126 // --- cells -----------------------------------------------------------------------------
127
129 [[nodiscard]] const CellVertices& cell_vertices(Index c) const {
130 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
131 return cells_[as_size(c)];
132 }
133 [[nodiscard]] std::span<const CellVertices> cells() const noexcept { return cells_; }
134
136 [[nodiscard]] const CellEdges& cell_edges(Index c) const {
137 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
138 return cell_edges_[as_size(c)];
139 }
142 [[nodiscard]] const CellEdgeFlags& cell_edge_flipped(Index c) const {
143 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
144 return cell_edge_flipped_[as_size(c)];
145 }
146
148 [[nodiscard]] const CellFaces& cell_faces(Index c) const
149 requires(Dim == 3)
150 {
151 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
152 return cell_faces_[as_size(c)];
153 }
157 requires(Dim == 3)
158 {
159 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
160 return cell_face_permutations_[as_size(c)];
161 }
162
164 [[nodiscard]] const CellFacets& cell_facets(Index c) const {
165 if constexpr (Dim == 2) {
166 return cell_edges(c);
167 } else {
168 return cell_faces(c);
169 }
170 }
171
172 // --- edges and faces -------------------------------------------------------------------
173
175 [[nodiscard]] const EdgeVertices& edge_vertices(Index e) const {
176 HPFEM_ASSERT(e >= 0 && e < num_edges(), "edge index out of range");
177 return edges_[as_size(e)];
178 }
179 [[nodiscard]] std::span<const EdgeVertices> edges() const noexcept { return edges_; }
180
182 [[nodiscard]] const FaceVertices& face_vertices(Index f) const
183 requires(Dim == 3)
184 {
185 HPFEM_ASSERT(f >= 0 && f < num_faces(), "face index out of range");
186 return faces_[as_size(f)];
187 }
188 [[nodiscard]] std::span<const FaceVertices> faces() const noexcept
189 requires(Dim == 3)
190 {
191 return faces_;
192 }
193
195 [[nodiscard]] const FacetVertices& facet_vertices(Index f) const {
196 if constexpr (Dim == 2) {
197 return edge_vertices(f);
198 } else {
199 return face_vertices(f);
200 }
201 }
202
203 // --- connectivity ----------------------------------------------------------------------
204
207 [[nodiscard]] const FacetCells& facet_cells(Index f) const {
208 HPFEM_ASSERT(f >= 0 && f < num_facets(), "facet index out of range");
209 return facet_cells_[as_size(f)];
210 }
212 [[nodiscard]] const FacetLocalIndices& facet_local_indices(Index f) const {
213 HPFEM_ASSERT(f >= 0 && f < num_facets(), "facet index out of range");
214 return facet_local_indices_[as_size(f)];
215 }
217 [[nodiscard]] bool is_boundary_facet(Index f) const {
219 }
221 [[nodiscard]] std::span<const Index> boundary_facets() const noexcept { return boundary_facets_; }
222 [[nodiscard]] Index num_boundary_facets() const noexcept {
223 return static_cast<Index>(boundary_facets_.size());
224 }
226 [[nodiscard]] const CellNeighbors& cell_neighbors(Index c) const {
227 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
228 return cell_neighbors_[as_size(c)];
229 }
231 [[nodiscard]] std::span<const Index> edge_cells(Index e) const {
232 HPFEM_ASSERT(e >= 0 && e < num_edges(), "edge index out of range");
233 const auto begin = as_size(edge_cell_offsets_[as_size(e)]);
234 const auto end = as_size(edge_cell_offsets_[as_size(e) + 1]);
235 return std::span<const Index>(edge_cell_data_).subspan(begin, end - begin);
236 }
237
238 // --- hanging entities (one-irregular local refinement) ---------------------------------
239
242 struct HangingEdge {
244 std::array<Index, 2> children;
246 };
249 struct HangingFace {
251 std::array<Index, 4> children;
252 };
253 enum class HangingRole : std::uint8_t { kNone, kParent, kChild };
254
260 void set_hanging(std::vector<HangingEdge> edges, std::vector<HangingFace> faces);
261 [[nodiscard]] std::span<const HangingEdge> hanging_edges() const noexcept {
262 return hanging_edges_;
263 }
264 [[nodiscard]] std::span<const HangingFace> hanging_faces() const noexcept {
265 return hanging_faces_;
266 }
268 [[nodiscard]] bool is_conforming() const noexcept { return hanging_edges_.empty(); }
270 [[nodiscard]] HangingRole facet_hanging_role(Index f) const {
271 HPFEM_ASSERT(f >= 0 && f < num_facets(), "facet index out of range");
272 return facet_hanging_role_.empty() ? HangingRole::kNone
273 : static_cast<HangingRole>(facet_hanging_role_[as_size(f)]);
274 }
276 [[nodiscard]] Index hanging_parent_facet(Index f) const {
277 return facet_hanging_role(f) == HangingRole::kChild ? facet_hanging_link_[as_size(f)]
279 }
282 [[nodiscard]] std::span<const Index> hanging_child_facets(Index f) const;
283
284 // --- lookup by vertices ----------------------------------------------------------------
285
287 [[nodiscard]] Index edge_id(Index a, Index b) const;
289 [[nodiscard]] Index face_id(Index a, Index b, Index c) const
290 requires(Dim == 3);
292 [[nodiscard]] Index facet_id(FacetVertices vertices) const;
293
294 // --- tags (physical groups) ------------------------------------------------------------
295
296 [[nodiscard]] Tag cell_tag(Index c) const {
297 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
298 return cell_tags_[as_size(c)];
299 }
300 [[nodiscard]] std::span<const Tag> cell_tags() const noexcept { return cell_tags_; }
301 void set_cell_tag(Index c, Tag tag) {
302 HPFEM_ASSERT(c >= 0 && c < num_cells(), "cell index out of range");
303 cell_tags_[as_size(c)] = tag;
304 }
305 [[nodiscard]] Tag facet_tag(Index f) const {
306 HPFEM_ASSERT(f >= 0 && f < num_facets(), "facet index out of range");
307 return facet_tags_[as_size(f)];
308 }
309 [[nodiscard]] std::span<const Tag> facet_tags() const noexcept { return facet_tags_; }
310 void set_facet_tag(Index f, Tag tag) {
311 HPFEM_ASSERT(f >= 0 && f < num_facets(), "facet index out of range");
312 facet_tags_[as_size(f)] = tag;
313 }
318 void set_facet_tags(std::span<const FacetVertices> facets, std::span<const Tag> tags);
322 [[nodiscard]] std::vector<Index> cells_with_tag(Tag tag) const;
323 [[nodiscard]] std::vector<Index> facets_with_tag(Tag tag) const;
324
325 // --- geometry order --------------------------------------------------------------------
326
328 [[nodiscard]] int geometry_order() const noexcept { return edge_nodes_.empty() ? 1 : 2; }
334 void set_edge_nodes(std::vector<Vertex> nodes);
336 [[nodiscard]] const Vertex& edge_node(Index e) const {
337 HPFEM_ASSERT(geometry_order() == 2, "mesh has no second-order edge nodes");
338 HPFEM_ASSERT(e >= 0 && e < num_edges(), "edge index out of range");
339 return edge_nodes_[as_size(e)];
340 }
341 [[nodiscard]] std::span<const Vertex> edge_nodes() const noexcept { return edge_nodes_; }
342
346 void set_tag_name(int dim, Tag tag, std::string name);
347 [[nodiscard]] const std::string& tag_name(int dim, Tag tag) const;
348 [[nodiscard]] std::optional<Tag> tag_by_name(int dim, std::string_view name) const;
350 [[nodiscard]] const std::map<Tag, std::string>& tag_names(int dim) const {
351 return tag_names_[names_slot(dim)];
352 }
353
354 private:
356 [[nodiscard]] static std::size_t names_slot(int dim);
357 void validate() const;
358 void derive_edges();
359 void derive_faces(); // no-op for Dim == 2
360 void derive_connectivity();
361
362 std::vector<Vertex> vertices_;
363 std::vector<CellVertices> cells_;
364
365 std::vector<EdgeVertices> edges_;
366 std::vector<CellEdges> cell_edges_;
367 std::vector<CellEdgeFlags> cell_edge_flipped_;
368
369 std::vector<FaceVertices> faces_;
370 std::vector<CellFaces> cell_faces_;
371 std::vector<CellFacePermutations> cell_face_permutations_;
372
373 std::vector<FacetCells> facet_cells_;
374 std::vector<FacetLocalIndices> facet_local_indices_;
375 std::vector<CellNeighbors> cell_neighbors_;
376 std::vector<Index> boundary_facets_;
377 std::vector<Index> edge_cell_offsets_;
378 std::vector<Index> edge_cell_data_;
379
380 std::vector<HangingEdge> hanging_edges_;
381 std::vector<HangingFace> hanging_faces_;
382 std::vector<std::uint8_t> facet_hanging_role_;
383 std::vector<Index> facet_hanging_link_;
384
385 std::vector<Tag> cell_tags_;
386 std::vector<Tag> facet_tags_;
387 std::vector<Vertex> edge_nodes_;
388 std::array<std::map<Tag, std::string>, 2> tag_names_;
389};
390
391extern template class Mesh<2>;
392extern template class Mesh<3>;
393
394} // namespace hpfem::mesh
Definition mesh.hpp:61
const std::string & tag_name(int dim, Tag tag) const
Index num_faces() const noexcept
Definition mesh.hpp:98
void set_tag_name(int dim, Tag tag, std::string name)
const Vertex & edge_node(Index e) const
Second-order node of edge e; requires geometry_order() == 2.
Definition mesh.hpp:336
std::span< const Vertex > edge_nodes() const noexcept
Definition mesh.hpp:341
const EdgeVertices & edge_vertices(Index e) const
Global vertex indices of edge e, ascending; the edge is oriented from [0] to [1].
Definition mesh.hpp:175
const std::map< Tag, std::string > & tag_names(int dim) const
All named tags of entity dimension dim.
Definition mesh.hpp:350
Index num_cells() const noexcept
Definition mesh.hpp:111
void set_edge_nodes(std::vector< Vertex > nodes)
std::vector< Index > cells_with_tag(Tag tag) const
Ids of all cells / facets carrying tag, ascending. O(N).
Tag cell_tag(Index c) const
Definition mesh.hpp:296
std::span< const FaceVertices > faces() const noexcept
Definition mesh.hpp:188
void set_facet_tags(std::span< const FacetVertices > facets, std::span< const Tag > tags)
std::vector< Index > facets_with_tag(Tag tag) const
CellFacets CellNeighbors
Definition mesh.hpp:82
Mesh(std::vector< Vertex > vertices, std::vector< CellVertices > cells, std::vector< Tag > cell_tags)
const CellNeighbors & cell_neighbors(Index c) const
Neighbour of cell c across its local facet k, kInvalidIndex on the boundary.
Definition mesh.hpp:226
void set_cell_tag(Index c, Tag tag)
Definition mesh.hpp:301
const CellFacePermutations & cell_face_permutations(Index c) const
Definition mesh.hpp:156
std::span< const CellVertices > cells() const noexcept
Definition mesh.hpp:133
std::span< const Index > edge_cells(Index e) const
All cells containing edge e, ascending (one or two in 2D, any number in 3D).
Definition mesh.hpp:231
std::array< Index, static_cast< std::size_t >(Dim+1)> CellVertices
Definition mesh.hpp:67
Tag facet_tag(Index f) const
Definition mesh.hpp:305
Mesh(std::vector< Vertex > vertices, std::vector< CellVertices > cells)
bool is_conforming() const noexcept
True if the mesh has no hanging entities.
Definition mesh.hpp:268
void set_vertex(Index v, const Vertex &x)
std::span< const EdgeVertices > edges() const noexcept
Definition mesh.hpp:179
int geometry_order() const noexcept
1: affine cells (vertices only); 2: one extra node per edge (set_edge_nodes).
Definition mesh.hpp:328
std::array< Index, 2 > EdgeVertices
Definition mesh.hpp:68
const CellEdgeFlags & cell_edge_flipped(Index c) const
Definition mesh.hpp:142
static constexpr LocalIndex kFacetsPerCell
Definition mesh.hpp:75
const CellFacets & cell_facets(Index c) const
Global facet ids of cell c: cell_edges(c) in 2D, cell_faces(c) in 3D.
Definition mesh.hpp:164
const CellFaces & cell_faces(Index c) const
Global face ids of cell c, indexed by local face number (3D only).
Definition mesh.hpp:148
std::array< std::uint8_t, static_cast< std::size_t >(kFacesPerCell)> CellFacePermutations
Definition mesh.hpp:80
static constexpr LocalIndex kFacesPerCell
meaningful for Dim = 3 only
Definition mesh.hpp:74
Index num_edges() const noexcept
Definition mesh.hpp:97
HangingRole
Definition mesh.hpp:253
static constexpr LocalIndex kEdgesPerCell
Definition mesh.hpp:73
std::span< const Tag > facet_tags() const noexcept
Definition mesh.hpp:309
std::span< const Tag > cell_tags() const noexcept
Definition mesh.hpp:300
const FacetVertices & facet_vertices(Index f) const
Global vertex indices of facet f, ascending: an edge in 2D, a face in 3D.
Definition mesh.hpp:195
std::span< const Index > hanging_child_facets(Index f) const
Index num_boundary_facets() const noexcept
Definition mesh.hpp:222
static constexpr LocalIndex kVerticesPerCell
Definition mesh.hpp:72
Index hanging_parent_facet(Index f) const
Parent facet of a hanging child facet, kInvalidIndex for any other facet.
Definition mesh.hpp:276
std::array< Index, static_cast< std::size_t >(kFacetsPerCell)> CellFacets
Definition mesh.hpp:81
std::optional< Tag > tag_by_name(int dim, std::string_view name) const
void set_facet_tag(Index f, Tag tag)
Definition mesh.hpp:310
std::span< const HangingEdge > hanging_edges() const noexcept
Definition mesh.hpp:261
void set_hanging(std::vector< HangingEdge > edges, std::vector< HangingFace > faces)
const FacetCells & facet_cells(Index f) const
Definition mesh.hpp:207
std::span< const HangingFace > hanging_faces() const noexcept
Definition mesh.hpp:264
const CellVertices & cell_vertices(Index c) const
Global vertex indices of cell c in local order (SimplexTopology<Dim>).
Definition mesh.hpp:129
std::array< Index, static_cast< std::size_t >(kFacesPerCell)> CellFaces
Definition mesh.hpp:79
std::array< bool, static_cast< std::size_t >(kEdgesPerCell)> CellEdgeFlags
Definition mesh.hpp:78
std::span< const Vertex > vertices() const noexcept
Definition mesh.hpp:119
std::array< Index, static_cast< std::size_t >(kEdgesPerCell)> CellEdges
Definition mesh.hpp:77
std::span< const Index > boundary_facets() const noexcept
Ids of all boundary facets, ascending.
Definition mesh.hpp:221
Index tag_boundary(Tag tag)
Assigns tag to every boundary facet that is still untagged; returns how many.
Index num_facets() const noexcept
Number of codimension-1 entities: edges in 2D, faces in 3D.
Definition mesh.hpp:104
std::array< Index, 3 > FaceVertices
Definition mesh.hpp:69
Index face_id(Index a, Index b, Index c) const
Id of the face with vertices {a, b, c} in any order, kInvalidIndex if absent (3D).
Index edge_id(Index a, Index b) const
Id of the edge with vertices {a, b} in any order, kInvalidIndex if absent. O(log E).
const Vertex & vertex(Index v) const
Definition mesh.hpp:115
const FaceVertices & face_vertices(Index f) const
Global vertex indices of face f, ascending (3D only).
Definition mesh.hpp:182
bool is_boundary_facet(Index f) const
A facet with one cell that is neither a hanging parent nor a hanging child.
Definition mesh.hpp:217
std::array< LocalIndex, 2 > FacetLocalIndices
Definition mesh.hpp:84
const CellEdges & cell_edges(Index c) const
Global edge ids of cell c, indexed by local edge number.
Definition mesh.hpp:136
HangingRole facet_hanging_role(Index f) const
Role of facet f: hanging parent (coarse side), hanging child (fine side) or none.
Definition mesh.hpp:270
const FacetLocalIndices & facet_local_indices(Index f) const
Local facet number of facet f inside facet_cells(f)[0] and [1] (-1 if absent).
Definition mesh.hpp:212
std::array< Index, 2 > FacetCells
Definition mesh.hpp:83
std::array< Index, static_cast< std::size_t >(Dim)> FacetVertices
Definition mesh.hpp:70
Index num_vertices() const noexcept
Definition mesh.hpp:96
Point< Dim > Vertex
Definition mesh.hpp:66
Index facet_id(FacetVertices vertices) const
Id of the facet with the given vertices in any order, kInvalidIndex if absent.
#define HPFEM_ASSERT(cond, msg)
Definition error.hpp:48
Definition adaptive_mesh.hpp:19
std::int32_t Tag
Integer tag of a physical group (Gmsh convention: positive; 0 means untagged).
Definition mesh.hpp:23
constexpr Tag kNoTag
Definition mesh.hpp:24
Eigen::Matrix< Real, Dim, 1 > Point
Definition types.hpp:27
constexpr std::size_t as_size(Index i) noexcept
Converts a non-negative global or local index to a container index.
Definition types.hpp:34
std::int64_t Index
global indices (DoFs, entities)
Definition types.hpp:17
std::int32_t LocalIndex
element-local indices
Definition types.hpp:18
constexpr Index kInvalidIndex
"No entity" marker, e.g. the neighbour across a boundary facet.
Definition types.hpp:31
Definition mesh.hpp:242
std::array< Index, 2 > children
Definition mesh.hpp:244
Index parent
Definition mesh.hpp:243
Index vertex
Definition mesh.hpp:245
Definition mesh.hpp:249
std::array< Index, 4 > children
Definition mesh.hpp:251
Index parent
Definition mesh.hpp:250
Local topology tables of the reference simplex in Dim dimensions.
Definition simplex_topology.hpp:27