GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/dg/dg_geometry.cpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 231 245 94.3%
Functions: 23 23 100.0%
Branches: 231 406 56.9%

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_geometry.hpp"
8
9 #include <algorithm>
10 #include <array>
11 #include <cmath>
12 #include <cstddef>
13 #include <iterator>
14 #include <limits>
15 #include <numeric>
16 #include <ranges>
17 #include <stdexcept>
18 #include <unordered_map>
19 #include <utility>
20 #include <vector>
21 #include <xtensor-blas/xlinalg.hpp>
22 #include <xtensor/core/xmath.hpp>
23 #include <xtensor/generators/xbuilder.hpp>
24 #include <xtensor/views/xslice.hpp>
25 #include <xtensor/views/xview.hpp>
26
27 #include "oiseau/dg/detail/cell_mapping.hpp"
28 #include "oiseau/dg/detail/geometry.hpp"
29 #include "oiseau/dg/nodal/ref_element.hpp"
30 #include "oiseau/dg/ref_element_type.hpp"
31 #include "oiseau/mesh/cell.hpp"
32 #include "oiseau/mesh/geometry.hpp"
33 #include "oiseau/mesh/mesh.hpp"
34 #include "oiseau/mesh/topology.hpp"
35 #include "xtensor/containers/xadapt.hpp"
36
37 namespace oiseau::dg {
38 namespace {
39
40 using detail::Matrix;
41 using detail::Point;
42
43 403 Matrix edge_matrix(const std::vector<Point>& points, const std::array<std::size_t, 4>& basis,
44 std::size_t dimension) {
45
1/2
✓ Branch 2 taken 403 times.
✗ Branch 3 not taken.
403 Matrix edges = xt::zeros<double>({dimension, dimension});
46
2/2
✓ Branch 0 taken 560 times.
✓ Branch 1 taken 403 times.
963 for (std::size_t column = 0; column < dimension; ++column) {
47 560 const auto edge = points[basis[column + 1]] - points[basis[0]];
48
4/8
✓ Branch 1 taken 560 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 560 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 560 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 560 times.
✗ Branch 12 not taken.
560 xt::view(edges, xt::all(), column) = xt::view(edge, xt::range(0, dimension));
49 560 }
50 403 return edges;
51 ✗ }
52
53 struct VertexBasis {
54 std::array<std::size_t, 4> indices{};
55 double determinant = 0.0;
56 };
57
58 88 void consider_basis_completions(const std::vector<Point>& points, std::size_t dimension,
59 std::size_t first, std::size_t second, VertexBasis& best) {
60
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 73 times.
88 const auto third_start = dimension > 2 ? second + 1 : 1;
61
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 73 times.
88 const auto third_end = dimension > 2 ? points.size() : 2;
62
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 88 times.
168 for (auto third = third_start; third < third_end; ++third) {
63 80 const std::array<std::size_t, 4> indices = {0, first, second, third};
64
2/4
✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
80 const auto determinant = std::abs(xt::linalg::det(edge_matrix(points, indices, dimension)));
65
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 32 times.
80 if (determinant > best.determinant) {
66 48 best = {.indices = indices, .determinant = determinant};
67 }
68 }
69 88 }
70
71 } // namespace
72
73 namespace detail {
74
75 38 double triangle_area(const Point& a, const Point& b, const Point& c) {
76
2/4
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
38 return xt::linalg::norm(xt::linalg::cross(b - a, c - a), 2) / 2.0;
77 }
78
79 557 std::array<double, 9> padded_matrix(const Matrix& matrix, std::size_t dimension) {
80 557 std::array<double, 9> output{};
81
1/2
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
557 auto padded = xt::adapt(output, std::array<std::size_t, 2>{3, 3});
82
4/8
✓ Branch 1 taken 557 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 557 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 557 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 557 times.
✗ Branch 11 not taken.
557 xt::view(padded, xt::range(0, dimension), xt::range(0, dimension)) = matrix;
83 1114 return output;
84 557 }
85
86 620 std::array<double, 3> point_array(const Point& point) {
87 620 std::array<double, 3> output{};
88
2/4
✓ Branch 1 taken 620 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 620 times.
✗ Branch 5 not taken.
620 std::ranges::copy(std::views::take(point, 3), output.begin());
89 620 return output;
90 }
91
92 326 bool is_degenerate(const Matrix& matrix, double determinant) {
93
2/4
✓ Branch 3 taken 326 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 326 times.
✗ Branch 7 not taken.
326 const auto scale = xt::amax(xt::abs(matrix))();
94
6/8
✓ Branch 1 taken 325 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 325 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 325 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 325 times.
326 if (!std::isfinite(determinant) || !std::isfinite(scale) || scale == 0.0) return true;
95 const auto relative_threshold =
96 325 100.0 * std::numeric_limits<double>::epsilon() * std::pow(scale, matrix.shape()[0]);
97 325 return std::abs(determinant) <= relative_threshold;
98 }
99
100 280 double affine_tolerance(const std::vector<Point>& points, std::size_t dimension) {
101 280 double coordinate_scale = 0.0;
102 280 double cell_diameter = 0.0;
103
104
2/2
✓ Branch 2 taken 626 times.
✓ Branch 3 taken 280 times.
1812 for (auto point = points.cbegin(); point != points.cend(); ++point) {
105
2/2
✓ Branch 0 taken 804 times.
✓ Branch 1 taken 626 times.
1430 for (std::size_t coordinate = 0; coordinate < dimension; ++coordinate) {
106
1/2
✓ Branch 1 taken 804 times.
✗ Branch 2 not taken.
804 coordinate_scale = std::max(coordinate_scale, std::abs((*point)(coordinate)));
107 }
108 626 cell_diameter =
109
1/2
✓ Branch 2 taken 626 times.
✗ Branch 3 not taken.
1252 std::accumulate(std::next(point), points.cend(), cell_diameter,
110 432 [point](double diameter, const Point& other) {
111
1/2
✓ Branch 2 taken 432 times.
✗ Branch 3 not taken.
864 return std::max(diameter, xt::linalg::norm(*point - other, 2));
112 });
113 }
114
115 280 constexpr double factor = 100.0 * std::numeric_limits<double>::epsilon();
116 560 return factor * std::max(coordinate_scale, cell_diameter);
117 }
118
119 630 Point physical_vertex(const mesh::Geometry& geometry, std::size_t vertex, std::size_t dimension) {
120
1/2
✓ Branch 1 taken 630 times.
✗ Branch 2 not taken.
630 const auto coordinates = geometry.x();
121
3/4
✓ Branch 2 taken 630 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 629 times.
630 if (vertex >= coordinates.size() / geometry.dim()) {
122
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::out_of_range("Cell references a vertex outside the mesh geometry");
123 }
124
125
1/2
✓ Branch 2 taken 629 times.
✗ Branch 3 not taken.
629 Point point = xt::zeros<double>({dimension});
126
2/2
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 628 times.
1437 for (std::size_t coordinate = 0; coordinate < dimension; ++coordinate) {
127
1/2
✓ Branch 1 taken 809 times.
✗ Branch 2 not taken.
809 point(coordinate) = coordinates[vertex * geometry.dim() + coordinate];
128
2/2
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 808 times.
809 if (!std::isfinite(point(coordinate))) {
129
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("Mesh coordinates must be finite");
130 }
131 }
132
3/4
✓ Branch 1 taken 648 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 627 times.
648 for (std::size_t coordinate = dimension; coordinate < geometry.dim(); ++coordinate) {
133
1/2
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
21 const auto value = coordinates[vertex * geometry.dim() + coordinate];
134
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
21 if (!std::isfinite(value)) throw std::invalid_argument("Mesh coordinates must be finite");
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 20 times.
21 if (value != 0.0) {
136
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("DGGeometry supports coordinates in the mesh dimensions only");
137 }
138 }
139 627 return point;
140 2 }
141
142 47 std::array<std::size_t, 4> independent_vertices(const std::vector<Point>& reference_points,
143 std::size_t dimension) {
144 47 VertexBasis best;
145
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 13 times.
47 const auto second_end = dimension > 1 ? reference_points.size() : 2;
146
2/2
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 47 times.
149 for (std::size_t first = 1; first < reference_points.size(); ++first) {
147
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 15 times.
102 const auto second_start = dimension > 1 ? first + 1 : 1;
148
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 102 times.
190 for (auto second = second_start; second < second_end; ++second) {
149
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 consider_basis_completions(reference_points, dimension, first, second, best);
150 }
151 }
152
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 46 times.
47 if (best.determinant <= std::numeric_limits<double>::epsilon()) {
153
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::logic_error("Reference element has degenerate vertices");
154 }
155 46 return best.indices;
156 }
157
158 758 double face_measure(const std::vector<Point>& vertices, std::size_t face_dimension) {
159
4/4
✓ Branch 0 taken 493 times.
✓ Branch 1 taken 230 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 1 times.
758 switch (face_dimension) {
160 493 case 0:
161 // A point face has measure 1 by convention.
162 493 return 1.0;
163 230 case 1:
164
1/2
✓ Branch 4 taken 230 times.
✗ Branch 5 not taken.
230 return xt::linalg::norm(vertices[1] - vertices[0], 2);
165 34 case 2:
166
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 1 times.
34 if (vertices.size() == 3) {
167 33 return triangle_area(vertices[0], vertices[1], vertices[2]);
168 }
169 1 return triangle_area(vertices[0], vertices[1], vertices[2]) +
170 1 triangle_area(vertices[0], vertices[2], vertices[3]);
171 1 default:
172
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::logic_error("Unsupported face dimension");
173 }
174 }
175
176 902 Point centroid(const std::vector<Point>& vertices) {
177
1/2
✓ Branch 4 taken 902 times.
✗ Branch 5 not taken.
902 Point output = xt::zeros<double>({vertices.front().size()});
178
1/2
✓ Branch 2 taken 902 times.
✗ Branch 3 not taken.
1804 output = std::ranges::fold_left(vertices, std::move(output), [](Point sum, const Point& vertex) {
179 1420 sum += vertex;
180 1420 return sum;
181 902 });
182
1/2
✓ Branch 2 taken 902 times.
✗ Branch 3 not taken.
902 output /= static_cast<double>(vertices.size());
183 902 return output;
184 ✗ }
185
186 623 Point face_normal(const std::vector<Point>& vertices, const Point& cell_centroid,
187 std::size_t dimension) {
188
1/2
✓ Branch 2 taken 623 times.
✗ Branch 3 not taken.
623 Point normal = xt::zeros<double>({dimension});
189
1/2
✓ Branch 1 taken 623 times.
✗ Branch 2 not taken.
623 const auto face_centroid = centroid(vertices);
190
2/2
✓ Branch 0 taken 469 times.
✓ Branch 1 taken 154 times.
623 if (dimension == 1) {
191
1/2
✓ Branch 2 taken 469 times.
✗ Branch 3 not taken.
469 normal = face_centroid - cell_centroid;
192
2/2
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 21 times.
154 } else if (dimension == 2) {
193 133 const auto edge = vertices[1] - vertices[0];
194
2/4
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 133 times.
✗ Branch 5 not taken.
133 normal(0) = edge(1);
195
2/4
✓ Branch 1 taken 133 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 133 times.
✗ Branch 5 not taken.
133 normal(1) = -edge(0);
196 133 } else {
197
1/2
✓ Branch 7 taken 21 times.
✗ Branch 8 not taken.
21 normal = xt::linalg::cross(vertices[1] - vertices[0], vertices[2] - vertices[0]);
198 }
199
200
1/2
✓ Branch 1 taken 623 times.
✗ Branch 2 not taken.
623 const auto length = xt::linalg::norm(normal, 2);
201
5/6
✓ Branch 1 taken 623 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 622 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 622 times.
623 if (!std::isfinite(length) || length == 0.0) {
202
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("A cell face must have nonzero measure");
203 }
204
1/2
✓ Branch 1 taken 622 times.
✗ Branch 2 not taken.
622 normal /= length;
205
3/4
✓ Branch 2 taken 622 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 61 times.
✓ Branch 6 taken 561 times.
622 if (xt::linalg::vdot(normal, face_centroid - cell_centroid) < 0.0) {
206
1/2
✓ Branch 1 taken 61 times.
✗ Branch 2 not taken.
61 normal *= -1.0;
207 }
208 622 return normal;
209 624 }
210
211 } // namespace detail
212
213 using namespace detail;
214
215 namespace {
216
217 struct ReferenceFaceGeometry {
218 std::vector<std::size_t> vertices;
219 double measure = 0.0;
220 };
221
222 struct ReferenceCellGeometry {
223 // Points, basis indices, and face indices all use mesh-local vertex order.
224 std::vector<Point> points;
225 std::array<std::size_t, 4> basis_vertices{};
226 Matrix inverse_edges;
227 std::vector<ReferenceFaceGeometry> faces;
228 };
229
230 43 std::vector<ReferenceFaceGeometry> make_reference_faces(mesh::CellType cell_type,
231 const std::vector<Point>& points,
232 std::size_t face_dimension) {
233
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 const auto local_faces = cell_type->get_entity_vertices(static_cast<int>(face_dimension));
234 43 std::vector<ReferenceFaceGeometry> faces;
235
1/2
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 faces.reserve(local_faces.size());
236
2/2
✓ Branch 2 taken 134 times.
✓ Branch 3 taken 43 times.
220 for (const auto& local_face : local_faces) {
237 134 std::vector<std::size_t> face_vertices;
238 134 std::vector<Point> face_points;
239
1/2
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
134 face_vertices.reserve(local_face.size());
240
1/2
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
134 face_points.reserve(local_face.size());
241
2/2
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 134 times.
524 for (const auto vertex : local_face) {
242
3/6
✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 256 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 256 times.
256 if (vertex < 0 || std::cmp_greater_equal(vertex, points.size())) {
243 ✗ throw std::logic_error("Cell face references an invalid local vertex");
244 }
245 256 const auto index = static_cast<std::size_t>(vertex);
246
1/2
✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
256 face_vertices.push_back(index);
247
1/2
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
256 face_points.push_back(points[index]);
248 }
249
1/2
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
134 const auto measure = face_measure(face_points, face_dimension);
250
3/6
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 134 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 134 times.
134 if (!std::isfinite(measure) || measure <= 0.0) {
251 ✗ throw std::logic_error("Reference face has zero measure");
252 }
253 134 faces.push_back({.vertices = std::move(face_vertices), .measure = measure});
254 134 }
255 43 return faces;
256
2/6
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 134 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
311 }
257
258 43 ReferenceCellGeometry make_reference_geometry(mesh::CellType cell_type, std::size_t vertex_count,
259 std::size_t dimension) {
260
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 std::vector<std::size_t> local_indices(vertex_count);
261
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 std::ranges::iota(local_indices, std::size_t{0});
262
1/2
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 const auto reference_to_mesh = reference_ordered_vertices(cell_type, local_indices);
263
2/4
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 43 times.
✗ Branch 6 not taken.
43 const auto reference_element = nodal::get_ref_element(ref_type_for(cell_type->kind()), 1);
264
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 43 times.
43 if (reference_element->number_of_nodes() != vertex_count) {
265 ✗ throw std::logic_error("Reference element and mesh cell vertex counts must match");
266 }
267
268 43 const auto& reference_nodes = reference_element->r();
269 43 std::vector<Point> reference_points;
270
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 reference_points.reserve(vertex_count);
271
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 43 times.
177 for (std::size_t vertex = 0; vertex < vertex_count; ++vertex) {
272
1/2
✓ Branch 2 taken 134 times.
✗ Branch 3 not taken.
134 Point point = xt::zeros<double>({dimension});
273
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 134 times.
390 for (std::size_t coordinate = 0; coordinate < dimension; ++coordinate) {
274 256 point(coordinate) =
275
4/6
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 232 times.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 232 times.
✗ Branch 7 not taken.
256 dimension == 1 ? reference_nodes(vertex) : reference_nodes(vertex, coordinate);
276 }
277
1/2
✓ Branch 1 taken 134 times.
✗ Branch 2 not taken.
134 reference_points.push_back(std::move(point));
278 134 }
279
280
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 auto basis = independent_vertices(reference_points, dimension);
281
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 const auto edges = edge_matrix(reference_points, basis, dimension);
282
3/6
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 43 times.
43 if (is_degenerate(edges, xt::linalg::det(edges))) {
283 ✗ throw std::logic_error("Reference element has degenerate vertices");
284 }
285
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 43 times.
163 for (std::size_t vertex = 0; vertex <= dimension; ++vertex) {
286 120 basis[vertex] = reference_to_mesh[basis[vertex]];
287 }
288
289 43 ReferenceCellGeometry reference{
290 ✗ .points = std::vector<Point>(vertex_count),
291 .basis_vertices = basis,
292 43 .inverse_edges = xt::linalg::inv(edges),
293 .faces = {},
294
5/12
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 43 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 43 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 43 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 43 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
129 };
295
2/2
✓ Branch 0 taken 134 times.
✓ Branch 1 taken 43 times.
177 for (std::size_t vertex = 0; vertex < vertex_count; ++vertex) {
296 268 reference.points[reference_to_mesh[vertex]] = std::move(reference_points[vertex]);
297 }
298
299
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 reference.faces = make_reference_faces(cell_type, reference.points, dimension - 1);
300 43 return reference;
301 43 }
302
303 31 std::size_t validated_mesh_dimension(const mesh::Topology& topology,
304 const mesh::Geometry& geometry) {
305 31 const auto dimension = static_cast<std::size_t>(topology.dimension());
306
2/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
31 if (dimension < 1 || dimension > 3) {
307 ✗ throw std::invalid_argument("DGGeometry supports meshes with dimensions 1 through 3");
308 }
309
6/12
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 31 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 31 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 31 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 31 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 31 times.
31 if (geometry.dim() < dimension || geometry.x().size() % geometry.dim() != 0) {
310 ✗ throw std::invalid_argument("Mesh coordinate data is inconsistent with its dimension");
311 }
312
3/6
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 31 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 31 times.
31 if (topology.cell_types().size() != topology.conn().size()) {
313 ✗ throw std::invalid_argument("Mesh cell types and connectivity counts must match");
314 }
315 31 return dimension;
316 }
317
318 280 DGGeometry::CellGeometry make_cell_geometry(const mesh::Geometry& geometry,
319 const std::vector<std::size_t>& mesh_vertices,
320 const ReferenceCellGeometry& reference,
321 std::size_t dimension, std::size_t cell_index) {
322
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 280 times.
280 if (mesh_vertices.size() != reference.points.size()) {
323 ✗ throw std::invalid_argument("Cell connectivity does not match its cell type");
324 }
325
326 280 std::vector<Point> physical_nodes;
327
1/2
✓ Branch 2 taken 280 times.
✗ Branch 3 not taken.
280 physical_nodes.reserve(mesh_vertices.size());
328
2/4
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 280 times.
✗ Branch 5 not taken.
280 std::ranges::transform(mesh_vertices, std::back_inserter(physical_nodes),
329 626 [&geometry, dimension](std::size_t vertex) {
330 626 return physical_vertex(geometry, vertex, dimension);
331 });
332
1/2
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
280 const auto physical_edges = edge_matrix(physical_nodes, reference.basis_vertices, dimension);
333
1/2
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
280 const auto jacobian = xt::linalg::dot(physical_edges, reference.inverse_edges);
334
1/2
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
280 const auto determinant = xt::linalg::det(jacobian);
335
4/6
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 280 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 279 times.
280 if (is_degenerate(jacobian, determinant)) {
336
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("Cell vertices define a degenerate element");
337 }
338
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 const auto inverse_jacobian = xt::linalg::inv(jacobian);
339
340
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 const auto tolerance = affine_tolerance(physical_nodes, dimension);
341 279 const auto anchor = reference.basis_vertices[0];
342
2/2
✓ Branch 1 taken 622 times.
✓ Branch 2 taken 278 times.
900 for (std::size_t vertex_index = 0; vertex_index < physical_nodes.size(); ++vertex_index) {
343 622 const auto reference_offset = reference.points[vertex_index] - reference.points[anchor];
344
1/2
✓ Branch 1 taken 622 times.
✗ Branch 2 not taken.
622 const auto expected = xt::linalg::dot(jacobian, reference_offset);
345 622 const auto actual = physical_nodes[vertex_index] - physical_nodes[anchor];
346
3/4
✓ Branch 2 taken 622 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 621 times.
622 if (xt::linalg::norm(expected - actual, 2) > tolerance) {
347
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("DGGeometry requires affine cell mappings");
348 }
349 624 }
350
351 278 std::vector<DGGeometry::FaceGeometry> faces;
352
1/2
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
278 faces.reserve(reference.faces.size());
353
1/2
✓ Branch 1 taken 278 times.
✗ Branch 2 not taken.
278 const auto cell_centroid = centroid(physical_nodes);
354
2/2
✓ Branch 2 taken 619 times.
✓ Branch 3 taken 278 times.
1175 for (const auto& reference_face : reference.faces) {
355 619 std::vector<Point> physical_face;
356
1/2
✓ Branch 2 taken 619 times.
✗ Branch 3 not taken.
619 physical_face.reserve(reference_face.vertices.size());
357
1/2
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
619 std::ranges::transform(
358
1/2
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
619 reference_face.vertices, std::back_inserter(physical_face),
359 790 [&physical_nodes](std::size_t vertex) -> const Point& { return physical_nodes[vertex]; });
360
1/2
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
619 const auto measure = face_measure(physical_face, dimension - 1);
361
3/6
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 619 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 619 times.
619 if (!std::isfinite(measure) || measure <= 0.0) {
362 ✗ throw std::invalid_argument("A cell face must have finite, nonzero measure");
363 }
364 ✗ faces.push_back(
365
2/4
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 619 times.
✗ Branch 5 not taken.
1238 {.outward_normal = point_array(face_normal(physical_face, cell_centroid, dimension)),
366 .measure = measure,
367
1/2
✓ Branch 1 taken 619 times.
✗ Branch 2 not taken.
619 .surface_jacobian = measure / reference_face.measure});
368 619 }
369
370 return {
371 .mesh_cell_index = cell_index,
372 .dimension = dimension,
373
1/2
✓ Branch 1 taken 278 times.
✗ Branch 2 not taken.
278 .jacobian = padded_matrix(jacobian, dimension),
374
2/4
✓ Branch 1 taken 278 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 278 times.
✗ Branch 5 not taken.
556 .inverse_jacobian = padded_matrix(inverse_jacobian, dimension),
375 .determinant = determinant,
376 278 .faces = std::move(faces),
377 556 };
378
1/2
✓ Branch 1 taken 278 times.
✗ Branch 2 not taken.
1119 }
379
380 } // namespace
381
382 31 DGGeometry::DGGeometry(const mesh::Mesh& mesh) : m_mesh(mesh) {
383
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 const auto& topology = mesh.topology();
384
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 const auto& geometry = mesh.geometry();
385
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 const auto dimension = validated_mesh_dimension(topology, geometry);
386
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 const auto cell_types = topology.cell_types();
387
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 const auto connectivity = topology.conn();
388
389
2/4
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
31 m_cells.resize(topology.n_cells());
390 31 std::unordered_map<mesh::CellType, ReferenceCellGeometry> reference_geometry;
391
2/2
✓ Branch 1 taken 292 times.
✓ Branch 2 taken 29 times.
321 for (std::size_t cell_index = 0; cell_index < cell_types.size(); ++cell_index) {
392 292 const auto* cell_type = cell_types[cell_index];
393
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 292 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
292 if (cell_type == nullptr) throw std::invalid_argument("Mesh cell type cannot be null");
394
3/4
✓ Branch 1 taken 292 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 280 times.
292 if (std::cmp_not_equal(cell_type->dimension(), dimension)) continue;
395
396 280 const auto& mesh_vertices = connectivity[cell_index];
397
1/2
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
280 auto [entry, inserted] = reference_geometry.try_emplace(cell_type);
398
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 237 times.
280 if (inserted) {
399
1/2
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
43 entry->second = make_reference_geometry(cell_type, mesh_vertices.size(), dimension);
400 }
401 278 m_cells[cell_index] =
402
2/2
✓ Branch 2 taken 278 times.
✓ Branch 3 taken 2 times.
558 make_cell_geometry(geometry, mesh_vertices, entry->second, dimension, cell_index);
403 }
404 33 }
405
406 296 bool DGGeometry::has_cell(std::size_t mesh_cell_index) const {
407
2/4
✓ Branch 1 taken 296 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 296 times.
✗ Branch 6 not taken.
296 return mesh_cell_index < m_cells.size() && m_cells[mesh_cell_index].has_value();
408 }
409
410 292 const DGGeometry::CellGeometry& DGGeometry::cell(std::size_t mesh_cell_index) const {
411
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292 times.
292 if (!has_cell(mesh_cell_index)) {
412 ✗ throw std::out_of_range("No DG geometry is available for this mesh cell");
413 }
414 292 return *m_cells[mesh_cell_index];
415 }
416
417 } // namespace oiseau::dg
418