|
| 1 | +using OrdinaryDiffEq |
| 2 | +using LinearAlgebra |
| 3 | +using Plots |
| 4 | +using LaTeXStrings |
| 5 | +using ForwardDiff: ForwardDiff |
| 6 | +using Zygote: Zygote |
| 7 | +#using Mooncake # plante à l'instalation |
| 8 | +using SciMLSensitivity |
| 9 | +using DifferentiationInterface |
| 10 | +using DataFrames |
| 11 | + |
| 12 | +include("../../src/CTDiffFlow.jl") |
| 13 | +using .CTDiffFlow |
| 14 | +include("../../src/myode43/myode43.jl") |
| 15 | + |
| 16 | +function my_euler(fun,t0,x0,tf,λ,N) |
| 17 | + ti = t0; xi = x0 |
| 18 | + h = (tf-t0)/N |
| 19 | + for i in 1:N |
| 20 | + xi += h*fun(xi,λ,ti) |
| 21 | + ti = ti+h |
| 22 | + end |
| 23 | + return xi |
| 24 | +end |
| 25 | + |
| 26 | + |
| 27 | +# Definition of the edo |
| 28 | +A(λ) = [λ[1] 0 0 ; 0 λ[2] 0 ; 0 0 λ[1]-λ[2]] |
| 29 | +fun1(x,λ,t) = A(λ)*x |
| 30 | +funx0(λ) = [λ[2],1.,1]; |
| 31 | +N = 10 |
| 32 | +t0 = 0. ; tf = 1.; tspan = (t0,tf); |
| 33 | +λ = [1.,2]; |
| 34 | +reltol = 1.e-8 |
| 35 | +abstol = reltol |
| 36 | +# ∂λ_flow(λ) |
| 37 | +sol_∂λ_flow = [λ[2]*exp(λ[1]*tf) exp(λ[1]*tf) |
| 38 | + 0. tf*exp(λ[2]*tf) |
| 39 | + tf*exp((λ[1]-λ[2])*tf) -tf*exp((λ[1]-λ[2])*tf)] |
| 40 | + |
| 41 | + |
| 42 | +#dict_Zygote = Dict(:∂λ_sol => sol_∂λ_flow) |
| 43 | +#dict_ForwardDiff = Dict(:∂λ_sol => sol_∂λ_flow) |
| 44 | + |
| 45 | +df = DataFrame(AutoDiff = String[], algo=String[], Jacobian = Matrix{<:Real}[]) |
| 46 | +push!(df, ("solution", "", sol_∂λ_flow)) |
| 47 | + |
| 48 | +# Zygote with my_euler |
| 49 | +_flow(λ) = my_euler(fun1,t0,funx0(λ),tf,λ,N) |
| 50 | +#dict_Zygote[:my_euler] = jacobian(_flow,AutoZygote(),λ) |
| 51 | +#dict_ForwardDiff[:my_euler] = jacobian(_flow,AutoForwardDiff(),λ) |
| 52 | +push!(df, ("Zygote", "my-euler", jacobian(_flow,AutoZygote(),λ))) |
| 53 | +push!(df, ("ForwardDiff", "my-euler", jacobian(_flow,AutoForwardDiff(),λ))) |
| 54 | +# Zygote with numerical integration |
| 55 | +function _flow_int(λ) |
| 56 | + ivp = ODEProblem(fun1, funx0(λ), (t0,tf), λ) |
| 57 | + #algo = get(ode_kwargs, :alg, Tsit5()) |
| 58 | + #println("algo = ", algo) |
| 59 | + |
| 60 | + sol = solve(ivp, alg = Euler(),reltol=reltol,abstol=abstol,adaptive=false, dt = (tf-t0)/N) |
| 61 | + return sol.u[end] |
| 62 | +end |
| 63 | + |
| 64 | +#dict_Zygote[:Euler] = jacobian(_flow_int,AutoZygote(),λ) |
| 65 | +#dict_ForwardDiff[:Euler] = jacobian(_flow_int,AutoForwardDiff(),λ) |
| 66 | +push!(df, ("Zygote", "Euler", jacobian(_flow_int,AutoZygote(),λ))) |
| 67 | +push!(df, ("ForwardDiff", "Euler", jacobian(_flow_int,AutoForwardDiff(),λ))) |
| 68 | +# Zygote with ∂λ_flow |
| 69 | +∂λ_flow = CTDiffFlow.build_∂λ_flow(fun1,t0,funx0,tf,λ; backend=AutoZygote()) |
| 70 | +#dict_Zygote[:CTDiffFlowZygoteEuler] = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol, alg = Euler(),adaptive=false, dt = (tf-t0)/N)#,print_times=false) |
| 71 | +push!(df, ("Zygote", "CTDiffFlow-Euler", ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol, alg = Euler(),adaptive=false, dt = (tf-t0)/N))) |
| 72 | + |
| 73 | +# ForwardDiff with ∂λ_flow |
| 74 | +∂λ_flow = CTDiffFlow.build_∂λ_flow(fun1,t0,funx0,tf,λ; backend=AutoForwardDiff()) |
| 75 | +#dict_ForwardDiff[:CTDiffFlowForwardDiffEuler] = ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol, alg = Euler(),adaptive=false, dt = (tf-t0)/N)#,print_times=false) |
| 76 | +push!(df, ("ForwardDiff", "my-euler", ∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol, alg = Euler(),adaptive=false, dt = (tf-t0)/N))) |
| 77 | + |
| 78 | +function _flow(λ) |
| 79 | + T, X = myode43(fun1,funx0(λ),λ,(t0,tf),abstol,reltol) |
| 80 | + return X[end] |
| 81 | +end |
| 82 | + |
| 83 | +#dict_Zygote[:myode43Zygote] = jacobian(_flow,AutoZygote(),λ) |
| 84 | +#dict_ForwardDiff[:myode43ForwardDiff] = jacobian(_flow,AutoForwardDiff(),λ) |
| 85 | +push!(df, ("Zygote", "my-ode43", [NaN;;])) |
| 86 | +push!(df, ("ForwardDiff", "my-ode43", jacobian(_flow,AutoForwardDiff(),λ))) |
| 87 | + |
| 88 | +jacobian(_flow,AutoForwardDiff(),λ) |
| 89 | + |
| 90 | +latexify(df,arraystyle=:pmatrix,env=:tabular) |
| 91 | + |
| 92 | +sol_Zygote = df[in.(df.AutoDiff, Ref(["solution","Zygote"])),:] |
| 93 | +sol_ForwardDiff = df[in.(df.AutoDiff, Ref(["solution", "ForwardDiff"])),:] |
0 commit comments