hp-FEM
Adaptive hp-FEM suite for nano-optics
Loading...
Searching...
No Matches
reference_element.hpp
Go to the documentation of this file.
1#pragma once
7
8#include <array>
9#include <cmath>
10#include <cstddef>
11
12#include "hpfem/core/error.hpp"
13#include "hpfem/core/types.hpp"
15
16namespace hpfem::fespace {
17
19template <int Dim>
21 static_assert(Dim == 2 || Dim == 3, "ReferenceElement exists for Dim = 2 and Dim = 3");
22
24 using FacetPoint = Point<Dim - 1>;
25
26 static constexpr int kDim = Dim;
27 static constexpr LocalIndex kNumVertices = Dim + 1;
28 static constexpr LocalIndex kNumEdges = Topology::kNumEdges;
29 static constexpr LocalIndex kNumFacets = Dim + 1;
31 static constexpr const auto& kEdgeVertices = Topology::kEdgeVertices;
32 static constexpr const auto& kFacetVertices = Topology::kFacetVertices;
33
35 [[nodiscard]] static Point<Dim> vertex(LocalIndex i) {
36 HPFEM_ASSERT(i >= 0 && i < kNumVertices, "vertex index out of range");
38 if (i > 0) x(i - 1) = 1.0;
39 return x;
40 }
42 [[nodiscard]] static constexpr Real volume() noexcept { return Dim == 2 ? 0.5 : 1.0 / 6.0; }
43 [[nodiscard]] static Point<Dim> centroid() {
44 return Point<Dim>::Constant(1.0 / static_cast<Real>(Dim + 1));
45 }
46
48 [[nodiscard]] static std::array<Real, static_cast<std::size_t>(Dim + 1)> barycentric(
49 const Point<Dim>& xi) {
50 std::array<Real, static_cast<std::size_t>(Dim + 1)> lambda{};
51 lambda[0] = 1.0;
52 for (int d = 0; d < Dim; ++d) {
53 lambda[static_cast<std::size_t>(d + 1)] = xi(d);
54 lambda[0] -= xi(d);
55 }
56 return lambda;
57 }
59 [[nodiscard]] static std::array<Point<Dim>, static_cast<std::size_t>(Dim + 1)>
61 std::array<Point<Dim>, static_cast<std::size_t>(Dim + 1)> grad;
62 grad[0] = -Point<Dim>::Ones();
63 for (int d = 0; d < Dim; ++d) {
64 grad[static_cast<std::size_t>(d + 1)] = Point<Dim>::Zero();
65 grad[static_cast<std::size_t>(d + 1)](d) = 1.0;
66 }
67 return grad;
68 }
70 [[nodiscard]] static bool contains(const Point<Dim>& xi, Real tol = 1e-12) {
71 for (const Real l : barycentric(xi)) {
72 if (l < -tol) return false;
73 }
74 return true;
75 }
76
78 [[nodiscard]] static Point<Dim> facet_normal(LocalIndex k) {
79 HPFEM_ASSERT(k >= 0 && k < kNumFacets, "facet index out of range");
80 // The facet opposite vertex i has normal -e_i (i > 0) or (1,...,1)/sqrt(Dim) (i = 0).
81 const LocalIndex opposite = opposite_vertex(k);
82 if (opposite == 0) return Point<Dim>::Constant(1.0 / std::sqrt(static_cast<Real>(Dim)));
84 n(opposite - 1) = -1.0;
85 return n;
86 }
88 [[nodiscard]] static Real facet_measure(LocalIndex k) {
89 HPFEM_ASSERT(k >= 0 && k < kNumFacets, "facet index out of range");
90 const bool slanted = opposite_vertex(k) == 0;
91 if constexpr (Dim == 2) {
92 return slanted ? std::sqrt(2.0) : 1.0;
93 } else {
94 return slanted ? std::sqrt(3.0) / 2.0 : 0.5;
95 }
96 }
98 [[nodiscard]] static LocalIndex opposite_vertex(LocalIndex k) {
99 const auto& fv = kFacetVertices[static_cast<std::size_t>(k)];
100 LocalIndex sum = 0;
101 for (const LocalIndex v : fv) sum += v;
102 return static_cast<LocalIndex>(Dim * (Dim + 1) / 2 - sum); // all vertices sum to Dim(Dim+1)/2
103 }
104
107 [[nodiscard]] static Point<Dim> facet_point(LocalIndex k, const FacetPoint& eta) {
108 HPFEM_ASSERT(k >= 0 && k < kNumFacets, "facet index out of range");
109 const auto& fv = kFacetVertices[static_cast<std::size_t>(k)];
110 Point<Dim> x = vertex(fv[0]);
111 for (int j = 0; j < Dim - 1; ++j) {
112 x += eta(j) * (vertex(fv[static_cast<std::size_t>(j + 1)]) - vertex(fv[0]));
113 }
114 return x;
115 }
117 [[nodiscard]] static Point<Dim> edge_point(LocalIndex k, Real t) {
118 HPFEM_ASSERT(k >= 0 && k < kNumEdges, "edge index out of range");
119 const auto& ev = kEdgeVertices[static_cast<std::size_t>(k)];
120 return vertex(ev[0]) + t * (vertex(ev[1]) - vertex(ev[0]));
121 }
122};
123
124} // namespace hpfem::fespace
#define HPFEM_ASSERT(cond, msg)
Definition error.hpp:48
Definition cell_layout.hpp:13
double Real
Definition types.hpp:15
Eigen::Matrix< Real, Dim, 1 > Point
Definition types.hpp:27
std::int32_t LocalIndex
element-local indices
Definition types.hpp:18
Geometry and numbering of the reference simplex in Dim dimensions.
Definition reference_element.hpp:20
static std::array< Point< Dim >, static_cast< std::size_t >(Dim+1)> barycentric_gradients()
Constant reference gradients .
Definition reference_element.hpp:60
static constexpr LocalIndex kNumEdges
Definition reference_element.hpp:28
static constexpr Real volume() noexcept
Measure of the reference simplex: 1/2 (triangle), 1/6 (tetrahedron).
Definition reference_element.hpp:42
static bool contains(const Point< Dim > &xi, Real tol=1e-12)
True if all barycentric coordinates are (closed simplex).
Definition reference_element.hpp:70
static constexpr LocalIndex kNumFacets
Definition reference_element.hpp:29
Point< Dim - 1 > FacetPoint
point of the reference facet ([0,1] or triangle)
Definition reference_element.hpp:24
static Point< Dim > edge_point(LocalIndex k, Real t)
Point at parameter t ∈ [0,1] on local edge k, from kEdgeVertices[k][0] to [1].
Definition reference_element.hpp:117
static Point< Dim > facet_normal(LocalIndex k)
Outward unit normal of local facet k.
Definition reference_element.hpp:78
static Point< Dim > vertex(LocalIndex i)
Vertex i: , the i-th unit vector.
Definition reference_element.hpp:35
static constexpr const auto & kEdgeVertices
Local vertices of each edge / facet: the binding tables of mesh::SimplexTopology.
Definition reference_element.hpp:31
static Real facet_measure(LocalIndex k)
Measure of local facet k in reference coordinates (length in 2D, area in 3D).
Definition reference_element.hpp:88
static Point< Dim > facet_point(LocalIndex k, const FacetPoint &eta)
Definition reference_element.hpp:107
static Point< Dim > centroid()
Definition reference_element.hpp:43
static std::array< Real, static_cast< std::size_t >(Dim+1)> barycentric(const Point< Dim > &xi)
Barycentric coordinates .
Definition reference_element.hpp:48
static LocalIndex opposite_vertex(LocalIndex k)
Local vertex opposite facet k (the one not contained in it).
Definition reference_element.hpp:98
static constexpr int kDim
Definition reference_element.hpp:26
static constexpr const auto & kFacetVertices
Definition reference_element.hpp:32
static constexpr LocalIndex kNumVertices
Definition reference_element.hpp:27
Local topology tables of the reference simplex in Dim dimensions.
Definition simplex_topology.hpp:27