CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
evolver_erk_impl.h
1#ifndef __EVOERKIMPL__
2#define __EVOERKIMPL__
3
4#include <algorithm>
5#include <cfloat>
6#include <cmath>
7#include <vector>
8
9#include "common.h" // class_test, class_stop
10#include "evolver_erk.h"
11#include "evolver_options.h"
12#include "nonfinite.h"
13
23template <class Tab>
24void evolver_erk_run(EvolverDerivs derivs,
25 double x_ini,
26 double x_end,
27 double* y,
28 int y_size,
29 const EvolverOptions& options) {
30 const double tolerance = options.rtol;
31 const int* used_in_output = options.used_in_output;
32 const double* x_sampling = options.x_sampling;
33 const int x_size = options.x_sampling_size;
34 const EvolverOutput output = options.output.value();
35 const auto& print_variables = options.print_variables;
36
37 /* Step-controller settings travel per call. They used to live in process-wide
38 state because the old evolver signature had nowhere to put them, which meant
39 three modules each re-establishing them before evolving, and a fourth that
40 forgot would silently inherit another cosmology's controller. */
41 const ErkControllerConfig cfg = options.erk.value_or(ErkControllerConfig{});
42
43 /* Every step-size rule below reads cfg.safety and cfg.fac_min, including the
44 `legacy` ones, which used to hard-code 0.8 and 0.1. At the defaults those ARE
45 0.8 and 0.1, so `legacy` is unchanged; the point is that a knob the input
46 documents as the safety factor should not be silently ignored by the shipped
47 controller. */
48
49 /* Contract: x_sampling is a non-null array of
50 output points, and tolerance > 0 (threshold below divides by it). */
51 class_test(x_sampling == nullptr, "%s requires a non-null x_sampling array", Tab::kName);
52 class_test(tolerance <= 0., "%s requires tolerance > 0 (got %e)", Tab::kName, tolerance);
53
54 /* Counters are local to this call, so they need no synchronisation -- the
55 relaxed atomics they replace cost a handful of operations per STEP, which
56 was enough that the profiling binary had an environment variable to switch
57 them off before timing. Null means "not collecting". */
58 EvolverStats counters;
59 ErkHistograms* const histograms = options.histograms;
60
61 const int neq = y_size;
62 const double rtol = tolerance;
63 const double abstol = options.abstol;
64 const double threshold = abstol / rtol;
65 const double pow_grow = 1.0 / Tab::kOrder;
66 constexpr int s = Tab::kStages;
67 constexpr int deg = Tab::kDenseDeg;
68
69 /* The derivative at the right end of an accepted step. FSAL pairs already have
70 it as the last stage; otherwise it costs one extra evaluation, which also
71 supplies the next step's k0. */
72 constexpr int idx_end = Tab::kFsal ? (s - 1) : s;
73 constexpr int k_rows = Tab::kFsal ? s : (s + 1);
74
75 std::vector<double> ynew(neq);
76 std::vector<double> ytemp(neq);
77 std::vector<double> err(neq);
78 std::vector<double> yinterp(neq);
79 std::vector<double> dyinterp(neq);
80 std::vector<double> ki(static_cast<size_t>(k_rows) * neq);
81 double bi_vec_y[s];
82 double bi_vec_dy[s];
83 double thpow[deg + 1];
84
85 /* History for the quintic Hermite interpolant: the value and the derivative at
86 the END of the step before this one. Allocated only when that interpolant is
87 selected -- it is two more vectors of the state size per workspace. */
88 const bool want_hermite = (cfg.interpolant == ErkInterpolant::hermite3);
89 std::vector<double> y_prev;
90 std::vector<double> f_prev;
91 double t_prev = 0.0;
92 bool have_prev = false;
93 if (want_hermite) {
94 y_prev.resize(neq);
95 f_prev.resize(neq);
96 }
97
98 double t = x_ini;
99
100 /* initialise k0 = f(t, y) */
101 derivs(t, y, ki.data());
102 ++counters.derivs_evaluations;
103
104 const double hmax = fabs(x_end - x_ini) / 10.0;
105 double absh;
106 if (x_size > 1) {
107 absh = std::min(hmax, fabs(x_sampling[1] - x_sampling[0]));
108 }
109 else {
110 absh = hmax;
111 }
112 if (absh == 0.0) {
113 absh = hmax;
114 }
115
116 /* initial step from derivative scale */
117 double rh = 0.0;
118 for (int k = 0; k < neq; k++) {
119 double maxtmp = std::max(fabs(y[k]), threshold);
120 rh = std::max(rh, fabs(ki[k]) / maxtmp);
121 }
122 rh /= cfg.safety * pow(rtol, pow_grow);
123 if (absh * rh > 1.0) {
124 absh = 1.0 / rh;
125 }
126
127 const int tdir = (x_end > x_ini) ? 1 : -1;
128 double hnew = absh * tdir;
129 bool nofailed = true;
130
131 /* PI memory: the previous ACCEPTED step's error ratio. Hairer's DOPRI5 seeds
132 this at 1e-4 so the first steps are not throttled by an absent history. */
133 double ratio_old = 1e-4;
134 const double alpha_pi = pow_grow - 0.75 * cfg.pi_beta;
135
136 int idx = 0;
137 if ((t - x_end) * tdir < 0.0) {
138 for (idx = 0; (idx < x_size) && ((x_sampling[idx] - t) * tdir < 0.0); idx++) {
139 ;
140 }
141 }
142
143 const bool step_to_output = (cfg.output_stepping == ErkOutputStepping::exact);
144
145 while ((t - x_end) * tdir < 0.0) {
146 double h = hnew;
147 const double hmin = 100.0 * DBL_MIN * fabs(t);
148 class_test(fabs(h) < hmin,
149 "%s: step size %e fell below minimum %e at x=%e",
150 Tab::kName,
151 fabs(h),
152 hmin,
153 t);
154 if (fabs(h) > 0.9 * fabs(x_end - t)) {
155 h = x_end - t;
156 }
157
158 /* In `exact` output stepping the step is truncated so it ends on the next
159 sampling point. `clamped` records that the length was imposed rather than
160 chosen: the controller must then NOT read its own proposal back from this
161 step, because a truncated step has a smaller error for reasons that say
162 nothing about how large the next step may be. */
163 bool clamped = false;
164 double t_clamped = 0.0;
165 if (step_to_output && idx < x_size) {
166 const double dt_out = (x_sampling[idx] - t) * tdir;
167 /* `<=` and not `<`: when the step already ends on the point, t + h still
168 only lands within a rounding of it, which is enough to miss the equality
169 test at emission and push an exactly-known state through the interpolant. */
170 if (dt_out > 0.0 && dt_out <= fabs(h)) {
171 h = tdir * dt_out;
172 clamped = true;
173 t_clamped = x_sampling[idx];
174 }
175 }
176
177 /* stage 0 (FSAL: ki[0..neq) already holds k0) */
178 for (int k = 0; k < neq; k++) {
179 ynew[k] = y[k] + h * Tab::b[0] * ki[k];
180 err[k] = h * Tab::e[0] * ki[k];
181 }
182 for (int i = 1; i < s; i++) {
183 for (int k = 0; k < neq; k++) {
184 ytemp[k] = y[k];
185 }
186 for (int j = 0; j < i; j++) {
187 const double aij = Tab::A[i * s + j];
188 for (int k = 0; k < neq; k++) {
189 ytemp[k] += h * aij * ki[j * neq + k];
190 }
191 }
192 derivs(t + Tab::c[i] * h, ytemp.data(), ki.data() + i * neq);
193 ++counters.derivs_evaluations;
194 for (int k = 0; k < neq; k++) {
195 ynew[k] += h * Tab::b[i] * ki[i * neq + k];
196 err[k] += h * Tab::e[i] * ki[i * neq + k];
197 }
198 }
199
200 /* The non-finite flag is accumulated INSIDE the error reduction, not tested on
201 errmax afterwards, because the reduction cannot carry a NaN out:
202 `errtemp > errmax` is FALSE for NaN, so a NaN component leaves errmax at 0 and
203 the step is ACCEPTED -- h then grows and the run completes, silently, with a
204 poisoned state. Inf behaves differently (inf > x is true), which is why the
205 observed decaying-NCDM failure hung on rejection instead of returning garbage.
206 Both modes have to be caught, so the test is per component. */
207 double errmax = 0.0;
208 int nonfinite = 0;
209 for (int k = 0; k < neq; k++) {
210 double errtemp = fabs(err[k] / std::max(threshold, fabs(ynew[k])));
211 nonfinite |= static_cast<int>(IsNonFinite(ynew[k]) || IsNonFinite(err[k]));
212 errmax = std::max(errmax, errtemp);
213 }
214 /* `errmax` stays the quantity reported in diagnostics; `errnorm` is what the
215 controller acts on. They coincide unless the RMS norm is selected, which
216 is a second reduction rather than a modification of the first so that the
217 max-norm path keeps its exact historical arithmetic. */
218 double errnorm = errmax;
219 if (cfg.norm == ErkErrorNorm::rms_norm) {
220 double acc = 0.0;
221 for (int k = 0; k < neq; k++) {
222 const double e_k = err[k] / std::max(threshold, fabs(ynew[k]));
223 acc += e_k * e_k;
224 }
225 errnorm = sqrt(acc / neq);
226 }
227
228 /* A non-finite state must ABORT, not be handed back to the step controller. The
229 O(neq) search for the offending index runs only on the failure path.
230
231 On the Inf path the run does not stop, it GRINDS: errmax > rtol is taken, the
232 step is rejected, h shrinks, and the only exit is class_test(fabs(h) < hmin)
233 with hmin = 100*DBL_MIN*|x|. h converges ONTO that value without going below
234 it, so the guard never fires. Measured on the decaying-NCDM sector at Gamma=1e8
235 (dr_N_q=30, k=0.5): NaN at x=523.76, then h pinned at 1.1654e-303 with 99.4% of
236 steps rejected, repeating identically -- two multi-hour runs burned on what
237 should have been an immediate error.
238
239 IsNonFinite is a bit test rather than std::isnan because classpp is built with
240 -ffast-math, under which std::isnan may be folded to false (see nonfinite.h). */
241 if (nonfinite != 0) {
242 int kbad = 0;
243 for (int k = 0; k < neq; k++) {
244 if (IsNonFinite(ynew[k]) || IsNonFinite(err[k])) {
245 kbad = k;
246 break;
247 }
248 }
249 class_stop(
250 "%s: non-finite state at x=%e (h=%e, errmax=%e): y[%d]=%e, err[%d]=%e. The "
251 "integration cannot recover -- rejecting and shrinking h only converges onto the "
252 "step-size floor. Something upstream produced NaN/Inf.",
253 Tab::kName,
254 t,
255 h,
256 errmax,
257 kbad,
258 ynew[kbad],
259 kbad,
260 err[kbad]);
261 }
262
263 if (errnorm > rtol) {
264 erk_detail::Record(&counters.steps_rejected, histograms, false, t, errnorm / rtol);
265 if (cfg.kind == ErkControllerKind::legacy) {
266 /* Standard ode45 control: first failure shrinks by the error-proportional
267 factor and marks nofailed; consecutive failures halve. (This corrects a
268 transcription bug in the PhD2024-EBH branch where nofailed was never
269 set, making the halving path dead code.) */
270 if (nofailed) {
271 nofailed = false;
272 hnew = tdir * std::max(hmin,
273 fabs(h) * std::max(cfg.fac_min,
274 cfg.safety * pow(rtol / errnorm, pow_grow)));
275 }
276 else {
277 hnew = tdir * std::max(hmin, 0.5 * fabs(h));
278 }
279 }
280 else {
281 double fac = cfg.safety * pow(rtol / errnorm, pow_grow);
282 fac = std::max(cfg.fac_min, std::min(fac, 1.0));
283 if (!nofailed) {
284 fac = std::min(fac, 0.5); /* repeated failures: force a real cut */
285 }
286 nofailed = false;
287 hnew = tdir * std::max(hmin, fabs(h) * fac);
288 }
289 continue;
290 }
291
292 /* step accepted */
293 erk_detail::Record(&counters.steps_accepted, histograms, true, t, errnorm / rtol);
294
295 if (!Tab::kFsal) {
296 for (int k = 0; k < neq; k++) {
297 ytemp[k] = ynew[k];
298 }
299 derivs(t + h, ytemp.data(), ki.data() + idx_end * neq);
300 ++counters.derivs_evaluations;
301 }
302
303 if (print_variables.has_value()) {
304 (*print_variables)(t + h, ynew.data(), ki.data() + idx_end * neq);
305 }
306
307 if (clamped) {
308 /* keep the controller's own proposal; this step's length was imposed */
309 hnew = tdir * std::max(hmin, fabs(hnew));
310 }
311 else if (cfg.kind == ErkControllerKind::legacy) {
312 hnew = tdir *
313 std::max(hmin,
314 fabs(h) * std::max(cfg.fac_min, cfg.safety * pow(rtol / errnorm, pow_grow)));
315 }
316 else {
317 const double ratio = errnorm / rtol;
318 double fac;
319 if (cfg.kind == ErkControllerKind::pi && ratio > 0.0) {
320 fac = cfg.safety * pow(ratio, -alpha_pi) * pow(ratio_old, cfg.pi_beta);
321 }
322 else {
323 fac = cfg.safety * pow(rtol / errnorm, pow_grow);
324 }
325 fac = std::max(cfg.fac_min, std::min(fac, cfg.fac_max));
326 /* Never grow immediately after a rejection: the step that just failed is
327 evidence that the local scale shrank. Measured on LCDM + Mnu this is
328 worth little on its own -- with the growth cap it moves the rejection
329 rate only from 13.3% to 12.1% -- so it is the PI memory term above, not
330 this, that takes rejections down to ~6%. */
331 if (!nofailed) {
332 fac = std::min(fac, 1.0);
333 }
334 hnew = tdir * std::max(hmin, fabs(h) * fac);
335 ratio_old = std::max(ratio, 1e-4);
336 }
337 nofailed = true;
338 /* A truncated step ends ON the sampling point by construction. Recomputing
339 t + h instead would land within a rounding of it, which is enough to miss
340 the equality test below and send an exactly-known state through the
341 interpolant. */
342 const double tnew = clamped ? t_clamped : (t + h);
343
344 /* emit output at all sampling points within (t, tnew] */
345 for (; (idx < x_size) && ((tnew - x_sampling[idx]) * tdir >= 0.0); idx++) {
346 if (tnew == x_sampling[idx]) {
347 ++counters.exact_points;
348 output(tnew, ynew.data(), ki.data() + idx_end * neq, idx);
349 }
350 else {
351 ++counters.dense_points;
352 const double ti = x_sampling[idx];
353 const double th = (ti - t) / h;
354 /* theta^m, m = 0..deg. Squaring for m = 2 and m = 4 is not an
355 optimisation: it is the association the hand-written Dormand-Prince
356 interpolant used, and keeping it makes this driver reproduce the
357 previous rkdp45 output bit for bit. */
358 thpow[0] = 1.0;
359 thpow[1] = th;
360 if (deg >= 2) {
361 thpow[2] = th * th;
362 }
363 if (deg >= 3) {
364 thpow[3] = thpow[2] * th;
365 }
366 if (deg >= 4) {
367 thpow[4] = thpow[2] * thpow[2];
368 }
369 for (int m = 5; m <= deg; m++) {
370 thpow[m] = thpow[m - 1] * th;
371 }
372 for (int i = 0; i < s; i++) {
373 double vy = 0.0;
374 double vd = 0.0;
375 for (int m = 0; m < deg; m++) {
376 const double r = Tab::R[i * deg + m];
377 vy += r * thpow[m + 1];
378 vd += r * (m + 1) * thpow[m];
379 }
380 bi_vec_y[i] = vy;
381 bi_vec_dy[i] = vd;
382 }
383 for (int k = 0; k < neq; k++) {
384 if (used_in_output[k]) {
385 yinterp[k] = y[k];
386 dyinterp[k] = 0.0;
387 for (int i = 0; i < s; i++) {
388 yinterp[k] += h * bi_vec_y[i] * ki[i * neq + k];
389 dyinterp[k] += bi_vec_dy[i] * ki[i * neq + k];
390 }
391 }
392 }
393
394 if (want_hermite && have_prev) {
395 /* Quintic Hermite through (t_prev, t, tnew), value and derivative at
396 each. The six basis functions depend only on the node positions and
397 on ti, so they are built once and then applied to every component --
398 the same cost per component as the tableau extension it replaces.
399
400 h_i(x) = [1 - 2 (x - x_i) L_i'(x_i)] L_i(x)^2
401 hbar_i(x) = (x - x_i) L_i(x)^2
402 with L_i the Lagrange basis of the three nodes. */
403 const double x0 = t_prev;
404 const double x1 = t;
405 const double x2 = tnew;
406 const double d01 = x0 - x1;
407 const double d02 = x0 - x2;
408 const double d12 = x1 - x2;
409 /* Coincident nodes would make this singular; they cannot occur (each
410 node is a strictly later accepted step end) but a zero-length step
411 would, so fall back rather than divide by it. */
412 if (d01 != 0.0 && d02 != 0.0 && d12 != 0.0) {
413 const double u0 = ti - x0;
414 const double u1 = ti - x1;
415 const double u2 = ti - x2;
416 const double L0 = (u1 * u2) / (d01 * d02);
417 const double L1 = (u0 * u2) / (-d01 * d12);
418 const double L2 = (u0 * u1) / (d02 * d12);
419 /* L_i'(x_i) = sum_{j != i} 1/(x_i - x_j) */
420 const double Lp0 = 1.0 / d01 + 1.0 / d02;
421 const double Lp1 = -1.0 / d01 + 1.0 / d12;
422 const double Lp2 = -1.0 / d02 - 1.0 / d12;
423 /* derivative of L_i at ti, for the interpolant's own derivative */
424 const double dL0 = (u1 + u2) / (d01 * d02);
425 const double dL1 = (u0 + u2) / (-d01 * d12);
426 const double dL2 = (u0 + u1) / (d02 * d12);
427
428 const double L0s = L0 * L0;
429 const double L1s = L1 * L1;
430 const double L2s = L2 * L2;
431 const double w0 = (1.0 - 2.0 * u0 * Lp0) * L0s;
432 const double w1 = (1.0 - 2.0 * u1 * Lp1) * L1s;
433 const double w2 = (1.0 - 2.0 * u2 * Lp2) * L2s;
434 const double v0 = u0 * L0s;
435 const double v1 = u1 * L1s;
436 const double v2 = u2 * L2s;
437 const double dw0 = -2.0 * Lp0 * L0s + (1.0 - 2.0 * u0 * Lp0) * 2.0 * L0 * dL0;
438 const double dw1 = -2.0 * Lp1 * L1s + (1.0 - 2.0 * u1 * Lp1) * 2.0 * L1 * dL1;
439 const double dw2 = -2.0 * Lp2 * L2s + (1.0 - 2.0 * u2 * Lp2) * 2.0 * L2 * dL2;
440 const double dv0 = L0s + u0 * 2.0 * L0 * dL0;
441 const double dv1 = L1s + u1 * 2.0 * L1 * dL1;
442 const double dv2 = L2s + u2 * 2.0 * L2 * dL2;
443
444 const double* f1 = ki.data(); /* f at t (FSAL k0) */
445 const double* f2 = ki.data() + idx_end * neq; /* f at tnew */
446 for (int k = 0; k < neq; k++) {
447 if (used_in_output[k]) {
448 yinterp[k] = w0 * y_prev[k] + w1 * y[k] + w2 * ynew[k] + v0 * f_prev[k] +
449 v1 * f1[k] + v2 * f2[k];
450 dyinterp[k] = dw0 * y_prev[k] + dw1 * y[k] + dw2 * ynew[k] + dv0 * f_prev[k] +
451 dv1 * f1[k] + dv2 * f2[k];
452 }
453 }
454 }
455 }
456 output(ti, yinterp.data(), dyinterp.data(), idx);
457 }
458 }
459
460 if (want_hermite) {
461 /* This step's LEFT end becomes the history for the next step. It must be
462 copied before y and ki are overwritten below -- and only for the
463 components the interpolant can ever be asked about, since this copy runs
464 on every accepted step while the interpolant runs on roughly half of
465 them. */
466 for (int k = 0; k < neq; k++) {
467 if (used_in_output[k]) {
468 y_prev[k] = y[k];
469 f_prev[k] = ki[k];
470 }
471 }
472 t_prev = t;
473 have_prev = true;
474 }
475 for (int k = 0; k < neq; k++) {
476 y[k] = ynew[k];
477 ki[k] = ki[idx_end * neq + k]; /* FSAL: last stage becomes next k0 */
478 }
479 t = tnew;
480 }
481
482 if (options.stats != nullptr) {
483 *options.stats = counters;
484 }
485}
486
487#endif
Definition output.h:24