-
-
Notifications
You must be signed in to change notification settings - Fork 220
Time dependence in a non-autonomous linear ODE problem does not appear to propagate correctly to the solver #2661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Replacing 9c9
< function update_func(A, u, p, t)
---
> function update_func!(A, u, p, t)
15c15
< prob = ODEProblem(MatrixOperator(A0; update_func), u0, tspan)
---
> prob = ODEProblem(MatrixOperator(A0; update_func!), u0, tspan) Could this be a documentation issue rather than a bug? I don't know how it affects the results, but the order of |
Oh I see. Yeah... well we should check to make sure the right update func is defined and give a better error message, since that's a bit tricky. But yes, it seems like OrdinaryDiffEq is working in the sense that if you give it the right function it will do it, but what we should do is that if |
Could it be that there is still something wrong with the out-of-place Here is an example, slightly modified from the one above, where the two versions ( using Pkg; Pkg.activate(@__DIR__)
using OrdinaryDiffEq
using Plots
α = 0.1
t0 = 0.0
t1 = 10.0
dt = 0.01
p = (; α)
tspan = (t0, t1)
u0 = ones(2)
A0 = zeros(2, 2)
function update_func!(A, u, p, t)
A[1, 2] = - p.α * t
A[2, 1] = p.α * t
return nothing
end
function update_func(A, u, p, t)
newA = zero(2,2)
update_func!(newA, u, p, t)
return newA
end
F(p, t, t0) = p.α * (t^2 - t0^2) / 2
function analytic_solution(u0, p, t, t0)
u1 = cos(F(p, t, t0)) * u0[1] - sin(F(p, t, t0)) * u0[2]
u2 = sin(F(p, t, t0)) * u0[1] + cos(F(p, t, t0)) * u0[2]
return [u1, u2]
end
analytic_solution_matrix(times, u0, p, t0) = reduce(hcat, (t -> analytic_solution(u0, p, t, t0)).(times)) |> permutedims
function solve_and_plot(matrixOp, u0, tspan, p; plotKwargs...)
prob = ODEProblem(matrixOp, u0, tspan, p)
sol = solve(prob, MagnusGL8(); dt)
plot(sol; label=["u[1] (magnus)" "u[2] (magnus)"], seriestype=:line, lw=7, opacity=0.3, plotKwargs...)
plot!(sol.t, analytic_solution_matrix(sol.t, u0, p, t0); label=["u[1] (exact)" "u[2] (exact)"])
end
# In-place
A! = MatrixOperator(A0; update_func!)
p1 = solve_and_plot(A!, u0, tspan, p; title="in-place")
# Out-of-place
A = MatrixOperator(A0; update_func)
p2 = solve_and_plot(A, u0, tspan, p; title="out-of-place")
plot(p1, p2; size=(900, 400))
savefig("inplace_vs_outofplace.png") environment/versioninfo
(The Julia version is still 1.10.8, but I also tried fresh environments with 1.11.4 and the 1.12 beta with the same results) |
Yeah something seems funky in the out of place one. |
I tried to figure out what's going on and so far I think the out-of-place Putting a @show newA in At least from what I could see here There seems to be no "re-routing" of the function call between the in-place and out-of-place versions depending on which one was given. My idea would be to do some inspection on the provided
I'd be happy to give it a try implementing a solution, but I'm not sure the idea above is the right way to go. |
Describe the bug 🐞
To define time-dependent ODE problems for Non-autonomous Linear ODE / Lie Group Problems,
MatrixOperator
is used.The
update_func
keyword argument ofMatrixOperator
specifies the time dependence of the ODE problem.However, even when
update_func
is provided, the resulting numerical solution does not appear to reflect the time dependence of the ODE problem.The computed result was the same as without
update_func
.Expected behavior
The computed results should differ between cases with and without
update_func
specified.Minimal Reproducible Example 👇
Different behavior is expected between the solid and dashed lines.
Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
This is forwarded from this post. Please refer to the original post for more details on the ODE problem presented in this issue.
The text was updated successfully, but these errors were encountered: