Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
integrator_simplexify.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: Francesco Daniele <francesco.daniele@polimi.it>
22// Author: Matteo Caldana <matteo.caldana@polimi.it>
23// Author: Pedro Peixoto <ppeixoto@usp.br>
24
25#ifndef VULPES_ASSEMBLE_INTEGRATOR_SIMPLEXIFY_HPP_
26#define VULPES_ASSEMBLE_INTEGRATOR_SIMPLEXIFY_HPP_
27
28#include <algorithm>
29#include <iostream>
30#include <memory>
31#include <optional>
32#include <tuple>
33#include <vector>
34
39
40namespace vulpes {
41namespace assemble {
42
71template <
72 typename MeshType,
73 typename FeExpressionTuple,
74 typename FeExpressionFaceTuple = std::tuple<>>
76 MeshType,
77 FeExpressionTuple,
78 FeExpressionFaceTuple> {
79 public:
81 using index_t = typename MeshType::index_t;
82
85 MeshType,
86 FeExpressionTuple,
87 FeExpressionFaceTuple>;
88
90 static inline constexpr std::size_t dim = MeshType::self_dim;
91
93 static inline constexpr index_t s_n_expr =
95 static inline constexpr index_t s_n_face_expr =
97 static inline constexpr index_t s_n_expr_bilinear =
99 static inline constexpr index_t s_n_expr_linear =
101 static inline constexpr index_t s_n_face_expr_bilinear =
103 static inline constexpr index_t s_n_face_expr_linear =
105
108 static inline constexpr index_t s_n_blocks =
110
113
116
118 using MappingType = std::decay_t<decltype(FeType::getMapping(CellType{}))>;
119
132 const MeshType& mesh,
133 CellManager<MeshType, FeType>& cell_manager,
134 const FeExpressionTuple& fe_expression,
135 const FeExpressionFaceTuple& fe_expression_face =
136 FeExpressionFaceTuple{});
137
150 void integrate(index_t cell_id) override;
151
157 void setCellCache(bool enable) {
158 m_cell_cache_enabled = enable;
159 m_cell_cache.clear();
160 }
161
162 protected:
163 struct CellCache {
164 std::unique_ptr<geometry::Simplexify<dim>> simplexification;
165 std::vector<typename fe::FeValues<dim>::ReferenceTables> tables;
166 };
167
168 // evalOnReferenceFrame, served by the cache of the current cell if enabled
169 template <typename QRule>
170 void evalReferenceFrame(fe::FeValues<dim>& fe_values, const QRule& qrule) {
171 if (!m_curr_cache) {
172 fe_values.evalOnReferenceFrame(this->m_fe_space.getBasisFast(), qrule);
173 return;
174 }
175 auto& tables = m_curr_cache->tables;
176 const auto& basis = this->m_fe_space.getBasisFast();
177 if (m_table_slot >= tables.size()) {
178 tables.emplace_back();
179 }
180 auto& saved = tables[m_table_slot];
181 if (saved.basis == &basis &&
182 static_cast<std::size_t>(saved.weights.size()) == qrule.nodes.size()) {
183 fe_values.loadReferenceFrame(saved);
184 } else {
185 fe_values.evalOnReferenceFrame(basis, qrule);
186 fe_values.saveReferenceFrame(saved);
187 }
188 ++m_table_slot;
189 }
190
192 std::vector<CellCache> m_cell_cache;
194 std::size_t m_table_slot = 0;
195
213 template <typename SimplexMatrixType, typename VertexMatrixType>
215 const SimplexMatrixType& simplexes,
216 const VertexMatrixType& vertexes,
217 int32_t k);
218
232 void integrateVolumeCell();
233
254 template <typename FaceSubsimplexMatrix, typename SimplexPtsMatrix>
256 const FaceSubsimplexMatrix& face_subsimplexes,
257 const SimplexPtsMatrix& simplexification_pts,
258 index_t k);
259
269
276 std::shared_ptr<qrule::QuadratureRule<double, dim>> m_qrule_d_ref = nullptr;
277
284
291 std::shared_ptr<qrule::QuadratureRule<double, dim - 1>> m_qrule_bndry_d_ref =
292 nullptr;
293
301};
302
303template <
304 typename MeshType,
305 typename FeExpressionTuple,
306 typename FeExpressionFaceTuple>
308 MeshType,
309 FeExpressionTuple,
310 FeExpressionFaceTuple>::
311 WeakFormIntegratorSimplexify(
312 const MeshType& mesh,
313 CellManager<MeshType, FeType>& cell_manager,
314 const FeExpressionTuple& fe_expression,
315 const FeExpressionFaceTuple& fe_expression_face)
317 mesh, cell_manager, fe_expression, fe_expression_face) {
318 // cache quadrature rule
322 //
325 m_qrule_bndry_d.nodes.resize(m_qrule_bndry_d_ref->nodes.size());
326}
327
328/* integrate method definition */
329template <
330 typename MeshType,
331 typename FeExpressionTuple,
332 typename FeExpressionFaceTuple>
334 MeshType,
335 FeExpressionTuple,
336 FeExpressionFaceTuple>::integrate(index_t cell_id) {
337 this->preallocate(cell_id);
338 auto& cell = this->getCell(this->m_cell_id, true);
339
340 const bool need_bnd = s_n_face_expr || this->m_need_neigh_values;
341 m_curr_cache = nullptr;
343 if (static_cast<std::size_t>(cell_id) >= m_cell_cache.size()) {
344 m_cell_cache.resize(cell_id + 1);
345 }
346 m_curr_cache = &m_cell_cache[cell_id];
347 m_table_slot = 0;
348 }
349 Profiler::start("integrate::simplexification");
350 std::optional<geometry::Simplexify<dim>> local_simplexification;
351 const geometry::Simplexify<dim>* simplexification_ptr = nullptr;
352 if (m_curr_cache) {
353 if (!m_curr_cache->simplexification) {
354 m_curr_cache->simplexification =
355 std::make_unique<geometry::Simplexify<dim>>(cell, need_bnd);
356 }
357 simplexification_ptr = m_curr_cache->simplexification.get();
358 } else {
359 simplexification_ptr = &local_simplexification.emplace(cell, need_bnd);
360 }
361 const auto& simplexification = *simplexification_ptr;
362 Profiler::stop("integrate::simplexification");
363 const auto& simplexes = simplexification.getSimplexesList();
364 const auto& vertexes = simplexification.getPointList();
365 // compute volume integrals
366 if constexpr (s_n_expr) {
367 // for each simplex
368 for (int32_t k = 0; k < simplexification.getNumSimplexes(); ++k) {
369 PROFILE(mapQuadraturePointsToSimplex(simplexes, vertexes, k));
370 // update evaluations of basis functions and mappings
372 this->m_fe_values.evalAndApplyMapping(
373 *(this->m_mapping), this->m_update_flags | fe::UpdateWeights);
375 }
376 }
377 // compute boundary integrals
378 if constexpr (s_n_face_expr) {
379 // loop on faces
380 for (this->m_face_id = 0; this->m_face_id < cell.nfaces();
381 ++this->m_face_id) {
382 PROFILE(this->findCellNeigh());
383 const auto face_subsimplexes =
384 simplexification.getSubsimplexesListFacet(this->m_face_id);
385 const index_t nsubsimplexes = face_subsimplexes.cols();
386 const auto simplexification_pts = simplexification.getPointList();
387 // iterate face sub-simplexification: triangles in 3D, a single line in 2D
388 for (index_t k = 0; k < nsubsimplexes; ++k) {
389 Profiler::start("integrate::updateQptsAndMappingForThisAndNeigh");
391 face_subsimplexes, simplexification_pts, k);
392 Profiler::stop("integrate::updateQptsAndMappingForThisAndNeigh");
394 }
395 }
396 }
397 this->applyBasisScaling();
398}
399
400/* mapQuadraturePointsToSimplex definition */
401template <
402 typename MeshType,
403 typename FeExpressionTuple,
404 typename FeExpressionFaceTuple>
405template <typename SimplexMatrixType, typename VertexMatrixType>
407 MeshType,
408 FeExpressionTuple,
409 FeExpressionFaceTuple>::
410 mapQuadraturePointsToSimplex(
411 const SimplexMatrixType& simplexes,
412 const VertexMatrixType& vertexes,
413 int32_t k) {
414 const std::size_t n_q_nodes_simplex = m_qrule_d.nodes.size();
415 const Eigen::Vector<double, dim> translation =
416 vertexes.col(simplexes.coeff(0, k));
417 Eigen::Matrix<double, dim, dim> jacobian;
418 for (std::size_t d = 0; d < dim; ++d) {
419 jacobian.col(d) = vertexes.col(simplexes.coeff(d + 1, k)) - translation;
420 }
421 const double jac_det = std::abs(vmath::determinant(jacobian));
422 // map the q nodes to the simplex
423 for (std::size_t q = 0; q < n_q_nodes_simplex; ++q) {
424 m_qrule_d.nodes[q].weight = m_qrule_d_ref->nodes[q].weight * jac_det;
425 const Eigen::Map<const Eigen::Vector<double, dim>> pt_map_src(
426 m_qrule_d_ref->nodes[q].point.data());
427 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
428 m_qrule_d.nodes[q].point.data());
429 pt_map_dst = jacobian * pt_map_src + translation;
430 }
431}
432
433/* integrateVolumeCell definition */
434template <
435 typename MeshType,
436 typename FeExpressionTuple,
437 typename FeExpressionFaceTuple>
439 MeshType,
440 FeExpressionTuple,
441 FeExpressionFaceTuple>::integrateVolumeCell() {
442 auto [bilinear_fe_expression, non_bilinear_fe_expression] =
444 const std::size_t n_q_nodes_simplex = m_qrule_d.nodes.size();
445 const uint32_t n_dof_loc = this->m_fe_space.getBasis().size();
446 std::apply(
447 [this](auto&&... exprs) {
448 ((exprs->precompute(
449 this->m_fe_values, this->m_cell_manager.getCells())),
450 ...);
451 },
452 this->m_fe_expression);
453 // apply quadrature rule for each pair basis function (i, j)
454 for (uint32_t q = 0; q < n_q_nodes_simplex; ++q) {
455 const double Jdx = this->m_fe_values.weights()[q];
456 for (uint32_t i = 0; i < n_dof_loc; ++i) {
457 // TODO: it is worth it to make further local copies
458 // of data used in the expression to increase cache locality
459 // this should have ~10% speed-up
460 if constexpr (s_n_expr_bilinear) {
461 for (uint32_t j = 0; j < n_dof_loc; ++j) {
462 uint32_t expr_id = 0;
463 // TODO: evaluate if the std::apply is better as the most inner
464 // loop (like now) or in the most outer loop
465 std::apply(
466 [this, &expr_id, i, j, q, Jdx, n_dof_loc](auto&&... exprs) {
467 (([this, &expr_id, i, j, q, Jdx, n_dof_loc](auto&& expr) {
468 [this, expr_id, i, j, q, Jdx, expr,
469 n_dof_loc]<std::size_t... d>(std::index_sequence<d...>) {
470 (
471 [&]<std::size_t D>() {
472 constexpr auto I = D % s_n_blocks;
473 constexpr auto J = D / s_n_blocks;
474 using EvalType =
475 decltype(expr->template evaluate<J, I>(
476 this->m_fe_values, j, i, q));
477 if constexpr (!std::is_same_v<EvalType, void>) {
478 this->m_integrals(
479 i * s_n_blocks + I, j * s_n_blocks + J,
480 expr_id) +=
481 expr->template evaluate<J, I>(
482 this->m_fe_values, j, i, q) *
483 Jdx;
484 }
485 }.template operator()<d>(),
486 ...);
487 }(std::make_index_sequence<s_n_blocks * s_n_blocks>{});
488 expr_id++;
489 }(exprs)),
490 ...);
491 },
492 bilinear_fe_expression);
493 }
494 }
495 if constexpr (s_n_expr_linear) {
496 constexpr auto UNDEF = static_cast<std::size_t>(-1);
497 uint32_t expr_id = 0;
498 std::apply(
499 [this, &expr_id, i, q, n_dof_loc](auto&&... exprs) {
500 (([this, &expr_id, i, q, n_dof_loc](auto&& expr) {
501 [this, expr_id, i, q, expr,
502 n_dof_loc]<std::size_t... d>(std::index_sequence<d...>) {
503 (
504 [&]<std::size_t D>() {
505 using EvalType =
506 decltype(expr->template evaluate<UNDEF, D>(
507 this->m_fe_values, -1, i, q));
508 if constexpr (!std::is_same_v<EvalType, void>) {
509 this->m_rhs(i * s_n_blocks + D, expr_id) +=
510 expr->template evaluate<UNDEF, D>(
511 this->m_fe_values, -1, i, q) *
512 this->m_fe_values.weights()[q];
513 }
514 }.template operator()<d>(),
515 ...);
516 }(std::make_index_sequence<s_n_blocks>{});
517 expr_id++;
518 }(exprs)),
519 ...);
520 },
521 non_bilinear_fe_expression);
522 }
523 }
524 }
525}
526
527/* updateQptsAndMappingForThisAndNeigh definition */
528template <
529 typename MeshType,
530 typename FeExpressionTuple,
531 typename FeExpressionFaceTuple>
532template <typename FaceSubsimplexMatrix, typename SimplexPtsMatrix>
534 MeshType,
535 FeExpressionTuple,
536 FeExpressionFaceTuple>::
537 updateQptsAndMappingForThisAndNeigh(
538 const FaceSubsimplexMatrix& face_subsimplexes,
539 const SimplexPtsMatrix& simplexification_pts,
540 index_t k) {
541 auto& cell = this->getCell(this->m_cell_id);
542 const std::size_t n_q_nodes_subsimplex = m_qrule_bndry_d.nodes.size();
543 // build the transformation from the reference (sub)simplex to subsimplex
544 const Eigen::Vector<int32_t, dim> subsimplex = face_subsimplexes.col(k);
545 Eigen::Vector<double, dim> translation =
546 simplexification_pts.col(subsimplex[0]);
547 Eigen::Matrix<double, dim, dim - 1> jacobian;
548 for (std::size_t d = 0; d < dim - 1; ++d) {
549 jacobian.col(d) = simplexification_pts.col(subsimplex[d + 1]) - translation;
550 }
551 // map the q nodes to the simplex
552 for (std::size_t q = 0; q < n_q_nodes_subsimplex; ++q) {
553 m_qrule_bndry_d.nodes[q].weight = m_qrule_bndry_d_ref->nodes[q].weight;
554 const Eigen::Map<const Eigen::Vector<double, dim - 1>> pt_map_src(
555 m_qrule_bndry_d_ref->nodes[q].point.data());
556 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
557 m_qrule_bndry_d.nodes[q].point.data());
558 pt_map_dst = jacobian * pt_map_src + translation;
559 }
560
561 // update evaluations and mappings
563 // set the normal, needed to use the Nanson rule
564 const auto normal = cell.normals().col(this->m_face_id);
565 this->m_fe_values.setFaceNormal(normal);
566 auto bnd_mapping = *(this->m_mapping);
567 // transformation area of quadrature for sub-simplex
568 // the new area divided by the reference area i.e.
569 // the measure of orthogonal simplex in [0, 1]^(dim - 1)
570 constexpr double std_subsimp = 1. / vmath::factorial(dim - 1);
571 const double subsimplex_jdet = vmath::area(jacobian) / std_subsimp;
572 // nanson rule
573 // ||JF^-TN||, where F jacobian of ref-to-physic, J is |det(F)|,
574 // N unit outer in ref. By exploiting linearity and F is diagonal we have
575 // J/||Fn||, where n is the unit normal in physical space.
576 // Recall that normals are contravariant
577 const double jac_det = this->m_mapping->jacobianDet({});
578 const auto jac = this->m_mapping->getJacobian();
579 const double nanson = jac_det / jac.cwiseProduct(normal).norm();
580 // we just rescale quadrature weights accordingly
581 bnd_mapping.setJacobianDet(nanson * subsimplex_jdet);
582 this->m_fe_values.evalAndApplyMapping(
583 bnd_mapping, this->m_update_flags_face | fe::UpdateWeights);
584 // if we need the neigh we need the bounding box of the neigh cell
585 // to compute the transformation. This is the composition of three
586 // transformations since the cell is in reference configuration:
587 // 1. (sub)simplex-to-reference-subsimplex
588 // 2. bbox-transformation of the neigh
589 // 3. inverse-bbox-tranformation of this cell
590 // For periodic boundaries, a shift in physical coordinates is added
591 // before applying the neighbor's inverse transformation (step 3)
592 if (this->m_need_neigh_values && this->m_cell_neigh_info.have_neigh) {
593 // map reference quadrature nodes to the one in the bounding
594 // box inverse bbox -> neigh bbox
595 const auto& neigh_mapping =
596 *this->m_cell_manager.getMapping(this->m_cell_neigh_info.neigh_cell_id);
597
598 const Eigen::Array<double, dim, 1> compose_jacobian_bbox =
599 neigh_mapping.getJacobianInv().array() *
600 this->m_mapping->getJacobian().array();
601
602 // Composed translation: current->physical->neighbor with periodic shift
603 const Eigen::Array<double, dim, 1> compose_translation_bbox =
604 neigh_mapping.getJacobianInv().array() *
605 (this->m_mapping->getTranslation().array() +
606 this->m_cell_manager.getPeriodicShift(
607 this->m_cell_neigh_info.neigh_cell_id, this->m_cell_id)) +
608 neigh_mapping.getTranslationInv().array();
609
610 // map the q nodes to the simplex
611 jacobian.array().colwise() *= compose_jacobian_bbox;
612 translation =
613 compose_jacobian_bbox * translation.array() + compose_translation_bbox;
614 for (std::size_t q = 0; q < n_q_nodes_subsimplex; ++q) {
615 m_qrule_bndry_d.nodes[q].weight = m_qrule_bndry_d_ref->nodes[q].weight;
616 const Eigen::Map<const Eigen::Vector<double, dim - 1>> pt_map_src(
617 m_qrule_bndry_d_ref->nodes[q].point.data());
618 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
619 m_qrule_bndry_d.nodes[q].point.data());
620 pt_map_dst = jacobian * pt_map_src + translation;
621 }
622 // compute updated evaluation for the neigh
623 evalReferenceFrame(this->m_fe_values.neighValues(), m_qrule_bndry_d);
624 this->m_fe_values.neighValues().evalAndApplyMapping(
625 neigh_mapping, this->m_update_flags_face);
626 }
627}
628
629/* integrateSurfaceSubcell definition */
630template <
631 typename MeshType,
632 typename FeExpressionTuple,
633 typename FeExpressionFaceTuple>
635 MeshType,
636 FeExpressionTuple,
637 FeExpressionFaceTuple>::integrateSurfaceSubcell() {
638 const uint32_t n_dof_loc = this->m_fe_space.getBasis().size();
639 const std::size_t n_q_nodes_subsimplex = m_qrule_bndry_d.nodes.size();
640 // TODO: evaluate if the std::apply is better as the most
641 // inner loop or in the most outer loop
642 auto [bilinear_fe_expression, non_bilinear_fe_expression] =
644 if constexpr (s_n_face_expr_bilinear) {
646 bilinear_fe_expression, [this, n_q_nodes_subsimplex, n_dof_loc](
647 const auto& expr, Eigen::Index expr_id) {
648 Eigen::Map<Eigen::MatrixXd> curr_integral =
649 this->getCurrFaceIntegral(expr, expr_id);
650 if (!curr_integral.size()) {
651 return;
652 }
653
654 [&]<std::size_t... d>(std::index_sequence<d...>) {
655 (
656 [&]() {
657 for (uint32_t q = 0; q < n_q_nodes_subsimplex; ++q) {
658 const double Jdx = this->m_fe_values.weights()[q];
659 for (uint32_t i = 0; i < n_dof_loc; ++i) {
660 for (uint32_t j = 0; j < n_dof_loc; ++j) {
661 constexpr auto I = d % s_n_blocks;
662 constexpr auto J = d / s_n_blocks;
663 using EvalType = decltype(expr->template evaluate<J, I>(
664 this->m_fe_values, j, i, q));
665 if constexpr (!std::is_same_v<EvalType, void>) {
666 curr_integral(
667 i * s_n_blocks + I, j * s_n_blocks + J) +=
668 expr->template evaluate<J, I>(
669 this->m_fe_values, j, i, q) *
670 Jdx;
671 }
672 }
673 }
674 }
675 }(),
676 ...);
677 }(std::make_index_sequence<s_n_blocks * s_n_blocks>{});
678 });
679 }
680 if constexpr (s_n_face_expr_linear) {
681 constexpr auto UNDEF = static_cast<std::size_t>(-1);
683 non_bilinear_fe_expression,
684 [this, n_q_nodes_subsimplex, n_dof_loc](
685 const auto& expr, Eigen::Index expr_id) {
686 // getCurrFaceRhs handles: have_neigh guard, precompute, and routing
687 // to m_face_rhs_neigh (neigh test fn) or m_face_rhs (local test fn).
688 // For non-neigh exprs the returned map is a view into the same column
689 // of m_face_rhs as the old direct write, so behavior is unchanged.
690 Eigen::Map<Eigen::VectorXd> curr_rhs =
691 this->getCurrFaceRhs(expr, expr_id);
692 if (!curr_rhs.size()) {
693 return;
694 }
695
696 [&]<std::size_t... d>(std::index_sequence<d...>) {
697 (
698 [&]<std::size_t D>() {
699 using EvalType = decltype(expr->template evaluate<UNDEF, D>(
700 this->m_fe_values, -1, 0, 0));
701 if constexpr (!std::is_same_v<EvalType, void>) {
702 for (uint32_t q = 0; q < n_q_nodes_subsimplex; ++q) {
703 const double dx = this->m_fe_values.weights()[q];
704 for (uint32_t i = 0; i < n_dof_loc; ++i) {
705 curr_rhs(i * s_n_blocks + D) +=
706 expr->template evaluate<UNDEF, D>(
707 this->m_fe_values, -1, i, q) *
708 dx;
709 }
710 }
711 }
712 }.template operator()<d>(),
713 ...);
714 }(std::make_index_sequence<s_n_blocks>{});
715 });
716 }
717}
718
734template <typename MeshType, typename FeSpaceType, typename ExprType>
736 public:
737 using index_t = typename MeshType::index_t;
738 static constexpr std::size_t Dim = MeshType::self_dim;
739
749 const MeshType& mesh,
751 const ExprType& expression,
752 std::optional<uint32_t> quadrature_order = std::nullopt)
753 : m_mesh(mesh),
754 m_cell_manager(cell_manager),
755 m_expression(expression),
756 m_quadrature_order(quadrature_order.value_or(
757 static_cast<uint32_t>(1 + expression.computeOrder()))),
758 m_qrule_ref(qrule::QuadratureLibrary<Dim, double>::getRule(
759 qrule::QuadratureElemType::Simplex, m_quadrature_order)) {}
760
765 double integrate() {
766 double integral = 0.0;
767 const index_t n_cells = [&] {
768 if constexpr (MeshType::parallel()) {
769 return m_mesh.nLocEntity(Dim);
770 } else {
771 return m_mesh.nEntity(Dim);
772 }
773 }();
774
775 // Loop over all cells
776 for (index_t cell_id = 0; cell_id < n_cells; ++cell_id) {
777 integral += integrateCell(cell_id);
778 }
779
780 return integral;
781 }
782
788 double integrateCell(index_t cell_id) {
789 double cell_integral = 0.0;
790
791 const auto& cell = m_cell_manager.getCell(cell_id);
792
793 // Simplexify cell (polygons -> simplices)
794 geometry::Simplexify<Dim> simplexification(
795 cell, false); // no boundary needed
796 const auto& simplexes = simplexification.getSimplexesList();
797 const auto& vertexes = simplexification.getPointList();
798
799 // Get mapping and FE space for this cell
800 const auto& mapping = *m_cell_manager.getMapping(cell_id);
801 const auto& fe_space = m_cell_manager.getFeSpace();
802
803 // Integrate over each simplex
804 for (int32_t k = 0; k < simplexification.getNumSimplexes(); ++k) {
805 cell_integral += integrateSimplexDirect(
806 cell_id, k, simplexes, vertexes, mapping, fe_space);
807 }
808
809 return cell_integral;
810 }
811
812 private:
813 const MeshType& m_mesh;
815 const ExprType& m_expression;
817 std::shared_ptr<qrule::QuadratureRule<double, Dim>> m_qrule_ref;
818
823 index_t cell_id,
824 int32_t simplex_id,
825 const Eigen::MatrixXi& simplexes,
826 const Eigen::MatrixXd& vertexes,
827 const auto& mapping,
828 const auto& fe_space) {
829 double simplex_integral = 0.0;
830
832
833 mapQPointsToPhysicalSimplex(qrule, simplexes, vertexes, simplex_id);
834
835 fe::FeValues<Dim> fe_values;
836 fe_values.setCellId(cell_id);
837 if constexpr (std::decay_t<decltype(fe_space)>::needBasisScaling()) {
838 fe_values.setBasisScaling(std::sqrt(mapping.jacobianDet({})));
839 }
840 fe_values.evalOnReferenceFrame(fe_space.getBasisFast(), qrule);
841
842 // Compute update flags needed by the expression
843 // This ensures coordinate systems are properly synchronized for all
844 // sub-expressions
845 fe::UpdateFlagsType update_flags = m_expression.computeUpdateFlags();
846 fe_values.evalAndApplyMapping(mapping, update_flags | fe::UpdateWeights);
847
848 // Precompute expression
849 m_expression.precompute(fe_values, m_cell_manager.getCells());
850
851 // Accumulate integral - like integrateVolumeCell but WITHOUT basis loops
852 // For pure scalar integration: template <0,0> for scalar component,
853 // but pass -1 for both trial and test basis indices (no basis functions)
854 const std::size_t n_q_nodes = qrule.nodes.size();
855 for (std::size_t q = 0; q < n_q_nodes; ++q) {
856 const double Jdx = fe_values.weights()[q];
857 // Scalar expression: no trial function (-1), no test function (-1)
858 double expr_value =
859 m_expression.template evaluate<0, 0>(fe_values, -1, -1, q);
860 simplex_integral += expr_value * Jdx;
861 }
862
863 return simplex_integral;
864 }
865
873 const Eigen::MatrixXi& simplexes,
874 const Eigen::MatrixXd& vertexes,
875 int32_t k) {
876 const std::size_t n_q_nodes = qrule.nodes.size();
877
878 // Extract simplex vertex positions
879 const Eigen::Vector<double, Dim> translation =
880 vertexes.col(simplexes.coeff(0, k));
881 Eigen::Matrix<double, Dim, Dim> jacobian;
882 for (std::size_t d = 0; d < Dim; ++d) {
883 jacobian.col(d) = vertexes.col(simplexes.coeff(d + 1, k)) - translation;
884 }
885 const double jac_det = std::abs(vmath::determinant(jacobian));
886
887 // Map quadrature points and scale weights by Jacobian determinant
888 for (std::size_t q = 0; q < n_q_nodes; ++q) {
889 qrule.nodes[q].weight = m_qrule_ref->nodes[q].weight * jac_det;
890 const Eigen::Map<const Eigen::Vector<double, Dim>> pt_map_src(
891 m_qrule_ref->nodes[q].point.data());
892 Eigen::Map<Eigen::Vector<double, Dim>> pt_map_dst(
893 qrule.nodes[q].point.data());
894 pt_map_dst = jacobian * pt_map_src + translation;
895 }
896 }
897};
898
899} // namespace assemble
900} // namespace vulpes
901
902#endif // VULPES_ASSEMBLE_INTEGRATOR_SIMPLEXIFY_HPP_
static void stop(const char *label)
stop(label) when recording is on.
Definition profiler.hpp:90
static void start(const char *label)
start(label) when recording is on.
Definition profiler.hpp:84
Manages geometrical information about the cell and its mapping to the reference element.
Definition cell_manager.hpp:62
uint32_t m_quadrature_order
Definition integrator_simplexify.hpp:816
ScalarExpressionIntegrator(const MeshType &mesh, CellManager< MeshType, FeSpaceType > &cell_manager, const ExprType &expression, std::optional< uint32_t > quadrature_order=std::nullopt)
Construct scalar integrator.
Definition integrator_simplexify.hpp:748
double integrateSimplexDirect(index_t cell_id, int32_t simplex_id, const Eigen::MatrixXi &simplexes, const Eigen::MatrixXd &vertexes, const auto &mapping, const auto &fe_space)
Integrate expression over a single simplex using quadrature.
Definition integrator_simplexify.hpp:822
void mapQPointsToPhysicalSimplex(qrule::QuadratureRule< double, Dim > &qrule, const Eigen::MatrixXi &simplexes, const Eigen::MatrixXd &vertexes, int32_t k)
Map quadrature points from reference simplex to physical simplex Direct copy of logic from WeakFormIn...
Definition integrator_simplexify.hpp:871
std::shared_ptr< qrule::QuadratureRule< double, Dim > > m_qrule_ref
Definition integrator_simplexify.hpp:817
const MeshType & m_mesh
Definition integrator_simplexify.hpp:813
double integrate()
Integrate the scalar expression over the entire domain.
Definition integrator_simplexify.hpp:765
CellManager< MeshType, FeSpaceType > & m_cell_manager
Definition integrator_simplexify.hpp:814
typename MeshType::index_t index_t
Definition integrator_simplexify.hpp:737
const ExprType & m_expression
Definition integrator_simplexify.hpp:815
double integrateCell(index_t cell_id)
Integrate the scalar expression over one local cell.
Definition integrator_simplexify.hpp:788
static constexpr std::size_t Dim
Definition integrator_simplexify.hpp:738
Eigen::Map< Eigen::VectorXd > getCurrFaceRhs(const auto &expr, Eigen::Index expr_id)
Definition integrator_base.hpp:380
fe::UpdateFlagsType m_update_flags
Definition integrator_base.hpp:557
WeakFormIntegratorBase(const MeshType &mesh, CellManager< MeshType, FeType > &cell_manager, const FeExpressionTuple &fe_expression, const std::tuple<> &fe_expression_face)
Definition integrator_base.hpp:126
static constexpr index_t s_n_face_expr_linear
Definition integrator_base.hpp:102
Eigen::Map< Eigen::MatrixXd > getCurrFaceIntegral(const auto &expr, Eigen::Index expr_id)
Definition integrator_base.hpp:345
Eigen::Tensor< double, 3 > m_integrals
Definition integrator_base.hpp:562
std::remove_pointer_t< std::tuple_element_t< 0, FeExpressionTuple > >::FeSpaceType FeType
Definition integrator_base.hpp:73
void preallocate(index_t cell_id)
Definition integrator_base.hpp:239
CellManager< MeshType, FeType > & m_cell_manager
Definition integrator_base.hpp:532
const CellType & getCell(index_t cell_id, bool update_mapping=false)
Definition integrator_base.hpp:222
static constexpr index_t s_n_face_expr_bilinear
Definition integrator_base.hpp:99
fe::UpdateFlagsType m_update_flags_face
Definition integrator_base.hpp:560
Integrator that simplexifies cells and integrates weak forms on them.
Definition integrator_simplexify.hpp:78
WeakFormIntegratorBase< MeshType, FeExpressionTuple, FeExpressionFaceTuple > WeakFormIntegratorBaseType
Base class alias.
Definition integrator_simplexify.hpp:84
void integrateVolumeCell()
Integrate the volume (cell interior) over the currently mapped simplex.
Definition integrator_simplexify.hpp:441
static constexpr index_t s_n_face_expr
Definition integrator_simplexify.hpp:95
void integrateSurfaceSubcell()
Integrate the boundary contribution on the currently mapped face subsimplex.
Definition integrator_simplexify.hpp:637
static constexpr index_t s_n_blocks
Definition integrator_simplexify.hpp:108
void integrate(index_t cell_id) override
Perform integration for a single cell.
Definition integrator_simplexify.hpp:336
bool m_cell_cache_enabled
Definition integrator_simplexify.hpp:191
void setCellCache(bool enable)
Definition integrator_simplexify.hpp:157
WeakFormIntegratorSimplexify(const MeshType &mesh, CellManager< MeshType, FeType > &cell_manager, const FeExpressionTuple &fe_expression, const FeExpressionFaceTuple &fe_expression_face=FeExpressionFaceTuple{})
Construct the simplexifying integrator.
Definition integrator_simplexify.hpp:311
std::vector< CellCache > m_cell_cache
Definition integrator_simplexify.hpp:192
static constexpr index_t s_n_expr
Number of expressions (compile-time constants forwarded from base).
Definition integrator_simplexify.hpp:93
static constexpr index_t s_n_face_expr_bilinear
Definition integrator_simplexify.hpp:101
void mapQuadraturePointsToSimplex(const SimplexMatrixType &simplexes, const VertexMatrixType &vertexes, int32_t k)
Map quadrature nodes from the reference simplex to physical simplex k.
Definition integrator_simplexify.hpp:410
static constexpr index_t s_n_expr_linear
Definition integrator_simplexify.hpp:99
static constexpr index_t s_n_expr_bilinear
Definition integrator_simplexify.hpp:97
void evalReferenceFrame(fe::FeValues< dim > &fe_values, const QRule &qrule)
Definition integrator_simplexify.hpp:170
typename MeshType::index_t index_t
Index type taken from the MeshType.
Definition integrator_simplexify.hpp:81
geometry::Cell< dim, double, index_t > CellType
Cell type alias with compile-time dimension.
Definition integrator_simplexify.hpp:115
std::shared_ptr< qrule::QuadratureRule< double, dim > > m_qrule_d_ref
Shared pointer to the (volume) quadrature rule reference (cached).
Definition integrator_simplexify.hpp:276
void updateQptsAndMappingForThisAndNeigh(const FaceSubsimplexMatrix &face_subsimplexes, const SimplexPtsMatrix &simplexification_pts, index_t k)
Update boundary quadrature points & mappings for the current face subsimplex.
Definition integrator_simplexify.hpp:537
qrule::QuadratureRule< double, dim > m_qrule_d
Quadrature rule object for volume integrals (mutable, per-simplex).
Definition integrator_simplexify.hpp:283
static constexpr index_t s_n_face_expr_linear
Definition integrator_simplexify.hpp:103
qrule::QuadratureRule< double, dim > m_qrule_bndry_d
Quadrature rule object used for face/sub-simplex integration.
Definition integrator_simplexify.hpp:300
static constexpr std::size_t dim
Spatial dimension of the mesh (compile-time).
Definition integrator_simplexify.hpp:90
std::decay_t< decltype(FeType::getMapping(CellType{}))> MappingType
Mapping type deduced from the FE type's getMapping.
Definition integrator_simplexify.hpp:118
std::size_t m_table_slot
Definition integrator_simplexify.hpp:194
WeakFormIntegratorBaseType::FeType FeType
Finite element type alias (from base).
Definition integrator_simplexify.hpp:112
std::shared_ptr< qrule::QuadratureRule< double, dim - 1 > > m_qrule_bndry_d_ref
Shared pointer to the boundary quadrature rule (reference).
Definition integrator_simplexify.hpp:291
CellCache * m_curr_cache
Definition integrator_simplexify.hpp:193
This class works as a cache table for computing efficiently the value of the basis function and its d...
Definition fe_values.hpp:94
void setCellId(index_t cell_id)
Definition fe_values.hpp:144
constexpr const auto & weights() const
Definition fe_values.hpp:105
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
void saveReferenceFrame(ReferenceTables &tables) const
Copies the reference-frame tables of the last evalOnReferenceFrame.
Definition fe_values.hpp:207
void loadReferenceFrame(const ReferenceTables &tables)
Definition fe_values.hpp:217
void setBasisScaling(real_t scaling)
Definition fe_values.hpp:150
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
A templated class that simplifies a geometric cell into simplices (triangles or tetrahedra).
Definition simplexify.hpp:411
const PointsViewType & getPointList() const
Gets the list of points in the simplexification.
Definition simplexify.hpp:514
const SimplexesViewType & getSimplexesList() const
Gets the list of simplices in the simplexification.
Definition simplexify.hpp:528
index_t getNumSimplexes() const
Gets the number of simplices in the simplexification.
Definition simplexify.hpp:495
static QRuleRef getRule(QuadratureElemType elem_type, std::size_t order)
Retrieves a quadrature rule for a given element type and order.
Definition qlibrary.hpp:127
constexpr auto split_expression_tuple(std::tuple< Ts... > t)
Definition integrator_traits.hpp:145
Definition cell_manager.hpp:39
std::underlying_type_t< UpdateFlags > UpdateFlagsType
Definition fe_values.hpp:65
@ UpdateWeights
Definition fe_values.hpp:57
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition gauss.cpp:39
@ Simplex
Quadrature over a simplex (e.g., triangles, tetrahedra).
Definition qlibrary.hpp:43
T area(const Eigen::Matrix< T, Dim, Dim - 1, Options > &mat)
Definition vmath.hpp:439
constexpr T factorial(T n)
Computes the factorial of a pseudo-arithmetic number iteratively.
Definition vmath.hpp:348
T determinant(const Eigen::Matrix< T, Dim, Dim, Options > &mat)
Computes the determinant of a 2x2 or 3x3 matrix.
Definition vmath.hpp:428
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
#define PROFILE(func)
Time a single execution of func under the label #func using Profiler::start()/stop().
Definition profiler.hpp:44
Definition integrator_simplexify.hpp:163
std::unique_ptr< geometry::Simplexify< dim > > simplexification
Definition integrator_simplexify.hpp:164
std::vector< typename fe::FeValues< dim >::ReferenceTables > tables
Definition integrator_simplexify.hpp:165
Represents a quadrature rule consisting of a set of points and associated weights.
Definition qrule.hpp:69