Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
polynomial3d.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_POLYNOMIALS_POLYNOMIAL3D_HPP_
24#define VULPES_POLYNOMIALS_POLYNOMIAL3D_HPP_
25
26#include <cstddef>
27#include <ostream>
28#include <utility>
29#include <vector>
30
34
35namespace vulpes {
36namespace polynomial {
37
51template <typename T, bool IsSpaceP = true>
52class Polynomial3D : public PolynomialBase<T, 3, Polynomial3D<T, IsSpaceP>> {
53 public:
55 explicit Polynomial3D(std::size_t size) : m_coefficients(size) {}
56
57 constexpr std::size_t dim() const { return m_coefficients.dim(); }
58 constexpr std::size_t order() const { return m_coefficients.dim() - 1; }
59
60 const T& coef(std::size_t i, std::size_t j, std::size_t k) const {
61 return m_coefficients(i, j, k);
62 }
63 T& coef(std::size_t i, std::size_t j, std::size_t k) {
64 return m_coefficients(i, j, k);
65 }
66
67 auto& data() { return m_coefficients; }
68 const auto& data() const { return m_coefficients; }
69
76 Eigen::Vector<Polynomial3D<T, IsSpaceP>, 3> gradient() const {
77 return Eigen::Vector<Polynomial3D<T, IsSpaceP>, 3>{
79 }
80
87 Polynomial3D<T, IsSpaceP> derivative(std::size_t component) const {
88 assert(component < 3);
90 if (component == 0) {
91 for (std::size_t i = 1; i < dim(); ++i) {
92 const auto jmax = dim() - IsSpaceP * i;
93 for (std::size_t j = 0; j < jmax; ++j) {
94 const auto kmax = dim() - IsSpaceP * (i + j);
95 for (std::size_t k = 0; k < kmax; ++k) {
96 result.coef(i - 1, j, k) = coef(i, j, k) * T(i);
97 }
98 }
99 }
100
101 } else if (component == 1) {
102 for (std::size_t i = 0; i < dim(); ++i) {
103 const auto jmax = dim() - IsSpaceP * i;
104 for (std::size_t j = 1; j < jmax; ++j) {
105 const auto kmax = dim() - IsSpaceP * (i + j);
106 for (std::size_t k = 0; k < kmax; ++k) {
107 result.coef(i, j - 1, k) = coef(i, j, k) * T(j);
108 }
109 }
110 }
111 } else {
112 for (std::size_t i = 0; i < dim(); ++i) {
113 const auto jmax = dim() - IsSpaceP * i;
114 for (std::size_t j = 0; j < jmax; ++j) {
115 const auto kmax = dim() - IsSpaceP * (i + j);
116 for (std::size_t k = 1; k < kmax; ++k) {
117 result.coef(i, j, k - 1) = coef(i, j, k) * T(k);
118 }
119 }
120 }
121 }
122 return result;
123 }
124
126 const Eigen::Map<Eigen::Vector<T, -1>>& monomial_integrals) const {
127 assert(m_coefficients.size() <= monomial_integrals.size());
128 T integral = 0.0;
129 for (std::size_t i = 0; i < m_coefficients.size(); ++i) {
130 integral += m_coefficients.data()[i] * monomial_integrals[i];
131 }
132 return integral;
133 }
134
138 T evaluate(const Eigen::Vector<T, 3>& x) const { return evaluate(x.data()); }
139
144 T evaluate(const T* x) const {
145 // WARNING: this is not thread safe
146 static std::vector<T> partial_eval_yz;
147 static std::vector<T> partial_eval_x;
148 if (partial_eval_x.size() < dim()) {
149 partial_eval_yz.resize(dim() * dim());
150 partial_eval_x.resize(dim());
151 }
152
153 // Step 1: Evaluate in z for each (i,j)
154 for (std::size_t i = 0; i < dim(); ++i) {
155 const auto jmax = dim() - IsSpaceP * i;
156 for (std::size_t j = 0; j < jmax; ++j) {
157 T Q_ij = T(0);
158 const auto kmax = dim() - IsSpaceP * (i + j);
159 for (std::size_t k = kmax - 1; k > 0; --k) {
160 Q_ij = Q_ij * x[2] + m_coefficients(i, j, k);
161 }
162 partial_eval_yz[i * dim() + j] = Q_ij * x[2] + m_coefficients(i, j, 0);
163 }
164 }
165
166 // Step 2: Evaluate in y for each i
167 for (std::size_t i = 0; i < dim(); ++i) {
168 T Q_i = T(0);
169 const auto jmax = dim() - IsSpaceP * i;
170 for (std::size_t j = jmax - 1; j > 0; --j) {
171 Q_i = Q_i * x[1] + partial_eval_yz[i * dim() + j];
172 }
173 partial_eval_x[i] = Q_i * x[1] + partial_eval_yz[i * dim()];
174 }
175
176 // Step 3: Final evaluation in x
177 T result = T(0);
178 for (std::size_t i = dim() - 1; i > 0; --i) {
179 result = result * x[0] + partial_eval_x[i];
180 }
181 return result * x[0] + partial_eval_x[0];
182 }
183
187 template <typename U>
190 std::transform(
191 m_coefficients.begin(), m_coefficients.end(), result.data().begin(),
193 return result;
194 }
195
197 const Polynomial3D<T, IsSpaceP>& other) {
198 return add_<true>(other);
199 }
200
202 const Polynomial3D<T, IsSpaceP>& other) {
203 return add_<false>(other);
204 }
205
207 const Polynomial3D<T, IsSpaceP>& other) {
208 std::size_t new_dim = dim() + other.dim();
209 decltype(m_coefficients) new_coeffs(new_dim);
210
211 for (std::size_t i = 0; i < dim(); ++i) {
212 const auto jmax = dim() - IsSpaceP * i;
213 for (std::size_t j = 0; j < jmax; ++j) {
214 const auto kmax = dim() - IsSpaceP * (i + j);
215 for (std::size_t k = 0; k < kmax; ++k) {
216 for (std::size_t l = 0; l < other.dim(); ++l) {
217 const auto mmax = other.dim() - IsSpaceP * l;
218 for (std::size_t m = 0; m < mmax; ++m) {
219 const auto nmax = other.dim() - IsSpaceP * (l + m);
220 for (std::size_t n = 0; n < nmax; ++n) {
221 new_coeffs(i + l, j + m, k + n) +=
222 coef(i, j, k) * other.coef(l, m, n);
223 }
224 }
225 }
226 }
227 }
228 }
229
230 m_coefficients = std::move(new_coeffs);
231 return *this;
232 }
233
235 std::transform(
236 m_coefficients.begin(), m_coefficients.end(), m_coefficients.begin(),
237 [&](const auto& val) { return val * scalar; });
238 return *this;
239 }
240
241 friend std::ostream& operator<<(std::ostream& os, const Polynomial3D& poly) {
242 for (std::size_t i = 0; i < poly.dim(); ++i) {
243 const auto jmax = poly.dim() - IsSpaceP * i;
244 for (std::size_t j = 0; j < jmax; ++j) {
245 const auto kmax = poly.dim() - IsSpaceP * (i + j);
246 for (std::size_t k = 0; k < kmax; ++k) {
247 if (poly.m_coefficients(i, j, k) != T(0)) {
248 if (i > 0 || j > 0 || k > 0) {
249 os << " + ";
250 }
251 os << poly.m_coefficients(i, j, k);
252 if (i > 0) {
253 os << "x^" << i;
254 }
255 if (j > 0) {
256 os << "y^" << j;
257 }
258 if (k > 0) {
259 os << "z^" << k;
260 }
261 }
262 }
263 }
264 }
265 return os;
266 }
267
268 protected:
270
274 template <bool Add>
276 if (dim() < other.dim()) {
277 auto result_coef = other.m_coefficients;
278 if constexpr (!Add) {
279 // Negate coefficients for subtraction
280 std::transform(
281 result_coef.begin(), result_coef.end(), result_coef.begin(),
282 [](const auto& val) { return -val; });
283 }
284 for (std::size_t i = 0; i < dim(); ++i) {
285 const auto jmax = dim() - IsSpaceP * i;
286 for (std::size_t j = 0; j < jmax; ++j) {
287 const auto kmax = dim() - IsSpaceP * (i + j);
288 for (std::size_t k = 0; k < kmax; ++k) {
289 result_coef(i, j, k) += coef(i, j, k);
290 }
291 }
292 }
293 m_coefficients = std::move(result_coef);
294 } else {
295 for (std::size_t i = 0; i < other.dim(); ++i) {
296 const auto jmax = other.dim() - IsSpaceP * i;
297 for (std::size_t j = 0; j < jmax; ++j) {
298 const auto kmax = other.dim() - IsSpaceP * (i + j);
299 for (std::size_t k = 0; k < kmax; ++k) {
300 if constexpr (Add) {
301 m_coefficients(i, j, k) += other.coef(i, j, k);
302 } else {
303 m_coefficients(i, j, k) -= other.coef(i, j, k);
304 }
305 }
306 }
307 }
308 }
309 return *this;
310 }
311};
312} // namespace polynomial
313} // namespace vulpes
314
315#endif // VULPES_POLYNOMIALS_POLYNOMIAL3D_HPP_
A class that represents a tetrahedral matrix or tensor, with optional support for higher-dimensional ...
Definition tensor.hpp:307
Polynomial3D< T, IsSpaceP > & operator+=(const Polynomial3D< T, IsSpaceP > &other)
Definition polynomial3d.hpp:196
Polynomial3D< U, IsSpaceP > to() const
Cast the polynomial to a different coefficient type.
Definition polynomial3d.hpp:188
Polynomial3D< T, IsSpaceP > & add_(const Polynomial3D< T, IsSpaceP > &other)
add or subtract two polynomials depending of template parameter
Definition polynomial3d.hpp:275
T & coef(std::size_t i, std::size_t j, std::size_t k)
Definition polynomial3d.hpp:63
const T & coef(std::size_t i, std::size_t j, std::size_t k) const
Definition polynomial3d.hpp:60
const auto & data() const
Definition polynomial3d.hpp:68
Polynomial3D< T, IsSpaceP > derivative(std::size_t component) const
Computes the derivative of the polynomial.
Definition polynomial3d.hpp:87
constexpr std::size_t order() const
Definition polynomial3d.hpp:58
Polynomial3D()
Definition polynomial3d.hpp:54
Polynomial3D< T, IsSpaceP > & operator*=(const Polynomial3D< T, IsSpaceP > &other)
Definition polynomial3d.hpp:206
Polynomial3D(std::size_t size)
Definition polynomial3d.hpp:55
datastructure::TetraTensor< IsSpaceP, false, T, std::size_t > m_coefficients
Definition polynomial3d.hpp:269
T evaluate(const T *x) const
Evaluation of the polynomial with Horner's rule in a point passed as a raw buffer.
Definition polynomial3d.hpp:144
T integrate(const Eigen::Map< Eigen::Vector< T, -1 > > &monomial_integrals) const
Definition polynomial3d.hpp:125
constexpr std::size_t dim() const
Definition polynomial3d.hpp:57
Polynomial3D< T, IsSpaceP > & operator-=(const Polynomial3D< T, IsSpaceP > &other)
Definition polynomial3d.hpp:201
T evaluate(const Eigen::Vector< T, 3 > &x) const
Evaluation of the polynomial with Horner's rule.
Definition polynomial3d.hpp:138
Eigen::Vector< Polynomial3D< T, IsSpaceP >, 3 > gradient() const
Computes the gradient of the polynomial.
Definition polynomial3d.hpp:76
Polynomial3D< T, IsSpaceP > & operator*=(const T &scalar)
Definition polynomial3d.hpp:234
friend std::ostream & operator<<(std::ostream &os, const Polynomial3D &poly)
Definition polynomial3d.hpp:241
auto & data()
Definition polynomial3d.hpp:67
Base template class for a general polynomial.
Definition polynomial.hpp:46
Definition monomial.hpp:39
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
Definition cell_manager.hpp:38