Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
fe_expr_base.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_FE_FE_EXPRESSIONS_FE_EXPR_BASE_HPP_
25#define VULPES_FE_FE_EXPRESSIONS_FE_EXPR_BASE_HPP_
26
27#include <algorithm>
28#include <cstddef>
29#include <utility>
30#include <vector>
31
35
36namespace vulpes {
37namespace fe {
38
39// forward declaration
40template <std::size_t Dim>
42
43template <std::size_t Dim>
44using CellsType = std::vector<std::unique_ptr<CellType<Dim>>>;
45
46template <std::size_t Dim, typename FeExpression>
48
57
58namespace internal {
59
68static inline VULPES_ALWAYS_INLINE auto hcat(const auto& u, const auto& v) {
69 using TypeU = std::decay_t<decltype(u)>;
70 using PolyTypeU = typename TypeU::Scalar;
71 using TypeV = std::decay_t<decltype(v)>;
72 using PolyTypeV = typename TypeV::Scalar;
73 static_assert(std::is_same_v<PolyTypeU, PolyTypeV>);
74 assert(u.rows() == v.rows());
75 return Eigen::Matrix<PolyTypeU, -1, -1>{
76 (Eigen::Matrix<PolyTypeU, -1, -1>(u.rows(), u.cols() + v.cols()) << u, v)
77 .finished()};
78}
79
89template <typename FromMat, typename ToPoly>
90auto liftConstant(const FromMat& m) {
91 constexpr Eigen::Index Rows = FromMat::RowsAtCompileTime;
92 constexpr Eigen::Index Cols = FromMat::ColsAtCompileTime;
93 using Lifted = Eigen::Matrix<ToPoly, Rows, Cols>;
94
95 Lifted out;
96 // build the "1" polynomial
97 const typename ToPoly::ScalarType one_scalar(1);
98 const ToPoly one_poly(one_scalar);
99
100 for (Eigen::Index i = 0; i < m.rows(); ++i) {
101 for (Eigen::Index j = 0; j < m.cols(); ++j) {
102 assert(m(i, j) == 1.0);
103 out(i, j) = one_poly;
104 }
105 }
106 return out;
107}
108
118template <typename FromMat, typename ToPoly>
119auto liftDimension(const FromMat& m) {
120 constexpr Eigen::Index Rows = FromMat::RowsAtCompileTime;
121 constexpr Eigen::Index Cols = FromMat::ColsAtCompileTime;
122 using Lifted = Eigen::Matrix<ToPoly, Rows, Cols>;
123
124 Lifted out;
125 for (Eigen::Index i = 0; i < m.rows(); ++i) {
126 for (Eigen::Index j = 0; j < m.cols(); ++j) {
127 out(i, j) = m(i, j).template liftDimension<0, 2 * FromMat::Scalar::dim>();
128 }
129 }
130 return out;
131}
132
142 const auto& u, const auto& v) {
143 using TypeU = std::decay_t<decltype(u)>;
144 using PolyU = typename TypeU::Scalar;
145 using TypeV = std::decay_t<decltype(v)>;
146 using PolyV = typename TypeV::Scalar;
147
148 if constexpr (std::is_same_v<PolyU, PolyV>) {
149 return std::pair<TypeU, TypeV>{u, v};
150 } else if constexpr (std::is_same_v<PolyU, double>) {
151 // lift constant to polynomial, the actual coef should be
152 // handled by the evaluatePolyCoef method
153 return std::pair{liftConstant<TypeU, PolyV>(u), v};
154 } else if constexpr (std::is_same_v<PolyV, double>) {
155 // lift constant to polynomial, the actual coef should be
156 // handled by the evaluatePolyCoef method
157 return std::pair{u, liftConstant<TypeV, PolyU>(v)};
158 } else if constexpr (PolyU::dim < PolyV::dim) {
159 return std::pair{liftDimension<TypeU, PolyV>(u), v};
160 } else if constexpr (PolyU::dim > PolyV::dim) {
161 return std::pair{u, liftDimension<TypeV, PolyU>(v)};
162 } else {
163 static_assert(false, "unhandled combination of polynomial types");
164 }
165}
166
181 const auto& u, const auto& v) {
182 using TypeU = std::decay_t<decltype(u)>;
183 using PolyTypeU = typename TypeU::Scalar;
184 using TypeV = std::decay_t<decltype(v)>;
185 using PolyTypeV = typename TypeV::Scalar;
186 // assume that polynomials are already lifted to the correct space
187 static_assert(std::is_same_v<PolyTypeU, PolyTypeV>);
188 assert(u.rows() == v.rows());
189 Eigen::Matrix<PolyTypeU, -1, -1> result(u.rows(), u.cols() * v.cols());
190 Eigen::Index idx = 0;
191 for (Eigen::Index i = 0; i < u.cols(); ++i) {
192 for (Eigen::Index j = 0; j < v.cols(); ++j) {
193 for (Eigen::Index k = 0; k < u.rows(); ++k) {
194 result(k, idx) = u(k, i) * v(k, j);
195 }
196 idx++;
197 }
198 }
199 return result;
200}
201} // namespace internal
202
209template <std::size_t Dim, typename FeExpression>
211 public:
213 inline static constexpr bool is_leaf = false;
215 inline static constexpr uint8_t is_bilinear = IsNothing;
218 inline static constexpr std::size_t k_index_begin = -1;
220 inline static constexpr std::size_t k_index_end = 0;
221
223 using FeSpaceType = void;
225 constexpr const auto& feSpace() const { return self().feSpace(); }
226
239 template <std::size_t J = 0, std::size_t I = 0>
240 decltype(auto) evaluate(
241 const FeValues<Dim>& fe_values,
242 std::size_t j,
243 std::size_t i,
244 std::size_t q) const {
245 return self().evaluate(fe_values, j, i, q);
246 }
247
265 decltype(auto) evaluatePoly(
266 const FeValues<Dim>& fe_values,
267 std::size_t j,
268 std::size_t i,
269 std::size_t bi,
270 std::size_t bj) const {
271 return self().evaluatePoly(fe_values, j, i, bi, bj);
272 }
273
290 decltype(auto) evaluatePolyCoef(
291 const auto& mapping,
292 const auto& fe_values,
293 std::size_t bi,
294 std::size_t bj) const {
295 return self().evaluatePolyCoef(mapping, fe_values, bi, bj);
296 }
297
305 return self().computeUpdateFlags();
306 }
307
313 constexpr std::size_t computeOrder() const { return self().computeOrder(); }
314
323 constexpr bool precompute(
324 const FeValues<Dim>& fe_values, const CellsType<Dim>& cells) const {
325 return self().precompute(fe_values, cells);
326 }
327
336
337 protected:
338 const FeExpression& self() const {
339 return static_cast<FeExpression const&>(*this);
340 }
341};
342
343} // namespace fe
344} // namespace vulpes
345
346#endif // VULPES_FE_FE_EXPRESSIONS_FE_EXPR_BASE_HPP_
Base class for Finite Element Expressions.
Definition fe_expr_base.hpp:210
decltype(auto) evaluate(const FeValues< Dim > &fe_values, std::size_t j, std::size_t i, std::size_t q) const
Evaluate the finite element expression evaluated at function basis i and j and at quadrature point q.
Definition fe_expr_base.hpp:240
static constexpr uint8_t is_bilinear
Indicates the appearance of trial/test functions in the expression.
Definition fe_expr_base.hpp:215
static constexpr std::size_t k_index_begin
Index the block of the expression starts from (for vectorial problems).
Definition fe_expr_base.hpp:218
constexpr UpdateFlagsType computeUpdateFlags() const
Compute the update flags required by the expression for the update in the FeValues.
Definition fe_expr_base.hpp:304
static constexpr bool is_leaf
Indicates if the expression is a leaf node in the expression tree.
Definition fe_expr_base.hpp:213
static constexpr std::size_t k_index_end
Index the block of the expression ends to (for vectorial problems).
Definition fe_expr_base.hpp:220
constexpr std::size_t computeOrder() const
Compute the polynomial order required by the expression.
Definition fe_expr_base.hpp:313
constexpr FeExpressionUnaryMinus< Dim, FeExpression > operator-() const
Unary minus operator for the FE expression.
Definition fe_expr_base.hpp:333
decltype(auto) evaluatePoly(const FeValues< Dim > &fe_values, std::size_t j, std::size_t i, std::size_t bi, std::size_t bj) const
Compute the polynomial associated to the expression for basis i and j and block bi and bj.
Definition fe_expr_base.hpp:265
void FeSpaceType
Type of the associated Finite Element Space.
Definition fe_expr_base.hpp:223
decltype(auto) evaluatePolyCoef(const auto &mapping, const auto &fe_values, std::size_t bi, std::size_t bj) const
Compute the coefficients of the polynomial associated to the expression for basis i and j and block b...
Definition fe_expr_base.hpp:290
constexpr bool precompute(const FeValues< Dim > &fe_values, const CellsType< Dim > &cells) const
Precompute any necessary data for the expression based on the FE values and the cells.
Definition fe_expr_base.hpp:323
const FeExpression & self() const
Definition fe_expr_base.hpp:338
constexpr const auto & feSpace() const
Get the associated Finite Element Space.
Definition fe_expr_base.hpp:225
Definition fe_expr_unary.hpp:40
This class works as a cache table for computing efficiently the value of the basis function and its d...
Definition fe_values.hpp:94
#define VULPES_ALWAYS_INLINE
Force the compiler to inline the annotated function.
Definition macros.hpp:30
Definition fe_expr_base.hpp:58
auto liftDimension(const FromMat &m)
Convert a matrix of polynomials of lower dimension into a matrix of polynomials of higher dimension (...
Definition fe_expr_base.hpp:119
static VULPES_ALWAYS_INLINE auto cartesian_hcat(const auto &u, const auto &v)
Horizontal concatenation of the cartesian product of the columns of two matrices.
Definition fe_expr_base.hpp:180
static VULPES_ALWAYS_INLINE auto lift_to_same_space(const auto &u, const auto &v)
Given two matrices of polynomials u and v, lift the one with lower dimension to the higher dimension,...
Definition fe_expr_base.hpp:141
static VULPES_ALWAYS_INLINE auto hcat(const auto &u, const auto &v)
Horizontally concatenate two matrices that have the same number of rows.
Definition fe_expr_base.hpp:68
auto liftConstant(const FromMat &m)
Convert a matrix of scalars into a matrix of polynomials (constant polynomials).
Definition fe_expr_base.hpp:90
Definition dof_policy.hpp:45
geometry::Cell< Dim, double, int32_t > CellType
Definition fe_expr_base.hpp:41
std::underlying_type_t< UpdateFlags > UpdateFlagsType
Definition fe_values.hpp:65
std::vector< std::unique_ptr< CellType< Dim > > > CellsType
Definition fe_expr_base.hpp:44
IntegrationForm
Enumeration for the type of integration form (presence of test/trial functions).
Definition fe_expr_base.hpp:51
@ IsLinearTest
Definition fe_expr_base.hpp:53
@ IsNothing
Definition fe_expr_base.hpp:52
@ IsLinearTrial
Definition fe_expr_base.hpp:54
@ IsBilinear
Definition fe_expr_base.hpp:55
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition cell_manager.hpp:38