Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
frugal.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2025 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_FRUGAL_HPP_
24#define VULPES_QUADRATURE_FRUGAL_HPP_
25
26#include <algorithm>
27#include <array>
28#include <cassert>
29#include <cstddef>
30#include <iterator>
31#include <set>
32#include <tuple>
33#include <utility>
34#include <vector>
35
36#include <Eigen/Core>
37#include <Eigen/Dense>
38
45
46namespace vulpes {
47namespace qfree {
48
49namespace internal {
50template <typename FloatType, std::size_t Dim>
51std::tuple<
52 Eigen::Matrix<FloatType, -1, -1>,
53 Eigen::Matrix<FloatType, -1, -1>,
54 Eigen::Matrix<FloatType, Dim, -1>>
55vandermondeDecomposition(std::size_t order) {
56 const auto gl_rule = qrule::gmpGaussLegendreQuadrature(order + 1, 256);
57
58 const std::size_t n_pts = vmath::pow(order + 1, Dim);
59 const std::size_t n_monomials = vmath::binomial(order + Dim, Dim);
60
61 Eigen::Matrix<FloatType, -1, -1> vandermondeT(n_monomials, n_pts);
62 Eigen::Matrix<FloatType, Dim, -1> pts(Dim, n_pts);
63
64 for (std::size_t j = 0; j < n_pts; ++j) {
65 // write `j` in radix `order + 1` to find the cartesian index of the point
66 std::size_t idx = j;
67 for (std::size_t d = 0; d < Dim; ++d) {
68 pts(d, j) = vmath::to<FloatType>(
69 gl_rule.nodes[order - (idx % (order + 1))].point[0]);
70 idx /= (order + 1);
71 }
72 for (std::size_t i = 0; i < n_monomials; ++i) {
74 FloatType monomial = 1;
75 for (std::size_t d = 0; d < Dim; ++d) {
76 monomial *= vmath::pow(pts(d, j), exp[d]);
77 }
78 vandermondeT(i, j) = monomial;
79 }
80 }
81 Eigen::ColPivHouseholderQR<decltype(vandermondeT)> qr(vandermondeT);
82 Eigen::Matrix<FloatType, -1, -1> Q = qr.householderQ();
83 Eigen::Matrix<FloatType, -1, -1> R =
84 qr.matrixQR().template triangularView<Eigen::Upper>();
85 Eigen::VectorXi permutationIndices = qr.colsPermutation().indices();
86 Eigen::Matrix<FloatType, -1, -1> R_truncated =
87 R.block(0, 0, n_monomials, n_monomials);
88 Eigen::Matrix<FloatType, Dim, -1> pts_truncated =
89 pts(Eigen::placeholders::all, permutationIndices.head(n_monomials));
90
91 return std::tuple(Q, R_truncated, pts_truncated);
92}
93} // namespace internal
94
95template <typename FloatType, typename IndexType, std::size_t Dim>
98 using MatrixType = Eigen::Matrix<FloatType, -1, -1>;
99 using QRType = std::tuple<MatrixType, MatrixType, MatrixType>;
100 static std::vector<QRType> s_vandermonde_collection;
101 static std::vector<bool> s_vandermonde_collection_built;
102 assert(integrals.dim() > 0);
103 const std::size_t order = integrals.dim() - 1;
104 if (s_vandermonde_collection.size() <= order) {
105 s_vandermonde_collection.resize(order + 1);
106 s_vandermonde_collection_built.resize(order + 1, false);
107 }
108 if (!s_vandermonde_collection_built[order]) {
109 s_vandermonde_collection_built[order] = true;
110 s_vandermonde_collection[order] =
112 }
113 const auto& [Q, R, pts] = s_vandermonde_collection[order];
114 const Eigen::Map<const Eigen::Vector<FloatType, -1>> integral_map(
115 integrals.begin(), integrals.numel());
116 assert(integrals.numel() == pts.cols());
117 const Eigen::Vector<FloatType, -1> y =
118 R.template triangularView<Eigen::Upper>().solve(
119 Q.transpose() * integral_map);
120
122 rule.degree = order;
123 for (int64_t i = 0; i < pts.cols(); ++i) {
124 std::array<FloatType, Dim> pt;
125 for (std::size_t d = 0; d < Dim; ++d) {
126 pt[d] = pts(d, i);
127 }
128 rule.nodes.emplace_back(pt, y(i));
129 }
130 return rule;
131}
132
133} // namespace qfree
134} // namespace vulpes
135#endif // VULPES_QUADRATURE_FRUGAL_HPP_
std::conditional_t< Dim==2, TriMatrix< Simplex, false, DataType, IndexType, const DataType * >, std::conditional_t< Dim==3, TetraTensor< Simplex, false, DataType, IndexType, const DataType * >, const DataType * > > SimplexDataView
Definition tensor.hpp:507
Definition monomial.hpp:44
std::array< T, Dim > getMonomial(std::size_t n)
Computes the Dim-D monomial exponents for a given linear index.
Definition monomial.hpp:199
Definition frugal.hpp:49
std::tuple< Eigen::Matrix< FloatType, -1, -1 >, Eigen::Matrix< FloatType, -1, -1 >, Eigen::Matrix< FloatType, Dim, -1 > > vandermondeDecomposition(std::size_t order)
Definition frugal.hpp:55
Definition cell.hpp:49
qrule::QuadratureRule< FloatType, Dim > computeFrugalQuadrature(datastructure::SimplexDataView< Dim, true, FloatType, IndexType > integrals)
Definition frugal.hpp:96
QuadratureRule1Dmp gmpGaussLegendreQuadrature(std::size_t n, std::size_t precision)
Computes a Gauss-Legendre quadrature rule with arbitrary precision.
Definition gauss.cpp:41
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
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
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