|
| 1 | +using ADTypes |
| 2 | +using DifferentiationInterface |
| 3 | +using ModelingToolkit, DifferentialEquations |
| 4 | +using TaylorDiff, ForwardDiff |
| 5 | +using Enzyme, Zygote, ReverseDiff |
| 6 | +using SciMLSensitivity |
| 7 | + |
| 8 | +@parameters a |
| 9 | +@variables t x1(t) |
| 10 | +D = Differential(t) |
| 11 | +states = [x1] |
| 12 | +parameters = [a] |
| 13 | + |
| 14 | +@named pre_model = ODESystem([D(x1) ~ a * x1], t, states, parameters) |
| 15 | +model = structural_simplify(pre_model) |
| 16 | + |
| 17 | +ic = Dict(x1 => 1.0) |
| 18 | +p_true = Dict(a => 2.0) |
| 19 | + |
| 20 | +problem = ODEProblem{true, SciMLBase.FullSpecialize}(model, ic, [0.0, 1.0], p_true) |
| 21 | +soln = ModelingToolkit.solve(problem, Tsit5(), abstol = 1e-12, reltol = 1e-12) |
| 22 | +display(soln(0.5, idxs = [x1])) |
| 23 | + |
| 24 | +function different_time(new_ic, new_params, new_t) |
| 25 | + #newprob = ODEProblem{true, SciMLBase.FullSpecialize}(model, new_ic, [0.0, new_t*2], new_params) |
| 26 | + #newprob = remake(problem, u0=new_ic, tspan = [0.0, new_t], p = new_params) |
| 27 | + newprob = remake(problem, u0 = new_ic, tspan = [0.0, new_t], p = new_params) |
| 28 | + newprob = remake(newprob, u0 = typeof(new_t).(newprob.u0)) |
| 29 | + new_soln = ModelingToolkit.solve(newprob, Tsit5(), abstol = 1e-12, reltol = 1e-12) |
| 30 | + return (soln(new_t, idxs = [x1])) |
| 31 | +end |
| 32 | + |
| 33 | +function just_t(new_t) |
| 34 | + return different_time(ic, p_true, new_t)[1] |
| 35 | +end |
| 36 | +display(different_time(ic, p_true, 2e-5)) |
| 37 | +display(just_t(0.5)) |
| 38 | + |
| 39 | +#display(ForwardDiff.derivative(just_t,1.0)) |
| 40 | +display(TaylorDiff.derivative(just_t, 1.0, 1)) #isnan error |
| 41 | +#display(value_and_gradient(just_t, AutoForwardDiff(), 1.0)) |
| 42 | +#display(value_and_gradient(just_t, AutoReverseDiff(), 1.0)) |
| 43 | +#display(value_and_gradient(just_t, AutoEnzyme(Enzyme.Reverse), 1.0)) |
| 44 | +#display(value_and_gradient(just_t, AutoEnzyme(Enzyme.Forward), 1.0)) |
| 45 | +#display(value_and_gradient(just_t, AutoZygote(), 1.0)) |
0 commit comments