OrdinaryDiffEq.jl icon indicating copy to clipboard operation
OrdinaryDiffEq.jl copied to clipboard

DEDataArrays not supported with some solvers?

Open vlepori opened this issue 5 years ago • 5 comments

Problem with DEDataArray fails with algorithm Rosenbrock23(), but seems to work with most others (eg Tsit5()). Not a big deal but wanted to report it. Simple example:

using DifferentialEquations

mutable struct VType{T} <: DEDataVector{T}
    x::Array{T,1}
    r::Array{T,1}
    a::Array{T,2}
end
function f(du,u,p,t) 
  temp = u.a*u
  for i in 1:length(du)
    du[i] =  u[i]*(u.r[i] - temp[i])
  end
end
function condition(u,t,integrator)
  t > 40
end
function affect!(integrator)
  for c in full_cache(integrator)
    c.r[1] = 1.2
    c.r[2] = 1.0
  end
end
save_positions = (true,true)
cb = DiscreteCallback(condition, affect!, save_positions=save_positions)
u0=VType([10.0,10.0], [1.0,1.2], [0.01 0.005; 0.005 0.01])
tspan = [0.0, 1.0e+2]
prob = ODEProblem(f,u0,tspan)
sol = solve(prob, Rosenbrock23(), callback = cb, saveat = 1) # fails
sol = solve(prob, Tsit5(), callback = cb, saveat = 1); # works

Also, unrelated, but defining f without argument p also seems to break the program, although p is never used (?)

vlepori avatar Sep 16 '20 14:09 vlepori

Also, unrelated, but defining f without argument p also seems to break the program, although p is never used (?)

That is intentional, ODE problems require you to use the signature f(u, p, t) for out-of-place functions and f(du, u, p, t) for in-place functions.

devmotion avatar Sep 16 '20 14:09 devmotion

Yeah, I'm not sure how possible it is to fix this case, which is why more and more I'm moving to remove DEDataArrays to an external package and not have them be in the DiffEq docs.

ChrisRackauckas avatar Sep 16 '20 16:09 ChrisRackauckas

Thank you for the answer @ChrisRackauckas . It seems to concern all of the stiff solvers, which I'm afraid makes the DEDataArray approach unfeasible for me. Do you have any advice on how to otherwise pass on / where to store quantities that are changed with callbacks during integration, and whose values over time I later need to retrieve? I was able to make it work reading and writing to global variables, but it seemed like pretty bad style, and possibly inefficient.

Thanks @devmotion , missed that one!

vlepori avatar Sep 17 '20 12:09 vlepori

Have you seen ComponentArrays.jl? Maybe you could use it instead of DEDataArrays.

devmotion avatar Sep 17 '20 13:09 devmotion

Thanks for the tip, I looked into it but there seems to be no method to resize! a ComponentArray and its SubArrays yet, so one cannot change the system's size while solving the ODE.

vlepori avatar Sep 18 '20 08:09 vlepori