| 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_space.hpp" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <array> | ||
| 11 | #include <cstddef> | ||
| 12 | #include <map> | ||
| 13 | #include <span> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <utility> | ||
| 16 | #include <vector> | ||
| 17 | #include <xtensor-blas/xlinalg.hpp> | ||
| 18 | #include <xtensor/containers/xadapt.hpp> | ||
| 19 | #include <xtensor/containers/xbuffer_adaptor.hpp> | ||
| 20 | #include <xtensor/core/xtensor_forward.hpp> | ||
| 21 | #include <xtensor/views/xslice.hpp> | ||
| 22 | #include <xtensor/views/xview.hpp> | ||
| 23 | |||
| 24 | #include "oiseau/dg/detail/cell_mapping.hpp" | ||
| 25 | #include "oiseau/dg/nodal/ref_element.hpp" | ||
| 26 | #include "oiseau/dg/ref_element_type.hpp" | ||
| 27 | #include "oiseau/mesh/cell.hpp" | ||
| 28 | #include "oiseau/mesh/geometry.hpp" | ||
| 29 | #include "oiseau/mesh/mesh.hpp" | ||
| 30 | #include "oiseau/mesh/topology.hpp" | ||
| 31 | |||
| 32 | namespace oiseau::dg { | ||
| 33 | namespace { | ||
| 34 | |||
| 35 | using GroupKey = std::pair<mesh::CellKind, unsigned>; | ||
| 36 | |||
| 37 | struct ActiveCell { | ||
| 38 | std::size_t mesh_index = 0; | ||
| 39 | mesh::CellType type = nullptr; | ||
| 40 | unsigned order = 0; | ||
| 41 | }; | ||
| 42 | |||
| 43 | 37 | int validated_mesh_dimension(const mesh::Topology& topology, const mesh::Geometry& geometry) { | |
| 44 |
4/6✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 37 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 36 times.
|
37 | if (topology.cell_types().size() != topology.conn().size()) { |
| 45 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("Mesh cell types and connectivity counts must match"); |
| 46 | } | ||
| 47 |
9/12✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 35 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 35 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 34 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 34 times.
|
36 | if (geometry.dim() == 0 || geometry.x().size() % geometry.dim() != 0) { |
| 48 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("Mesh coordinate data is inconsistent with its dimension"); |
| 49 | } | ||
| 50 | |||
| 51 | 34 | const auto dimension = topology.dimension(); | |
| 52 |
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) { |
| 53 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("DGSpace supports meshes with dimensions 1 through 3"); |
| 54 | } | ||
| 55 | 33 | return dimension; | |
| 56 | } | ||
| 57 | |||
| 58 | 14 | std::vector<unsigned> uniform_orders(const mesh::Mesh& mesh, unsigned order) { | |
| 59 |
2/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
|
14 | const auto cell_types = mesh.topology().cell_types(); |
| 60 |
2/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
|
14 | const auto dimension = mesh.topology().dimension(); |
| 61 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | const auto cell_count = static_cast<std::size_t>(std::ranges::count_if( |
| 62 | 269 | cell_types, [dimension](const auto* cell) { return cell->dimension() == dimension; })); | |
| 63 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
28 | return std::vector<unsigned>(cell_count, order); |
| 64 | } | ||
| 65 | |||
| 66 | 33 | std::vector<ActiveCell> active_cells(std::span<const mesh::CellType> cell_types, | |
| 67 | const std::vector<unsigned>& orders, int dimension) { | ||
| 68 | 33 | const auto count = static_cast<std::size_t>(std::ranges::count_if( | |
| 69 | 306 | cell_types, [dimension](const auto* cell) { return cell->dimension() == dimension; })); | |
| 70 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 31 times.
|
33 | if (orders.size() != count) { |
| 71 | throw std::invalid_argument( | ||
| 72 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | "A polynomial order must be provided for every top-dimensional cell"); |
| 73 | } | ||
| 74 | |||
| 75 | 31 | std::vector<ActiveCell> cells; | |
| 76 |
1/2✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
|
31 | cells.reserve(count); |
| 77 | 31 | std::size_t order_index = 0; | |
| 78 |
2/2✓ Branch 1 taken 297 times.
✓ Branch 2 taken 29 times.
|
326 | for (std::size_t mesh_index = 0; mesh_index < cell_types.size(); ++mesh_index) { |
| 79 | 297 | const auto* type = cell_types[mesh_index]; | |
| 80 |
3/4✓ Branch 1 taken 297 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 283 times.
|
297 | if (type->dimension() != dimension) continue; |
| 81 | |||
| 82 | 283 | const auto order = orders[order_index++]; | |
| 83 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 282 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
283 | if (order == 0) throw std::invalid_argument("Polynomial orders must be greater than zero"); |
| 84 |
2/2✓ Branch 2 taken 281 times.
✓ Branch 3 taken 1 times.
|
282 | (void)ref_type_for(type->kind()); |
| 85 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | cells.push_back({.mesh_index = mesh_index, .type = type, .order = order}); |
| 86 | } | ||
| 87 | 29 | return cells; | |
| 88 | 2 | } | |
| 89 | |||
| 90 | } // namespace | ||
| 91 | |||
| 92 | 37 | DGSpace::DGSpace(const mesh::Mesh& mesh, const std::vector<unsigned>& orders) | |
| 93 |
1/2✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
|
37 | : m_mesh(mesh), m_orders(orders) { |
| 94 |
1/2✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
|
37 | const auto& topology = mesh.topology(); |
| 95 |
1/2✓ Branch 1 taken 37 times.
✗ Branch 2 not taken.
|
37 | const auto& geometry = mesh.geometry(); |
| 96 |
2/2✓ Branch 1 taken 33 times.
✓ Branch 2 taken 4 times.
|
37 | const auto dimension = validated_mesh_dimension(topology, geometry); |
| 97 |
1/2✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
|
33 | const auto connectivity = topology.conn(); |
| 98 |
3/4✓ Branch 1 taken 33 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29 times.
✓ Branch 5 taken 4 times.
|
33 | const auto cells = active_cells(topology.cell_types(), orders, dimension); |
| 99 | |||
| 100 | 29 | std::map<GroupKey, std::size_t> group_sizes; | |
| 101 |
3/4✓ Branch 5 taken 280 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 280 times.
✓ Branch 8 taken 29 times.
|
618 | for (const auto& cell : cells) ++group_sizes[{cell.type->kind(), cell.order}]; |
| 102 | |||
| 103 | 29 | std::map<GroupKey, std::size_t> group_indices; | |
| 104 |
2/2✓ Branch 7 taken 52 times.
✓ Branch 8 taken 29 times.
|
81 | for (const auto& [key, count] : group_sizes) { |
| 105 | const auto node_count = | ||
| 106 |
2/4✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
|
52 | nodal::get_ref_element(ref_type_for(key.first), key.second)->number_of_nodes(); |
| 107 | 52 | ElementGroup group{ | |
| 108 | 52 | .kind = key.first, | |
| 109 | 52 | .order = key.second, | |
| 110 | .cell_indices = {}, | ||
| 111 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
104 | .nodes = xt::xarray<double>::from_shape({count, node_count, geometry.dim()}), |
| 112 |
3/8✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 52 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
156 | }; |
| 113 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | group.cell_indices.reserve(count); |
| 114 |
1/2✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
|
52 | group_indices.emplace(key, m_element_groups.size()); |
| 115 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
104 | m_element_groups.emplace_back(std::move(group)); |
| 116 | 52 | } | |
| 117 | |||
| 118 |
2/4✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 29 times.
✗ Branch 6 not taken.
|
29 | const std::array<std::size_t, 2> node_shape = {geometry.x().size() / geometry.dim(), |
| 119 |
1/2✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
|
29 | geometry.dim()}; |
| 120 | auto mesh_nodes = | ||
| 121 |
3/6✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 29 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 29 times.
✗ Branch 10 not taken.
|
29 | xt::adapt(geometry.x().data(), geometry.x().size(), xt::no_ownership(), node_shape); |
| 122 | |||
| 123 |
2/2✓ Branch 2 taken 280 times.
✓ Branch 3 taken 28 times.
|
337 | for (const auto& cell : cells) { |
| 124 | 280 | const auto kind = cell.type->kind(); | |
| 125 |
1/2✓ Branch 3 taken 280 times.
✗ Branch 4 not taken.
|
280 | auto vertices = detail::reference_ordered_vertices(cell.type, connectivity[cell.mesh_index]); |
| 126 |
3/4✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 279 times.
|
280 | if (std::ranges::any_of(vertices, |
| 127 | 628 | [&node_shape](const auto vertex) { return vertex >= node_shape[0]; })) { | |
| 128 |
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"); |
| 129 | } | ||
| 130 | |||
| 131 |
1/2✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
|
279 | const auto ref_type = ref_type_for(kind); |
| 132 |
1/2✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
|
279 | const auto interpolation_element = nodal::get_ref_element(ref_type, 1); |
| 133 |
1/2✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
|
279 | const auto element = nodal::get_ref_element(ref_type, cell.order); |
| 134 |
2/4✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 279 times.
✗ Branch 6 not taken.
|
279 | const auto vertices_view = xt::view(mesh_nodes, xt::keep(vertices), xt::all()); |
| 135 | const auto interpolation_matrix = | ||
| 136 |
1/2✓ Branch 3 taken 279 times.
✗ Branch 4 not taken.
|
558 | xt::linalg::dot(interpolation_element->vandermonde(element->r()), |
| 137 |
2/4✓ Branch 3 taken 279 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 279 times.
✗ Branch 8 not taken.
|
837 | xt::linalg::inv(interpolation_element->v())); |
| 138 |
1/2✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
|
279 | const auto physical_nodes = xt::linalg::dot(interpolation_matrix, vertices_view); |
| 139 | |||
| 140 |
1/2✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
|
279 | auto& group = m_element_groups[group_indices.at({kind, cell.order})]; |
| 141 | 279 | const auto group_cell_index = group.cell_indices.size(); | |
| 142 |
2/4✓ Branch 3 taken 279 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 279 times.
✗ Branch 7 not taken.
|
279 | xt::view(group.nodes, group_cell_index, xt::all(), xt::all()) = physical_nodes; |
| 143 |
1/2✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
|
279 | group.cell_indices.push_back(cell.mesh_index); |
| 144 | 280 | } | |
| 145 | 50 | } | |
| 146 | |||
| 147 | 14 | DGSpace::DGSpace(const mesh::Mesh& mesh, unsigned order) | |
| 148 |
3/4✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 2 times.
|
14 | : DGSpace(mesh, uniform_orders(mesh, order)) {} |
| 149 | |||
| 150 | 6234 | std::span<const DGSpace::ElementGroup> DGSpace::element_groups() const { | |
| 151 | 6234 | return {m_element_groups}; | |
| 152 | } | ||
| 153 | |||
| 154 | 7 | const DGSpace::ElementGroup* DGSpace::element_group(mesh::CellKind kind, unsigned order) const { | |
| 155 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | const auto group = std::ranges::find_if(m_element_groups, [kind, order](const auto& candidate) { |
| 156 |
4/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
|
13 | return candidate.kind == kind && candidate.order == order; |
| 157 | }); | ||
| 158 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
|
20 | return group == m_element_groups.end() ? nullptr : &*group; |
| 159 | } | ||
| 160 | |||
| 161 | 7 | std::span<const unsigned> DGSpace::orders() const { return {m_orders}; } | |
| 162 | |||
| 163 | } // namespace oiseau::dg | ||
| 164 |