Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
fe_modal_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_MODAL_SIMPLEX_HPP_
24#define VULPES_FE_FE_SPACES_FE_MODAL_SIMPLEX_HPP_
25
26#include <algorithm>
27#include <vector>
28
29#include <Eigen/Dense>
30
34
35namespace vulpes {
36namespace fe {
37
38namespace internal {
39
44template <typename T>
45inline T jacobiP(
46 const T& x, std::size_t alpha, std::size_t beta, std::size_t N) {
47 const std::size_t alpha_1 = alpha + 1;
48 const std::size_t beta_1 = beta + 1;
49 const std::size_t alpha_beta_1 = alpha_1 + beta;
50 const T alpha_beta_1_T = T(alpha_beta_1);
51 //
52 const T gamma0 = T(vmath::pow(2, alpha_beta_1)) / alpha_beta_1_T *
53 T(vmath::gamma(alpha_1) * vmath::gamma(beta_1)) /
54 vmath::gamma(alpha_beta_1);
55 T jacobi_old_old = 1.0 / vmath::sqrt(gamma0);
56 if (N == 0) {
57 return jacobi_old_old;
58 }
59
60 const T alpha_1_beta_1_T = T(alpha_1 * beta_1);
61 const T alpha_beta_2_T = T(alpha_beta_1 + 1);
62 const T alpha_beta_3_T = T(alpha_beta_1 + 2);
63 const T _2 = T(2);
64 //
65 const T gamma1 = alpha_1_beta_1_T / alpha_beta_3_T * gamma0;
66 T jacobi_old =
67 (alpha_beta_2_T * x / 2 + (T(alpha) - T(beta)) / 2) / vmath::sqrt(gamma1);
68 if (N == 1) {
69 return jacobi_old;
70 }
71
72 T aold =
73 _2 / alpha_beta_2_T * vmath::sqrt(T(alpha_1_beta_1_T / alpha_beta_3_T));
74 T jacobi;
75 for (std::size_t i = 0; i < N - 1; ++i) {
76 const std::size_t h1 = 2 * (i + 1) + alpha + beta;
77 const T h1_2_T = T(h1 + 2);
78 const T anew = _2 / h1_2_T *
80 T(T((i + 2) * (i + 2 + alpha + beta) * (i + 2 + alpha) *
81 (i + 2 + beta)) /
82 T(h1 + 1) / T(h1 + 3)));
83 const T bnew = -(T(alpha * alpha) - T(beta * beta)) / T(h1) / h1_2_T;
84
85 jacobi = (1 / anew) * (-aold * jacobi_old_old + (x - bnew) * jacobi_old);
86 aold = anew;
87
88 jacobi_old_old = jacobi_old;
89 jacobi_old = jacobi;
90 }
91
92 return jacobi;
93}
94
99template <typename T>
101 const Eigen::Vector<T, 2>& ab, const std::array<std::size_t, 2>& ij) {
102 const T h1 = jacobiP(ab[0], 0, 0, ij[0]);
103 const T h2 = jacobiP(ab[1], 2 * ij[0] + 1, 0, ij[1]);
104 return vmath::sqrt(T(2.0)) * h1 * h2 * T(vmath::pow(T(1 - ab[1]), ij[0]));
105}
106
111template <typename T>
113 const Eigen::Vector<T, 3>& abc, const std::array<std::size_t, 3>& ijk) {
114 const T h1 = jacobiP(abc[0], 0, 0, ijk[0]);
115 const T h2 = jacobiP(abc[1], 2 * ijk[0] + 1, 0, ijk[1]);
116 const T h3 = jacobiP(abc[2], 2 * (ijk[0] + ijk[1]) + 2, 0, ijk[2]);
117 return T(2) * vmath::sqrt(T(2.0)) * h1 * h2 *
118 T(vmath::pow(T(1 - abc[1]), ijk[0])) * h3 *
119 vmath::pow(T(1 - abc[2]), ijk[0] + ijk[1]);
120}
121
125template <typename T>
126inline Eigen::Vector<T, 2> rs2ab(const Eigen::Vector<T, 2>& rs) {
127 if (rs[1] == T(1)) {
128 return {T(-1), rs[1]};
129 }
130 return {2 * (1 + rs[0]) / (T(1) - rs[1]) - 1, rs[1]};
131}
132
136template <typename T>
137inline Eigen::Vector<T, 3> rst2abc(const Eigen::Vector<T, 3>& rst) {
138 const T a = (rst[1] + rst[2] != T(0))
139 ? 2 * (1 + rst[0]) / (-rst[1] - rst[2]) - 1
140 : T(-1);
141 const T b = (rst[2] != T(1)) ? 2 * (1 + rst[1]) / (1 - rst[2]) - 1 : T(-1);
142 return {a, b, rst[2]};
143}
144
149template <typename T, std::size_t Dim>
150T evalSimplex(const Eigen::Vector<T, Dim>& x, std::size_t n) {
151 if constexpr (Dim == 1) {
152 return jacobiP(x[0], 0, 0, n) * vmath::sqrt(T(2.0 / T(2 * n + 1)));
153 } else if constexpr (Dim == 2) {
155 } else if constexpr (Dim == 3) {
157 } else {
158 static_assert(Dim * 0 && "evalSimplex not implemented for this Dim");
159 }
160}
161
166template <typename T, std::size_t Dim>
167Eigen::Vector<T, -1> interpolateFunctionAtNodes(
168 const std::vector<Eigen::Vector<T, Dim>>& nodes,
169 const std::function<T(const Eigen::Vector<T, Dim>&)>& f,
170 std::size_t deg) {
171 using MatrixType = Eigen::Matrix<T, -1, -1>;
172 const std::size_t n_monomials = vmath::binomial(deg + Dim, Dim);
173 MatrixType vandermonde = MatrixType::Ones(nodes.size(), n_monomials);
174 assert(vandermonde.cols() <= vandermonde.rows());
175 for (std::size_t j = 0; j < n_monomials; ++j) {
176 const auto exps = monomial::getMonomial<Dim, std::size_t>(j);
177 for (std::size_t i = 0; i < nodes.size(); ++i) {
178 const auto& pt = nodes[i];
179 for (std::size_t d = 0; d < Dim; ++d) {
180 vandermonde(i, j) *= vmath::pow(pt[d], exps[d]);
181 }
182 }
183 }
184 Eigen::Vector<T, -1> evals(nodes.size());
185 for (std::size_t i = 0; i < nodes.size(); ++i) {
186 evals(i) = f(nodes[i]);
187 }
188
189 Eigen::ColPivHouseholderQR<MatrixType> qr_decomp(vandermonde);
190 const Eigen::Vector<T, -1> coef = qr_decomp.solve(evals);
191#ifndef DNDEBUG
192 const Eigen::Vector<T, -1> res = vandermonde * coef - evals;
193 assert(res.norm() < 1e-30);
194#endif
195 return coef;
196}
197
198} // namespace internal
199
203template <std::size_t Dim, typename DofPolicy>
205 Dim,
206 internal::real_t,
207 DofPolicy,
208 FeSpaceModalP<Dim, DofPolicy>> {
209 public:
211 explicit FeSpaceModalP(std::size_t order)
212 : FeSpaceBase<
213 Dim,
215 DofPolicy,
216 FeSpaceModalP<Dim, DofPolicy>>(order) {
217 const std::size_t basis_size = vmath::binomial(order + Dim, Dim);
218 for (std::size_t i = 0; i < basis_size; ++i) {
219 const auto exp = monomial::getMonomial<Dim, std::size_t>(i);
220 const auto sum = std::accumulate(exp.begin(), exp.end(), 0);
222 std::max(sum + 1, 2));
223 std::vector<Eigen::Vector<internal::real_t, Dim>> nodes;
224 constexpr double delta = 0.1;
225 for (const auto& pt : nodes_mpq) {
226 nodes.push_back(
227 pt.template cast<mpf_class>().array() /
228 internal::real_t(1 - 2 * delta) +
229 internal::real_t(delta));
230 }
231 const auto coef =
233 nodes,
234 [&](const Eigen::Vector<internal::real_t, Dim>& x) {
236 },
237 sum);
238
239 m_polys.emplace_back(coef.size());
240 for (int64_t mono_id = 0; mono_id < coef.size(); ++mono_id) {
241 // get the exponents of the current monomial
242 const std::array<std::size_t, Dim> exps =
244 // set the coefficient of the polynomial (dimension independent)
245 std::apply(
246 [&](auto... args) {
247 const auto c = vmath::to<internal::real_t>(coef(mono_id));
248 const auto c_clip = vmath::abs(c) < 1e-30 ? 0.0 : c;
249 m_polys.back().coef(args...) = c_clip;
250 },
251 exps);
252 }
253 }
254 }
255
256 const auto& getBasis() const { return m_polys; }
257
258 template <typename CellType>
259 inline static constexpr auto getMapping(const CellType& cell) {
260 // TODO: compute simplex mapping
261 return 0;
262 };
263
264 protected:
265 std::vector<Polynomial> m_polys;
266};
267
272template <std::size_t Dim>
274
275} // namespace fe
276} // namespace vulpes
277
278#endif // VULPES_FE_FE_SPACES_FE_MODAL_SIMPLEX_HPP_
internal::real_t real_t
Definition fe_space.hpp:82
constexpr std::size_t order() const
Definition fe_space.hpp:149
Modal FE Space on simplex elements.
Definition fe_modal_simplex.hpp:208
polynomial::NdPolynomial< internal::real_t, Dim > Polynomial
Definition fe_modal_simplex.hpp:210
static constexpr auto getMapping(const CellType &cell)
Definition fe_modal_simplex.hpp:259
const auto & getBasis() const
Definition fe_modal_simplex.hpp:256
FeSpaceModalP(std::size_t order)
Definition fe_modal_simplex.hpp:211
std::vector< Polynomial > m_polys
Definition fe_modal_simplex.hpp:265
Definition fe_expr_base.hpp:58
T simplex2DP(const Eigen::Vector< T, 2 > &ab, const std::array< std::size_t, 2 > &ij)
Evaluate the 2D modal basis functions on simplex elements in barycentric coordinates.
Definition fe_modal_simplex.hpp:100
mpf_class real_t
Definition fe_space.hpp:188
Eigen::Vector< T, 2 > rs2ab(const Eigen::Vector< T, 2 > &rs)
Map from (r,s) to (a,b) coordinates for 2D simplex.
Definition fe_modal_simplex.hpp:126
Eigen::Vector< T, -1 > interpolateFunctionAtNodes(const std::vector< Eigen::Vector< T, Dim > > &nodes, const std::function< T(const Eigen::Vector< T, Dim > &)> &f, std::size_t deg)
Interpolate a function f at given nodes in the Dim-simplex and return the coefficients of the interpo...
Definition fe_modal_simplex.hpp:167
T evalSimplex(const Eigen::Vector< T, Dim > &x, std::size_t n)
Evaluate the modal basis function of index n at point x in the Dim-simplex.
Definition fe_modal_simplex.hpp:150
T simplex3DP(const Eigen::Vector< T, 3 > &abc, const std::array< std::size_t, 3 > &ijk)
Evaluate the 3D modal basis functions on simplex elements in barycentric coordinates.
Definition fe_modal_simplex.hpp:112
T jacobiP(const T &x, std::size_t alpha, std::size_t beta, std::size_t N)
Evaluate the Jacobi Polynomial of degree N and parameters (alpha,beta) at point x.
Definition fe_modal_simplex.hpp:45
Eigen::Vector< T, 3 > rst2abc(const Eigen::Vector< T, 3 > &rst)
Map from (r,s,t) to (a,b,c) coordinates for 3D simplex.
Definition fe_modal_simplex.hpp:137
Definition dof_policy.hpp:45
geometry::Cell< Dim, double, int32_t > CellType
Definition fe_expr_base.hpp:41
FeSpaceModalP< Dim, DofPolicyFeSpaceDP< Dim > > FeSpaceModalLegendreDP
Modal DiscontinuousLegendre FE Space on simplex elements with optimal conditioning for simplex elemen...
Definition fe_modal_simplex.hpp:273
std::array< T, 2 > getMonomial2D(std::size_t n)
Computes the 2D monomial exponents for a given linear index.
Definition monomial.hpp:132
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::array< T, 3 > getMonomial3D(std::size_t n)
Computes the 3D monomial exponents for a given linear index.
Definition monomial.hpp:177
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
T gamma(const T &value)
Definition vmath.hpp:358
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
constexpr T binomial(T n, T k)
Definition vmath.hpp:386
T abs(const T &value)
Computes the absolute value of a number.
Definition vmath.hpp:192
T sqrt(const T &value)
Definition vmath.hpp:201
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
static std::vector< Point< real_t, Dim > > computeNodes(std::size_t n_pts)
Definition fe_lagrange_simplex.hpp:48