Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
qlibrary.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// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_QUADRATURE_QLIBRARY_HPP_
25#define VULPES_QUADRATURE_QLIBRARY_HPP_
26
27#include <cassert>
28#include <cstddef>
29#include <map>
30#include <memory>
31#include <stdexcept>
32#include <utility>
33
37
38namespace vulpes {
39namespace qrule {
40
47
81template <std::size_t Dim, typename FloatType>
83 public:
85 using float_t = FloatType;
89 using QRuleRef = std::shared_ptr<QRule>;
92 using QuadratureKey = std::pair<QuadratureElemType, std::size_t>;
93
106 static QRuleRef getRule(QuadratureElemType elem_type, std::size_t order);
107
108 protected:
110 static constexpr int s_gmp_precision = 256;
122 static QRuleRef makeRule(QuadratureElemType elem_type, std::size_t order);
123};
124
125template <std::size_t Dim, typename FloatType>
128 QuadratureElemType elem_type, std::size_t order) {
129 // rule storage
130 static std::map<QuadratureKey, QRuleRef> s_quadrature_library;
131 // lazy build of the rules
132 if constexpr (Dim >= 1 && Dim <= 3) {
133 const auto key = QuadratureKey{elem_type, order};
134 const auto it = s_quadrature_library.find(key);
135 if (it != s_quadrature_library.cend()) {
136 return it->second;
137 } else {
138 const QRuleRef rule =
140 s_quadrature_library[key] = rule;
141 return rule;
142 }
143 } else {
144 static_assert(
145 Dim * 0, "Not implemented quadrature rules for Dim > 3 or Dim == 0");
146 }
147};
148
149template <std::size_t Dim, typename FloatType>
152 QuadratureElemType elem_type, std::size_t order) {
154 switch (elem_type) {
155 case TensorLegendre: {
156 QuadratureRule<mpf_class, 1> qrule_mpf_1d =
158 qrule_mpf = buildTensorQuadrature<Dim>(qrule_mpf_1d);
159 break;
160 }
161 case TensorLobatto: {
162 QuadratureRule<mpf_class, 1> qrule_mpf_1d =
164 qrule_mpf = buildTensorQuadrature<Dim>(qrule_mpf_1d);
165 break;
166 }
167 case Simplex: {
168 assert(Dim > 0);
169 if constexpr (Dim == 1) {
170 // `order` is a degree, and n Gauss-Legendre points are exact to 2n-1.
171 const std::size_t n_pts = order / 2 + 1;
173 } else {
174 bool tabled = false;
175 if constexpr (Dim == 2) {
176 if (order <= num_triangle_tabled_rules) {
177 qrule_mpf = getTriangleTabledQuadrature(order);
178 tabled = true;
179 }
180 }
181 if constexpr (Dim == 3) {
182 if (order <= num_tetrahedron_tabled_rules) {
183 qrule_mpf = getTetrahedronTabledQuadrature(order);
184 tabled = true;
185 }
186 }
187 if (!tabled) {
188 const QuadratureRule<mpf_class, Dim + 1> qrule_bary_mpf =
190 qrule_mpf = baricentrycToCartesian(qrule_bary_mpf);
191 }
192 }
193 break;
194 }
195 default: {
196 throw std::logic_error("QuadratureLibrary: unknown quadrature type");
197 }
198 }
199 // rescale in [0, 1]
200 for (auto& pt : qrule_mpf.nodes) {
201 pt.weight /= (1 << Dim);
202 for (std::size_t d = 0; d < Dim; ++d) {
203 pt.point[d] = pt.point[d] * 0.5 + 0.5;
204 }
205 }
206 return std::make_shared<QRule>(castRule<float_t>(qrule_mpf));
207}
208
209} // namespace qrule
210} // namespace vulpes
211#endif // VULPES_QUADRATURE_QLIBRARY_HPP_
A library for managing and generating quadrature rules for numerical integration.
Definition qlibrary.hpp:82
static constexpr int s_gmp_precision
GMP precision used for high-accuracy floating-point computations.
Definition qlibrary.hpp:110
FloatType float_t
Floating-point type used for quadrature computations.
Definition qlibrary.hpp:85
static QRuleRef getRule(QuadratureElemType elem_type, std::size_t order)
Retrieves a quadrature rule for a given element type and order.
Definition qlibrary.hpp:127
std::pair< QuadratureElemType, std::size_t > QuadratureKey
Key type for identifying quadrature rules, based on element type and order.
Definition qlibrary.hpp:92
static QRuleRef makeRule(QuadratureElemType elem_type, std::size_t order)
Generates a quadrature rule for the specified element type and order.
Definition qlibrary.hpp:151
std::shared_ptr< QRule > QRuleRef
Type alias for a shared pointer to a quadrature rule.
Definition qlibrary.hpp:89
QuadratureRule< float_t, Dim > QRule
Type alias for a quadrature rule in the given dimension.
Definition qlibrary.hpp:87
Definition gauss.cpp:39
QuadratureRule< T, dim > buildTensorQuadrature(const QuadratureRule1D< T > &rule1d)
Constructs a tensor-product quadrature rule from a 1D quadrature rule.
Definition gauss.hpp:106
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
QuadratureElemType
Enum representing the type of quadrature element.
Definition qlibrary.hpp:42
@ TensorLobatto
Tensor-product Gauss-Lobatto quadrature.
Definition qlibrary.hpp:45
@ Simplex
Quadrature over a simplex (e.g., triangles, tetrahedra).
Definition qlibrary.hpp:43
@ TensorLegendre
Tensor-product Gauss-Legendre quadrature.
Definition qlibrary.hpp:44
QuadratureRule1Dmp gmpGaussLegendreQuadrature(std::size_t n, std::size_t precision)
Computes a Gauss-Legendre quadrature rule with arbitrary precision.
Definition gauss.cpp:41
QuadratureRule< T, Dim - 1 > baricentrycToCartesian(const QuadratureRule< T, Dim > &rule_bary)
Converts a barycentric quadrature rule to a Cartesian representation.
Definition simplex.hpp:55
QuadratureRule< V, Dim > castRule(const QuadratureRule< U, Dim > &rule_src)
Casts a quadrature rule from one data type to another.
Definition qrule.hpp:102
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
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
const QuadratureRule< mpf_class, 2 > & getTriangleTabledQuadrature(std::size_t deg)
Retrieves a precomputed quadrature rule for a triangle.
Definition simplex.tri.cpp:59
Definition cell_manager.hpp:38
Represents a quadrature rule consisting of a set of points and associated weights.
Definition qrule.hpp:69
std::vector< QuadraturePoint< T, Dim > > nodes
List of quadrature points.
Definition qrule.hpp:71