GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/mesh/topology.cpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 64 69 92.8%
Functions: 13 14 92.9%
Branches: 51 76 67.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/mesh/topology.hpp"
8
9 #include <algorithm>
10 #include <cstddef>
11 #include <map>
12 #include <span>
13 #include <stdexcept>
14 #include <utility>
15 #include <vector>
16
17 #include "oiseau/mesh/cell.hpp"
18
19 using namespace oiseau::mesh;
20
21 namespace {
22
23 using FaceLocation = std::pair<std::size_t, std::size_t>;
24
25 671 std::vector<std::size_t> canonical_face_key(std::span<const int> local_vertices,
26 std::span<const std::size_t> cell_vertices) {
27 671 std::vector<std::size_t> key;
28
1/2
✓ Branch 2 taken 671 times.
✗ Branch 3 not taken.
671 key.reserve(local_vertices.size());
29
2/2
✓ Branch 2 taken 912 times.
✓ Branch 3 taken 670 times.
2253 for (const int local_vertex : local_vertices) {
30
5/6
✓ Branch 0 taken 912 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 911 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 911 times.
912 if (local_vertex < 0 || static_cast<std::size_t>(local_vertex) >= cell_vertices.size()) {
31
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 throw std::invalid_argument("Cell connectivity does not match its cell type");
32 }
33
1/2
✓ Branch 2 taken 911 times.
✗ Branch 3 not taken.
911 key.push_back(cell_vertices[static_cast<std::size_t>(local_vertex)]);
34 }
35
1/2
✓ Branch 1 taken 670 times.
✗ Branch 2 not taken.
670 std::ranges::sort(key);
36 670 return key;
37 1 }
38
39 258 void connect_faces(FaceLocation face, FaceLocation other,
40 std::vector<std::vector<std::size_t>>& element_to_element,
41 std::vector<std::vector<std::size_t>>& element_to_face) {
42 258 const auto [cell_index, face_index] = face;
43 258 const auto [other_cell, other_face] = other;
44
6/6
✓ Branch 0 taken 257 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 256 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 256 times.
258 if (other_cell == cell_index || element_to_element[other_cell][other_face] != other_cell) {
45
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 throw std::invalid_argument("Non-manifold face is shared by more than two cells");
46 }
47 256 element_to_element[cell_index][face_index] = other_cell;
48 256 element_to_face[cell_index][face_index] = other_face;
49 256 element_to_element[other_cell][other_face] = cell_index;
50 256 element_to_face[other_cell][other_face] = face_index;
51 256 }
52
53 } // namespace
54
55 1 Topology::Topology() = default;
56 98 Topology::~Topology() = default;
57
58 49 Topology::Topology(std::vector<std::vector<std::size_t>>&& conn, std::vector<CellType>&& cell_types)
59 147 : m_conn(std::move(conn)), m_cell_types(std::move(cell_types)) {};
60
61 ✗ std::span<CellType> Topology::cell_types() {
62 ✗ m_dimension = -1;
63 ✗ return m_cell_types;
64 };
65 201 std::span<const CellType> Topology::cell_types() const { return m_cell_types; };
66
67 35 std::span<std::vector<std::size_t>> Topology::conn() { return m_conn; };
68 155 std::span<const std::vector<std::size_t>> Topology::conn() const { return m_conn; };
69 35 std::span<std::vector<std::size_t>> Topology::e_to_e() { return m_e_to_e; };
70 35 std::span<std::vector<std::size_t>> Topology::e_to_f() { return m_e_to_f; };
71
72 86 std::size_t Topology::n_cells() const { return m_conn.size(); }
73
74 185 int Topology::dimension() const {
75
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 46 times.
185 if (m_dimension >= 0) return m_dimension;
76
77 46 int dimension = 0;
78
2/2
✓ Branch 2 taken 330 times.
✓ Branch 3 taken 46 times.
422 for (const auto* cell : m_cell_types) {
79
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 330 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
330 if (cell == nullptr) throw std::invalid_argument("Cell type cannot be null");
80
1/2
✓ Branch 1 taken 330 times.
✗ Branch 2 not taken.
330 dimension = std::max(dimension, cell->dimension());
81 }
82 46 m_dimension = dimension;
83 46 return m_dimension;
84 }
85
86 36 void Topology::calculate_connectivity() {
87
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
36 if (m_conn.size() != m_cell_types.size()) {
88 ✗ throw std::invalid_argument("Cell connectivity and cell type counts must match");
89 }
90
91
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 const auto mesh_dimension = dimension();
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (mesh_dimension > 3) {
93 ✗ throw std::invalid_argument("Face connectivity supports meshes of dimension at most three");
94 }
95
96 // Each canonical face key records the first cell/local-face that uses it.
97 // A second use creates the two-way adjacency; a third is non-manifold.
98 36 std::map<std::vector<std::size_t>, FaceLocation> face_owner;
99
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
72 std::vector<std::vector<std::size_t>> element_to_element(m_conn.size());
100
1/2
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
36 std::vector<std::vector<std::size_t>> element_to_face(m_conn.size());
101
102
2/2
✓ Branch 1 taken 312 times.
✓ Branch 2 taken 33 times.
345 for (std::size_t cell_index = 0; cell_index < m_conn.size(); ++cell_index) {
103 312 const auto* cell = m_cell_types[cell_index];
104 // Gmsh meshes commonly include lower-dimensional boundary entities. They
105 // are not volume cells and therefore do not participate in this adjacency.
106
6/8
✓ Branch 0 taken 312 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 312 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 294 times.
✓ Branch 7 taken 18 times.
✓ Branch 8 taken 294 times.
312 if (mesh_dimension == 0 || cell->dimension() != mesh_dimension) continue;
107
108
1/2
✓ Branch 1 taken 294 times.
✗ Branch 2 not taken.
294 const auto face_vertices = cell->get_entity_vertices(mesh_dimension - 1);
109 294 const auto& cell_vertices = m_conn[cell_index];
110
1/2
✓ Branch 3 taken 294 times.
✗ Branch 4 not taken.
294 element_to_element[cell_index].resize(face_vertices.size(), cell_index);
111
1/2
✓ Branch 3 taken 294 times.
✗ Branch 4 not taken.
294 element_to_face[cell_index].resize(face_vertices.size());
112
113
2/2
✓ Branch 1 taken 671 times.
✓ Branch 2 taken 291 times.
962 for (std::size_t face_index = 0; face_index < face_vertices.size(); ++face_index) {
114
2/2
✓ Branch 4 taken 670 times.
✓ Branch 5 taken 1 times.
671 auto key = canonical_face_key(face_vertices[face_index], cell_vertices);
115 670 element_to_face[cell_index][face_index] = face_index;
116
117 670 const FaceLocation face{cell_index, face_index};
118
1/2
✓ Branch 1 taken 670 times.
✗ Branch 2 not taken.
1340 const auto [owner, inserted] = face_owner.emplace(std::move(key), face);
119
2/2
✓ Branch 0 taken 412 times.
✓ Branch 1 taken 258 times.
670 if (inserted) continue;
120
2/2
✓ Branch 3 taken 256 times.
✓ Branch 4 taken 2 times.
258 connect_faces(face, owner->second, element_to_element, element_to_face);
121
2/2
✓ Branch 1 taken 256 times.
✓ Branch 2 taken 412 times.
670 }
122 294 }
123
124 33 m_e_to_e = std::move(element_to_element);
125 33 m_e_to_f = std::move(element_to_face);
126 42 }
127