Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
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_QUADRATURE_SIMPLEX_HPP_
24#define VULPES_QUADRATURE_SIMPLEX_HPP_
25
26#include <gmp.h>
27#include <gmpxx.h>
28
29#include <array>
30#include <cstddef>
31#include <cstdint>
32#include <vector>
33
36
37namespace vulpes {
38namespace qrule {
39
54template <typename T, std::size_t Dim>
56 const QuadratureRule<T, Dim>& rule_bary) {
57 static_assert(Dim > 2);
58 QuadratureRule<T, Dim - 1> rule;
59 rule.degree = rule_bary.degree;
60 rule.nodes.reserve(rule_bary.nodes.size());
61 // volume of n-simplex = |det(A)| / n!
62 // where the columns of A are the difference of the vertices with respect to
63 // the first vertex. |det(A)| = 1 for the unit orthogonal simplex.
64 // We multiply by 2^n for rescaling in [-1, 1]^n
65 mpf_class weight0 = mpq_class(
66 (1U << (Dim - 1)), vmath::factorial(static_cast<uint32_t>(Dim - 1)));
67 for (const auto& node : rule_bary.nodes) {
68 rule.nodes.emplace_back();
69 rule.nodes.back().weight = node.weight * weight0;
70 for (std::size_t d = 0; d < (Dim - 1); ++d) {
71 // matvec with matrix of vertices
72 // we assume that the vertices are zero and the versors
73 // then we apply a homotety and a translation
74 rule.nodes.back().point[d] = node.point[d + 1] * T(2) - T(1);
75 }
76 }
77 return rule;
78}
79
101template <std::size_t Dim>
103 std::size_t deg, std::size_t precision) {
104 mpf_set_default_prec(precision);
105 // works only for odd degree
106 if (deg % 2 == 0) {
107 deg++;
108 }
109 std::size_t order = (deg - 1) / 2;
110 // pre-compute exponents of monomial of order up to `order` (grouped by order)
111 // this implementation is rather wastfull in terms of computations but it
112 // is not recursive, moreover, it should be easier to understand
113 using MonomialGroup = std::vector<std::array<std::size_t, Dim + 1>>;
114 std::vector<MonomialGroup> ordered_exponents(order + 1);
115 for (std::size_t i = 0; i < vmath::upow(order + 1, Dim + 1); ++i) {
116 std::array<std::size_t, Dim + 1> exponent;
117 std::size_t idx = i;
118 std::size_t sum = 0;
119 // write `i` in radix `order + 1`
120 for (std::size_t d = 0; d < Dim + 1; ++d) {
121 std::size_t digit = idx % (order + 1);
122 idx /= (order + 1);
123 exponent[d] = digit;
124 sum += digit;
125 }
126 // we are generating the cartesian product, skip if order too high
127 if (sum > order) {
128 continue;
129 }
130 ordered_exponents[sum].push_back(exponent);
131 }
132 // compute rule
133 QuadratureRule<mpf_class, Dim + 1> rule_barycentric;
134 rule_barycentric.degree = deg;
135 for (std::size_t i = 0; i < order + 1; ++i) {
136 const mpq_class num =
137 vmath::pow1(i) * vmath::upow(mpz_class(deg + Dim - 2 * i), deg);
138 const mpq_class den =
139 (vmath::upow(mpz_class(2), 2 * order) * vmath::factorial(mpz_class(i)) *
140 vmath::factorial(mpz_class(deg + Dim - i)));
141 mpq_class weight = num / den;
142 weight.canonicalize();
143
144 std::array<mpf_class, Dim + 1> node;
145 for (const auto& exponent : ordered_exponents[order - i]) {
146 for (std::size_t d = 0; d < Dim + 1; ++d) {
147 node[d] = mpq_class(2 * exponent[d] + 1, deg + Dim - 2 * i);
148 }
149 rule_barycentric.nodes.emplace_back(node, weight);
150 }
151 }
152
153 // normalize weights
154 mpf_class weights_sum = 0.0;
155 for (const auto& node : rule_barycentric.nodes) {
156 weights_sum += node.weight;
157 }
158 for (auto& node : rule_barycentric.nodes) {
159 node.weight /= weights_sum;
160 }
161
162 return rule_barycentric;
163}
164
168static inline constexpr std::size_t num_triangle_tabled_rules = 20;
180 std::size_t deg);
184static inline constexpr std::size_t num_tetrahedron_tabled_rules = 20;
195 std::size_t deg);
196
197} // namespace qrule
198} // namespace vulpes
199#endif // VULPES_QUADRATURE_SIMPLEX_HPP_
Definition gauss.cpp:39
static constexpr std::size_t num_triangle_tabled_rules
Number of precomputed quadrature rules for triangles.
Definition simplex.hpp:168
static constexpr std::size_t num_tetrahedron_tabled_rules
Number of precomputed quadrature rules for tetrahedra.
Definition simplex.hpp:184
QuadratureRule< T, Dim - 1 > baricentrycToCartesian(const QuadratureRule< T, Dim > &rule_bary)
Converts a barycentric quadrature rule to a Cartesian representation.
Definition simplex.hpp:55
const QuadratureRule< mpf_class, 3 > & getTetrahedronTabledQuadrature(std::size_t deg)
Retrieves a precomputed quadrature rule for a tetrahedron.
Definition simplex.tet.cpp:41
QuadratureRule< mpf_class, Dim+1 > mpfGrundammMoellerQuadrature(std::size_t deg, std::size_t precision)
Generates Grundamm-Moller quadrature rules in barycentric coordinates.
Definition simplex.hpp:102
const QuadratureRule< mpf_class, 2 > & getTriangleTabledQuadrature(std::size_t deg)
Retrieves a precomputed quadrature rule for a triangle.
Definition simplex.tri.cpp:59
constexpr int64_t pow1(T exp)
Computes (-1)^exp.
Definition vmath.hpp:381
constexpr T factorial(T n)
Computes the factorial of a pseudo-arithmetic number iteratively.
Definition vmath.hpp:348
constexpr U upow(U base, V exp, U result=U(1))
Computes the unsigned power of a number.
Definition vmath.hpp:107
Definition cell_manager.hpp:38
Represents a quadrature rule consisting of a set of points and associated weights.
Definition qrule.hpp:69
std::size_t degree
Polynomial degree of accuracy of the rule.
Definition qrule.hpp:73
std::vector< QuadraturePoint< T, Dim > > nodes
List of quadrature points.
Definition qrule.hpp:71