|
| 1 | +using TaylorDiff: TaylorDiff, extract_derivative, extract_derivative! |
| 2 | + |
| 3 | +@inline make_taylor(all::Vararg{X, P}) where {P, X <: AbstractArray} = TaylorArray( |
| 4 | + Base.first(all), Base.tail(all)) |
| 5 | +@inline make_taylor(all::Vararg{X, P}) where {P, X} = TaylorScalar(all) |
| 6 | + |
| 7 | +function initialize!(integrator, cache::ExplicitTaylor2ConstantCache) |
| 8 | + integrator.kshortsize = 3 |
| 9 | + integrator.k = typeof(integrator.k)(undef, integrator.kshortsize) |
| 10 | +end |
| 11 | + |
| 12 | +@muladd function perform_step!( |
| 13 | + integrator, cache::ExplicitTaylor2ConstantCache, repeat_step = false) |
| 14 | + @unpack t, dt, uprev, u, f, p = integrator |
| 15 | + k1 = f(uprev, p, t) |
| 16 | + u1 = make_taylor(uprev, k1) |
| 17 | + t1 = TaylorScalar{1}(t, one(t)) |
| 18 | + k2 = f(u1, p, t1).partials[1] |
| 19 | + u = @.. uprev + dt * k1 + dt^2 / 2 * k2 |
| 20 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 3) |
| 21 | + integrator.k[1] = k1 |
| 22 | + integrator.k[2] = k2 |
| 23 | + integrator.u = u |
| 24 | +end |
| 25 | + |
| 26 | +function initialize!(integrator, cache::ExplicitTaylor2Cache) |
| 27 | + integrator.kshortsize = 3 |
| 28 | + resize!(integrator.k, integrator.kshortsize) |
| 29 | + # Setup k pointers |
| 30 | + integrator.k[1] = cache.k1 |
| 31 | + integrator.k[2] = cache.k2 |
| 32 | + integrator.k[3] = cache.k3 |
| 33 | + return nothing |
| 34 | +end |
| 35 | + |
| 36 | +@muladd function perform_step!(integrator, cache::ExplicitTaylor2Cache, repeat_step = false) |
| 37 | + @unpack t, dt, uprev, u, f, p = integrator |
| 38 | + @unpack k1, k2, k3, utilde, tmp = cache |
| 39 | + |
| 40 | + # The following code is written to be fully non-allocating |
| 41 | + f(k1, uprev, p, t) |
| 42 | + u1 = make_taylor(uprev, k1) |
| 43 | + t1 = TaylorScalar{1}(t, one(t)) |
| 44 | + out1 = make_taylor(k1, k2) |
| 45 | + f(out1, u1, p, t1) |
| 46 | + @.. u = uprev + dt * k1 + dt^2 / 2 * k2 |
| 47 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, 3) |
| 48 | + return nothing |
| 49 | +end |
| 50 | + |
| 51 | +function initialize!(integrator, cache::ExplicitTaylorConstantCache{P}) where {P} |
| 52 | + integrator.kshortsize = P |
| 53 | + integrator.k = typeof(integrator.k)(undef, P) |
| 54 | +end |
| 55 | + |
| 56 | +@muladd function perform_step!( |
| 57 | + integrator, cache::ExplicitTaylorConstantCache{P}, repeat_step = false) where {P} |
| 58 | + @unpack t, dt, uprev, u, f, p = integrator |
| 59 | + @unpack jet = cache |
| 60 | + utaylor = jet(uprev, t) |
| 61 | + u = map(x -> evaluate_polynomial(x, dt), utaylor) |
| 62 | + if integrator.opts.adaptive |
| 63 | + utilde = TaylorDiff.get_coefficient(utaylor, P) * dt^(P + 1) |
| 64 | + atmp = calculate_residuals(utilde, uprev, u, integrator.opts.abstol, |
| 65 | + integrator.opts.reltol, integrator.opts.internalnorm, t) |
| 66 | + integrator.EEst = integrator.opts.internalnorm(atmp, t) |
| 67 | + end |
| 68 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, P + 1) |
| 69 | + integrator.u = u |
| 70 | +end |
| 71 | + |
| 72 | +function initialize!(integrator, cache::ExplicitTaylorCache{P}) where {P} |
| 73 | + integrator.kshortsize = P |
| 74 | + resize!(integrator.k, P) |
| 75 | + # Setup k pointers |
| 76 | + for i in 1:P |
| 77 | + integrator.k[i] = get_coefficient(cache.utaylor, i) |
| 78 | + end |
| 79 | + return nothing |
| 80 | +end |
| 81 | + |
| 82 | +@muladd function perform_step!( |
| 83 | + integrator, cache::ExplicitTaylorCache{P}, repeat_step = false) where {P} |
| 84 | + @unpack t, dt, uprev, u, f, p = integrator |
| 85 | + @unpack jet, utaylor, utilde, tmp, atmp, thread = cache |
| 86 | + |
| 87 | + jet(utaylor, uprev, t) |
| 88 | + for i in eachindex(utaylor) |
| 89 | + u[i] = @inline evaluate_polynomial(utaylor[i], dt) |
| 90 | + end |
| 91 | + if integrator.opts.adaptive |
| 92 | + @.. broadcast=false thread=thread utilde=TaylorDiff.get_coefficient(utaylor, P) * |
| 93 | + dt^(P + 1) |
| 94 | + calculate_residuals!(atmp, utilde, uprev, u, integrator.opts.abstol, |
| 95 | + integrator.opts.reltol, integrator.opts.internalnorm, t) |
| 96 | + integrator.EEst = integrator.opts.internalnorm(atmp, t) |
| 97 | + end |
| 98 | + OrdinaryDiffEqCore.increment_nf!(integrator.stats, P + 1) |
| 99 | + return nothing |
| 100 | +end |
0 commit comments