GCC Code Coverage Report


Directory: src/oiseau/
File: src/oiseau/time/rk4.hpp
Date: 2026-11-23 22:30:49
Exec Total Coverage
Lines: 25 25 100.0%
Functions: 10 18 55.6%
Branches: 23 36 63.9%

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 <cmath>
10 #include <stdexcept>
11 #include <utility>
12
13 #include "oiseau/time/detail/rhs.hpp"
14 #include "oiseau/time/integrator.hpp"
15
16 namespace oiseau::time {
17
18 /// One classical RK4 step with caller-provided RHS and state combination.
19 template <typename State, typename Rhs, typename AddScaled>
20 822 void rk4_step(State& state, double time, double time_step, Rhs&& rhs, AddScaled&& add_scaled_fn) {
21
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 793 times.
822 if (!std::isfinite(time)) {
22
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 throw std::invalid_argument("Time must be finite");
23 }
24
6/6
✓ Branch 1 taken 791 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 789 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 789 times.
818 if (!std::isfinite(time_step) || time_step <= 0.0) {
25
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 throw std::invalid_argument("Time step must be finite and positive");
26 }
27
28
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto k1 = rhs(time, state);
29
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto stage2 = add_scaled_fn(state, k1, 0.5 * time_step);
30
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto k2 = rhs(time + 0.5 * time_step, stage2);
31
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto stage3 = add_scaled_fn(state, k2, 0.5 * time_step);
32
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto k3 = rhs(time + 0.5 * time_step, stage3);
33
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto stage4 = add_scaled_fn(state, k3, time_step);
34
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 const auto k4 = rhs(time + time_step, stage4);
35
36
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 auto output = add_scaled_fn(state, k1, time_step / 6.0);
37
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 output = add_scaled_fn(output, k2, time_step / 3.0);
38
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 output = add_scaled_fn(output, k3, time_step / 3.0);
39
1/2
✓ Branch 1 taken 778 times.
✗ Branch 2 not taken.
810 output = add_scaled_fn(output, k4, time_step / 6.0);
40 810 state = std::move(output);
41 810 }
42
43 /// Classical fourth-order Runge-Kutta integrator.
44 class RK4 {
45 public:
46 template <typename System, AddScaledState State>
47 822 static void step(System& system, State& state, double time, double time_step) {
48 822 rk4_step(
49 state, time, time_step,
50
2/2
✓ Branch 1 taken 789 times.
✓ Branch 2 taken 6 times.
906 [&system](double stage_time, const State& value) {
51 3156 return detail::evaluate_rhs(system, stage_time, value);
52 },
53 6345 [](const State& base, const State& increment, double scale) {
54 5523 return add_scaled(base, increment, scale);
55 });
56 810 }
57 };
58
59 } // namespace oiseau::time
60