Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
fe_values.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_VALUES_HPP_
25#define VULPES_FE_FE_VALUES_HPP_
26
27#include <algorithm>
28#include <atomic>
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <type_traits>
33#include <vector>
34
35#include <Eigen/Dense>
36
43
44namespace vulpes {
45namespace fe {
46
51enum UpdateFlags : uint32_t {
53 UpdateMapping = (1ULL << 0ULL),
54 UpdateValues = (1ULL << 1ULL),
55 UpdateGradients = (1ULL << 2ULL),
56 UpdateHessians = (1ULL << 3ULL),
57 UpdateWeights = (1ULL << 4ULL),
58 UpdateQuadraturePts = (1ULL << 5ULL),
59 UpdateNormals = (1ULL << 6ULL),
60 UpdateId = (1ULL << 7ULL),
61 UpdateNeighborCell = (1ULL << 8ULL),
62 UpdateNeighborValues = (1ULL << 9ULL),
63 UpdateAll = 0xFFFFFFFFUL,
64};
65using UpdateFlagsType = std::underlying_type_t<UpdateFlags>;
66
67namespace internal {
69template <typename T>
70struct IsUnscaledSparsePolynomial : std::false_type {};
71template <std::size_t D, typename I>
73 polynomial::SparsePolynomial<D, I, double, false>> : std::true_type {};
74} // namespace internal
75
93template <std::size_t Dim>
94class FeValues {
95 public:
96 using real_t = double;
97 using index_t = int32_t;
98 using PhysicalQPts = Eigen::Matrix<real_t, Dim, -1>;
99 using FeValuesScalar = Eigen::Matrix<real_t, -1, -1>;
100 using FeValuesGrad = Eigen::Matrix<Eigen::Vector<real_t, Dim>, -1, -1>;
101 using QuadtratureWeigths = Eigen::Vector<real_t, -1>;
102
103 constexpr const auto& getShapeEval() const { return m_scalar_shapes; }
104 constexpr const auto& getGradEval() const { return m_grads_shapes; }
105 constexpr const auto& weights() const { return m_quadrature_weights; }
106 constexpr auto& weights() { return m_quadrature_weights; }
107 constexpr const auto& qpts() const { return m_physical_qpts; }
108 constexpr auto& qpts() { return m_physical_qpts; }
109 constexpr const auto& weightsRef() const { return m_quadrature_weights_ref; }
110 constexpr auto& weightsRef() { return m_quadrature_weights_ref; }
111 constexpr const auto& qptsRef() const { return m_physical_qpts_ref; }
112 constexpr auto& qptsRef() { return m_physical_qpts_ref; }
114 constexpr const void* tabulatedBasis() const { return m_tabulated_basis; }
121 constexpr std::uint64_t stamp() const {
122 return m_stamp_enabled ? m_stamp : 0;
123 }
124
128 void setStampEnabled(bool enabled) {
129 m_stamp_enabled = enabled;
130 if (m_fe_values_neigh) {
131 m_fe_values_neigh->setStampEnabled(enabled);
132 }
133 }
134
135 constexpr const Eigen::Vector<real_t, Dim>& getFaceNormal() const {
136 return m_face_normal;
137 }
138 void setFaceNormal(const Eigen::Vector<real_t, Dim>& normal) {
140 }
141 constexpr bool hasFaceNeigh() const { return m_face_has_neigh; }
142 void setHasFaceNeigh(bool has_neigh) { m_face_has_neigh = has_neigh; }
143 index_t getCellId() const { return m_cell_id; }
144 void setCellId(index_t cell_id) {
145 m_cell_id = cell_id;
146 m_stamp = nextStamp();
147 }
148
150 void setBasisScaling(real_t scaling) {
151 m_basis_scaling = scaling;
152 m_stamp = nextStamp();
153 }
154 index_t getFaceId() const { return m_face_id; }
155 void setFaceId(index_t face_id) { m_face_id = face_id; }
156
162 template <typename FloatType, typename Polynomial>
164 const std::vector<Polynomial>& basis,
166 const int64_t basis_size = basis.size();
167 const int64_t nodes_size = qrule.nodes.size();
168 resizeTables(basis_size, nodes_size);
169
171 evalSparseBasis(basis, qrule);
172 } else {
173 for (int64_t i = 0; i < basis_size; ++i) {
174 const auto& basis_func = basis[i];
175 const auto grad = basis_func.gradient();
176 for (int64_t j = 0; j < nodes_size; ++j) {
178 basis_func.evaluate(qrule.nodes[j].point.data()));
179 for (std::size_t d = 0; d < Dim; ++d) {
181 grad[d].evaluate(qrule.nodes[j].point.data()));
182 }
183 }
184 }
185 }
186
187 for (int64_t j = 0; j < nodes_size; ++j) {
189 for (std::size_t d = 0; d < Dim; ++d) {
190 m_physical_qpts_ref(d, j) = vmath::to<real_t>(qrule.nodes[j].point[d]);
191 }
192 }
193 m_tabulated_basis = &basis;
194 m_stamp = nextStamp();
195 }
196
205
209 tables.grads = m_grads_shapes_ref;
210 tables.qpts = m_physical_qpts_ref;
212 tables.basis = m_tabulated_basis;
213 }
214
218 resizeTables(tables.shapes.rows(), tables.shapes.cols());
220 m_grads_shapes_ref = tables.grads;
221 m_physical_qpts_ref = tables.qpts;
223 m_tabulated_basis = tables.basis;
224 m_stamp = nextStamp();
225 }
226
232 template <typename MappingType>
233 void evalAndApplyMapping(const MappingType& mapping, UpdateFlagsType flags) {
234 if (flags & UpdateWeights) {
235 for (int64_t i = 0; i < m_quadrature_weights.size(); ++i) {
237 m_quadrature_weights_ref[i] * mapping.jacobianDet({});
238 }
239 }
240
241 if (flags & UpdateGradients) {
242 for (int64_t i = 0; i < m_grads_shapes.rows(); ++i) {
243 for (int64_t j = 0; j < m_grads_shapes.cols(); ++j) {
244 m_grads_shapes(i, j) = mapping.jacobianInv({}).array() *
245 m_grads_shapes_ref(i, j).array();
246 }
247 }
248 }
249
250 if (flags & UpdateValues) {
252 }
253
254 if (flags & UpdateQuadraturePts) {
255 mapping.transform(m_physical_qpts_ref, m_physical_qpts);
256 }
257 m_stamp = nextStamp();
258 }
259
261 m_fe_values_neigh = std::make_unique<FeValues<Dim>>();
262 m_fe_values_neigh->setStampEnabled(m_stamp_enabled);
263 }
264 auto& neighValues() { return *m_fe_values_neigh; }
265 const auto& neighValues() const { return *m_fe_values_neigh; }
266
267 protected:
268 void resizeTables(int64_t basis_size, int64_t nodes_size) {
269 if ((m_scalar_shapes_ref.rows() != basis_size) ||
270 (m_scalar_shapes_ref.cols() != nodes_size)) {
271 m_scalar_shapes_ref.resize(basis_size, nodes_size);
272 m_grads_shapes_ref.resize(basis_size, nodes_size);
273 m_scalar_shapes.resize(basis_size, nodes_size);
274 m_grads_shapes.resize(basis_size, nodes_size);
275 m_physical_qpts_ref.resize(Dim, nodes_size);
276 m_physical_qpts.resize(Dim, nodes_size);
277 m_quadrature_weights_ref.resize(nodes_size);
278 m_quadrature_weights.resize(nodes_size);
279 }
280 }
281
282 static std::uint64_t nextStamp() {
283 static std::atomic<std::uint64_t> counter{0};
284 return counter.fetch_add(1, std::memory_order_relaxed) + 1;
285 }
286
287 // Values and gradients of an unscaled sparse basis with the powers of each
288 // coordinate tabulated once per node; the operations and their order are
289 // those of SparsePolynomial::evaluate on the basis and on its derivative().
290 template <typename FloatType, typename Polynomial>
292 const std::vector<Polynomial>& basis,
294 static_assert(
295 Polynomial::dim == Dim, "basis and FeValues dimensions differ");
296 static_assert(
297 std::is_same_v<FloatType, double>, "quadrature nodes must be double");
298 using ExpType = std::decay_t<decltype(basis[0].data()[0].exp()[0])>;
299 std::size_t n_pow = 1;
300 for (const auto& poly : basis) {
301 for (const auto& mono : poly.data()) {
302 for (const auto e : mono.exp()) {
303 n_pow = std::max<std::size_t>(n_pow, static_cast<std::size_t>(e) + 1);
304 }
305 }
306 }
307 m_pow_table.resize(Dim * n_pow);
308 const int64_t nodes_size = qrule.nodes.size();
309 for (int64_t j = 0; j < nodes_size; ++j) {
310 const double* pt = qrule.nodes[j].point.data();
311 for (std::size_t d = 0; d < Dim; ++d) {
312 for (std::size_t k = 0; k < n_pow; ++k) {
313 m_pow_table[d * n_pow + k] =
314 vmath::pow(pt[d], static_cast<ExpType>(k));
315 }
316 }
317 for (std::size_t i = 0; i < basis.size(); ++i) {
318 double value = 0.0;
319 Eigen::Vector<double, Dim> grad = Eigen::Vector<double, Dim>::Zero();
320 for (const auto& mono : basis[i].data()) {
321 const auto& e = mono.exp();
322 double term = mono.coef();
323 for (std::size_t d = 0; d < Dim; ++d) {
324 term *= m_pow_table[d * n_pow + e[d]];
325 }
326 value += term;
327 for (std::size_t c = 0; c < Dim; ++c) {
328 if (e[c] == 0) {
329 continue;
330 }
331 double dterm = mono.coef() * e[c];
332 if (dterm == 0.0) {
333 continue;
334 }
335 for (std::size_t d = 0; d < Dim; ++d) {
336 dterm *= m_pow_table[d * n_pow + e[d] - (d == c ? 1 : 0)];
337 }
338 grad[c] += dterm;
339 }
340 }
341 m_scalar_shapes_ref(i, j) = value;
342 m_grads_shapes_ref(i, j) = grad;
343 }
344 }
345 }
346
347 // storage of values on physical cell
352 Eigen::Vector<real_t, Dim> m_face_normal;
357 // storage of values on reference cell
362 const void* m_tabulated_basis = nullptr;
363 std::uint64_t m_stamp = 0;
364 bool m_stamp_enabled = false;
365 std::vector<double> m_pow_table;
366 // neighbor fe values
367 std::unique_ptr<FeValues<Dim>> m_fe_values_neigh;
368};
369} // namespace fe
370} // namespace vulpes
371
372#endif // VULPES_FE_FE_VALUES_HPP_
This class works as a cache table for computing efficiently the value of the basis function and its d...
Definition fe_values.hpp:94
Eigen::Vector< real_t, -1 > QuadtratureWeigths
Definition fe_values.hpp:101
index_t m_cell_id
Definition fe_values.hpp:354
Eigen::Matrix< real_t, -1, -1 > FeValuesScalar
Definition fe_values.hpp:99
constexpr std::uint64_t stamp() const
Definition fe_values.hpp:121
index_t getFaceId() const
Definition fe_values.hpp:154
void setFaceNormal(const Eigen::Vector< real_t, Dim > &normal)
Definition fe_values.hpp:138
const auto & neighValues() const
Definition fe_values.hpp:265
FeValuesScalar m_scalar_shapes_ref
Definition fe_values.hpp:358
void resizeTables(int64_t basis_size, int64_t nodes_size)
Definition fe_values.hpp:268
void setCellId(index_t cell_id)
Definition fe_values.hpp:144
constexpr const auto & qpts() const
Definition fe_values.hpp:107
void setFaceId(index_t face_id)
Definition fe_values.hpp:155
std::vector< double > m_pow_table
Definition fe_values.hpp:365
PhysicalQPts m_physical_qpts
Definition fe_values.hpp:350
constexpr const auto & weights() const
Definition fe_values.hpp:105
index_t getCellId() const
Definition fe_values.hpp:143
constexpr auto & qpts()
Definition fe_values.hpp:108
Eigen::Vector< real_t, Dim > m_face_normal
Definition fe_values.hpp:352
std::uint64_t m_stamp
Definition fe_values.hpp:363
void evalOnReferenceFrame(const std::vector< Polynomial > &basis, const qrule::QuadratureRule< FloatType, Dim > &qrule)
Compute and cache the evaluation of the basis on the quadrature points passed as argument....
Definition fe_values.hpp:163
constexpr const auto & qptsRef() const
Definition fe_values.hpp:111
constexpr auto & weightsRef()
Definition fe_values.hpp:110
FeValuesGrad m_grads_shapes
Definition fe_values.hpp:349
PhysicalQPts m_physical_qpts_ref
Definition fe_values.hpp:360
constexpr const auto & getShapeEval() const
Definition fe_values.hpp:103
real_t m_basis_scaling
Definition fe_values.hpp:355
bool m_stamp_enabled
Definition fe_values.hpp:364
void createNeighValues()
Definition fe_values.hpp:260
QuadtratureWeigths m_quadrature_weights_ref
Definition fe_values.hpp:361
constexpr const auto & getGradEval() const
Definition fe_values.hpp:104
Eigen::Matrix< real_t, Dim, -1 > PhysicalQPts
Definition fe_values.hpp:98
constexpr bool hasFaceNeigh() const
Definition fe_values.hpp:141
void saveReferenceFrame(ReferenceTables &tables) const
Copies the reference-frame tables of the last evalOnReferenceFrame.
Definition fe_values.hpp:207
int32_t index_t
Definition fe_values.hpp:97
constexpr auto & qptsRef()
Definition fe_values.hpp:112
constexpr const auto & weightsRef() const
Definition fe_values.hpp:109
void evalSparseBasis(const std::vector< Polynomial > &basis, const qrule::QuadratureRule< FloatType, Dim > &qrule)
Definition fe_values.hpp:291
real_t basisScaling() const
sqrt(det J) of the cell mapping, the factor scaled bases are divided by
Definition fe_values.hpp:149
QuadtratureWeigths m_quadrature_weights
Definition fe_values.hpp:351
bool m_face_has_neigh
Definition fe_values.hpp:353
FeValuesGrad m_grads_shapes_ref
Definition fe_values.hpp:359
const void * m_tabulated_basis
Definition fe_values.hpp:362
FeValuesScalar m_scalar_shapes
Definition fe_values.hpp:348
void setHasFaceNeigh(bool has_neigh)
Definition fe_values.hpp:142
void loadReferenceFrame(const ReferenceTables &tables)
Definition fe_values.hpp:217
std::unique_ptr< FeValues< Dim > > m_fe_values_neigh
Definition fe_values.hpp:367
static std::uint64_t nextStamp()
Definition fe_values.hpp:282
constexpr const void * tabulatedBasis() const
Basis tabulated by the last evalOnReferenceFrame, to compare by address.
Definition fe_values.hpp:114
constexpr auto & weights()
Definition fe_values.hpp:106
auto & neighValues()
Definition fe_values.hpp:264
index_t m_face_id
Definition fe_values.hpp:356
void setStampEnabled(bool enabled)
Definition fe_values.hpp:128
Eigen::Matrix< Eigen::Vector< real_t, Dim >, -1, -1 > FeValuesGrad
Definition fe_values.hpp:100
void setBasisScaling(real_t scaling)
Definition fe_values.hpp:150
double real_t
Definition fe_values.hpp:96
constexpr const Eigen::Vector< real_t, Dim > & getFaceNormal() const
Definition fe_values.hpp:135
void evalAndApplyMapping(const MappingType &mapping, UpdateFlagsType flags)
Update the local cache values on the physical element by passing the mapping and a flag signaling whi...
Definition fe_values.hpp:233
Definition fe_expr_base.hpp:58
Definition dof_policy.hpp:45
const auto normal(const FeSpace &)
Definition fe_expr_func.hpp:92
std::underlying_type_t< UpdateFlags > UpdateFlagsType
Definition fe_values.hpp:65
UpdateFlags
Flags that indicate which parts of FeValues shall be updated on each cell.
Definition fe_values.hpp:51
@ UpdateAll
Definition fe_values.hpp:63
@ UpdateNormals
Definition fe_values.hpp:59
@ UpdateWeights
Definition fe_values.hpp:57
@ UpdateNeighborCell
Definition fe_values.hpp:61
@ UpdateNothing
Definition fe_values.hpp:52
@ UpdateNeighborValues
Definition fe_values.hpp:62
@ UpdateValues
Definition fe_values.hpp:54
@ UpdateGradients
Definition fe_values.hpp:55
@ UpdateMapping
Definition fe_values.hpp:53
@ UpdateHessians
Definition fe_values.hpp:56
@ UpdateQuadraturePts
Definition fe_values.hpp:58
@ UpdateId
Definition fe_values.hpp:60
FeExpressionGradFunc< IndexBegin, IndexEnd, Dim, FeSpace, IsTest > grad(const FeExpressionFunc< IndexBegin, IndexEnd, Dim, FeSpace, IsTest > &w)
Definition fe_expr_basis.hpp:315
Definition monomial.hpp:39
Definition gauss.cpp:39
V to(const U &src)
Converts a value of one pseudo-arithmetic type to another.
Definition vmath.hpp:321
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
Reference-frame tables written by evalOnReferenceFrame.
Definition fe_values.hpp:198
FeValuesGrad grads
Definition fe_values.hpp:200
QuadtratureWeigths weights
Definition fe_values.hpp:202
FeValuesScalar shapes
Definition fe_values.hpp:199
const void * basis
Definition fe_values.hpp:203
PhysicalQPts qpts
Definition fe_values.hpp:201
True for a double SparsePolynomial with unscaled monomials.
Definition fe_values.hpp:70
Represents a quadrature rule consisting of a set of points and associated weights.
Definition qrule.hpp:69