Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
integrator_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_ASSEMBLE_INTEGRATOR_BASE_HPP_
25#define VULPES_ASSEMBLE_INTEGRATOR_BASE_HPP_
26
27#include <algorithm>
28#include <cassert>
29#include <cmath>
30#include <iostream>
31#include <stdexcept>
32#include <tuple>
33#include <unordered_map>
34
35#include <unsupported/Eigen/CXX11/Tensor>
36
43
44namespace vulpes {
45namespace assemble {
46
59template <
60 typename MeshType,
61 typename FeExpressionTuple,
62 typename FeExpressionFaceTuple = std::tuple<>>
64 static_assert(
66 "fe_expression must be a tuple of pointer to FeExpression");
67 static_assert(
69 "fe_expression_face must be a tuple of pointer to FeExpression");
70
71 public:
72 using index_t = typename MeshType::index_t;
73 using FeType = std::remove_pointer_t<
74 std::tuple_element_t<0, FeExpressionTuple>>::FeSpaceType;
76 static inline constexpr index_t s_n_expr =
77 std::tuple_size<FeExpressionTuple>{};
78
79 static inline constexpr index_t s_n_face_expr =
80 std::tuple_size<FeExpressionFaceTuple>{};
81
83 decltype(internal::split_expression_tuple(FeExpressionTuple{}).first);
85 decltype(internal::split_expression_tuple(FeExpressionTuple{}).second);
87 static inline constexpr index_t s_n_expr_bilinear =
88 std::tuple_size<FeExpressionTupleBilinear>{};
89
90 static inline constexpr index_t s_n_expr_linear =
91 std::tuple_size<FeExpressionTupleLinear>{};
92
94 decltype(internal::split_expression_tuple(FeExpressionFaceTuple{}).first);
96 decltype(internal::split_expression_tuple(FeExpressionFaceTuple{})
97 .second);
99 static inline constexpr index_t s_n_face_expr_bilinear =
100 std::tuple_size<FeExpressionTupleFaceBilinear>{};
101
102 static inline constexpr index_t s_n_face_expr_linear =
103 std::tuple_size<FeExpressionTupleFaceLinear>{};
104
105 static inline constexpr std::size_t dim = MeshType::self_dim;
107 using MappingType = std::decay_t<decltype(FeType::getMapping(CellType{}))>;
108
111 static inline constexpr uint32_t nblocks =
112 std::remove_pointer_t<
113 std::tuple_element_t<0, FeExpressionTuple>>::k_index_end -
114 std::remove_pointer_t<
115 std::tuple_element_t<0, FeExpressionTuple>>::k_index_begin;
116
127 const MeshType& mesh,
128 CellManager<MeshType, FeType>& cell_manager,
129 const FeExpressionTuple& fe_expression,
130 const FeExpressionFaceTuple& fe_expression_face)
131 : m_mesh(mesh),
132 m_cell_manager(cell_manager),
133 m_fe_expression(fe_expression),
134 m_fe_expression_face(fe_expression_face),
135 m_fe_space(std::get<0>(fe_expression)->feSpace()) {
136 // the projected fields do not change while one cell is integrated, so
137 // expression leaves may reuse values computed for the same stamp
138 m_fe_values.setStampEnabled(true);
139 const uint32_t n_dof_loc = m_fe_space.getBasis().size();
140 m_integrals.resize(
141 n_dof_loc * nblocks, n_dof_loc * nblocks, s_n_expr_bilinear);
142 m_rhs.resize(n_dof_loc * nblocks, s_n_expr_linear);
143 m_face_integrals.resize(
144 n_dof_loc * nblocks, n_dof_loc * nblocks, s_n_face_expr_bilinear);
145 m_face_rhs.resize(n_dof_loc * nblocks, s_n_face_expr_linear);
146 using index_t = typename MeshType::index_t;
147 index_t n_max_neigh = 0;
148 for (index_t cid = 0; cid < mesh.getDualMap().size(); ++cid) {
149 n_max_neigh = std::max(n_max_neigh, mesh.getDualMap()[cid].size());
150 }
152 n_dof_loc * nblocks, n_dof_loc * nblocks, s_n_face_expr, n_max_neigh);
153 m_face_rhs_neigh.resize(n_dof_loc * nblocks, s_n_face_expr, n_max_neigh);
157 }
158 virtual ~WeakFormIntegratorBase() = default;
159
165 virtual void integrate(index_t cell_id) = 0;
166
172 if constexpr (s_n_expr) {
173 m_update_flags |= std::apply(
174 [](const auto&... expr) {
175 return (expr->computeUpdateFlags() | ...);
176 },
178 }
179 if constexpr (s_n_face_expr) {
180 m_update_flags_face |= std::apply(
181 [](const auto&... expr) {
182 return (expr->computeUpdateFlags() | ...);
183 },
185 }
186 }
187
196 m_order = 1;
197 m_order_face = 1;
198 if constexpr (s_n_expr) {
199 m_order += std::apply(
200 [](const auto&... expr) {
201 return std::max({expr->computeOrder()...});
202 },
204 }
205 if constexpr (s_n_face_expr) {
206 m_order_face += std::apply(
207 [](const auto&... expr) {
208 return std::max({expr->computeOrder()...});
209 },
211 }
212 }
213
222 const CellType& getCell(index_t cell_id, bool update_mapping = false) {
223 const auto& cell = m_cell_manager.getCell(cell_id);
224 if (update_mapping) {
225 m_mapping = m_cell_manager.getMapping(cell_id).get();
226 if constexpr (FeType::needBasisScaling()) {
227 m_fe_values.setBasisScaling(std::sqrt(m_mapping->jacobianDet({})));
228 }
229 }
230 return cell;
231 }
232
239 void preallocate(index_t cell_id) {
240 m_integrals.setZero();
241 m_rhs.setZero();
245 if (m_need_neigh_cell) {
246 m_fe_values.createNeighValues();
247 }
248 m_cell_id = cell_id;
249 m_fe_values.setCellId(m_cell_id);
250 if constexpr (s_n_face_expr) {
251 m_face_integrals.setZero();
252 m_face_rhs.setZero();
253 }
254 m_face_integrals_neigh.setZero();
255 m_face_rhs_neigh.setZero();
256 }
257
263 m_fe_values.setFaceId(m_face_id);
264 // `findCellNeigh` builds the neighbor if we ask for it
265 // so we call it only if we need it
266 if (m_need_neigh_cell) {
268 if (m_cell_neigh_info.have_neigh) {
269 auto& neigh_fe_values = m_fe_values.neighValues();
270 neigh_fe_values.setCellId(m_cell_neigh_info.neigh_cell_id);
271 neigh_fe_values.setFaceId(m_cell_neigh_info.loc_neigh_face_id);
272 getCell(m_cell_neigh_info.neigh_cell_id);
273 if constexpr (FeType::needBasisScaling()) {
274 neigh_fe_values.setBasisScaling(std::sqrt(
275 m_cell_manager.getMapping(m_cell_neigh_info.neigh_cell_id)
276 ->jacobianDet({})));
277 }
278 }
279 } else {
281 }
282 m_fe_values.setHasFaceNeigh(m_cell_neigh_info.have_neigh);
283 }
284
295 if constexpr (FeType::needBasisScaling()) {
296 const auto& mapping = m_cell_manager.getMapping(m_cell_id);
297 const double scaling2 = mapping->jacobianDet({});
298 const double scaling = std::sqrt(scaling2);
299
300 m_rhs /= scaling;
301 Eigen::Map<Eigen::Vector<double, -1>> integrals_map(
302 m_integrals.data(), m_integrals.size());
303 integrals_map /= scaling2;
304
305 m_face_rhs /= scaling;
306 Eigen::Map<Eigen::Vector<double, -1>> face_integrals_map(
307 m_face_integrals.data(), m_face_integrals.size());
308 face_integrals_map /= scaling2;
309
310 const auto n_neigh =
311 m_need_neigh_cell ? m_mesh.getDualMap()[m_cell_id].size() : 0;
312 for (index_t ni = 0; ni < n_neigh; ++ni) {
313 const auto neigh_cell_id = m_mesh.getDualMap()[m_cell_id][ni];
314 const auto& neigh_map = m_cell_manager.getMapping(neigh_cell_id);
315 const double neigh_scaling = std::sqrt(neigh_map->jacobianDet({}));
316 Eigen::Map<Eigen::Vector<double, -1>> face_integrals_neigh_map(
317 &m_face_integrals_neigh(0, 0, 0, ni), m_face_integrals.size());
318 face_integrals_neigh_map /= (scaling * neigh_scaling);
319 Eigen::Map<Eigen::Vector<double, -1>> face_rhs_neigh_map(
320 &m_face_rhs_neigh(0, 0, ni),
321 m_face_rhs_neigh.dimension(0) * m_face_rhs_neigh.dimension(1));
322 face_rhs_neigh_map /= neigh_scaling;
323 }
324 }
325 }
326
345 Eigen::Map<Eigen::MatrixXd> getCurrFaceIntegral(
346 const auto& expr, Eigen::Index expr_id) {
347 static double s_dummy_mat = 0.0;
348 const bool expr_need_neigh =
349 expr->computeUpdateFlags() & fe::UpdateNeighborValues;
350 if (expr_need_neigh && (!m_cell_neigh_info.have_neigh)) {
351 return Eigen::Map<Eigen::MatrixXd>{&s_dummy_mat, 0, 0};
352 }
353 // the fe expression might want to skip a face
354 // e.g. for Dirichlet boundary conditions
355 if (!expr->precompute(m_fe_values, m_cell_manager.getCells())) {
356 return Eigen::Map<Eigen::MatrixXd>{&s_dummy_mat, 0, 0};
357 }
358 double* data_ptr =
359 expr_need_neigh
361 0, 0, expr_id, m_cell_neigh_info.loc_neigh_cell_id)
362 : &m_face_integrals(0, 0, expr_id);
363 const Eigen::Index n_dof_loc = m_fe_space.getBasis().size();
364 return Eigen::Map<Eigen::MatrixXd>{
365 data_ptr, n_dof_loc * nblocks, n_dof_loc * nblocks};
366 }
367
380 Eigen::Map<Eigen::VectorXd> getCurrFaceRhs(
381 const auto& expr, Eigen::Index expr_id) {
382 static double s_dummy_vec = 0.0;
383 const bool expr_need_neigh =
384 expr->computeUpdateFlags() & fe::UpdateNeighborValues;
385 if (expr_need_neigh && (!m_cell_neigh_info.have_neigh)) {
386 return Eigen::Map<Eigen::VectorXd>{&s_dummy_vec, 0};
387 }
388 if (!expr->precompute(m_fe_values, m_cell_manager.getCells())) {
389 return Eigen::Map<Eigen::VectorXd>{&s_dummy_vec, 0};
390 }
391 const Eigen::Index n_dof_loc = m_fe_space.getBasis().size();
392 double* data_ptr =
393 expr_need_neigh
394 ? &m_face_rhs_neigh(0, expr_id, m_cell_neigh_info.loc_neigh_cell_id)
395 : &m_face_rhs(0, expr_id);
396 return Eigen::Map<Eigen::VectorXd>{data_ptr, n_dof_loc * nblocks};
397 }
398
411 const auto check = [](auto it_inserted) {
412 const auto [it, inserted] = it_inserted;
413 if (!inserted) {
414 throw std::logic_error(
415 "the same weak form is used twice in one integrator");
416 }
417 };
418 auto [bilinear_fe_expression, non_bilinear_fe_expression] =
421 bilinear_fe_expression,
422 [this, &check](auto expr_ptr, Eigen::Index expr_id) {
423 check(m_wf_ptr_table.emplace(
424 static_cast<void*>(expr_ptr), &m_integrals(0, 0, expr_id)));
425 });
427 non_bilinear_fe_expression,
428 [this, &check](auto expr_ptr, Eigen::Index expr_id) {
429 check(m_wf_ptr_table.emplace(
430 static_cast<void*>(expr_ptr), &m_rhs(0, expr_id)));
431 });
432 auto [bilinear_fe_expression_face, non_bilinear_fe_expression_face] =
435 bilinear_fe_expression_face,
436 [this, &check](auto expr_ptr, Eigen::Index expr_id) {
437 const bool expr_need_neigh =
438 expr_ptr->computeUpdateFlags() & fe::UpdateNeighborValues;
439 if (expr_need_neigh) {
440 check(m_wf_ptr_table.emplace(
441 static_cast<void*>(expr_ptr),
442 &m_face_integrals_neigh(0, 0, expr_id, 0)));
443 } else {
444 check(m_wf_ptr_table.emplace(
445 static_cast<void*>(expr_ptr),
446 &m_face_integrals(0, 0, expr_id)));
447 }
448 });
450 non_bilinear_fe_expression_face,
451 [this, &check](auto expr_ptr, Eigen::Index expr_id) {
452 const bool expr_need_neigh =
453 expr_ptr->computeUpdateFlags() & fe::UpdateNeighborValues;
454 if (expr_need_neigh) {
455 check(m_wf_ptr_table.emplace(
456 static_cast<void*>(expr_ptr),
457 &m_face_rhs_neigh(0, expr_id, 0)));
458 } else {
459 check(m_wf_ptr_table.emplace(
460 static_cast<void*>(expr_ptr), &m_face_rhs(0, expr_id)));
461 }
462 });
463 }
464
466 const auto& getIntegrals() const { return m_integrals; }
468 const auto& getRhsIntegrals() const { return m_rhs; }
471 const auto& getFaceIntegrals() const { return m_face_integrals; }
473 const auto& getRhsFaceIntegrals() const { return m_face_rhs; }
476 const auto& getFaceIntegralsNeigh() const { return m_face_integrals_neigh; }
479 const auto& getRhsFaceIntegralsNeigh() const { return m_face_rhs_neigh; }
480
494 auto getIntegral(auto wf_ptr, index_t loc_neigh_id = -1) const {
495 assert(
496 (wf_ptr->computeUpdateFlags() & fe::UpdateNeighborValues) ||
497 (loc_neigh_id <= 0));
498 constexpr bool is_bilinear =
499 std::remove_pointer_t<decltype(wf_ptr)>::is_bilinear == fe::IsBilinear;
500 const Eigen::Index n_dof_loc = m_fe_space.getBasis().size();
501 const auto nrows = n_dof_loc * nblocks;
502 const auto ncols = is_bilinear ? nrows : 1;
503 const auto slice_neigh_size = nrows * ncols * s_n_face_expr;
504 const double* data = m_wf_ptr_table.at(wf_ptr);
505 const bool is_neigh_form =
506 (wf_ptr->computeUpdateFlags() & fe::UpdateNeighborValues) != 0;
507 if (is_neigh_form && loc_neigh_id < 0) {
508 const index_t n_neigh = is_bilinear ? m_face_integrals_neigh.dimension(3)
509 : m_face_rhs_neigh.dimension(2);
510 // Accumulate into a scratch buffer keyed by wf_ptr (rather than a
511 // freshly allocated matrix, and rather than one buffer shared by the
512 // whole integrator) so the returned view stays zero-copy at the call
513 // site while two DIFFERENT neigh-summed forms queried on the same
514 // integrator (e.g. compared via a-b) cannot alias one another.
515 Eigen::MatrixXd& scratch = m_neigh_sum_scratch[wf_ptr];
516 scratch.setZero(nrows, ncols);
517 for (index_t ni = 0; ni < n_neigh; ++ni) {
518 scratch += Eigen::Map<const Eigen::MatrixXd>{
519 data + ni * slice_neigh_size, nrows, ncols};
520 }
521 return Eigen::Map<const Eigen::MatrixXd>{scratch.data(), nrows, ncols};
522 }
523 const auto offset = std::max(index_t{0}, loc_neigh_id);
524 return Eigen::Map<const Eigen::MatrixXd>{
525 data + offset * slice_neigh_size, nrows, ncols};
526 }
527
528 protected:
530 const MeshType& m_mesh;
535 FeExpressionTuple m_fe_expression;
538 FeExpressionFaceTuple m_fe_expression_face;
543 std::size_t m_order = -1;
546 std::size_t m_order_face = -1;
549 std::unordered_map<void*, double*> m_wf_ptr_table;
554 mutable std::unordered_map<void*, Eigen::MatrixXd> m_neigh_sum_scratch;
562 Eigen::Tensor<double, 3> m_integrals;
564 Eigen::MatrixXd m_rhs;
567 Eigen::Tensor<double, 3> m_face_integrals;
569 Eigen::MatrixXd m_face_rhs;
572 Eigen::Tensor<double, 4> m_face_integrals_neigh;
577 Eigen::Tensor<double, 3> m_face_rhs_neigh;
578
582
588
598 const MappingType* m_mapping = nullptr;
599};
600
601} // namespace assemble
602} // namespace vulpes
603
604#endif // VULPES_ASSEMBLE_INTEGRATOR_BASE_HPP_
Manages geometrical information about the cell and its mapping to the reference element.
Definition cell_manager.hpp:62
const auto & getRhsFaceIntegralsNeigh() const
Get a const reference to the (linear) face rhs buffer where the test function belongs to the neighbor...
Definition integrator_base.hpp:479
Eigen::Map< Eigen::VectorXd > getCurrFaceRhs(const auto &expr, Eigen::Index expr_id)
Get a map to the rhs vector for the current face and expression.
Definition integrator_base.hpp:380
std::decay_t< decltype(FeType::getMapping(CellType{}))> MappingType
Definition integrator_base.hpp:107
std::unordered_map< void *, Eigen::MatrixXd > m_neigh_sum_scratch
Definition integrator_base.hpp:554
void applyBasisScaling()
Apply the basis scaling to the integrals and right-hand side contributions if needed.
Definition integrator_base.hpp:294
static constexpr std::size_t dim
Definition integrator_base.hpp:105
WeakFormIntegratorBase(const MeshType &mesh, CellManager< MeshType, FeType > &cell_manager, const FeExpressionTuple &fe_expression, const FeExpressionFaceTuple &fe_expression_face)
Construct a new Weak Form Integrator Base object.
Definition integrator_base.hpp:126
static constexpr index_t s_n_face_expr_linear
Definition integrator_base.hpp:102
void buildWeakFormPointerTable()
Build a lookup table that maps finite element expression pointers to their corresponding data storage...
Definition integrator_base.hpp:410
Eigen::Map< Eigen::MatrixXd > getCurrFaceIntegral(const auto &expr, Eigen::Index expr_id)
Get a map to the data corresponding to the face integral for the current cell and face being integrat...
Definition integrator_base.hpp:345
geometry::Cell< dim, double, index_t > CellType
Definition integrator_base.hpp:106
const auto & getFaceIntegralsNeigh() const
Get a const reference to the (bilinear) face integrals buffer where one basis function belongs to the...
Definition integrator_base.hpp:476
void computeMaxOrder()
Compute the maximum polynomial order among all finite element expressions.
Definition integrator_base.hpp:195
const auto & getIntegrals() const
Get a const reference to the (bilinear) volume integrals buffer.
Definition integrator_base.hpp:466
decltype(internal::split_expression_tuple(FeExpressionTuple{}).first) FeExpressionTupleBilinear
Definition integrator_base.hpp:82
typename MeshType::index_t index_t
Definition integrator_base.hpp:72
Eigen::Tensor< double, 3 > m_face_rhs_neigh
Definition integrator_base.hpp:577
const auto & getRhsFaceIntegrals() const
Get a const reference to the (linear) face right-hand side buffer.
Definition integrator_base.hpp:473
const auto & getRhsIntegrals() const
Get a const reference to the (linear) volume right-hand side buffer.
Definition integrator_base.hpp:468
std::unordered_map< void *, double * > m_wf_ptr_table
Definition integrator_base.hpp:549
Eigen::Tensor< double, 3 > m_face_integrals
Definition integrator_base.hpp:567
CellManager< MeshType, FeType >::NeighInfo CellNeighInfo
Definition integrator_base.hpp:589
virtual void integrate(index_t cell_id)=0
Integrate the weak form over a cell.
Eigen::Tensor< double, 3 > m_integrals
Definition integrator_base.hpp:562
Eigen::Tensor< double, 4 > m_face_integrals_neigh
Definition integrator_base.hpp:572
std::remove_pointer_t< std::tuple_element_t< 0, FeExpressionTuple > >::FeSpaceType FeType
Definition integrator_base.hpp:73
void computeUpdateFlags()
Compute the update flags indicating the values to be updated for each cell in the FEValues.
Definition integrator_base.hpp:171
auto getIntegral(auto wf_ptr, index_t loc_neigh_id=-1) const
Get the integral (matrix or vector) for the current cell.
Definition integrator_base.hpp:494
const auto & getFaceIntegrals() const
Get a const reference to the (bilinear) face integrals buffer where both basis functions belong to th...
Definition integrator_base.hpp:471
void preallocate(index_t cell_id)
Resize the internal storage of local integrals (matrices) to fit the number of finite element express...
Definition integrator_base.hpp:239
CellManager< MeshType, FeType > & m_cell_manager
Definition integrator_base.hpp:532
void findCellNeigh()
Find the neighboring cell across the current (local) face (m_face_id) and set up the ids for the neig...
Definition integrator_base.hpp:262
decltype(internal::split_expression_tuple(FeExpressionTuple{}).second) FeExpressionTupleLinear
Definition integrator_base.hpp:84
decltype(internal::split_expression_tuple(FeExpressionFaceTuple{}) .second) FeExpressionTupleFaceLinear
Definition integrator_base.hpp:95
decltype(internal::split_expression_tuple(FeExpressionFaceTuple{}).first) FeExpressionTupleFaceBilinear
Definition integrator_base.hpp:93
const CellType & getCell(index_t cell_id, bool update_mapping=false)
Get the cell object for a given cell id.
Definition integrator_base.hpp:222
static constexpr index_t s_n_face_expr_bilinear
Definition integrator_base.hpp:99
This class works as a cache table for computing efficiently the value of the basis function and its d...
Definition fe_values.hpp:94
constexpr auto split_expression_tuple(std::tuple< Ts... > t)
Definition integrator_traits.hpp:145
constexpr bool is_tuple_of_fe_expr_pointers_v
Definition integrator_traits.hpp:102
Definition cell_manager.hpp:39
std::underlying_type_t< UpdateFlags > UpdateFlagsType
Definition fe_values.hpp:65
@ UpdateNeighborCell
Definition fe_values.hpp:61
@ UpdateNeighborValues
Definition fe_values.hpp:62
@ IsBilinear
Definition fe_expr_base.hpp:55
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
void for_each_idxed(Tuple &&tuple, Func &&func)
Apply func to every element of tuple together with its index.
Definition vvutils.hpp:165
Definition cell_manager.hpp:38
Information about the neighbouring cell for a certain face.
Definition cell_manager.hpp:73