CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
evolver_erk.h
1#ifndef __EVOERK__
2#define __EVOERK__
3
36 static constexpr const char* kName = "rkdp45";
37 static constexpr int kStages = 7;
38 static constexpr int kOrder = 5;
39 static constexpr int kDenseDeg = 4;
40 static constexpr bool kFsal = true;
41
42 // clang-format off
43 static constexpr double c[7] = {0.0, 0.2, 0.3, 0.8, 8.0 / 9.0, 1.0, 1.0};
44
45 static constexpr double A[49] = {
46 /* row 0 */ 0., 0., 0., 0., 0., 0., 0.,
47 /* row 1 */ 0.2, 0., 0., 0., 0., 0., 0.,
48 /* row 2 */ 3.0 / 40.0, 9.0 / 40.0, 0., 0., 0., 0., 0.,
49 /* row 3 */ 44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0, 0., 0., 0., 0.,
50 /* row 4 */ 19372.0 / 6561.0, -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0, 0., 0.,
51 0.,
52 /* row 5 */ 9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0,
53 -5103.0 / 18656.0, 0., 0.,
54 /* row 6 */ 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0,
55 0.};
56
57 static constexpr double b[7] =
58 {35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0, 0.0};
59
60 static constexpr double e[7] = {71.0 / 57600.0,
61 0.0,
62 -71.0 / 16695.0,
63 71.0 / 1920.0,
64 -17253.0 / 339200.0,
65 22.0 / 525.0,
66 -1.0 / 40.0};
67
68 /* Shampine's free 4th-order continuous extension (the one MATLAB's ode45
69 uses), rewritten from the `i01..i03` + `ixx` spelling the hand-written
70 rkdp45 carried. Same numbers. */
71 static constexpr double R[28] = {
72 /* i=0 */ 1.0, -183.0 / 64.0, 37.0 / 12.0, -145.0 / 128.0,
73 /* i=1 */ 0.0, 0.0, 0.0, 0.0,
74 /* i=2 */ 0.0, 1500.0 / 371.0, -1000.0 / 159.0, 1000.0 / 371.0,
75 /* i=3 */ 0.0, -125.0 / 32.0, 125.0 / 12.0, -375.0 / 64.0,
76 /* i=4 */ 0.0, 9477.0 / 3392.0, -729.0 / 106.0, 25515.0 / 6784.0,
77 /* i=5 */ 0.0, -11.0 / 7.0, 11.0 / 3.0, -55.0 / 28.0,
78 /* i=6 */ 0.0, 1.5, -4.0, 2.5};
79 // clang-format on
80};
81
93 static constexpr const char* kName = "tsit5";
94 static constexpr int kStages = 7;
95 static constexpr int kOrder = 5;
96 static constexpr int kDenseDeg = 4;
97 static constexpr bool kFsal = true;
98
99 // clang-format off
100 static constexpr double c[7] = {0.0, 0.161, 0.327, 0.9, 0.9800255409045097, 1.0, 1.0};
101
102 static constexpr double A[49] = {
103 /* row 0 */ 0., 0., 0., 0., 0., 0., 0.,
104 /* row 1 */ 0.161, 0., 0., 0., 0., 0., 0.,
105 /* row 2 */ -0.008480655492356989, 0.335480655492357, 0., 0., 0., 0., 0.,
106 /* row 3 */ 2.8971530571054935, -6.359448489975075, 4.3622954328695815, 0., 0., 0., 0.,
107 /* row 4 */ 5.325864828439257, -11.748883564062828, 7.4955393428898365,
108 -0.09249506636175525, 0., 0., 0.,
109 /* row 5 */ 5.86145544294642, -12.92096931784711, 8.159367898576159, -0.071584973281401,
110 -0.028269050394068383, 0., 0.,
111 /* row 6 */ 0.09646076681806523, 0.01, 0.4798896504144996, 1.379008574103742,
112 -3.290069515436081, 2.324710524099774, 0.};
113
114 static constexpr double b[7] = {0.09646076681806523,
115 0.01,
116 0.4798896504144996,
117 1.379008574103742,
118 -3.290069515436081,
119 2.324710524099774,
120 0.0};
121
122 static constexpr double e[7] = {-0.001780011052226,
123 -0.000816434459657,
124 0.007880878010262,
125 -0.144711007173263,
126 0.582357165452555,
127 -0.458082105929187,
128 0.015151515151515};
129
130 static constexpr double R[28] = {
131 /* i=0 */ 1.0, -2.763706197274826, 2.9132554618219126, -1.0530884977290216,
132 /* i=1 */ 0.0, 0.13169999999999998, -0.2234, 0.1017,
133 /* i=2 */ 0.0, 3.9302962368947516, -5.941033872131505, 2.490627285651253,
134 /* i=3 */ 0.0, -12.411077166933676, 30.33818863028232, -16.548102889244902,
135 /* i=4 */ 0.0, 37.50931341651104, -88.1789048947664, 47.37952196281928,
136 /* i=5 */ 0.0, -27.896526289197286, 65.09189467479366, -34.8706578614966,
137 /* i=6 */ 0.0, 1.5, -4.0, 2.5};
138 // clang-format on
139};
140
146enum class ErkControllerKind {
147 legacy, /* uncapped I controller that also grows h right after a rejection */
148 capped, /* I controller with a growth cap and no growth right after a rejection */
149 pi /* Hairer/Gustafsson PI controller, capped */
150};
151
152enum class ErkErrorNorm {
153 max_norm, /* infinity norm of the scaled error (MATLAB ode45, historical CLASS) */
154 rms_norm /* root-mean-square, as in Hairer's DOPRI5 */
155};
156
174enum class ErkOutputStepping { dense, exact };
175
196enum class ErkInterpolant { tableau, hermite3 };
197
198struct ErkControllerConfig {
199 ErkControllerKind kind = ErkControllerKind::legacy;
200 ErkErrorNorm norm = ErkErrorNorm::max_norm;
201 ErkOutputStepping output_stepping = ErkOutputStepping::dense;
202 ErkInterpolant interpolant = ErkInterpolant::tableau;
203 double safety = 0.8;
204 double fac_min = 0.1; /* most h may shrink on an ACCEPTED step */
205 double fac_max = 5.0; /* most h may grow (ignored by `legacy`) */
206 double pi_beta = 0.04; /* PI memory exponent; Hairer's DOPRI5 default */
207};
208
209/* The process-wide evolver_erk_configure/evolver_erk_config pair is GONE. The
210 controller settings now travel in EvolverOptions::erk, per call. They were
211 global because the old sixteen-argument evolver signature had nowhere to put
212 them, which meant three modules each re-establishing them before evolving with
213 the same warning comment about Cosmology being lazy -- and a fourth evolving
214 module that forgot would silently inherit another cosmology's controller.
215 See docs/superpowers/specs/2026-09-06-evolver-options-design.md. */
216
218struct ErkStats {
219 long long steps_accepted = 0;
220 long long steps_rejected = 0;
221 long long derivs_calls = 0;
222 long long dense_points = 0; /* output points served by the continuous extension */
223 long long exact_points = 0; /* output points that landed exactly on a step end */
224};
225
226/* The process-wide stats API is GONE, as the controller config already was.
227 Counters now travel in EvolverOptions::stats, per call: they need no
228 synchronisation there, so the relaxed atomics -- a per-STEP cost the profiling
229 binary had an environment variable to switch off before timing -- are gone
230 too, and ndf15 reports through exactly the same struct. */
231
242 static constexpr int kErrBins = 32; /* log10(errnorm/rtol) in [-6, +2), 0.25/bin */
243 static constexpr double kErrLo = -6.0, kErrStep = 0.25;
244 static constexpr int kXBins = 32; /* log10(x) in [-2, +6), 0.25/bin */
245 static constexpr double kXLo = -2.0, kXStep = 0.25;
246 long long err_accepted[kErrBins] = {0};
247 long long err_rejected[kErrBins] = {0};
248 long long x_accepted[kXBins] = {0};
249 long long x_rejected[kXBins] = {0};
250
252 void Add(const ErkHistograms& other) {
253 for (int i = 0; i < kErrBins; ++i) {
254 err_accepted[i] += other.err_accepted[i];
255 err_rejected[i] += other.err_rejected[i];
256 }
257 for (int i = 0; i < kXBins; ++i) {
258 x_accepted[i] += other.x_accepted[i];
259 x_rejected[i] += other.x_rejected[i];
260 }
261 }
262};
263
264/* Histogram accumulation is a SEPARATE switch from the counters: it costs four
265 more atomic increments per step, which is enough to move a wall-time
266 measurement, and the counters are wanted in every benchmark while the
267 histograms are wanted only when diagnosing the controller. */
268/* Histograms likewise travel in EvolverOptions::histograms. Unlike the counters
269 they are NOT free -- each step costs a log10 to find its bin -- so a null
270 pointer, meaning "not collecting", is the gate. */
271
272namespace erk_detail {
277void Record(
278 long long* counter, ErkHistograms* histograms, bool accepted, double x, double err_ratio);
279} // namespace erk_detail
280
281#endif
Definition evolver_erk.h:35
Definition evolver_erk.h:241
void Add(const ErkHistograms &other)
Definition evolver_erk.h:252
Definition evolver_erk.h:218
Definition evolver_erk.h:92