25#ifndef VULPES_ASSEMBLE_INTEGRATOR_SIMPLEXIFY_HPP_
26#define VULPES_ASSEMBLE_INTEGRATOR_SIMPLEXIFY_HPP_
73 typename FeExpressionTuple,
74 typename FeExpressionFaceTuple = std::tuple<>>
78 FeExpressionFaceTuple> {
81 using index_t =
typename MeshType::index_t;
87 FeExpressionFaceTuple>;
90 static inline constexpr std::size_t
dim = MeshType::self_dim;
132 const MeshType& mesh,
134 const FeExpressionTuple& fe_expression,
135 const FeExpressionFaceTuple& fe_expression_face =
136 FeExpressionFaceTuple{});
165 std::vector<typename fe::FeValues<dim>::ReferenceTables>
tables;
169 template <
typename QRule>
176 const auto& basis = this->
m_fe_space.getBasisFast();
178 tables.emplace_back();
181 if (saved.basis == &basis &&
182 static_cast<std::size_t
>(saved.weights.size()) ==
qrule.nodes.size()) {
213 template <
typename SimplexMatrixType,
typename VertexMatrixType>
215 const SimplexMatrixType& simplexes,
216 const VertexMatrixType& vertexes,
254 template <
typename FaceSubsimplexMatrix,
typename SimplexPtsMatrix>
256 const FaceSubsimplexMatrix& face_subsimplexes,
257 const SimplexPtsMatrix& simplexification_pts,
276 std::shared_ptr<qrule::QuadratureRule<double, dim>>
m_qrule_d_ref =
nullptr;
305 typename FeExpressionTuple,
306 typename FeExpressionFaceTuple>
310 FeExpressionFaceTuple>::
311 WeakFormIntegratorSimplexify(
312 const MeshType& mesh,
314 const FeExpressionTuple& fe_expression,
315 const FeExpressionFaceTuple& fe_expression_face)
317 mesh, cell_manager, fe_expression, fe_expression_face) {
331 typename FeExpressionTuple,
332 typename FeExpressionFaceTuple>
343 if (
static_cast<std::size_t
>(cell_id) >=
m_cell_cache.size()) {
350 std::optional<geometry::Simplexify<dim>> local_simplexification;
355 std::make_unique<geometry::Simplexify<dim>>(cell, need_bnd);
357 simplexification_ptr =
m_curr_cache->simplexification.get();
359 simplexification_ptr = &local_simplexification.emplace(cell, need_bnd);
361 const auto& simplexification = *simplexification_ptr;
363 const auto& simplexes = simplexification.getSimplexesList();
364 const auto& vertexes = simplexification.getPointList();
368 for (int32_t k = 0; k < simplexification.getNumSimplexes(); ++k) {
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();
388 for (
index_t k = 0; k < nsubsimplexes; ++k) {
391 face_subsimplexes, simplexification_pts, k);
403 typename FeExpressionTuple,
404 typename FeExpressionFaceTuple>
405template <
typename SimplexMatrixType,
typename VertexMatrixType>
409 FeExpressionFaceTuple>::
410 mapQuadraturePointsToSimplex(
411 const SimplexMatrixType& simplexes,
412 const VertexMatrixType& vertexes,
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;
423 for (std::size_t q = 0; q < n_q_nodes_simplex; ++q) {
425 const Eigen::Map<const Eigen::Vector<double, dim>> pt_map_src(
427 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
429 pt_map_dst = jacobian * pt_map_src + translation;
436 typename FeExpressionTuple,
437 typename FeExpressionFaceTuple>
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();
447 [
this](
auto&&... exprs) {
454 for (uint32_t q = 0; q < n_q_nodes_simplex; ++q) {
456 for (uint32_t i = 0; i < n_dof_loc; ++i) {
461 for (uint32_t j = 0; j < n_dof_loc; ++j) {
462 uint32_t expr_id = 0;
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...>) {
471 [&]<std::size_t D>() {
475 decltype(expr->template evaluate<J, I>(
477 if constexpr (!std::is_same_v<EvalType, void>) {
481 expr->template evaluate<J, I>(
485 }.template operator()<d>(),
487 }(std::make_index_sequence<s_n_blocks * s_n_blocks>{});
492 bilinear_fe_expression);
496 constexpr auto UNDEF =
static_cast<std::size_t
>(-1);
497 uint32_t expr_id = 0;
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...>) {
504 [&]<std::size_t D>() {
506 decltype(expr->template evaluate<UNDEF, D>(
508 if constexpr (!std::is_same_v<EvalType, void>) {
510 expr->template evaluate<UNDEF, D>(
514 }.template operator()<d>(),
516 }(std::make_index_sequence<s_n_blocks>{});
521 non_bilinear_fe_expression);
530 typename FeExpressionTuple,
531 typename FeExpressionFaceTuple>
532template <
typename FaceSubsimplexMatrix,
typename SimplexPtsMatrix>
536 FeExpressionFaceTuple>::
537 updateQptsAndMappingForThisAndNeigh(
538 const FaceSubsimplexMatrix& face_subsimplexes,
539 const SimplexPtsMatrix& simplexification_pts,
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;
552 for (std::size_t q = 0; q < n_q_nodes_subsimplex; ++q) {
554 const Eigen::Map<
const Eigen::Vector<double,
dim - 1>> pt_map_src(
556 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
558 pt_map_dst = jacobian * pt_map_src + translation;
564 const auto normal = cell.normals().col(this->
m_face_id);
571 const double subsimplex_jdet =
vmath::area(jacobian) / std_subsimp;
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();
581 bnd_mapping.setJacobianDet(nanson * subsimplex_jdet);
595 const auto& neigh_mapping =
598 const Eigen::Array<double, dim, 1> compose_jacobian_bbox =
599 neigh_mapping.getJacobianInv().array() *
603 const Eigen::Array<double, dim, 1> compose_translation_bbox =
604 neigh_mapping.getJacobianInv().array() *
605 (this->
m_mapping->getTranslation().array() +
607 this->m_cell_neigh_info.neigh_cell_id, this->m_cell_id)) +
608 neigh_mapping.getTranslationInv().array();
611 jacobian.array().colwise() *= compose_jacobian_bbox;
613 compose_jacobian_bbox * translation.array() + compose_translation_bbox;
614 for (std::size_t q = 0; q < n_q_nodes_subsimplex; ++q) {
616 const Eigen::Map<
const Eigen::Vector<double,
dim - 1>> pt_map_src(
618 Eigen::Map<Eigen::Vector<double, dim>> pt_map_dst(
620 pt_map_dst = jacobian * pt_map_src + translation;
624 this->
m_fe_values.neighValues().evalAndApplyMapping(
632 typename FeExpressionTuple,
633 typename FeExpressionFaceTuple>
638 const uint32_t n_dof_loc = this->
m_fe_space.getBasis().size();
642 auto [bilinear_fe_expression, non_bilinear_fe_expression] =
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 =
650 if (!curr_integral.size()) {
654 [&]<std::size_t... d>(std::index_sequence<d...>) {
657 for (uint32_t q = 0; q < n_q_nodes_subsimplex; ++q) {
659 for (uint32_t i = 0; i < n_dof_loc; ++i) {
660 for (uint32_t j = 0; j < n_dof_loc; ++j) {
663 using EvalType =
decltype(expr->template evaluate<J, I>(
665 if constexpr (!std::is_same_v<EvalType, void>) {
668 expr->template evaluate<J, I>(
677 }(std::make_index_sequence<s_n_blocks * s_n_blocks>{});
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) {
690 Eigen::Map<Eigen::VectorXd> curr_rhs =
692 if (!curr_rhs.size()) {
696 [&]<std::size_t... d>(std::index_sequence<d...>) {
698 [&]<std::size_t D>() {
699 using EvalType =
decltype(expr->template evaluate<UNDEF, D>(
701 if constexpr (!std::is_same_v<EvalType, void>) {
702 for (uint32_t q = 0; q < n_q_nodes_subsimplex; ++q) {
704 for (uint32_t i = 0; i < n_dof_loc; ++i) {
706 expr->template evaluate<UNDEF, D>(
712 }.template operator()<d>(),
714 }(std::make_index_sequence<s_n_blocks>{});
734template <
typename MeshType,
typename FeSpaceType,
typename ExprType>
738 static constexpr std::size_t
Dim = MeshType::self_dim;
749 const MeshType& mesh,
751 const ExprType& expression,
752 std::optional<uint32_t> quadrature_order = std::nullopt)
757 static_cast<uint32_t>(1 + expression.computeOrder()))),
766 double integral = 0.0;
768 if constexpr (MeshType::parallel()) {
776 for (
index_t cell_id = 0; cell_id < n_cells; ++cell_id) {
789 double cell_integral = 0.0;
806 cell_id, k, simplexes, vertexes, mapping, fe_space);
809 return cell_integral;
825 const Eigen::MatrixXi& simplexes,
826 const Eigen::MatrixXd& vertexes,
828 const auto& fe_space) {
829 double simplex_integral = 0.0;
837 if constexpr (std::decay_t<
decltype(fe_space)>::needBasisScaling()) {
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];
859 m_expression.template evaluate<0, 0>(fe_values, -1, -1, q);
860 simplex_integral += expr_value * Jdx;
863 return simplex_integral;
873 const Eigen::MatrixXi& simplexes,
874 const Eigen::MatrixXd& vertexes,
876 const std::size_t n_q_nodes =
qrule.nodes.size();
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;
888 for (std::size_t q = 0; q < n_q_nodes; ++q) {
890 const Eigen::Map<const Eigen::Vector<double, Dim>> pt_map_src(
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;
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
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
@ 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
Represents a quadrature rule consisting of a set of points and associated weights.
Definition qrule.hpp:69