| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (C) 2026 Tiago V. L. Amorim (@tiagovla) | ||
| 2 | // | ||
| 3 | // This file is part of oiseau (https://github.com/tiagovla/oiseau) | ||
| 4 | // | ||
| 5 | // SPDX-License-Identifier: GPL-3.0-or-later | ||
| 6 | |||
| 7 | #pragma once | ||
| 8 | |||
| 9 | #include <array> | ||
| 10 | #include <cstddef> | ||
| 11 | #include <memory> | ||
| 12 | #include <span> | ||
| 13 | #include <vector> | ||
| 14 | #include <xtensor/containers/xarray.hpp> | ||
| 15 | #include <xtensor/core/xtensor_forward.hpp> | ||
| 16 | |||
| 17 | #include "oiseau/dg/dg_connectivity.hpp" | ||
| 18 | #include "oiseau/dg/dg_geometry.hpp" | ||
| 19 | #include "oiseau/dg/dg_space.hpp" | ||
| 20 | #include "oiseau/mesh/mesh.hpp" | ||
| 21 | |||
| 22 | namespace oiseau::dg { | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @brief Precomputed volume and face integration operators for affine 1D, 2D, and 3D DG cells. | ||
| 26 | * @details The rules follow the supplied DGSpace cell groups and can be reused by | ||
| 27 | * different equation systems on the same space. | ||
| 28 | */ | ||
| 29 | class DGIntegration { | ||
| 30 | public: | ||
| 31 | /** @brief Array type for integration operators and quadrature data. */ | ||
| 32 | using Matrix = xt::xarray<double>; | ||
| 33 | |||
| 34 | /** @brief Reference-cell volume operators shared by one cell shape and polynomial order. */ | ||
| 35 | struct ReferenceRule { | ||
| 36 | /** @brief Reference mass matrix. */ | ||
| 37 | Matrix mass; | ||
| 38 | /** @brief Inverse reference mass matrix. */ | ||
| 39 | Matrix inverse_mass; | ||
| 40 | /** @brief Reference weak stiffness matrix in the @c r direction. */ | ||
| 41 | Matrix stiffness_r; | ||
| 42 | /** @brief Reference weak stiffness matrix in the @c s direction. */ | ||
| 43 | Matrix stiffness_s; | ||
| 44 | /** @brief Reference weak stiffness matrix in the @c t direction. */ | ||
| 45 | Matrix stiffness_t; | ||
| 46 | /** @brief Inverse mass times reference stiffness in the @c r direction. */ | ||
| 47 | Matrix weak_derivative_r; | ||
| 48 | /** @brief Inverse mass times reference stiffness in the @c s direction. */ | ||
| 49 | Matrix weak_derivative_s; | ||
| 50 | /** @brief Inverse mass times reference stiffness in the @c t direction. */ | ||
| 51 | Matrix weak_derivative_t; | ||
| 52 | /** @brief Strong nodal derivative matrix in the @c r direction. */ | ||
| 53 | Matrix derivative_r; | ||
| 54 | /** @brief Strong nodal derivative matrix in the @c s direction. */ | ||
| 55 | Matrix derivative_s; | ||
| 56 | /** @brief Strong nodal derivative matrix in the @c t direction. */ | ||
| 57 | Matrix derivative_t; | ||
| 58 | }; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @brief Weights and nodes on the canonical point, line, triangle, or square. | ||
| 62 | * @details A point has a single unit weight and no local coordinates. For other shapes, | ||
| 63 | * the rule integrates degree @f$2p+1@f$ exactly, per coordinate on the square. | ||
| 64 | * Here @f$p@f$ is the higher of the adjacent cell orders on an interior face and | ||
| 65 | * the local cell order on a boundary face. | ||
| 66 | */ | ||
| 67 | struct QuadratureRule { | ||
| 68 | /** @brief Coordinates of the quadrature points on the canonical face. */ | ||
| 69 | Matrix nodes; | ||
| 70 | /** @brief Weights in quadrature point order. */ | ||
| 71 | Matrix weights; | ||
| 72 | }; | ||
| 73 | |||
| 74 | /** @brief Face operators shared by cell shape, order, local face, quadrature, and orientation. */ | ||
| 75 | struct ReferenceFaceRule { | ||
| 76 | /** @brief Canonical face quadrature used by the operators. */ | ||
| 77 | std::shared_ptr<const QuadratureRule> quadrature; | ||
| 78 | /** @brief Trace interpolation matrix with shape [quadrature point, cell node]. */ | ||
| 79 | Matrix trace; | ||
| 80 | /** | ||
| 81 | * @brief Reference lift @f$M^{-1}T^TW@f$, stored as [quadrature point, test node]. | ||
| 82 | * @details @f$W@f$ uses canonical face weights. Physical scaling belongs to FaceRule. | ||
| 83 | */ | ||
| 84 | Matrix lift; | ||
| 85 | }; | ||
| 86 | |||
| 87 | /** @brief Integration data for one physical cell face. */ | ||
| 88 | struct FaceRule { | ||
| 89 | /** @brief Outward unit normal, padded to three coordinates. */ | ||
| 90 | std::array<double, 3> outward_normal{}; | ||
| 91 | /** @brief Mesh index of the neighbor cell, or DGConnectivity::no_neighbor on a boundary. */ | ||
| 92 | std::size_t neighbor_cell = DGConnectivity::no_neighbor; | ||
| 93 | /** @brief Reference face rule for the local cell. */ | ||
| 94 | std::shared_ptr<const ReferenceFaceRule> minus; | ||
| 95 | /** | ||
| 96 | * @brief Reference face rule for the neighbor cell, or @c nullptr on a boundary. | ||
| 97 | * @details On interior faces, both traces evaluate at the same physical points | ||
| 98 | * in the same order. The plus trace uses the neighbor's nodes. | ||
| 99 | */ | ||
| 100 | std::shared_ptr<const ReferenceFaceRule> plus; | ||
| 101 | /** @brief Ratio of physical face measure to canonical face measure. */ | ||
| 102 | double weight_scale = 0.0; | ||
| 103 | /** @brief The face weight scale divided by @f$|\det J|@f$ to scale the reference lift. */ | ||
| 104 | double lift_scale = 0.0; | ||
| 105 | |||
| 106 | /** @return Number of quadrature points on the face. */ | ||
| 107 | 459182 | std::size_t point_count() const { return minus->quadrature->weights.size(); } | |
| 108 | /** | ||
| 109 | * @brief Returns a quadrature weight scaled by the physical face measure ratio. | ||
| 110 | * @param q Quadrature point index. | ||
| 111 | */ | ||
| 112 | 13234 | double physical_weight(std::size_t q) const { | |
| 113 | 13234 | return weight_scale * minus->quadrature->weights(q); | |
| 114 | } | ||
| 115 | /** | ||
| 116 | * @brief Returns an entry of the physical lift for the local cell. | ||
| 117 | * @param q Quadrature point index. | ||
| 118 | * @param node Local test node index. | ||
| 119 | */ | ||
| 120 | 992 | double physical_lift(std::size_t q, std::size_t node) const { | |
| 121 | 992 | return lift_scale * minus->lift(q, node); | |
| 122 | } | ||
| 123 | |||
| 124 | /** @return True if the face has no neighbor cell. */ | ||
| 125 | 917817 | bool is_boundary() const { return neighbor_cell == DGConnectivity::no_neighbor; } | |
| 126 | }; | ||
| 127 | |||
| 128 | /** @brief Volume and face integration data for one mesh cell. */ | ||
| 129 | struct CellRule { | ||
| 130 | /** @brief Index of the DGSpace group, or DGConnectivity::no_neighbor for an inactive cell. */ | ||
| 131 | std::size_t group_index = DGConnectivity::no_neighbor; | ||
| 132 | /** @brief Index of this cell within its DGSpace group. */ | ||
| 133 | std::size_t group_cell_index = 0; | ||
| 134 | /** @brief Shared reference volume operators, or @c nullptr for an inactive cell. */ | ||
| 135 | std::shared_ptr<const ReferenceRule> reference; | ||
| 136 | /** @brief Absolute Jacobian determinant used to scale volume integrals. */ | ||
| 137 | double volume_jacobian = 0.0; | ||
| 138 | /** @brief Padded row-major 3 by 3 inverse Jacobian @f$\partial(r,s,t)/\partial(x,y,z)@f$. */ | ||
| 139 | std::array<double, 9> inverse_jacobian{}; | ||
| 140 | |||
| 141 | /** @return True if the cell belongs to a DGSpace element group. */ | ||
| 142 | 230414 | bool has_group() const { return group_index != DGConnectivity::no_neighbor; } | |
| 143 | /** @return Number of cell nodes, or zero for an inactive cell. */ | ||
| 144 |
1/2✓ Branch 1 taken 15366 times.
✗ Branch 2 not taken.
|
15366 | std::size_t node_count() const { return reference ? reference->mass.shape()[0] : 0; } |
| 145 | /** @return Physical mass matrix, or an empty matrix for an inactive cell. */ | ||
| 146 | Matrix mass() const; | ||
| 147 | /** @return Inverse physical mass matrix, or an empty matrix for an inactive cell. */ | ||
| 148 | Matrix inverse_mass() const; | ||
| 149 | /** | ||
| 150 | * @brief Returns the physical weak stiffness matrix in the x direction. | ||
| 151 | * @details @f$S_x(i,j) = \int (\partial_x\phi_i)\phi_j = (D_x^TM)(i,j)@f$. | ||
| 152 | * Rows are test nodes and columns are trial nodes. The y and z matrices follow | ||
| 153 | * the same convention. | ||
| 154 | * @return The stiffness matrix, or an empty matrix for an inactive cell. | ||
| 155 | */ | ||
| 156 | Matrix stiffness_x() const; | ||
| 157 | /** @return Physical y stiffness matrix, or an empty matrix if y is absent. */ | ||
| 158 | Matrix stiffness_y() const; | ||
| 159 | /** @return Physical z stiffness matrix, or an empty matrix if z is absent. */ | ||
| 160 | Matrix stiffness_z() const; | ||
| 161 | /** @brief Physical weak derivative @f$M^{-1}S_x@f$, stored as [test node, trial node]. */ | ||
| 162 | std::vector<double> weak_derivative_x; | ||
| 163 | /** @brief Physical weak derivative @f$M^{-1}S_y@f$, stored as [test node, trial node]. */ | ||
| 164 | std::vector<double> weak_derivative_y; | ||
| 165 | /** @brief Physical weak derivative @f$M^{-1}S_z@f$, stored as [test node, trial node]. */ | ||
| 166 | std::vector<double> weak_derivative_z; | ||
| 167 | /** @brief Face rules in local face order. */ | ||
| 168 | std::vector<FaceRule> faces; | ||
| 169 | }; | ||
| 170 | |||
| 171 | /** | ||
| 172 | * @brief Builds integration rules for the cells in a DG space. | ||
| 173 | * @param mesh Source mesh. | ||
| 174 | * @param space DG cell groups and polynomial orders. | ||
| 175 | * @param connectivity Face adjacency and orientation for the source mesh. | ||
| 176 | * @param geometry Cell and face geometry for the source mesh. | ||
| 177 | * @throws std::invalid_argument If the inputs refer to different meshes, a neighboring cell | ||
| 178 | * is missing from the space, or a polynomial order is too large for face quadrature. | ||
| 179 | */ | ||
| 180 | DGIntegration(const mesh::Mesh& mesh, const DGSpace& space, const DGConnectivity& connectivity, | ||
| 181 | const DGGeometry& geometry); | ||
| 182 | |||
| 183 | /** | ||
| 184 | * @return Rules in mesh cell order. Cells outside the DGSpace have empty matrices. | ||
| 185 | */ | ||
| 186 | 3106 | std::span<const CellRule> cells() const { return m_cells; } | |
| 187 | /** | ||
| 188 | * @brief Returns the integration rule for a mesh cell. | ||
| 189 | * @param mesh_cell_index Index in the source mesh. | ||
| 190 | * @return The cell rule, with empty matrices for a cell outside the DGSpace. | ||
| 191 | * @throws std::out_of_range If the mesh cell index is invalid. | ||
| 192 | */ | ||
| 193 | const CellRule& cell(std::size_t mesh_cell_index) const; | ||
| 194 | |||
| 195 | private: | ||
| 196 | std::vector<CellRule> m_cells; | ||
| 197 | }; | ||
| 198 | |||
| 199 | } // namespace oiseau::dg | ||
| 200 |