Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
fe_lagrange_simplex.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_FE_FE_SPACES_FE_LAGRANGE_SIMPLEX_HPP_
24#define VULPES_FE_FE_SPACES_FE_LAGRANGE_SIMPLEX_HPP_
25
26#include <cstddef>
27#include <limits>
28#include <utility>
29#include <vector>
30
36
37namespace vulpes {
38namespace fe {
39
40namespace internal {
41
45template <std::size_t Dim>
47 using real_t = mpq_class;
48 static std::vector<Point<real_t, Dim>> computeNodes(std::size_t n_pts) {
49 const auto n_pts1 = real_t(n_pts - 1);
50 std::vector<Point<real_t, Dim>> linspace;
51 for (std::size_t j = 0; j < vmath::pow(n_pts, Dim); ++j) {
53 std::size_t idx = j;
54 std::size_t idx_sum = 0;
55 for (std::size_t d = 0; d < Dim; ++d) {
56 const auto idx_d = idx % n_pts;
57 idx_sum += idx_d;
58 pt[d] = real_t(idx_d) / n_pts1;
59 idx /= n_pts;
60 }
61 if (idx_sum > (n_pts - 1)) {
62 continue;
63 }
64 linspace.push_back(pt);
65 }
66 return linspace;
67 }
68};
69
70// following code is heavily inspired from https://github.com/tcew/nodal-dg
71Eigen::VectorXd gaussLobattoRoots(Eigen::Index N);
72Eigen::VectorXd evalLegendre(const Eigen::VectorXd& x, Eigen::Index n);
73Eigen::VectorXd computeWarp(Eigen::Index N, const Eigen::VectorXd& rout);
74std::pair<Eigen::VectorXd, Eigen::VectorXd> computeWarpAndBlend(
75 Eigen::Index N,
76 double alpha,
77 const Eigen::VectorXd& L1,
78 const Eigen::VectorXd& L2,
79 const Eigen::VectorXd& L3);
80std::pair<Eigen::VectorXd, Eigen::VectorXd> nodes2D(Eigen::Index N);
81Eigen::Matrix2Xd xy2rs(const std::pair<Eigen::VectorXd, Eigen::VectorXd>& xy);
82Eigen::Vector3d normalize(const Eigen::Vector3d& v);
83Eigen::MatrixXd equinNodes3D(Eigen::Index N);
84Eigen::Matrix3Xd nodes3D(Eigen::Index p);
85Eigen::Matrix3Xd xyz2rst(const Eigen::Matrix3Xd& xyz);
86
95template <std::size_t Dim>
97 using real_t = mpf_class;
98 static std::vector<Point<real_t, Dim>> computeNodes(std::size_t n_pts) {
99 constexpr double k_tol = 2 * std::numeric_limits<double>::epsilon();
100 // we are upcasting the precision because we are taking this approximate
101 // nodes as exact. Indeed, the nodes univocally define the Lagrangian basis,
102 // thus, we take them as our ground truth even if (potentially) a better
103 // basis with smaller Lebesque constant could be found (by optimizing alpha)
104 std::vector<Point<real_t, Dim>> nodes;
105 const Eigen::Index order = static_cast<Eigen::Index>(n_pts - 1);
106 if constexpr (Dim == 1) {
107 const auto qrule_gl = qrule::gmpGaussLobattoQuadrature(n_pts, 256);
108 for (std::size_t i = 0; i < n_pts; ++i) {
109 nodes.emplace_back(qrule_gl.nodes[n_pts - i - 1].point[0]);
110 }
111 } else if constexpr (Dim == 2) {
112 const auto nodes_d = xy2rs(nodes2D(order));
113 for (Eigen::Index i = 0; i < nodes_d.cols(); ++i) {
114 real_t x = nodes_d(0, i);
115 real_t y = nodes_d(1, i);
116 // clip to border
117 if (x < k_tol) {
118 x = 0.0;
119 }
120 if (y < k_tol) {
121 y = 0.0;
122 }
123 if (x + y > 1.0 - k_tol) {
124 x /= (x + y);
125 y /= (x + y);
126 }
127 nodes.push_back(Point<real_t, Dim>{x, y});
128 }
129 } else if constexpr (Dim == 3) {
130 const auto nodes_d = xyz2rst(nodes3D(order));
131 for (Eigen::Index i = 0; i < nodes_d.cols(); ++i) {
132 // clip to border
133 const Point<real_t, Dim> node = nodes_d.col(i).template cast<real_t>();
134 Point<real_t, Dim> clipped_node = node.unaryExpr(
135 [](const real_t& v) { return v < k_tol ? real_t(0.0) : v; });
136 if (clipped_node.sum() > 1.0 - k_tol) {
137 clipped_node = clipped_node.array() / clipped_node.sum();
138 }
139 nodes.push_back(clipped_node);
140 }
141 } else {
142 static_assert(
143 Dim * 0 && "WarpAndBlendNodesPolicy not implemented for this Dim.");
144 }
145 return nodes;
146 }
147};
148} // namespace internal
149
160template <std::size_t Dim, typename DofPolicy, typename NodePolicy>
162 Dim,
163 internal::real_t,
164 DofPolicy,
165 FeSpaceLagrangeP<Dim, DofPolicy, NodePolicy>> {
166 public:
168 explicit FeSpaceLagrangeP(std::size_t order)
169 : FeSpaceBase<
170 Dim,
172 DofPolicy,
173 FeSpaceLagrangeP<Dim, DofPolicy, NodePolicy>>(order) {
174 const auto nodes = NodePolicy::computeNodes(order + 1);
175 const auto coef = internal::computeLagrangianPolyCoef<Dim>(nodes);
176 m_polys.reserve(coef.cols());
177 for (int64_t basis_id = 0; basis_id < coef.cols(); ++basis_id) {
178 // create a new polynomial with rows coefficients
179 m_polys.emplace_back(coef.rows());
180 for (int64_t mono_id = 0; mono_id < coef.rows(); ++mono_id) {
181 // get the exponents of the current monomial
182 const std::array<std::size_t, Dim> exps =
184 // set the coefficient of the polynomial (dimension independent)
185 std::apply(
186 [&](auto... args) {
187 m_polys.back().coef(args...) =
188 vmath::to<internal::real_t>(coef(mono_id, basis_id));
189 },
190 exps);
191 }
192 }
193
194 // save nodes
195 for (const auto& pt : nodes) {
196 m_nodes.push_back(pt.template cast<internal::real_t>());
197 }
198 }
199
200 const auto& getNodes() const { return m_nodes; }
201 const auto& getBasis() const { return m_polys; }
202 inline static constexpr FeType elemType() { return FeType::Simplex; };
203
204 template <typename CellType>
205 inline static constexpr auto getMapping(const CellType& cell) {
206 // TODO: compute simplex mapping
207 return 0;
208 };
209
210 protected:
211 std::vector<Polynomial> m_polys;
212 std::vector<Point<internal::real_t, Dim>> m_nodes;
213};
214
218template <std::size_t Dim>
220 Dim,
223
227template <std::size_t Dim>
229 Dim,
232
236template <std::size_t Dim>
238 Dim,
241
245template <std::size_t Dim>
247 Dim,
250
251} // namespace fe
252} // namespace vulpes
253
254#endif // VULPES_FE_FE_SPACES_FE_LAGRANGE_SIMPLEX_HPP_
General finite element space on a simplex with (nodal) Lagrangian basis function, can be either conti...
Definition fe_lagrange_simplex.hpp:165
static constexpr FeType elemType()
Definition fe_lagrange_simplex.hpp:202
std::vector< Point< internal::real_t, Dim > > m_nodes
Definition fe_lagrange_simplex.hpp:212
FeSpaceLagrangeP(std::size_t order)
Definition fe_lagrange_simplex.hpp:168
polynomial::NdPolynomial< internal::real_t, Dim > Polynomial
Definition fe_lagrange_simplex.hpp:167
const auto & getBasis() const
Definition fe_lagrange_simplex.hpp:201
static constexpr auto getMapping(const CellType &cell)
Definition fe_lagrange_simplex.hpp:205
const auto & getNodes() const
Definition fe_lagrange_simplex.hpp:200
Definition fe_expr_base.hpp:58
Eigen::VectorXd gaussLobattoRoots(Eigen::Index N)
Definition fe_lagrange_simplex.cpp:40
Eigen::Matrix3Xd xyz2rst(const Eigen::Matrix3Xd &xyz)
Definition fe_lagrange_simplex.cpp:264
Eigen::VectorXd evalLegendre(const Eigen::VectorXd &x, Eigen::Index n)
Definition fe_lagrange_simplex.cpp:49
std::pair< Eigen::VectorXd, Eigen::VectorXd > computeWarpAndBlend(Eigen::Index N, double alpha, const Eigen::VectorXd &L1, const Eigen::VectorXd &L2, const Eigen::VectorXd &L3)
Definition fe_lagrange_simplex.cpp:94
Eigen::Matrix3Xd nodes3D(Eigen::Index p)
Definition fe_lagrange_simplex.cpp:189
Eigen::MatrixXd equinNodes3D(Eigen::Index N)
Definition fe_lagrange_simplex.cpp:169
Eigen::Matrix2Xd xy2rs(const std::pair< Eigen::VectorXd, Eigen::VectorXd > &xy)
Definition fe_lagrange_simplex.cpp:151
std::pair< Eigen::VectorXd, Eigen::VectorXd > nodes2D(Eigen::Index N)
Definition fe_lagrange_simplex.cpp:126
Eigen::Vector3d normalize(const Eigen::Vector3d &v)
Definition fe_lagrange_simplex.cpp:165
Eigen::Matrix< FloatType, -1, -1 > computeLagrangianPolyCoef(const std::vector< Point< FloatType, Dim > > &points)
: Compute the coefficients of interpolatory lagrangian polynomials of a set of points
Definition fe_space.hpp:245
Eigen::VectorXd computeWarp(Eigen::Index N, const Eigen::VectorXd &rout)
Definition fe_lagrange_simplex.cpp:71
Definition dof_policy.hpp:45
FeType
Definition fe_space.hpp:48
@ Simplex
Definition fe_space.hpp:48
FeSpaceLagrangeP< Dim, DofPolicyFeSpaceCP< Dim >, internal::WarpAndBlendNodesPolicy< Dim > > FeSpaceSpectralCP
Continuous spectral finite element space with optimal conditioning.
Definition fe_lagrange_simplex.hpp:237
geometry::Cell< Dim, double, int32_t > CellType
Definition fe_expr_base.hpp:41
FeSpaceLagrangeP< Dim, DofPolicyFeSpaceCP< Dim >, internal::LagrangianNodesPolicy< Dim > > FeSpaceLagrangeCP
Continuous lagrangian finite element space with equi-spaced nodes.
Definition fe_lagrange_simplex.hpp:219
geometry::Point< DataType, Dim > Point
Definition fe_space.hpp:43
FeSpaceLagrangeP< Dim, DofPolicyFeSpaceDP< Dim >, internal::WarpAndBlendNodesPolicy< Dim > > FeSpaceSpectralDP
Discontinuous spectral finite element space with optimal conditioning.
Definition fe_lagrange_simplex.hpp:246
FeSpaceLagrangeP< Dim, DofPolicyFeSpaceDP< Dim >, internal::LagrangianNodesPolicy< Dim > > FeSpaceLagrangeDP
Discontinuous lagrangian finite element space with equi-spaced nodes.
Definition fe_lagrange_simplex.hpp:228
std::array< T, Dim > getMonomial(std::size_t n)
Computes the Dim-D monomial exponents for a given linear index.
Definition monomial.hpp:199
std::conditional_t< Dim==1, Polynomial< T >, std::conditional_t< Dim==2, Polynomial2D< T, IsSpaceP >, Polynomial3D< T, IsSpaceP > > > NdPolynomial
wrapper for dense polynomials in 1D, 2D and 3D
Definition ndpolynomial.hpp:41
QuadratureRule1Dmp gmpGaussLobattoQuadrature(std::size_t n, std::size_t precision)
Generates a Gauss-Lobatto quadrature rule in 1D with arbitrary-precision arithmetic.
Definition gauss.cpp:112
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
constexpr U pow(U base, V exp, U result=U(1))
Computes the power of a number.
Definition vmath.hpp:171
Definition cell_manager.hpp:38
Definition dof_policy.hpp:429
Definition dof_policy.hpp:671
Compute the equi-spaced nodes on a simplex in Dim dimensions.
Definition fe_lagrange_simplex.hpp:46
static std::vector< Point< real_t, Dim > > computeNodes(std::size_t n_pts)
Definition fe_lagrange_simplex.hpp:48
mpq_class real_t
Definition fe_lagrange_simplex.hpp:47
Compute the warp&blend nodes on a simplex in Dim dimensions See: Warburton, Tim. "An explicit constru...
Definition fe_lagrange_simplex.hpp:96
static std::vector< Point< real_t, Dim > > computeNodes(std::size_t n_pts)
Definition fe_lagrange_simplex.hpp:98
mpf_class real_t
Definition fe_lagrange_simplex.hpp:97