|
| 1 | +# ── MREEF: step-count sequence ──────────────────────────────────────────────── |
| 2 | + |
| 3 | +@inline function _mreef_sequence(seq::Symbol, order::Int) |
| 4 | + if seq === :harmonic |
| 5 | + return ntuple(j -> j, order) |
| 6 | + elseif seq === :romberg |
| 7 | + return ntuple(j -> 1 << (j - 1), order) |
| 8 | + else |
| 9 | + throw(ArgumentError("MREEF: unknown sequence `$seq`, choose :harmonic or :romberg")) |
| 10 | + end |
| 11 | +end |
| 12 | + |
| 13 | +# ── MREEF initialize! ───────────────────────────────────────────────────────── |
| 14 | + |
| 15 | +function initialize!(integrator, cache::MREEFCache) |
| 16 | + integrator.kshortsize = 2 |
| 17 | + (; fsalfirst, k) = cache |
| 18 | + integrator.fsalfirst = fsalfirst |
| 19 | + integrator.fsallast = k |
| 20 | + resize!(integrator.k, integrator.kshortsize) |
| 21 | + integrator.k[1] = integrator.fsalfirst |
| 22 | + integrator.k[2] = integrator.fsallast |
| 23 | + integrator.f.f1(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) |
| 24 | + integrator.f.f2(cache.tmp, integrator.uprev, integrator.p, integrator.t) |
| 25 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) # f1 |
| 26 | + integrator.stats.nf2 += 1 # f2 |
| 27 | + return integrator.fsalfirst .+= cache.tmp |
| 28 | +end |
| 29 | + |
| 30 | +function initialize!(integrator, cache::MREEFConstantCache) |
| 31 | + integrator.kshortsize = 2 |
| 32 | + integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) |
| 33 | + integrator.fsalfirst = integrator.f.f1(integrator.uprev, integrator.p, integrator.t) + |
| 34 | + integrator.f.f2(integrator.uprev, integrator.p, integrator.t) |
| 35 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) |
| 36 | + integrator.stats.nf2 += 1 |
| 37 | + integrator.fsallast = zero(integrator.fsalfirst) |
| 38 | + integrator.k[1] = integrator.fsalfirst |
| 39 | + return integrator.k[2] = integrator.fsallast |
| 40 | +end |
| 41 | + |
| 42 | +# ── MREEF perform_step! (in-place, MutableCache) ────────────────────────────── |
| 43 | +# |
| 44 | +# Base multirate Euler with nj macro intervals, m fast substeps each: |
| 45 | +# 1. k_slow = f.f2(u, p, t_mac) — frozen slow rate for the macro interval |
| 46 | +# 2. m fast substeps: u += h_fast*(k_slow + f.f1(u, p, t_fast)) |
| 47 | +# f1 = fast/stiff (large eigenvalues), f2 = slow/non-stiff (SciML convention). |
| 48 | +# Then apply Aitken–Neville Richardson extrapolation over T[1..order]. |
| 49 | + |
| 50 | +function perform_step!(integrator, cache::MREEFCache, repeat_step = false) |
| 51 | + (; t, dt, uprev, u, f, p) = integrator |
| 52 | + (; tmp, atmp, k_slow, k_fast, T) = cache |
| 53 | + alg = unwrap_alg(integrator, false) |
| 54 | + m = alg.m |
| 55 | + order = alg.order |
| 56 | + ns = _mreef_sequence(alg.seq, order) |
| 57 | + |
| 58 | + # Fill first tableau column: T[j] = base method with ns[j] macro intervals |
| 59 | + for j in 1:order |
| 60 | + nj = ns[j] |
| 61 | + h_mac = dt / nj |
| 62 | + h_fast = h_mac / m |
| 63 | + |
| 64 | + @.. broadcast = false T[j] = uprev |
| 65 | + |
| 66 | + for i_mac in 1:nj |
| 67 | + t_mac = t + (i_mac - 1) * h_mac |
| 68 | + |
| 69 | + # Slow evaluation (f2): frozen for all m fast substeps |
| 70 | + f.f2(k_slow, T[j], p, t_mac) |
| 71 | + integrator.stats.nf2 += 1 |
| 72 | + |
| 73 | + for i_fast in 1:m |
| 74 | + t_fast = t_mac + (i_fast - 1) * h_fast |
| 75 | + f.f1(k_fast, T[j], p, t_fast) |
| 76 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) |
| 77 | + @.. broadcast = false T[j] = T[j] + h_fast * k_slow + h_fast * k_fast |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + # Aitken–Neville Richardson extrapolation (in-place, reverse-row order) |
| 83 | + # Formula: T[j] <- T[j] + (T[j] - T[j-1]) / (ns[j]/ns[j-k] - 1) |
| 84 | + for k in 1:(order - 1) |
| 85 | + for j in order:-1:(k + 1) |
| 86 | + ratio = ns[j] / ns[j - k] |
| 87 | + @.. broadcast = false tmp = (T[j] - T[j - 1]) / (ratio - 1) |
| 88 | + @.. broadcast = false T[j] = T[j] + tmp |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + @.. broadcast = false u = T[order] |
| 93 | + |
| 94 | + return if integrator.opts.adaptive |
| 95 | + @.. broadcast = false tmp = T[order] - T[order - 1] |
| 96 | + calculate_residuals!( |
| 97 | + atmp, |
| 98 | + tmp, |
| 99 | + uprev, |
| 100 | + u, |
| 101 | + integrator.opts.abstol, |
| 102 | + integrator.opts.reltol, |
| 103 | + integrator.opts.internalnorm, |
| 104 | + t, |
| 105 | + ) |
| 106 | + integrator.EEst = integrator.opts.internalnorm(atmp, t) |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +# ── MREEF perform_step! (out-of-place, ConstantCache) ───────────────────────── |
| 111 | + |
| 112 | +@muladd function perform_step!(integrator, cache::MREEFConstantCache, repeat_step = false) |
| 113 | + (; t, dt, uprev, f, p) = integrator |
| 114 | + alg = unwrap_alg(integrator, false) |
| 115 | + m = alg.m |
| 116 | + order = alg.order |
| 117 | + ns = _mreef_sequence(alg.seq, order) |
| 118 | + T = cache.T |
| 119 | + |
| 120 | + for j in 1:order |
| 121 | + nj = ns[j] |
| 122 | + h_mac = dt / nj |
| 123 | + h_fast = h_mac / m |
| 124 | + |
| 125 | + u_cur = uprev |
| 126 | + for i_mac in 1:nj |
| 127 | + t_mac = t + (i_mac - 1) * h_mac |
| 128 | + k_slow = f.f2(u_cur, p, t_mac) |
| 129 | + integrator.stats.nf2 += 1 |
| 130 | + for i_fast in 1:m |
| 131 | + t_fast = t_mac + (i_fast - 1) * h_fast |
| 132 | + k_fast = f.f1(u_cur, p, t_fast) |
| 133 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1) |
| 134 | + u_cur = @.. broadcast = false u_cur + h_fast * k_slow + h_fast * k_fast |
| 135 | + end |
| 136 | + end |
| 137 | + T[j] = u_cur |
| 138 | + end |
| 139 | + |
| 140 | + # Aitken–Neville Richardson extrapolation |
| 141 | + for k in 1:(order - 1) |
| 142 | + for j in order:-1:(k + 1) |
| 143 | + ratio = ns[j] / ns[j - k] |
| 144 | + T[j] = @.. broadcast = false T[j] + (T[j] - T[j - 1]) / (ratio - 1) |
| 145 | + end |
| 146 | + end |
| 147 | + |
| 148 | + integrator.u = T[order] |
| 149 | + |
| 150 | + if integrator.opts.adaptive |
| 151 | + utilde = @.. broadcast = false T[order] - T[order - 1] |
| 152 | + atmp = calculate_residuals( |
| 153 | + utilde, |
| 154 | + uprev, |
| 155 | + integrator.u, |
| 156 | + integrator.opts.abstol, |
| 157 | + integrator.opts.reltol, |
| 158 | + integrator.opts.internalnorm, |
| 159 | + t, |
| 160 | + ) |
| 161 | + integrator.EEst = integrator.opts.internalnorm(atmp, t) |
| 162 | + end |
| 163 | +end |
0 commit comments