|
1 |
| -using TaylorDiff: TaylorDiff, TaylorScalar, TaylorArray, make_seed, value, partials, flatten |
2 |
| -using TaylorSeries |
3 |
| -using TaylorIntegration: jetcoeffs! |
4 |
| -using BenchmarkTools |
| 1 | +using TaylorDiff: TaylorDiff, TaylorScalar, make_seed, flatten, get_coefficient, |
| 2 | + set_coefficient, append_coefficient |
| 3 | +using TaylorSeries, TaylorIntegration |
| 4 | +using ODEProblemLibrary, OrdinaryDiffEq, BenchmarkTools, Symbolics |
5 | 5 |
|
6 |
| -""" |
7 |
| -No magic, just a type-stable way to generate a new object since TaylorScalar is immutable |
8 |
| -""" |
9 |
| -function setindex(x::TaylorScalar{T, P}, index, d) where {T, P} |
10 |
| - v = flatten(x) |
11 |
| - ntuple(i -> i == index + 1 ? d : v[i], Val(P + 1)) |> TaylorScalar |
12 |
| -end |
13 |
| - |
14 |
| -function setindex(x::TaylorArray{T, N, A, P}, index, d) where {T, N, A, P} |
15 |
| - v = flatten(x) |
16 |
| - ntuple(i -> i == index + 1 ? d : v[i], Val(P + 1)) |> TaylorArray |
17 |
| -end |
18 |
| - |
19 |
| -""" |
20 |
| -Computes the taylor integration of order P |
| 6 | +# There are two ways to compute the Taylor coefficients of a ODE solution |
| 7 | +# 1. Using naive repeated differentiation |
| 8 | +# 2. First simplify the expansion using Symbolics and then evaluate the expression |
21 | 9 |
|
22 |
| -- `f`: ODE function |
23 |
| -- `t`: constructed by TaylorScalar{P}(t0, one(t0)) |
24 |
| -- `x0`: initial value |
25 |
| -- `p`: parameters |
26 | 10 | """
|
27 |
| -function my_jetcoeffs(f, t::TaylorScalar{T, P}, x0, p) where {T, P} |
28 |
| - x = x0 isa AbstractArray ? TaylorArray{P}(x0) : TaylorScalar{P}(x0) |
29 |
| - for index in 1:P # computes x.partials[index] |
30 |
| - fx = f(x, p, t) |
31 |
| - d = index == 1 ? value(fx) : partials(fx)[index - 1] / index |
32 |
| - x = setindex(x, index, d) |
33 |
| - end |
34 |
| - x |
35 |
| -end |
36 |
| - |
37 |
| -""" |
38 |
| -Computes the taylor integration of order P |
| 11 | +# The first method |
39 | 12 |
|
40 |
| -- `f!`: ODE function, in non-allocating form |
41 |
| -- `t`: constructed by TaylorScalar{P}(t0, one(t0)) |
42 |
| -- `x0`: initial value |
43 |
| -- `p`: parameters |
| 13 | +For ODE u' = f(u, p, t) and initial condition (u0, t0), computes Taylor expansion of the solution `u` up to order `P` using repeated differentiation. |
44 | 14 | """
|
45 |
| -function my_jetcoeffs!(f!, t::TaylorScalar{T, P}, x0, p) where {T, P} |
46 |
| - x = x0 isa AbstractArray ? TaylorArray{P}(x0) : TaylorScalar{P}(x0) |
47 |
| - out = similar(x) |
48 |
| - for index in 1:P # computes x.partials[index] |
49 |
| - f!(out, x, p, t) |
50 |
| - d = index == 1 ? value(out) : partials(out)[index - 1] / index |
51 |
| - x = setindex(x, index, d) |
| 15 | +function jetcoeffs(f::ODEFunction{iip}, u0, p, t0, ::Val{P}) where {P, iip} |
| 16 | + t = TaylorScalar{P}(t0, one(t0)) |
| 17 | + u = make_seed(u0, zero(u0), Val(P)) |
| 18 | + fu = copy(u) |
| 19 | + for index in 1:P |
| 20 | + if iip |
| 21 | + f(fu, u, p, t) |
| 22 | + else |
| 23 | + fu = f(u, p, t) |
| 24 | + end |
| 25 | + d = get_coefficient(fu, index - 1) / index |
| 26 | + u = set_coefficient(u, index, d) |
52 | 27 | end
|
53 |
| - x |
| 28 | + u |
54 | 29 | end
|
55 | 30 |
|
56 | 31 | function scalar_test()
|
57 |
| - f(x, p, t) = x * x |
58 |
| - x0 = 0.1 |
59 |
| - t0 = 0.0 |
60 | 32 | P = 6
|
61 |
| - |
| 33 | + prob = prob_ode_linear |
| 34 | + t0 = prob.tspan[1] |
62 | 35 | # TaylorIntegration test
|
63 |
| - t = t0 + Taylor1(typeof(t0), P) |
64 |
| - x = Taylor1(x0, P) |
65 |
| - @btime jetcoeffs!($f, $t, $x, nothing) |
| 36 | + ts = t0 + Taylor1(typeof(t0), P) |
| 37 | + u_ts = Taylor1(prob.u0, P) |
| 38 | + @btime TaylorIntegration.jetcoeffs!($prob.f, $ts, $u_ts, $prob.p) |
66 | 39 |
|
67 | 40 | # TaylorDiff test
|
68 |
| - td = TaylorScalar{P}(t0, one(t0)) |
69 |
| - @btime my_jetcoeffs($f, $td, $x0, nothing) |
70 |
| - |
71 |
| - result = my_jetcoeffs(f, td, x0, nothing) |
72 |
| - @assert x.coeffs ≈ collect(flatten(result)) |
| 41 | + @btime jetcoeffs($prob.f, $prob.u0, $prob.p, $t0, Val($P)) |
| 42 | + u_td = jetcoeffs(prob.f, prob.u0, prob.p, t0, Val(P)) |
| 43 | + @assert u_ts.coeffs ≈ collect(flatten(u_td)) |
73 | 44 | end
|
74 | 45 |
|
75 | 46 | function array_test()
|
76 |
| - function lorenz(du, u, p, t) |
77 |
| - du[1] = 10.0(u[2] - u[1]) |
78 |
| - du[2] = u[1] * (28.0 - u[3]) - u[2] |
79 |
| - du[3] = u[1] * u[2] - (8 / 3) * u[3] |
80 |
| - return nothing |
81 |
| - end |
82 |
| - u0 = [1.0; 0.0; 0.0] |
83 |
| - t0 = 0.0 |
84 | 47 | P = 6
|
85 |
| - |
| 48 | + prob = prob_ode_lotkavolterra |
| 49 | + t0 = prob.tspan[1] |
86 | 50 | # TaylorIntegration test
|
87 |
| - t = t0 + Taylor1(typeof(t0), P) |
88 |
| - u = [Taylor1(x, P) for x in u0] |
89 |
| - du = similar(u) |
90 |
| - uaux = similar(u) |
91 |
| - @btime jetcoeffs!($lorenz, $t, $u, $du, $uaux, nothing) |
| 51 | + ts = t0 + Taylor1(typeof(t0), P) |
| 52 | + u_ts = [Taylor1(x, P) for x in prob.u0] |
| 53 | + du = similar(u_ts) |
| 54 | + uaux = similar(u_ts) |
| 55 | + @btime TaylorIntegration.jetcoeffs!($prob.f, $ts, $u_ts, $du, $uaux, $prob.p) |
92 | 56 |
|
93 | 57 | # TaylorDiff test
|
94 |
| - td = TaylorScalar{P}(t0, one(t0)) |
95 |
| - @btime my_jetcoeffs!($lorenz, $td, $u0, nothing) |
96 |
| - result = my_jetcoeffs!(lorenz, td, u0, nothing) |
97 |
| - for i in eachindex(u) |
98 |
| - @assert u[i].coeffs ≈ collect(flatten(result[i])) |
| 58 | + @btime jetcoeffs($prob.f, $prob.u0, $prob.p, $t0, Val($P)) |
| 59 | + u_td = jetcoeffs(prob.f, prob.u0, prob.p, t0, Val(P)) |
| 60 | + for i in eachindex(u_ts) |
| 61 | + @assert u_ts[i].coeffs ≈ collect(flatten(u_td[i])) |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +""" |
| 66 | +# The second method |
| 67 | +
|
| 68 | +For ODE u' = f(u, p, t) and initial condition (u0, t0), symbolically computes Taylor expansion of the solution `u` up to order `P`, and then builds a function to evaluate the expression. |
| 69 | +""" |
| 70 | +function build_jetcoeffs(f::ODEFunction{iip}, p, ::Val{P}, length = nothing) where {P, iip} |
| 71 | + @variables t0::Real |
| 72 | + u0 = isnothing(length) ? Symbolics.variable(:u0) : Symbolics.variables(:u0, 1:length) |
| 73 | + if iip |
| 74 | + @assert length isa Integer |
| 75 | + f0 = similar(u0) |
| 76 | + f(f0, u0, p, t0) |
| 77 | + else |
| 78 | + f0 = f(u0, p, t0) |
99 | 79 | end
|
| 80 | + u = TaylorDiff.make_seed(u0, f0, Val(1)) |
| 81 | + for index in 2:P |
| 82 | + t = TaylorScalar{index - 1}(t0, one(t0)) |
| 83 | + if iip |
| 84 | + fu = similar(u) |
| 85 | + f(fu, u, p, t) |
| 86 | + else |
| 87 | + fu = f(u, p, t) |
| 88 | + end |
| 89 | + d = get_coefficient(fu, index - 1) / index |
| 90 | + u = append_coefficient(u, d) |
| 91 | + end |
| 92 | + build_function(u, u0, t0; expression = Val(false), cse = true) |
| 93 | +end |
| 94 | + |
| 95 | +function simplify_scalar_test() |
| 96 | + P = 6 |
| 97 | + prob = prob_ode_linear |
| 98 | + t0 = prob.tspan[1] |
| 99 | + @btime jetcoeffs($prob.f, $prob.u0, $prob.p, $t0, Val($P)) |
| 100 | + fast_jetcoeffs = build_jetcoeffs(prob.f, prob.p, Val(P)) |
| 101 | + @btime $fast_jetcoeffs($prob.u0, $t0) |
| 102 | +end |
| 103 | + |
| 104 | +function simplify_array_test() |
| 105 | + P = 6 |
| 106 | + prob = prob_ode_lotkavolterra |
| 107 | + t0 = prob.tspan[1] |
| 108 | + @btime jetcoeffs($prob.f, $prob.u0, $prob.p, $t0, Val($P)) |
| 109 | + fast_oop, fast_iip = build_jetcoeffs(prob.f, prob.p, Val(P), length(prob.u0)) |
| 110 | + @btime $fast_oop($prob.u0, $t0) |
100 | 111 | end
|
0 commit comments