| 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 | #pragma once | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <cstddef> | ||
| 12 | #include <initializer_list> | ||
| 13 | #include <span> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <utility> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | #include "oiseau/dg/field.hpp" | ||
| 19 | |||
| 20 | namespace oiseau::dg { | ||
| 21 | |||
| 22 | /// Derivatives currently represented by the first-order equation DSL. | ||
| 23 | enum class Derivative { Time, X, Y, Z }; | ||
| 24 | |||
| 25 | struct DifferentialTerm { | ||
| 26 | Field field; | ||
| 27 | Derivative derivative = Derivative::Time; | ||
| 28 | }; | ||
| 29 | |||
| 30 | struct LinearTerm { | ||
| 31 | Field field; | ||
| 32 | Derivative derivative = Derivative::Time; | ||
| 33 | double coefficient = 0.0; | ||
| 34 | }; | ||
| 35 | |||
| 36 | /// A linear combination of scalar fields and their first derivatives. | ||
| 37 | class LinearExpression { | ||
| 38 | public: | ||
| 39 | LinearExpression() = default; | ||
| 40 | 70 | explicit LinearExpression(DifferentialTerm term) | |
| 41 |
3/6✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
✓ Branch 4 taken 70 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
210 | : m_terms{ |
| 42 |
1/8✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
210 | {.field = std::move(term.field), .derivative = term.derivative, .coefficient = 1.0}} {} |
| 43 | |||
| 44 | 122 | const std::vector<LinearTerm>& terms() const { return m_terms; } | |
| 45 | |||
| 46 | private: | ||
| 47 | 2 | explicit LinearExpression(std::vector<LinearTerm> terms) : m_terms(std::move(terms)) {} | |
| 48 | |||
| 49 | std::vector<LinearTerm> m_terms; | ||
| 50 | |||
| 51 | friend LinearExpression operator+(const LinearExpression& lhs, const LinearExpression& rhs); | ||
| 52 | friend LinearExpression operator+(LinearExpression&& lhs, const LinearExpression& rhs); | ||
| 53 | friend LinearExpression operator-(LinearExpression lhs, const LinearExpression& rhs); | ||
| 54 | friend LinearExpression operator-(LinearExpression expression); | ||
| 55 | friend LinearExpression operator*(double coefficient, LinearExpression expression); | ||
| 56 | }; | ||
| 57 | |||
| 58 | 31 | inline DifferentialTerm dt(const Field& field) { | |
| 59 | 31 | return {.field = field, .derivative = Derivative::Time}; | |
| 60 |
1/2✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
|
31 | } |
| 61 | 25 | inline DifferentialTerm dx(const Field& field) { | |
| 62 | 25 | return {.field = field, .derivative = Derivative::X}; | |
| 63 |
1/2✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
|
25 | } |
| 64 | 12 | inline DifferentialTerm dy(const Field& field) { | |
| 65 | 12 | return {.field = field, .derivative = Derivative::Y}; | |
| 66 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | } |
| 67 | 2 | inline DifferentialTerm dz(const Field& field) { | |
| 68 | 2 | return {.field = field, .derivative = Derivative::Z}; | |
| 69 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | } |
| 70 | |||
| 71 | 1 | inline LinearExpression operator+(const LinearExpression& lhs, const LinearExpression& rhs) { | |
| 72 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | auto terms = lhs.terms(); |
| 73 |
1/2✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
2 | terms.insert(terms.end(), rhs.terms().begin(), rhs.terms().end()); |
| 74 | 2 | return LinearExpression(std::move(terms)); | |
| 75 | 1 | } | |
| 76 | |||
| 77 | 39 | inline LinearExpression operator+(LinearExpression&& lhs, const LinearExpression& rhs) { | |
| 78 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38 times.
|
39 | if (&lhs == &rhs) { |
| 79 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | const auto terms = rhs.terms(); |
| 80 |
1/2✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
|
2 | lhs.m_terms.insert(lhs.m_terms.end(), terms.begin(), terms.end()); |
| 81 | 1 | return std::move(lhs); | |
| 82 | 1 | } | |
| 83 |
1/2✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
|
76 | lhs.m_terms.insert(lhs.m_terms.end(), rhs.terms().begin(), rhs.terms().end()); |
| 84 | 38 | return std::move(lhs); | |
| 85 | } | ||
| 86 | |||
| 87 | 47 | inline LinearExpression operator-(LinearExpression expression) { | |
| 88 |
2/2✓ Branch 2 taken 55 times.
✓ Branch 3 taken 47 times.
|
204 | for (auto& term : expression.m_terms) term.coefficient = -term.coefficient; |
| 89 | 47 | return expression; | |
| 90 | } | ||
| 91 | |||
| 92 | 35 | inline LinearExpression operator-(LinearExpression lhs, const LinearExpression& rhs) { | |
| 93 |
2/4✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 35 times.
✗ Branch 6 not taken.
|
70 | return std::move(lhs) + (-rhs); |
| 94 | } | ||
| 95 | |||
| 96 | 12 | inline LinearExpression operator-(DifferentialTerm term) { | |
| 97 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | return -LinearExpression(std::move(term)); |
| 98 | } | ||
| 99 | |||
| 100 | 14 | inline LinearExpression operator*(double coefficient, LinearExpression expression) { | |
| 101 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
|
14 | if (!std::isfinite(coefficient)) { |
| 102 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("Equation coefficient must be finite"); |
| 103 | } | ||
| 104 |
2/2✓ Branch 2 taken 13 times.
✓ Branch 3 taken 13 times.
|
52 | for (auto& term : expression.m_terms) term.coefficient *= coefficient; |
| 105 | 13 | return expression; | |
| 106 | } | ||
| 107 | |||
| 108 | inline LinearExpression operator*(LinearExpression expression, double coefficient) { | ||
| 109 | return coefficient * std::move(expression); | ||
| 110 | } | ||
| 111 | |||
| 112 | 14 | inline LinearExpression operator*(double coefficient, DifferentialTerm term) { | |
| 113 |
3/4✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 1 times.
|
16 | return coefficient * LinearExpression(std::move(term)); |
| 114 | } | ||
| 115 | |||
| 116 | inline LinearExpression operator*(DifferentialTerm term, double coefficient) { | ||
| 117 | return coefficient * LinearExpression(std::move(term)); | ||
| 118 | } | ||
| 119 | |||
| 120 | 2 | inline LinearExpression operator+(DifferentialTerm lhs, DifferentialTerm rhs) { | |
| 121 |
3/6✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
|
4 | return LinearExpression(std::move(lhs)) + LinearExpression(std::move(rhs)); |
| 122 | } | ||
| 123 | |||
| 124 | 1 | inline LinearExpression operator-(DifferentialTerm lhs, DifferentialTerm rhs) { | |
| 125 |
3/6✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
|
2 | return LinearExpression(std::move(lhs)) - LinearExpression(std::move(rhs)); |
| 126 | } | ||
| 127 | |||
| 128 | 1 | inline LinearExpression operator+(const LinearExpression& expression, DifferentialTerm term) { | |
| 129 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | return expression + LinearExpression(std::move(term)); |
| 130 | } | ||
| 131 | |||
| 132 | inline LinearExpression operator+(DifferentialTerm term, const LinearExpression& expression) { | ||
| 133 | return LinearExpression(std::move(term)) + expression; | ||
| 134 | } | ||
| 135 | |||
| 136 | 1 | inline LinearExpression operator-(const LinearExpression& expression, DifferentialTerm term) { | |
| 137 |
3/6✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
1 | return expression - LinearExpression(std::move(term)); |
| 138 | } | ||
| 139 | |||
| 140 | inline LinearExpression operator-(DifferentialTerm term, const LinearExpression& expression) { | ||
| 141 | return LinearExpression(std::move(term)) - expression; | ||
| 142 | } | ||
| 143 | |||
| 144 | struct Equation { | ||
| 145 | /// Terms normalized as `left-hand side - right-hand side == 0`. | ||
| 146 | std::vector<LinearTerm> terms; | ||
| 147 | }; | ||
| 148 | |||
| 149 | 29 | inline Equation operator==(const LinearExpression& lhs, const LinearExpression& rhs) { | |
| 150 |
3/6✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
|
58 | return Equation{(lhs - rhs).terms()}; |
| 151 | } | ||
| 152 | |||
| 153 | 11 | inline Equation operator==(DifferentialTerm lhs, DifferentialTerm rhs) { | |
| 154 |
3/6✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 11 times.
✗ Branch 10 not taken.
|
22 | return LinearExpression(std::move(lhs)) == LinearExpression(std::move(rhs)); |
| 155 | } | ||
| 156 | |||
| 157 | 3 | inline Equation operator==(const LinearExpression& lhs, DifferentialTerm rhs) { | |
| 158 |
2/4✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | return lhs == LinearExpression(std::move(rhs)); |
| 159 | } | ||
| 160 | |||
| 161 | 11 | inline Equation operator==(DifferentialTerm lhs, const LinearExpression& rhs) { | |
| 162 |
2/4✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 11 times.
✗ Branch 6 not taken.
|
11 | return LinearExpression(std::move(lhs)) == rhs; |
| 163 | } | ||
| 164 | |||
| 165 | /// A linear, first-order evolution system with one time derivative per field. | ||
| 166 | /// | ||
| 167 | /// Each equation is normalized internally as `lhs - rhs == 0`. For a | ||
| 168 | /// conservative system `du/dt + div(F) = 0`, `flux_coefficient` returns the | ||
| 169 | /// coefficient of a field in the corresponding directional flux. | ||
| 170 | /// | ||
| 171 | /// For example: | ||
| 172 | /// ```cpp | ||
| 173 | /// Field Ez{"Ez"}, Hx{"Hx"}, Hy{"Hy"}; | ||
| 174 | /// EquationSystem tm({Ez, Hx, Hy}, { | ||
| 175 | /// epsilon * dt(Ez) == dx(Hy) - dy(Hx), | ||
| 176 | /// mu * dt(Hx) == -dy(Ez), | ||
| 177 | /// mu * dt(Hy) == dx(Ez), | ||
| 178 | /// }); | ||
| 179 | /// ``` | ||
| 180 | class EquationSystem { | ||
| 181 | public: | ||
| 182 | 20 | EquationSystem(std::initializer_list<Field> fields, std::initializer_list<Equation> equations) | |
| 183 |
4/6✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13 times.
✓ Branch 8 taken 7 times.
|
100 | : EquationSystem(std::vector<Field>(fields), std::vector<Equation>(equations)) {} |
| 184 | |||
| 185 | 23 | EquationSystem(std::vector<Field> fields, std::vector<Equation> equations) | |
| 186 | 69 | : m_fields(std::move(fields)), m_equations(std::move(equations)) { | |
| 187 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 10 times.
|
23 | validate(); |
| 188 | 43 | } | |
| 189 | |||
| 190 | 9306 | std::span<const Field> fields() const { return m_fields; } | |
| 191 | 11 | std::span<const Equation> equations() const { return m_equations; } | |
| 192 | |||
| 193 | /// Position of a field in states whose last axis follows `fields()` order. | ||
| 194 | 192 | std::size_t field_index(const Field& field) const { | |
| 195 | 192 | const auto found = std::ranges::find_if( | |
| 196 |
1/2✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
|
464 | m_fields, [&field](const Field& candidate) { return candidate.same_as(field); }); |
| 197 |
2/2✓ Branch 1 taken 188 times.
✓ Branch 2 taken 4 times.
|
572 | if (found != m_fields.end()) return static_cast<std::size_t>(found - m_fields.begin()); |
| 198 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | throw std::invalid_argument("Equation references field '" + field.name() + |
| 199 |
2/4✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
|
12 | "' that is not declared in the system"); |
| 200 | } | ||
| 201 | |||
| 202 | /// Coefficient in the conservative flux for `derivative` in this equation. | ||
| 203 | 47 | double flux_coefficient(const Field& equation_field, Derivative derivative, | |
| 204 | const Field& flux_field) const { | ||
| 205 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 46 times.
|
47 | if (derivative == Derivative::Time) { |
| 206 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("A time derivative is not a spatial flux direction"); |
| 207 | } | ||
| 208 | 46 | const auto equation_index = field_index(equation_field); | |
| 209 | 45 | (void)field_index(flux_field); | |
| 210 | 44 | const auto& equation = m_equations[m_equation_for_field[equation_index]]; | |
| 211 | 44 | double time_coefficient = 0.0; | |
| 212 | 44 | double spatial_coefficient = 0.0; | |
| 213 |
2/2✓ Branch 2 taken 109 times.
✓ Branch 3 taken 44 times.
|
197 | for (const auto& term : equation.terms) { |
| 214 |
5/6✓ Branch 0 taken 44 times.
✓ Branch 1 taken 65 times.
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 44 times.
✓ Branch 6 taken 65 times.
|
109 | if (term.derivative == Derivative::Time && term.field.same_as(equation_field)) { |
| 215 | 44 | time_coefficient += term.coefficient; | |
| 216 |
6/6✓ Branch 0 taken 39 times.
✓ Branch 1 taken 26 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 27 times.
✓ Branch 6 taken 38 times.
|
65 | } else if (term.derivative == derivative && term.field.same_as(flux_field)) { |
| 217 | 27 | spatial_coefficient += term.coefficient; | |
| 218 | } | ||
| 219 | } | ||
| 220 | 44 | return spatial_coefficient / time_coefficient; | |
| 221 | } | ||
| 222 | |||
| 223 | private: | ||
| 224 | 23 | void validate() { | |
| 225 |
5/6✓ Branch 1 taken 22 times.
✓ Branch 2 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 22 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 22 times.
|
23 | if (m_fields.empty() || m_fields.size() < m_equations.size()) { |
| 226 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("EquationSystem requires one equation for each declared field"); |
| 227 | } | ||
| 228 | 22 | const std::span<const Field> declared_fields(m_fields); | |
| 229 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 21 times.
|
52 | for (std::size_t i = 0; i < m_fields.size(); ++i) { |
| 230 |
3/4✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 30 times.
|
31 | if (std::ranges::any_of(declared_fields.subspan(i + 1), [this, i](const Field& candidate) { |
| 231 | 11 | return m_fields[i].same_as(candidate); | |
| 232 | })) { | ||
| 233 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("EquationSystem contains a field more than once"); |
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 |
1/2✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | std::vector<bool> has_evolution_equation(m_fields.size(), false); |
| 238 |
1/2✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
21 | m_equation_for_field.assign(m_fields.size(), 0); |
| 239 |
2/2✓ Branch 1 taken 27 times.
✓ Branch 2 taken 15 times.
|
42 | for (std::size_t equation_index = 0; equation_index < m_equations.size(); ++equation_index) { |
| 240 | 27 | const auto& equation = m_equations[equation_index]; | |
| 241 | 27 | std::size_t time_term_count = 0; | |
| 242 | 27 | const Field* target = nullptr; | |
| 243 |
2/2✓ Branch 2 taken 62 times.
✓ Branch 3 taken 25 times.
|
114 | for (const auto& term : equation.terms) { |
| 244 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 61 times.
|
62 | if (!std::isfinite(term.coefficient)) { |
| 245 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | throw std::invalid_argument("Equation coefficient must be finite"); |
| 246 | } | ||
| 247 |
2/2✓ Branch 1 taken 60 times.
✓ Branch 2 taken 1 times.
|
61 | (void)field_index(term.field); |
| 248 |
4/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 1 times.
|
60 | if (term.derivative == Derivative::Time && term.coefficient != 0.0) { |
| 249 | 25 | ++time_term_count; | |
| 250 | 25 | target = &term.field; | |
| 251 | } | ||
| 252 | } | ||
| 253 |
3/4✓ Branch 0 taken 22 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
|
25 | if (time_term_count != 1 || target == nullptr) { |
| 254 | throw std::invalid_argument( | ||
| 255 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | "Each equation must contain exactly one nonzero time derivative term"); |
| 256 | } | ||
| 257 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | const auto target_index = field_index(*target); |
| 258 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 21 times.
|
22 | if (has_evolution_equation[target_index]) { |
| 259 | 1 | throw std::invalid_argument("EquationSystem has multiple evolution equations for field '" + | |
| 260 |
3/6✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
|
2 | target->name() + "'"); |
| 261 | } | ||
| 262 | 21 | has_evolution_equation[target_index] = true; | |
| 263 | 21 | m_equation_for_field[target_index] = equation_index; | |
| 264 | } | ||
| 265 |
3/4✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 13 times.
|
15 | if (std::ranges::find(has_evolution_equation, false) != has_evolution_equation.end()) { |
| 266 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | throw std::invalid_argument("EquationSystem is missing an evolution equation for a field"); |
| 267 | } | ||
| 268 | 21 | } | |
| 269 | |||
| 270 | std::vector<Field> m_fields; | ||
| 271 | std::vector<Equation> m_equations; | ||
| 272 | std::vector<std::size_t> m_equation_for_field; | ||
| 273 | }; | ||
| 274 | |||
| 275 | } // namespace oiseau::dg | ||
| 276 |