GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/dg/dg_connectivity.cpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 55 61 90.2%
Functions: 4 4 100.0%
Branches: 52 98 53.1%

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_connectivity.hpp"
8
9 #include <algorithm>
10 #include <cstddef>
11 #include <stdexcept>
12 #include <utility>
13 #include <vector>
14
15 #include "oiseau/mesh/cell.hpp"
16 #include "oiseau/mesh/topology.hpp"
17
18 namespace oiseau::dg {
19 namespace {
20
21 /**
22 * @brief Collects mesh vertex indices for a face in the cell type's local face order.
23 * @param cell Cell type that defines the local face vertices.
24 * @param cell_vertices Mesh vertex indices for the cell in local vertex order.
25 * @param face_index Face index within the cell.
26 * @return Mesh vertex indices in face vertex order.
27 * @throws std::out_of_range If the face index is invalid.
28 * @throws std::invalid_argument If the face references a missing local cell vertex.
29 */
30 1016 std::vector<std::size_t> face_vertices(const mesh::Cell& cell,
31 const std::vector<std::size_t>& cell_vertices,
32 std::size_t face_index) {
33
2/4
✓ Branch 1 taken 1016 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1016 times.
✗ Branch 5 not taken.
1016 const auto local_faces = cell.get_entity_vertices(cell.dimension() - 1);
34
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1016 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1016 if (face_index >= local_faces.size()) throw std::out_of_range("Cell face index is invalid");
35
36 1016 std::vector<std::size_t> vertices;
37
1/2
✓ Branch 3 taken 1016 times.
✗ Branch 4 not taken.
1016 vertices.reserve(local_faces[face_index].size());
38
2/2
✓ Branch 3 taken 1164 times.
✓ Branch 4 taken 1016 times.
3196 for (const auto local_vertex : local_faces[face_index]) {
39
3/6
✓ Branch 0 taken 1164 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1164 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1164 times.
1164 if (local_vertex < 0 || static_cast<std::size_t>(local_vertex) >= cell_vertices.size()) {
40 ✗ throw std::invalid_argument("Cell connectivity does not match its cell type");
41 }
42
1/2
✓ Branch 2 taken 1164 times.
✗ Branch 3 not taken.
1164 vertices.push_back(cell_vertices[static_cast<std::size_t>(local_vertex)]);
43 }
44 1016 return vertices;
45 1016 }
46
47 /**
48 * @brief Matches positions on a face to positions on its neighbor by mesh vertex index.
49 * @param local_vertices Mesh vertex indices in this face's order.
50 * @param neighbor_vertices Mesh vertex indices in the neighbor face's order.
51 * @return The neighbor's position for each vertex in @p local_vertices.
52 * @throws std::invalid_argument If the face sizes differ or a local vertex is missing from the
53 * neighbor.
54 */
55 508 std::vector<std::size_t> vertex_permutation(const std::vector<std::size_t>& local_vertices,
56 const std::vector<std::size_t>& neighbor_vertices) {
57
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 508 times.
508 if (local_vertices.size() != neighbor_vertices.size()) {
58 ✗ throw std::invalid_argument("Adjacent faces have different numbers of vertices");
59 }
60
61 508 std::vector<std::size_t> permutation;
62
1/2
✓ Branch 2 taken 508 times.
✗ Branch 3 not taken.
508 permutation.reserve(local_vertices.size());
63
2/2
✓ Branch 2 taken 582 times.
✓ Branch 3 taken 508 times.
1598 for (const auto vertex : local_vertices) {
64
1/2
✓ Branch 1 taken 582 times.
✗ Branch 2 not taken.
582 const auto position = std::ranges::find(neighbor_vertices, vertex);
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 582 times.
1164 if (position == neighbor_vertices.end()) {
66 ✗ throw std::invalid_argument("Adjacent faces do not share the same vertices");
67 }
68
1/2
✓ Branch 2 taken 582 times.
✗ Branch 3 not taken.
1164 permutation.push_back(static_cast<std::size_t>(position - neighbor_vertices.begin()));
69 }
70 508 return permutation;
71 ✗ }
72
73 } // namespace
74
75 34 DGConnectivity::DGConnectivity(mesh::Mesh& mesh) : m_mesh(mesh) {
76
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 auto& topology = mesh.topology();
77
1/2
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
34 const auto dimension = topology.dimension();
78
3/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
34 if (dimension < 1 || dimension > 3) {
79
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("DGConnectivity supports meshes with dimensions 1 through 3");
80 }
81
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 1 times.
33 topology.calculate_connectivity();
82
83
1/2
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
32 const auto cell_types = std::as_const(topology).cell_types();
84
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
32 const auto connectivity = topology.conn();
85
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
32 const auto cell_neighbors = topology.e_to_e();
86
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
32 const auto neighbor_faces = topology.e_to_f();
87
2/4
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
32 m_faces.resize(topology.n_cells());
88
2/2
✓ Branch 1 taken 304 times.
✓ Branch 2 taken 32 times.
336 for (std::size_t cell_index = 0; cell_index < cell_types.size(); ++cell_index) {
89 304 const auto* cell = cell_types[cell_index];
90
3/4
✓ Branch 1 taken 304 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 286 times.
304 if (cell->dimension() != dimension) continue;
91
92
1/2
✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
286 const auto local_faces = cell->get_entity_vertices(dimension - 1);
93
3/6
✓ Branch 3 taken 286 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 286 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 286 times.
572 if (cell_neighbors[cell_index].size() != local_faces.size() ||
94 286 neighbor_faces[cell_index].size() != local_faces.size()) {
95 ✗ throw std::runtime_error("Mesh face connectivity is inconsistent");
96 }
97
1/2
✓ Branch 3 taken 286 times.
✗ Branch 4 not taken.
286 m_faces[cell_index].resize(local_faces.size());
98
99
2/2
✓ Branch 1 taken 652 times.
✓ Branch 2 taken 286 times.
938 for (std::size_t face_index = 0; face_index < local_faces.size(); ++face_index) {
100 652 const auto neighbor_cell = cell_neighbors[cell_index][face_index];
101 // Mesh topology points boundary faces back to their own cell.
102
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 508 times.
652 if (neighbor_cell == cell_index) continue;
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 508 times.
508 if (neighbor_cell >= cell_types.size()) {
104 ✗ throw std::out_of_range("Mesh face references an invalid neighboring cell");
105 }
106
107 508 const auto neighbor_face = neighbor_faces[cell_index][face_index];
108 508 const auto* neighbor_type = cell_types[neighbor_cell];
109
1/2
✓ Branch 2 taken 508 times.
✗ Branch 3 not taken.
508 const auto local_vertices = face_vertices(*cell, connectivity[cell_index], face_index);
110 const auto neighbor_vertices =
111
1/2
✓ Branch 2 taken 508 times.
✗ Branch 3 not taken.
508 face_vertices(*neighbor_type, connectivity[neighbor_cell], neighbor_face);
112
113 508 auto& connection = m_faces[cell_index][face_index];
114 508 connection.neighbor_cell = neighbor_cell;
115 508 connection.neighbor_face = neighbor_face;
116
1/2
✓ Branch 1 taken 508 times.
✗ Branch 2 not taken.
508 connection.vertex_permutation = vertex_permutation(local_vertices, neighbor_vertices);
117 508 }
118 286 }
119 34 }
120
121 290 std::span<const DGConnectivity::FaceConnection> DGConnectivity::faces(
122 std::size_t mesh_cell_index) const {
123
3/4
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 289 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
290 if (mesh_cell_index >= m_faces.size()) throw std::out_of_range("Mesh cell index is invalid");
124 289 return m_faces[mesh_cell_index];
125 }
126
127 } // namespace oiseau::dg
128