GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/dg/dg_integration.cpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 247 253 97.6%
Functions: 24 24 100.0%
Branches: 209 340 61.5%

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 #include "oiseau/dg/dg_integration.hpp"
8
9 #include <algorithm>
10 #include <array>
11 #include <cstddef>
12 #include <cstdlib>
13 #include <limits>
14 #include <map>
15 #include <memory>
16 #include <span>
17 #include <stdexcept>
18 #include <tuple>
19 #include <utility>
20 #include <vector>
21 #include <xtensor-blas/xlinalg.hpp>
22 #include <xtensor/containers/xarray.hpp>
23 #include <xtensor/generators/xbuilder.hpp>
24 #include <xtensor/misc/xmanipulation.hpp>
25 #include <xtensor/views/xslice.hpp>
26 #include <xtensor/views/xview.hpp>
27
28 #include "oiseau/dg/detail/cell_mapping.hpp"
29 #include "oiseau/dg/detail/integration.hpp"
30 #include "oiseau/dg/dg_connectivity.hpp"
31 #include "oiseau/dg/dg_geometry.hpp"
32 #include "oiseau/dg/dg_space.hpp"
33 #include "oiseau/dg/nodal/ref_element.hpp"
34 #include "oiseau/dg/ref_element_type.hpp"
35 #include "oiseau/mesh/cell.hpp"
36 #include "oiseau/mesh/mesh.hpp"
37 #include "oiseau/mesh/topology.hpp"
38 #include "oiseau/utils/integration.hpp"
39
40 namespace oiseau::dg {
41 namespace detail {
42
43 using Matrix = DGIntegration::Matrix;
44 using Point = std::array<double, 3>;
45
46 constexpr FacePermutation identity_permutation{0, 1, 2, 3};
47 constexpr std::array canonical_face_kinds{mesh::CellKind::Point, mesh::CellKind::Interval,
48 mesh::CellKind::Triangle, mesh::CellKind::Quadrilateral};
49 constexpr std::array canonical_face_measures{1.0, 2.0, 2.0, 4.0};
50
51 struct ReferenceOperators {
52 std::shared_ptr<nodal::RefElement> element;
53 std::shared_ptr<const DGIntegration::ReferenceRule> volume;
54 Matrix inverse_vandermonde;
55 std::vector<Point> vertices;
56 std::vector<std::vector<int>> faces;
57 };
58
59 41 ReferenceOperators make_reference_operators(mesh::CellKind kind, unsigned order) {
60
2/4
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
41 auto element = nodal::get_ref_element(ref_type_for(kind), order);
61
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 auto inverse_vandermonde = element->inv_v();
62
63
1/2
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
41 auto volume = std::make_shared<DGIntegration::ReferenceRule>();
64
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 volume->mass = element->mass();
65
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 volume->inverse_mass = element->inv_mass();
66 41 const auto& derivatives = element->d();
67 150 const auto add_operators = [&](const auto& values, Matrix& derivative, Matrix& stiffness,
68 Matrix& weak_derivative) {
69 150 derivative = values;
70
1/2
✓ Branch 3 taken 75 times.
✗ Branch 4 not taken.
150 stiffness = xt::linalg::dot(xt::transpose(derivative), volume->mass);
71
1/2
✓ Branch 2 taken 75 times.
✗ Branch 3 not taken.
150 weak_derivative = xt::linalg::dot(volume->inverse_mass, stiffness);
72 191 };
73
2/2
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 30 times.
41 if (derivatives.dimension() == 2) {
74
1/2
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 add_operators(derivatives, volume->derivative_r, volume->stiffness_r,
75 11 volume->weak_derivative_r);
76 } else {
77
2/4
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
30 add_operators(xt::view(derivatives, xt::all(), xt::all(), 0), volume->derivative_r,
78 30 volume->stiffness_r, volume->weak_derivative_r);
79
2/4
✓ Branch 3 taken 30 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
30 add_operators(xt::view(derivatives, xt::all(), xt::all(), 1), volume->derivative_s,
80 30 volume->stiffness_s, volume->weak_derivative_s);
81
2/2
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 26 times.
30 if (derivatives.shape()[2] == 3) {
82
2/4
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
4 add_operators(xt::view(derivatives, xt::all(), xt::all(), 2), volume->derivative_t,
83 4 volume->stiffness_t, volume->weak_derivative_t);
84 }
85 }
86 41 return {.element = std::move(element),
87 41 .volume = std::move(volume),
88 41 .inverse_vandermonde = std::move(inverse_vandermonde),
89 .vertices = {},
90 82 .faces = {}};
91
3/6
✗ Branch 3 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 41 times.
✓ Branch 9 taken 41 times.
✗ Branch 10 not taken.
205 }
92
93 41 std::vector<Point> reference_vertex_coordinates(mesh::CellType type,
94 const std::vector<std::size_t>& mesh_vertices) {
95
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 const auto vertices = detail::reference_ordered_vertices(type, mesh_vertices);
96
2/4
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 41 times.
✗ Branch 6 not taken.
41 const auto reference = nodal::get_ref_element(ref_type_for(type->kind()), 1);
97 41 const auto& nodes = reference->r();
98
1/2
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
41 const auto dimension = static_cast<std::size_t>(type->dimension());
99
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 std::vector<Point> output(mesh_vertices.size());
100
2/2
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 41 times.
167 for (std::size_t reference_vertex = 0; reference_vertex < vertices.size(); ++reference_vertex) {
101
1/2
✓ Branch 2 taken 126 times.
✗ Branch 3 not taken.
126 const auto position = std::ranges::find(mesh_vertices, vertices[reference_vertex]);
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
252 if (position == mesh_vertices.end()) {
103 ✗ throw std::logic_error("Reference vertex is missing from mesh connectivity");
104 }
105 126 const auto mesh_vertex = static_cast<std::size_t>(position - mesh_vertices.begin());
106
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 126 times.
372 for (std::size_t d = 0; d < dimension; ++d) {
107 246 output[mesh_vertex][d] =
108
4/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 224 times.
✓ Branch 3 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 224 times.
✗ Branch 7 not taken.
246 dimension == 1 ? nodes(reference_vertex) : nodes(reference_vertex, d);
109 }
110 }
111 41 return output;
112 41 }
113
114 43 DGIntegration::QuadratureRule make_face_quadrature(mesh::CellKind face_kind, unsigned order) {
115 using mesh::CellKind;
116
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 31 times.
43 if (face_kind == CellKind::Point) {
117
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
48 return {.nodes = Matrix::from_shape({1, 0}), .weights = Matrix{1.0}};
118 }
119
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 30 times.
31 if (order > (static_cast<unsigned>(std::numeric_limits<int>::max()) - 2U) / 2U) {
120
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("Polynomial order is too large for DG face quadrature");
121 }
122 30 const auto degree = static_cast<int>(2U * order + 1U);
123
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 25 times.
30 if (face_kind == CellKind::Triangle) {
124
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 auto [nodes, weights] = utils::integration::triangle_cubature(degree);
125 5 return {.nodes = std::move(nodes), .weights = std::move(weights)};
126
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
10 }
127
128
1/2
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
25 auto [nodes, weights] = utils::integration::quadrature(degree);
129
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 2 times.
25 if (face_kind == CellKind::Interval) {
130 23 return {.nodes = std::move(nodes), .weights = std::move(weights)};
131 }
132
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (face_kind != CellKind::Quadrilateral) {
133
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::logic_error("DGIntegration does not support this face shape");
134 }
135
136 1 const auto count = nodes.size();
137
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 Matrix square_nodes = Matrix::from_shape({count * count, std::size_t{2}});
138
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 Matrix square_weights = Matrix::from_shape({count * count});
139
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (std::size_t i = 0; i < count; ++i) {
140
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (std::size_t j = 0; j < count; ++j) {
141 9 const auto q = i * count + j;
142
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 square_nodes(q, 0) = nodes(i);
143
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 square_nodes(q, 1) = nodes(j);
144
3/6
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
9 square_weights(q) = weights(i) * weights(j);
145 }
146 }
147 1 return {.nodes = std::move(square_nodes), .weights = std::move(square_weights)};
148
8/18
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
✓ Branch 11 taken 5 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 23 times.
✗ Branch 17 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✗ Branch 26 not taken.
✓ Branch 27 taken 24 times.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 34 not taken.
✓ Branch 35 taken 1 times.
149 }
149
150 513 std::array<double, 4> corner_weights(std::size_t corners, const Matrix& nodes, std::size_t q) {
151
2/2
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 106 times.
513 if (corners == 2) {
152 407 const auto u = 0.5 * (1.0 + nodes(q));
153 407 return {1.0 - u, u, 0.0, 0.0};
154 }
155 106 const auto u = 0.5 * (1.0 + nodes(q, 0));
156 106 const auto v = 0.5 * (1.0 + nodes(q, 1));
157
1/2
✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
106 if (corners == 3) return {1.0 - u - v, u, v, 0.0};
158 ✗ return {(1.0 - u) * (1.0 - v), u * (1.0 - v), u * v, (1.0 - u) * v};
159 }
160
161 138 Matrix face_points(std::span<const Point> cell_vertices, std::span<const int> face_vertices,
162 std::size_t dimension, const FacePermutation& permutation,
163 const DGIntegration::QuadratureRule& quadrature) {
164
3/4
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 116 times.
✓ Branch 7 taken 22 times.
✗ Branch 8 not taken.
138 if (face_vertices.size() == 1) return Matrix{cell_vertices[face_vertices.front()][0]};
165
1/2
✓ Branch 3 taken 116 times.
✗ Branch 4 not taken.
116 Matrix points = xt::zeros<double>({quadrature.weights.size(), dimension});
166
2/2
✓ Branch 1 taken 512 times.
✓ Branch 2 taken 116 times.
628 for (std::size_t q = 0; q < quadrature.weights.size(); ++q) {
167
1/2
✓ Branch 2 taken 512 times.
✗ Branch 3 not taken.
512 const auto weights = corner_weights(face_vertices.size(), quadrature.nodes, q);
168
2/2
✓ Branch 1 taken 1130 times.
✓ Branch 2 taken 512 times.
1642 for (std::size_t corner = 0; corner < face_vertices.size(); ++corner) {
169 1130 const auto& vertex = cell_vertices[face_vertices[permutation[corner]]];
170
2/2
✓ Branch 0 taken 2578 times.
✓ Branch 1 taken 1130 times.
3708 for (std::size_t d = 0; d < dimension; ++d) {
171
1/2
✓ Branch 3 taken 2578 times.
✗ Branch 4 not taken.
2578 points(q, d) += weights[corner] * vertex[d];
172 }
173 }
174 }
175 116 return points;
176 116 }
177
178 486 FacePermutation face_permutation(std::span<const std::size_t> neighbor_positions,
179 std::size_t corners) {
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 486 times.
486 if (neighbor_positions.size() != corners) {
181 ✗ throw std::logic_error("DGIntegration face permutation has the wrong size");
182 }
183
184 486 FacePermutation permutation = identity_permutation;
185 486 std::array<bool, 4> seen{};
186
2/2
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 486 times.
1020 for (std::size_t i = 0; i < corners; ++i) {
187 534 const auto position = neighbor_positions[i];
188
3/6
✓ Branch 0 taken 534 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 534 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 534 times.
534 if (position >= corners || seen[position]) {
189 ✗ throw std::logic_error("DGIntegration face permutation is invalid");
190 }
191 534 permutation[i] = position;
192 534 seen[position] = true;
193 }
194 972 return permutation;
195 }
196
197 136 Matrix interpolation_on_points(const ReferenceOperators& operators, const Matrix& points) {
198
1/2
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
136 const auto vandermonde = operators.element->vandermonde(points);
199
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
272 return xt::linalg::dot(vandermonde, operators.inverse_vandermonde);
200 136 }
201
202 15 Matrix physical_stiffness(const DGIntegration::CellRule& cell, std::size_t d) {
203
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
15 if (!cell.reference) return {};
204 15 const auto& reference = *cell.reference;
205 15 const auto has_y = reference.stiffness_s.dimension() == 2;
206 15 const auto has_z = reference.stiffness_t.dimension() == 2;
207
9/10
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
15 if ((d == 1 && !has_y) || (d == 2 && !has_z)) return {};
208 13 const auto& inverse_jacobian = cell.inverse_jacobian;
209
1/2
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
13 Matrix stiffness = inverse_jacobian[d] * reference.stiffness_r;
210
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 5 taken 13 times.
✗ Branch 6 not taken.
13 if (has_y) stiffness += inverse_jacobian[3 + d] * reference.stiffness_s;
211
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
13 if (has_z) stiffness += inverse_jacobian[6 + d] * reference.stiffness_t;
212
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 stiffness *= cell.volume_jacobian;
213 13 return stiffness;
214 13 }
215
216 } // namespace detail
217
218 using namespace detail;
219
220 namespace {
221
222 23 std::vector<ReferenceOperators> make_group_operators(const mesh::Topology& topology,
223 std::span<const DGSpace::ElementGroup> groups,
224 int face_dimension,
225 std::span<DGIntegration::CellRule> cells) {
226
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto cell_types = topology.cell_types();
227
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto connectivity = topology.conn();
228 23 std::vector<ReferenceOperators> reference_operators;
229
1/2
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 reference_operators.reserve(groups.size());
230
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 23 times.
64 for (std::size_t group_index = 0; group_index < groups.size(); ++group_index) {
231 41 const auto& group = groups[group_index];
232 auto& reference =
233
2/4
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 41 times.
✗ Branch 5 not taken.
41 reference_operators.emplace_back(make_reference_operators(group.kind, group.order));
234 41 const auto first_cell = group.cell_indices.front();
235 reference.vertices =
236
1/2
✓ Branch 3 taken 41 times.
✗ Branch 4 not taken.
41 reference_vertex_coordinates(cell_types[first_cell], connectivity[first_cell]);
237
1/2
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
41 reference.faces = cell_types[first_cell]->get_entity_vertices(face_dimension);
238
2/2
✓ Branch 1 taken 266 times.
✓ Branch 2 taken 41 times.
307 for (std::size_t row = 0; row < group.cell_indices.size(); ++row) {
239 266 auto& cell_rule = cells[group.cell_indices[row]];
240 266 cell_rule.group_index = group_index;
241 266 cell_rule.group_cell_index = row;
242 }
243 }
244 23 return reference_operators;
245 ✗ }
246
247 304 void set_weak_derivative(std::vector<double>& derivative, const DGIntegration::CellRule& cell,
248 std::size_t dimension, std::size_t d) {
249 304 const auto nodes = cell.node_count();
250 304 const auto& reference = *cell.reference;
251 304 const auto& inverse_jacobian = cell.inverse_jacobian;
252 304 const auto has_y = dimension >= 2;
253 304 const auto has_z = dimension == 3;
254 304 derivative.resize(nodes * nodes);
255
2/2
✓ Branch 0 taken 1468 times.
✓ Branch 1 taken 304 times.
1772 for (std::size_t test = 0; test < nodes; ++test) {
256
2/2
✓ Branch 0 taken 11264 times.
✓ Branch 1 taken 1468 times.
12732 for (std::size_t trial = 0; trial < nodes; ++trial) {
257 11264 auto entry = inverse_jacobian[d] * reference.weak_derivative_r(test, trial);
258
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 2048 times.
11264 if (has_y) {
259 9216 entry += inverse_jacobian[3 + d] * reference.weak_derivative_s(test, trial);
260 }
261
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 10568 times.
11264 if (has_z) {
262 696 entry += inverse_jacobian[6 + d] * reference.weak_derivative_t(test, trial);
263 }
264 11264 derivative[test * nodes + trial] = entry;
265 }
266 }
267 304 }
268
269 266 void set_volume_rule(DGIntegration::CellRule& cell,
270 const std::shared_ptr<const DGIntegration::ReferenceRule>& reference,
271 const DGGeometry::CellGeometry& geometry) {
272 266 cell.reference = reference;
273 266 cell.volume_jacobian = std::abs(geometry.determinant);
274 266 cell.inverse_jacobian = geometry.inverse_jacobian;
275
276 266 const auto dimension = geometry.dimension;
277 266 set_weak_derivative(cell.weak_derivative_x, cell, dimension, 0);
278
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 232 times.
266 if (dimension >= 2) set_weak_derivative(cell.weak_derivative_y, cell, dimension, 1);
279
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 262 times.
266 if (dimension == 3) set_weak_derivative(cell.weak_derivative_z, cell, dimension, 2);
280 266 }
281
282 2 FacePermutation inverse_face_permutation(const FacePermutation& permutation, std::size_t corners) {
283 2 auto inverse = identity_permutation;
284
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (std::size_t corner = 0; corner < corners; ++corner) {
285 6 inverse[permutation[corner]] = corner;
286 }
287 2 return inverse;
288 }
289
290 class FaceRuleBuilder {
291 using QuadratureRule = DGIntegration::QuadratureRule;
292 using ReferenceFaceRule = DGIntegration::ReferenceFaceRule;
293 using FaceKey = std::tuple<std::size_t, std::size_t, unsigned, FacePermutation>;
294
295 public:
296 23 FaceRuleBuilder(std::span<const DGSpace::ElementGroup> groups,
297 std::span<const ReferenceOperators> references, std::size_t dimension)
298 23 : m_groups(groups), m_references(references), m_dimension(dimension) {}
299
300 266 void add_cell_faces(std::vector<DGIntegration::CellRule>& cells, std::size_t cell_index,
301 std::span<const DGConnectivity::FaceConnection> face_connections,
302 const DGGeometry::CellGeometry& geometry) {
303 266 auto& cell_rule = cells[cell_index];
304 266 const auto group_index = cell_rule.group_index;
305 266 const auto& local_faces = m_references[group_index].faces;
306 266 cell_rule.faces.reserve(local_faces.size());
307
2/2
✓ Branch 1 taken 580 times.
✓ Branch 2 taken 266 times.
846 for (std::size_t face_index = 0; face_index < local_faces.size(); ++face_index) {
308 580 const auto& connection = face_connections[face_index];
309
1/2
✓ Branch 1 taken 580 times.
✗ Branch 2 not taken.
580 auto& face_rule = cell_rule.faces.emplace_back();
310 580 const auto& face_geometry = geometry.faces[face_index];
311 580 face_rule.outward_normal = face_geometry.outward_normal;
312 580 face_rule.neighbor_cell = connection.neighbor_cell;
313
314 580 const auto corners = local_faces[face_index].size();
315 580 auto neighbor_group = group_index;
316 580 auto quadrature_order = m_groups[group_index].order;
317 580 auto minus_permutation = identity_permutation;
318 580 auto plus_permutation = identity_permutation;
319
2/2
✓ Branch 1 taken 486 times.
✓ Branch 2 taken 94 times.
580 if (!connection.is_boundary()) {
320
1/2
✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
486 const auto& neighbor = cells.at(connection.neighbor_cell);
321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 486 times.
486 if (!neighbor.has_group()) {
322 ✗ throw std::invalid_argument("DGIntegration neighbor cell is absent from DGSpace");
323 }
324 486 neighbor_group = neighbor.group_index;
325 486 quadrature_order = std::max(quadrature_order, m_groups[neighbor_group].order);
326
1/2
✓ Branch 2 taken 486 times.
✗ Branch 3 not taken.
486 const auto permutation = face_permutation(connection.vertex_permutation, corners);
327 // Both directions use one 3D face orientation, even for nonsymmetric cubature.
328
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 482 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
486 if (geometry.dimension == 3 && connection.neighbor_cell < cell_index) {
329 2 minus_permutation = inverse_face_permutation(permutation, corners);
330 } else {
331 484 plus_permutation = permutation;
332 }
333 }
334 face_rule.minus =
335
1/2
✓ Branch 1 taken 580 times.
✗ Branch 2 not taken.
580 face_reference(group_index, face_index, quadrature_order, minus_permutation);
336 580 face_rule.weight_scale = face_geometry.measure / canonical_face_measures[corners - 1];
337 580 face_rule.lift_scale = face_rule.weight_scale / cell_rule.volume_jacobian;
338
2/2
✓ Branch 1 taken 486 times.
✓ Branch 2 taken 94 times.
580 if (!connection.is_boundary()) {
339
1/2
✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
972 face_rule.plus = face_reference(neighbor_group, connection.neighbor_face, quadrature_order,
340 486 plus_permutation);
341 }
342 }
343 266 }
344
345 private:
346 1066 std::shared_ptr<const ReferenceFaceRule> face_reference(std::size_t group_index,
347 std::size_t face_index, unsigned order,
348 const FacePermutation& permutation) {
349 1066 const auto& reference = m_references[group_index];
350
1/2
✓ Branch 1 taken 1066 times.
✗ Branch 2 not taken.
1066 const auto& vertices = reference.faces.at(face_index);
351
1/2
✓ Branch 2 taken 1066 times.
✗ Branch 3 not taken.
1066 const auto face_kind = canonical_face_kinds.at(vertices.size() - 1);
352
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 906 times.
1066 const auto face_order = face_kind == mesh::CellKind::Point ? 0U : order;
353 1066 const FaceKey key{group_index, face_index, face_order, permutation};
354
3/4
✓ Branch 1 taken 1066 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 930 times.
✓ Branch 6 taken 136 times.
1066 if (const auto found = m_face_rules.find(key); found != m_face_rules.end()) {
355 930 return found->second;
356 }
357
358
1/2
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
136 auto& quadrature = m_quadratures[{face_kind, face_order}];
359
2/2
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 99 times.
136 if (!quadrature) {
360 quadrature =
361
2/4
✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 37 times.
✗ Branch 5 not taken.
37 std::make_shared<const QuadratureRule>(make_face_quadrature(face_kind, face_order));
362 }
363 const auto points =
364
1/2
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
136 face_points(reference.vertices, vertices, m_dimension, permutation, *quadrature);
365
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
136 Matrix trace = interpolation_on_points(reference, points);
366
1/2
✓ Branch 3 taken 136 times.
✗ Branch 4 not taken.
136 Matrix lift = xt::linalg::dot(trace, xt::transpose(reference.volume->inverse_mass));
367
2/2
✓ Branch 2 taken 532 times.
✓ Branch 3 taken 136 times.
668 for (std::size_t q = 0; q < quadrature->weights.size(); ++q) {
368
2/2
✓ Branch 2 taken 5681 times.
✓ Branch 3 taken 532 times.
6213 for (std::size_t node = 0; node < lift.shape()[1]; ++node) {
369
2/4
✓ Branch 2 taken 5681 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 5681 times.
✗ Branch 6 not taken.
5681 lift(q, node) *= quadrature->weights(q);
370 }
371 }
372
2/8
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 136 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
408 auto rule = std::make_shared<const ReferenceFaceRule>(ReferenceFaceRule{
373
1/2
✓ Branch 3 taken 136 times.
✗ Branch 4 not taken.
272 .quadrature = quadrature, .trace = std::move(trace), .lift = std::move(lift)});
374
1/2
✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
136 m_face_rules.emplace(key, rule);
375 136 return rule;
376 136 }
377
378 std::span<const DGSpace::ElementGroup> m_groups;
379 std::span<const ReferenceOperators> m_references;
380 std::size_t m_dimension;
381 std::map<std::pair<mesh::CellKind, unsigned>, std::shared_ptr<const QuadratureRule>>
382 m_quadratures;
383 std::map<FaceKey, std::shared_ptr<const ReferenceFaceRule>> m_face_rules;
384 };
385
386 } // namespace
387
388 26 DGIntegration::DGIntegration(const mesh::Mesh& mesh, const DGSpace& space,
389 26 const DGConnectivity& connectivity, const DGGeometry& geometry) {
390
8/8
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 23 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 23 times.
26 if (&space.mesh() != &mesh || &connectivity.mesh() != &mesh || &geometry.mesh() != &mesh) {
391
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 throw std::invalid_argument("DGIntegration inputs must refer to the same mesh");
392 }
393
394
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto groups = space.element_groups();
395
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto& topology = mesh.topology();
396
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto cell_count = topology.n_cells();
397
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 const auto dimension = topology.dimension();
398
1/2
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
23 m_cells.resize(cell_count);
399
1/2
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 const auto reference_operators = make_group_operators(topology, groups, dimension - 1, m_cells);
400
401 23 FaceRuleBuilder face_rules(groups, reference_operators, static_cast<std::size_t>(dimension));
402
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 23 times.
299 for (std::size_t cell_index = 0; cell_index < cell_count; ++cell_index) {
403 276 auto& cell_rule = m_cells[cell_index];
404
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 266 times.
276 if (!cell_rule.has_group()) continue;
405
406
1/2
✓ Branch 1 taken 266 times.
✗ Branch 2 not taken.
266 const auto& cell_geometry = geometry.cell(cell_index);
407
1/2
✓ Branch 2 taken 266 times.
✗ Branch 3 not taken.
266 set_volume_rule(cell_rule, reference_operators[cell_rule.group_index].volume, cell_geometry);
408
2/4
✓ Branch 1 taken 266 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 266 times.
✗ Branch 5 not taken.
266 face_rules.add_cell_faces(m_cells, cell_index, connectivity.faces(cell_index), cell_geometry);
409 }
410 26 }
411
412 8 DGIntegration::Matrix DGIntegration::CellRule::mass() const {
413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 if (!reference) return {};
414
1/2
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
8 return volume_jacobian * reference->mass;
415 }
416
417 13 DGIntegration::Matrix DGIntegration::CellRule::inverse_mass() const {
418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
13 if (!reference) return {};
419
1/2
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
26 return reference->inverse_mass / volume_jacobian;
420 }
421
422 6 DGIntegration::Matrix DGIntegration::CellRule::stiffness_x() const {
423 6 return physical_stiffness(*this, 0);
424 }
425
426 7 DGIntegration::Matrix DGIntegration::CellRule::stiffness_y() const {
427 7 return physical_stiffness(*this, 1);
428 }
429
430 2 DGIntegration::Matrix DGIntegration::CellRule::stiffness_z() const {
431 2 return physical_stiffness(*this, 2);
432 }
433
434 452691 const DGIntegration::CellRule& DGIntegration::cell(std::size_t mesh_cell_index) const {
435 452691 return m_cells.at(mesh_cell_index);
436 }
437
438 } // namespace oiseau::dg
439