| 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/solver/linear_dg.hpp" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <array> | ||
| 11 | #include <cmath> | ||
| 12 | #include <cstddef> | ||
| 13 | #include <memory> | ||
| 14 | #include <span> | ||
| 15 | #include <stdexcept> | ||
| 16 | #include <utility> | ||
| 17 | #include <vector> | ||
| 18 | #include <xtensor/containers/xarray.hpp> | ||
| 19 | #include <xtensor/core/xtensor_forward.hpp> | ||
| 20 | #include <xtensor/generators/xbuilder.hpp> | ||
| 21 | |||
| 22 | #include "oiseau/dg/dg_connectivity.hpp" | ||
| 23 | #include "oiseau/dg/dg_geometry.hpp" | ||
| 24 | #include "oiseau/dg/dg_integration.hpp" | ||
| 25 | #include "oiseau/dg/dg_space.hpp" | ||
| 26 | #include "oiseau/dg/equation.hpp" | ||
| 27 | #include "oiseau/dg/field.hpp" | ||
| 28 | #include "oiseau/mesh/mesh.hpp" | ||
| 29 | #include "oiseau/mesh/topology.hpp" | ||
| 30 | |||
| 31 | namespace oiseau::dg::solver { | ||
| 32 | template <std::size_t Dimension> | ||
| 33 | struct LinearDG<Dimension>::Impl { | ||
| 34 | struct FaceFluxOperators { | ||
| 35 | Normal normal{}; | ||
| 36 | std::vector<double> normal_flux_matrix; // [equation, field] | ||
| 37 | }; | ||
| 38 | |||
| 39 | struct CellFluxOperators { | ||
| 40 | std::vector<FaceFluxOperators> faces; | ||
| 41 | }; | ||
| 42 | |||
| 43 | struct NodalValues { | ||
| 44 | const double* data; | ||
| 45 | std::size_t node_count; | ||
| 46 | }; | ||
| 47 | |||
| 48 | struct FaceScratch { | ||
| 49 | 6160 | explicit FaceScratch(std::size_t fields) | |
| 50 |
4/8✓ Branch 1 taken 3080 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3080 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3080 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 3080 times.
✗ Branch 11 not taken.
|
55440 | : left(fields), right(fields), left_flux(fields), right_flux(fields) {} |
| 51 | |||
| 52 | std::vector<double> left; | ||
| 53 | std::vector<double> right; | ||
| 54 | std::vector<double> left_flux; | ||
| 55 | std::vector<double> right_flux; | ||
| 56 | }; | ||
| 57 | |||
| 58 | using ReferenceFlux = std::array<std::vector<double>, Dimension>; | ||
| 59 | |||
| 60 | std::shared_ptr<DGSpace> owned_space; | ||
| 61 | const DGSpace* space = nullptr; | ||
| 62 | DGConnectivity connectivity; | ||
| 63 | DGGeometry geometry; | ||
| 64 | DGIntegration integration; | ||
| 65 | EquationSystem equations; | ||
| 66 | NumericalFlux numerical_flux; | ||
| 67 | BoundaryState boundary_state; | ||
| 68 | std::array<std::vector<std::vector<double>>, Dimension> flux_matrices; | ||
| 69 | std::vector<CellFluxOperators> normal_flux_operators; | ||
| 70 | |||
| 71 | 20 | Impl(mesh::Mesh& mesh, const std::vector<unsigned>& orders, EquationSystem input_equations, | |
| 72 | NumericalFlux input_numerical_flux, BoundaryState input_boundary_state) | ||
| 73 | 20 | : owned_space(std::make_shared<DGSpace>(check_mesh_dimension(mesh), orders)), | |
| 74 | 20 | space(owned_space.get()), | |
| 75 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | connectivity(mesh), |
| 76 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | geometry(mesh), |
| 77 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | integration(mesh, *space, connectivity, geometry), |
| 78 | 40 | equations(std::move(input_equations)), | |
| 79 | 40 | numerical_flux(std::move(input_numerical_flux)), | |
| 80 | 40 | boundary_state(std::move(input_boundary_state)) { | |
| 81 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
|
20 | initialize_flux_matrices(); |
| 82 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | initialize_normal_flux_operators(); |
| 83 | 52 | } | |
| 84 | |||
| 85 | 6 | Impl(mesh::Mesh& mesh, const DGSpace& input_space, EquationSystem input_equations, | |
| 86 | NumericalFlux input_numerical_flux, BoundaryState input_boundary_state) | ||
| 87 | 6 | : space(&input_space), | |
| 88 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
6 | connectivity(check_mesh_dimension(mesh)), |
| 89 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | geometry(mesh), |
| 90 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | integration(mesh, input_space, connectivity, geometry), |
| 91 | 12 | equations(std::move(input_equations)), | |
| 92 | 12 | numerical_flux(std::move(input_numerical_flux)), | |
| 93 | 18 | boundary_state(std::move(input_boundary_state)) { | |
| 94 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | initialize_flux_matrices(); |
| 95 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
6 | initialize_normal_flux_operators(); |
| 96 | 6 | } | |
| 97 | |||
| 98 | 26 | static mesh::Mesh& check_mesh_dimension(mesh::Mesh& mesh) { | |
| 99 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
26 | if (mesh.topology().dimension() != static_cast<int>(Dimension)) { |
| 100 | ✗ | throw std::invalid_argument("LinearDG mesh dimension must match the solver dimension"); | |
| 101 | } | ||
| 102 | 26 | return mesh; | |
| 103 | } | ||
| 104 | |||
| 105 | 26 | void validate_flux_policy() const { | |
| 106 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
|
26 | if (!numerical_flux) { |
| 107 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("LinearDG requires a numerical flux policy"); |
| 108 | } | ||
| 109 | |||
| 110 | if constexpr (Dimension < 3) { | ||
| 111 |
3/4✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 10 times.
|
35 | if (std::ranges::any_of(equations.equations(), [](const Equation& equation) { |
| 112 | 42 | return std::ranges::any_of(equation.terms, [](const LinearTerm& term) { | |
| 113 |
1/2✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
|
46 | const auto unsupported = term.derivative == Derivative::Z || |
| 114 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21 times.
|
23 | (Dimension == 1 && term.derivative == Derivative::Y); |
| 115 |
5/8✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 21 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
|
29 | return unsupported && term.coefficient != 0.0; |
| 116 | 13 | }); | |
| 117 | })) { | ||
| 118 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("Equation derivative exceeds the LinearDG mesh dimension"); |
| 119 | } | ||
| 120 | } | ||
| 121 | 22 | } | |
| 122 | |||
| 123 | 26 | void initialize_flux_matrices() { | |
| 124 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 2 times.
|
26 | validate_flux_policy(); |
| 125 | 22 | const auto fields = equations.fields(); | |
| 126 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 11 times.
|
52 | for (auto& matrix : flux_matrices) { |
| 127 |
2/4✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
|
30 | matrix.assign(fields.size(), std::vector<double>(fields.size(), 0.0)); |
| 128 | } | ||
| 129 | 22 | constexpr std::array directions = {Derivative::X, Derivative::Y, Derivative::Z}; | |
| 130 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 11 times.
|
52 | for (std::size_t direction = 0; direction < Dimension; ++direction) { |
| 131 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 2 taken 15 times.
|
64 | for (std::size_t equation = 0; equation < fields.size(); ++equation) { |
| 132 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 17 times.
|
76 | for (std::size_t field = 0; field < fields.size(); ++field) { |
| 133 | 42 | flux_matrices[direction][equation][field] = | |
| 134 |
1/2✓ Branch 4 taken 21 times.
✗ Branch 5 not taken.
|
42 | equations.flux_coefficient(fields[equation], directions[direction], fields[field]); |
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | 22 | } | |
| 139 | |||
| 140 | 972 | void initialize_normal_flux_matrix(FaceFluxOperators& face, std::size_t fields) const { | |
| 141 | 972 | face.normal_flux_matrix.resize(fields * fields); | |
| 142 |
2/2✓ Branch 0 taken 490 times.
✓ Branch 1 taken 486 times.
|
1952 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 143 |
2/2✓ Branch 0 taken 498 times.
✓ Branch 1 taken 490 times.
|
1976 | for (std::size_t field = 0; field < fields; ++field) { |
| 144 |
2/2✓ Branch 0 taken 534 times.
✓ Branch 1 taken 498 times.
|
2064 | for (std::size_t direction = 0; direction < Dimension; ++direction) { |
| 145 | 1068 | face.normal_flux_matrix[equation * fields + field] += | |
| 146 | 1068 | face.normal[direction] * flux_matrices[direction][equation][field]; | |
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | 972 | } | |
| 151 | |||
| 152 | 22 | void initialize_normal_flux_operators() { | |
| 153 | 22 | const auto fields = equations.fields().size(); | |
| 154 |
1/2✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
|
22 | normal_flux_operators.reserve(integration.cells().size()); |
| 155 |
2/2✓ Branch 3 taken 239 times.
✓ Branch 4 taken 11 times.
|
522 | for (const auto& cell_rule : integration.cells()) { |
| 156 |
1/2✓ Branch 1 taken 239 times.
✗ Branch 2 not taken.
|
478 | auto& cell = normal_flux_operators.emplace_back(); |
| 157 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 237 times.
|
478 | if (!cell_rule.has_group()) continue; |
| 158 | |||
| 159 |
1/2✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
|
474 | cell.faces.reserve(cell_rule.faces.size()); |
| 160 |
2/2✓ Branch 2 taken 486 times.
✓ Branch 3 taken 237 times.
|
1920 | for (const auto& face_rule : cell_rule.faces) { |
| 161 |
1/2✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
|
972 | auto& face = cell.faces.emplace_back(); |
| 162 |
1/2✓ Branch 2 taken 486 times.
✗ Branch 3 not taken.
|
972 | std::copy_n(face_rule.outward_normal.begin(), Dimension, face.normal.begin()); |
| 163 |
1/2✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
|
972 | initialize_normal_flux_matrix(face, fields); |
| 164 | } | ||
| 165 | } | ||
| 166 | 22 | } | |
| 167 | |||
| 168 | 6222 | static void validate_state_strides(const xt::xarray<double>& values) { | |
| 169 | 6222 | const auto& shape = values.shape(); | |
| 170 | 6222 | const auto& strides = values.strides(); | |
| 171 |
4/4✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3105 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 5 times.
|
6222 | const auto wrong_field_stride = shape[2] > 1 && strides[2] != 1; |
| 172 |
3/4✓ Branch 1 taken 3111 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 3110 times.
|
6222 | const auto wrong_node_stride = shape[1] > 1 && std::cmp_not_equal(strides[1], shape[2]); |
| 173 | 6222 | const auto wrong_cell_stride = | |
| 174 |
3/4✓ Branch 1 taken 3075 times.
✓ Branch 2 taken 36 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3075 times.
|
6222 | shape[0] > 1 && std::cmp_not_equal(strides[0], shape[1] * shape[2]); |
| 175 |
4/6✓ Branch 0 taken 3110 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3110 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3110 times.
|
6222 | if (wrong_field_stride || wrong_node_stride || wrong_cell_stride) { |
| 176 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("DGState arrays must use contiguous row-major storage"); |
| 177 | } | ||
| 178 | 6220 | } | |
| 179 | |||
| 180 | 6190 | void validate_state(const State& state) const { | |
| 181 |
1/2✓ Branch 1 taken 3095 times.
✗ Branch 2 not taken.
|
6190 | const auto groups = space->element_groups(); |
| 182 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3094 times.
|
6190 | if (state.groups.size() != groups.size()) { |
| 183 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("DGState group count does not match DGSpace"); |
| 184 | } | ||
| 185 |
2/2✓ Branch 1 taken 3112 times.
✓ Branch 2 taken 3092 times.
|
12408 | for (std::size_t group_index = 0; group_index < groups.size(); ++group_index) { |
| 186 | 6224 | const auto& values = state.groups[group_index]; | |
| 187 | 6224 | const auto& nodes = groups[group_index].nodes; | |
| 188 |
1/2✓ Branch 5 taken 3111 times.
✗ Branch 6 not taken.
|
12446 | if (values.dimension() != 3 || values.shape()[0] != nodes.shape()[0] || |
| 189 |
6/8✓ Branch 0 taken 3111 times.
✓ Branch 1 taken 1 times.
✓ Branch 6 taken 3111 times.
✗ Branch 7 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3111 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 3111 times.
|
12446 | values.shape()[1] != nodes.shape()[1] || values.shape()[2] != equations.fields().size()) { |
| 190 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument( |
| 191 | "DGState arrays must have shape [group cells, group nodes, number of fields]"); | ||
| 192 | } | ||
| 193 |
2/2✓ Branch 1 taken 3110 times.
✓ Branch 2 taken 1 times.
|
6222 | validate_state_strides(values); |
| 194 | } | ||
| 195 | 6184 | } | |
| 196 | |||
| 197 | 6186 | State zero_state() const { | |
| 198 | 6186 | State state; | |
| 199 | 6186 | const auto fields = equations.fields().size(); | |
| 200 |
1/2✓ Branch 1 taken 3093 times.
✗ Branch 2 not taken.
|
6186 | const auto groups = space->element_groups(); |
| 201 |
1/2✓ Branch 2 taken 3093 times.
✗ Branch 3 not taken.
|
6186 | state.groups.reserve(groups.size()); |
| 202 |
2/2✓ Branch 2 taken 3111 times.
✓ Branch 3 taken 3093 times.
|
18594 | for (const auto& group : groups) { |
| 203 | 6222 | const auto& shape = group.nodes.shape(); | |
| 204 | 6222 | const std::array<std::size_t, 3> state_shape = {shape[0], shape[1], fields}; | |
| 205 |
1/2✓ Branch 2 taken 3111 times.
✗ Branch 3 not taken.
|
6222 | state.groups.emplace_back(xt::zeros<double>(state_shape)); |
| 206 | } | ||
| 207 | 6186 | return state; | |
| 208 | ✗ | } | |
| 209 | |||
| 210 | 1660 | static double sample_initial_condition(const InitialCondition& value, | |
| 211 | const xt::xarray<double>& nodes, std::size_t cell, | ||
| 212 | std::size_t node) { | ||
| 213 | if constexpr (Dimension == 1) { | ||
| 214 | 1360 | return value(nodes(cell, node, 0)); | |
| 215 | } else if constexpr (Dimension == 2) { | ||
| 216 | 272 | return value(nodes(cell, node, 0), nodes(cell, node, 1)); | |
| 217 | } else { | ||
| 218 | 28 | return value(nodes(cell, node, 0), nodes(cell, node, 1), nodes(cell, node, 2)); | |
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | 28 | void set_initial_condition(State& state, const Field& field, | |
| 223 | const InitialCondition& value) const { | ||
| 224 |
3/4✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
28 | if (!value) throw std::invalid_argument("Initial condition callback cannot be empty"); |
| 225 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 2 taken 1 times.
|
26 | validate_state(state); |
| 226 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | const auto field_index = equations.field_index(field); |
| 227 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
24 | const auto groups = space->element_groups(); |
| 228 |
2/2✓ Branch 1 taken 21 times.
✓ Branch 2 taken 12 times.
|
66 | for (std::size_t group_index = 0; group_index < groups.size(); ++group_index) { |
| 229 | 42 | const auto& nodes = groups[group_index].nodes; | |
| 230 |
2/2✓ Branch 2 taken 242 times.
✓ Branch 3 taken 21 times.
|
526 | for (std::size_t cell = 0; cell < nodes.shape()[0]; ++cell) { |
| 231 |
2/2✓ Branch 2 taken 830 times.
✓ Branch 3 taken 242 times.
|
2144 | for (std::size_t node = 0; node < nodes.shape()[1]; ++node) { |
| 232 |
1/2✓ Branch 1 taken 830 times.
✗ Branch 2 not taken.
|
1660 | state.groups[group_index](cell, node, field_index) = |
| 233 |
1/2✓ Branch 1 taken 830 times.
✗ Branch 2 not taken.
|
1660 | sample_initial_condition(value, nodes, cell, node); |
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | 24 | } | |
| 238 | |||
| 239 | 20 | State initial_state(const Field& field, const InitialCondition& value) const { | |
| 240 | 20 | auto state = zero_state(); | |
| 241 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | set_initial_condition(state, field, value); |
| 242 | 20 | return state; | |
| 243 | ✗ | } | |
| 244 | |||
| 245 | 1376394 | static NodalValues nodal_values(const State& state, const DGIntegration::CellRule& cell, | |
| 246 | std::size_t fields) { | ||
| 247 | 1376394 | const auto& group = state.groups[cell.group_index]; | |
| 248 | 1376394 | const auto node_count = group.shape()[1]; | |
| 249 | 1376394 | return {group.data() + cell.group_cell_index * node_count * fields, node_count}; | |
| 250 | } | ||
| 251 | |||
| 252 | 1376572 | std::array<double, Dimension> reference_flux_at_node( | |
| 253 | const double* node_values, std::size_t fields, std::size_t equation, | ||
| 254 | const std::array<double, 9>& inverse_jacobian) const { | ||
| 255 | 1376572 | std::array<double, Dimension> physical_flux{}; | |
| 256 |
2/2✓ Branch 0 taken 688450 times.
✓ Branch 1 taken 688286 times.
|
2753472 | for (std::size_t direction = 0; direction < Dimension; ++direction) { |
| 257 |
2/2✓ Branch 0 taken 688454 times.
✓ Branch 1 taken 688450 times.
|
2753808 | for (std::size_t field = 0; field < fields; ++field) { |
| 258 | 1376908 | physical_flux[direction] += flux_matrices[direction][equation][field] * node_values[field]; | |
| 259 | } | ||
| 260 | } | ||
| 261 | 1376572 | std::array<double, Dimension> transformed_flux{}; | |
| 262 |
2/2✓ Branch 0 taken 688450 times.
✓ Branch 1 taken 688286 times.
|
2753472 | for (std::size_t axis = 0; axis < Dimension; ++axis) { |
| 263 |
2/2✓ Branch 0 taken 688806 times.
✓ Branch 1 taken 688450 times.
|
2754512 | for (std::size_t direction = 0; direction < Dimension; ++direction) { |
| 264 | 1377612 | transformed_flux[axis] += inverse_jacobian[axis * 3 + direction] * physical_flux[direction]; | |
| 265 | } | ||
| 266 | } | ||
| 267 | 1376572 | return transformed_flux; | |
| 268 | } | ||
| 269 | |||
| 270 | 1376572 | static double flux_divergence(const ReferenceFlux& reference_flux, | |
| 271 | const std::array<const double*, 3>& derivatives, | ||
| 272 | std::size_t node_count, std::size_t fields, std::size_t test, | ||
| 273 | std::size_t equation) { | ||
| 274 | 1376572 | double value = 0.0; | |
| 275 |
2/2✓ Branch 0 taken 2066140 times.
✓ Branch 1 taken 688286 times.
|
5508852 | for (std::size_t trial = 0; trial < node_count; ++trial) { |
| 276 | 4132280 | const auto index = test * node_count + trial; | |
| 277 |
2/2✓ Branch 0 taken 2067996 times.
✓ Branch 1 taken 2066140 times.
|
8268272 | for (std::size_t axis = 0; axis < Dimension; ++axis) { |
| 278 | 4135992 | value += derivatives[axis][index] * reference_flux[axis][trial * fields + equation]; | |
| 279 | } | ||
| 280 | } | ||
| 281 | 1376572 | return value; | |
| 282 | } | ||
| 283 | |||
| 284 | 458786 | void set_volume_derivative(const DGIntegration::CellRule& cell, NodalValues values, | |
| 285 | std::size_t fields, double* derivative, | ||
| 286 | ReferenceFlux& reference_flux) const { | ||
| 287 | 458786 | const auto node_count = values.node_count; | |
| 288 | 458786 | const std::array derivatives = {cell.reference->derivative_r.data(), | |
| 289 | 458786 | cell.reference->derivative_s.data(), | |
| 290 | 458786 | cell.reference->derivative_t.data()}; | |
| 291 |
3/4✓ Branch 1 taken 229409 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 229409 times.
✓ Branch 4 taken 229393 times.
|
917604 | for (auto& flux : reference_flux) flux.resize(node_count * fields); |
| 292 | // The affine inverse Jacobian is constant, so transform the flux before differentiating. | ||
| 293 |
2/2✓ Branch 0 taken 688284 times.
✓ Branch 1 taken 229393 times.
|
1835354 | for (std::size_t trial = 0; trial < node_count; ++trial) { |
| 294 |
2/2✓ Branch 0 taken 688286 times.
✓ Branch 1 taken 688284 times.
|
2753140 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 295 | 1376572 | const auto flux = reference_flux_at_node(values.data + trial * fields, fields, equation, | |
| 296 | 1376572 | cell.inverse_jacobian); | |
| 297 |
2/2✓ Branch 0 taken 688450 times.
✓ Branch 1 taken 688286 times.
|
2753472 | for (std::size_t axis = 0; axis < Dimension; ++axis) { |
| 298 | 1376900 | reference_flux[axis][trial * fields + equation] = flux[axis]; | |
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 |
2/2✓ Branch 0 taken 688284 times.
✓ Branch 1 taken 229393 times.
|
1835354 | for (std::size_t test = 0; test < node_count; ++test) { |
| 303 |
2/2✓ Branch 0 taken 688286 times.
✓ Branch 1 taken 688284 times.
|
2753140 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 304 | 1376572 | derivative[test * fields + equation] = | |
| 305 | 1376572 | -flux_divergence(reference_flux, derivatives, node_count, fields, test, equation); | |
| 306 | } | ||
| 307 | } | ||
| 308 | 458786 | } | |
| 309 | |||
| 310 | 1823334 | static void interpolate_trace(std::vector<double>& trace, const double* basis, | |
| 311 | NodalValues values) { | ||
| 312 | 1823334 | const auto fields = trace.size(); | |
| 313 |
2/2✓ Branch 0 taken 2737258 times.
✓ Branch 1 taken 911667 times.
|
7297850 | for (std::size_t node = 0; node < values.node_count; ++node) { |
| 314 |
2/2✓ Branch 0 taken 2737262 times.
✓ Branch 1 taken 2737258 times.
|
10949040 | for (std::size_t field = 0; field < fields; ++field) { |
| 315 | 5474524 | trace[field] += basis[node] * values.data[node * fields + field]; | |
| 316 | } | ||
| 317 | } | ||
| 318 | 1823334 | } | |
| 319 | |||
| 320 | 917944 | std::vector<double> face_flux_correction(const FaceFluxOperators& operators, | |
| 321 | FaceScratch& scratch) const { | ||
| 322 | 917944 | const auto fields = scratch.left.size(); | |
| 323 |
2/2✓ Branch 0 taken 458974 times.
✓ Branch 1 taken 458972 times.
|
1835892 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 324 | 917948 | const auto* normal_matrix = operators.normal_flux_matrix.data() + equation * fields; | |
| 325 | 917948 | double interior_flux = 0.0; | |
| 326 | 917948 | double exterior_flux = 0.0; | |
| 327 |
2/2✓ Branch 0 taken 458978 times.
✓ Branch 1 taken 458974 times.
|
1835904 | for (std::size_t field = 0; field < fields; ++field) { |
| 328 | 917956 | interior_flux += normal_matrix[field] * scratch.left[field]; | |
| 329 | 917956 | exterior_flux += normal_matrix[field] * scratch.right[field]; | |
| 330 | } | ||
| 331 | 917948 | scratch.left_flux[equation] = interior_flux; | |
| 332 | 917948 | scratch.right_flux[equation] = exterior_flux; | |
| 333 | } | ||
| 334 |
1/2✓ Branch 3 taken 458972 times.
✗ Branch 4 not taken.
|
917944 | auto correction = numerical_flux(scratch.left, scratch.right, operators.normal, |
| 335 | 917944 | scratch.left_flux, scratch.right_flux); | |
| 336 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 458971 times.
|
917944 | if (correction.size() != fields) { |
| 337 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("Numerical flux must return one value per equation field"); |
| 338 | } | ||
| 339 |
2/2✓ Branch 0 taken 458973 times.
✓ Branch 1 taken 458971 times.
|
1835888 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 340 | 917946 | correction[equation] -= scratch.left_flux[equation]; | |
| 341 | } | ||
| 342 | 917942 | return correction; | |
| 343 | 2 | } | |
| 344 | |||
| 345 | 917608 | void add_face_derivative(const DGIntegration::FaceRule& face, const FaceFluxOperators& operators, | |
| 346 | NodalValues cell, NodalValues neighbor, double* derivative, | ||
| 347 | FaceScratch& scratch) const { | ||
| 348 | 917608 | const auto fields = scratch.left.size(); | |
| 349 | 917608 | const auto node_count = cell.node_count; | |
| 350 | 917608 | const auto face_points = face.point_count(); | |
| 351 |
2/2✓ Branch 1 taken 458973 times.
✓ Branch 2 taken 458802 times.
|
1835550 | for (std::size_t q = 0; q < face_points; ++q) { |
| 352 |
1/2✓ Branch 1 taken 458973 times.
✗ Branch 2 not taken.
|
917946 | std::ranges::fill(scratch.left, 0.0); |
| 353 |
1/2✓ Branch 1 taken 458973 times.
✗ Branch 2 not taken.
|
917946 | std::ranges::fill(scratch.right, 0.0); |
| 354 | 917946 | interpolate_trace(scratch.left, face.minus->trace.data() + q * node_count, cell); | |
| 355 |
2/2✓ Branch 1 taken 6279 times.
✓ Branch 2 taken 452694 times.
|
917946 | if (face.is_boundary()) { |
| 356 | 12558 | scratch.right = | |
| 357 |
4/6✓ Branch 1 taken 6147 times.
✓ Branch 2 taken 132 times.
✓ Branch 5 taken 6147 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 132 times.
✗ Branch 9 not taken.
|
25116 | boundary_state ? boundary_state(scratch.left, operators.normal) : scratch.left; |
| 358 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6278 times.
|
12558 | if (scratch.right.size() != fields) { |
| 359 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument( |
| 360 | "Boundary state must return one value for every equation field"); | ||
| 361 | } | ||
| 362 | } else { | ||
| 363 | 905388 | interpolate_trace(scratch.right, face.plus->trace.data() + q * neighbor.node_count, | |
| 364 | neighbor); | ||
| 365 | } | ||
| 366 | |||
| 367 |
2/2✓ Branch 1 taken 458971 times.
✓ Branch 2 taken 1 times.
|
917944 | const auto correction = face_flux_correction(operators, scratch); |
| 368 | 917942 | const auto* lift = face.minus->lift.data() + q * node_count; | |
| 369 |
2/2✓ Branch 0 taken 1378528 times.
✓ Branch 1 taken 458971 times.
|
3674998 | for (std::size_t test = 0; test < node_count; ++test) { |
| 370 |
2/2✓ Branch 0 taken 1378532 times.
✓ Branch 1 taken 1378528 times.
|
5514120 | for (std::size_t equation = 0; equation < fields; ++equation) { |
| 371 | 2757064 | derivative[test * fields + equation] -= | |
| 372 | 2757064 | face.lift_scale * lift[test] * correction[equation]; | |
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | 917604 | } | |
| 377 | |||
| 378 | 6164 | State rhs(const State& state) const { | |
| 379 |
2/2✓ Branch 1 taken 3080 times.
✓ Branch 2 taken 2 times.
|
6164 | validate_state(state); |
| 380 |
1/2✓ Branch 1 taken 3080 times.
✗ Branch 2 not taken.
|
6160 | auto output = zero_state(); |
| 381 | 6160 | const auto fields = equations.fields().size(); | |
| 382 |
1/2✓ Branch 1 taken 3080 times.
✗ Branch 2 not taken.
|
6160 | FaceScratch scratch(fields); |
| 383 | 6160 | ReferenceFlux reference_flux; | |
| 384 | |||
| 385 | 6160 | const auto cell_rules = integration.cells(); | |
| 386 |
2/2✓ Branch 1 taken 229397 times.
✓ Branch 2 taken 3078 times.
|
464950 | for (std::size_t cell_index = 0; cell_index < cell_rules.size(); ++cell_index) { |
| 387 | 458794 | const auto& cell = cell_rules[cell_index]; | |
| 388 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 229393 times.
|
458794 | if (!cell.has_group()) continue; |
| 389 | 458786 | const auto values = nodal_values(state, cell, fields); | |
| 390 | 458786 | auto* derivative = output.groups[cell.group_index].data() + | |
| 391 | 458786 | cell.group_cell_index * values.node_count * fields; | |
| 392 |
1/2✓ Branch 1 taken 229393 times.
✗ Branch 2 not taken.
|
458786 | set_volume_derivative(cell, values, fields, derivative, reference_flux); |
| 393 | |||
| 394 | 458786 | const auto& operators = normal_flux_operators[cell_index]; | |
| 395 |
2/2✓ Branch 1 taken 458804 times.
✓ Branch 2 taken 229391 times.
|
1376390 | for (std::size_t face_index = 0; face_index < cell.faces.size(); ++face_index) { |
| 396 | 917608 | const auto& face = cell.faces[face_index]; | |
| 397 |
3/4✓ Branch 1 taken 6178 times.
✓ Branch 2 taken 452626 times.
✓ Branch 4 taken 452626 times.
✗ Branch 5 not taken.
|
917608 | const auto& neighbor = face.is_boundary() ? cell : integration.cell(face.neighbor_cell); |
| 398 |
2/2✓ Branch 3 taken 458802 times.
✓ Branch 4 taken 2 times.
|
917608 | add_face_derivative(face, operators.faces[face_index], values, |
| 399 | nodal_values(state, neighbor, fields), derivative, scratch); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | 6156 | return output; | |
| 403 | 6168 | } | |
| 404 | }; | ||
| 405 | |||
| 406 | template <std::size_t Dimension> | ||
| 407 | 14 | LinearDG<Dimension>::LinearDG(mesh::Mesh& mesh, const std::vector<unsigned>& orders, | |
| 408 | EquationSystem equations, WaveSpeed wave_speed, | ||
| 409 | BoundaryState boundary_state) | ||
| 410 |
1/2✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
32 | : LinearDG(mesh, orders, std::move(equations), rusanov(std::move(wave_speed)), |
| 411 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
|
44 | std::move(boundary_state)) {} |
| 412 | |||
| 413 | template <std::size_t Dimension> | ||
| 414 | 20 | LinearDG<Dimension>::LinearDG(mesh::Mesh& mesh, const std::vector<unsigned>& orders, | |
| 415 | EquationSystem equations, NumericalFlux numerical_flux, | ||
| 416 | BoundaryState boundary_state) | ||
| 417 | 40 | : m_impl(std::make_unique<Impl>(mesh, orders, std::move(equations), std::move(numerical_flux), | |
| 418 | 20 | std::move(boundary_state))) {} | |
| 419 | |||
| 420 | template <std::size_t Dimension> | ||
| 421 | 6 | LinearDG<Dimension>::LinearDG(mesh::Mesh& mesh, const DGSpace& space, EquationSystem equations, | |
| 422 | WaveSpeed wave_speed, BoundaryState boundary_state) | ||
| 423 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
12 | : LinearDG(mesh, space, std::move(equations), rusanov(std::move(wave_speed)), |
| 424 |
1/2✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
18 | std::move(boundary_state)) {} |
| 425 | |||
| 426 | template <std::size_t Dimension> | ||
| 427 | 6 | LinearDG<Dimension>::LinearDG(mesh::Mesh& mesh, const DGSpace& space, EquationSystem equations, | |
| 428 | NumericalFlux numerical_flux, BoundaryState boundary_state) | ||
| 429 | 12 | : m_impl(std::make_unique<Impl>(mesh, space, std::move(equations), std::move(numerical_flux), | |
| 430 | 6 | std::move(boundary_state))) {} | |
| 431 | |||
| 432 | template <std::size_t Dimension> | ||
| 433 | 22 | LinearDG<Dimension>::~LinearDG() = default; | |
| 434 | |||
| 435 | template <std::size_t Dimension> | ||
| 436 | ✗ | LinearDG<Dimension>::LinearDG(LinearDG&&) noexcept = default; | |
| 437 | |||
| 438 | template <std::size_t Dimension> | ||
| 439 | ✗ | LinearDG<Dimension>& LinearDG<Dimension>::operator=(LinearDG&&) noexcept = default; | |
| 440 | |||
| 441 | template <std::size_t Dimension> | ||
| 442 | 8 | const DGSpace& LinearDG<Dimension>::space() const { | |
| 443 | 8 | return *m_impl->space; | |
| 444 | } | ||
| 445 | |||
| 446 | template <std::size_t Dimension> | ||
| 447 | ✗ | const EquationSystem& LinearDG<Dimension>::equations() const { | |
| 448 | ✗ | return m_impl->equations; | |
| 449 | } | ||
| 450 | |||
| 451 | template <std::size_t Dimension> | ||
| 452 | 20 | auto LinearDG<Dimension>::rusanov(WaveSpeed wave_speed) -> NumericalFlux { | |
| 453 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
20 | if (!wave_speed) throw std::invalid_argument("Rusanov flux requires a wave-speed bound"); |
| 454 | 459009 | return [wave_speed = std::move(wave_speed)]( | |
| 455 | std::span<const double> left, std::span<const double> right, const Normal& normal, | ||
| 456 | std::span<const double> left_flux, std::span<const double> right_flux) { | ||
| 457 |
12/24✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 53 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 53 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 53 times.
✓ Branch 14 taken 164 times.
✗ Branch 15 not taken.
✓ Branch 18 taken 164 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 164 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 164 times.
✓ Branch 26 taken 458752 times.
✗ Branch 27 not taken.
✓ Branch 30 taken 458752 times.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 458752 times.
✗ Branch 34 not taken.
✓ Branch 35 taken 458752 times.
|
917938 | if (left.size() != right.size() || left.size() != left_flux.size() || |
| 458 | 458969 | left.size() != right_flux.size()) { | |
| 459 | ✗ | throw std::invalid_argument("Rusanov flux state and physical flux sizes must match"); | |
| 460 | } | ||
| 461 | 458969 | const auto speed = wave_speed(normal); | |
| 462 |
9/18✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 53 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 53 times.
✓ Branch 8 taken 164 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 164 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 164 times.
✓ Branch 15 taken 458752 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 458752 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 458752 times.
|
458969 | if (!std::isfinite(speed) || speed < 0.0) { |
| 463 | ✗ | throw std::invalid_argument("Rusanov wave-speed bound must be finite and nonnegative"); | |
| 464 | } | ||
| 465 |
3/6✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 164 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 458752 times.
✗ Branch 11 not taken.
|
458969 | std::vector<double> flux(left.size()); |
| 466 |
6/6✓ Branch 1 taken 53 times.
✓ Branch 2 taken 53 times.
✓ Branch 4 taken 164 times.
✓ Branch 5 taken 164 times.
✓ Branch 7 taken 458752 times.
✓ Branch 8 taken 458752 times.
|
917938 | for (std::size_t field = 0; field < flux.size(); ++field) { |
| 467 | 458969 | flux[field] = | |
| 468 | 458969 | 0.5 * (left_flux[field] + right_flux[field]) - 0.5 * speed * (right[field] - left[field]); | |
| 469 | } | ||
| 470 | 458969 | return flux; | |
| 471 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
40 | }; |
| 472 | } | ||
| 473 | |||
| 474 | template <std::size_t Dimension> | ||
| 475 | 6 | auto LinearDG<Dimension>::zero_state() const -> State { | |
| 476 | 6 | return m_impl->zero_state(); | |
| 477 | } | ||
| 478 | |||
| 479 | template <std::size_t Dimension> | ||
| 480 | 20 | auto LinearDG<Dimension>::initial_state(const Field& field, const InitialCondition& value) const | |
| 481 | -> State { | ||
| 482 | 20 | return m_impl->initial_state(field, value); | |
| 483 | } | ||
| 484 | |||
| 485 | template <std::size_t Dimension> | ||
| 486 | 8 | void LinearDG<Dimension>::set_initial_condition(State& state, const Field& field, | |
| 487 | const InitialCondition& value) const { | ||
| 488 | 8 | m_impl->set_initial_condition(state, field, value); | |
| 489 | 4 | } | |
| 490 | |||
| 491 | template <std::size_t Dimension> | ||
| 492 | 6164 | auto LinearDG<Dimension>::rhs(const State& state) const -> State { | |
| 493 | 6164 | return m_impl->rhs(state); | |
| 494 | } | ||
| 495 | |||
| 496 | template class LinearDG<1>; | ||
| 497 | template class LinearDG<2>; | ||
| 498 | template class LinearDG<3>; | ||
| 499 | |||
| 500 | } // namespace oiseau::dg::solver | ||
| 501 |