SciML / DifferentialEquations.jl

Multi-language suite for high-performance solvers of differential equations and scientific machine learning (SciML) components. Ordinary differential equations (ODEs), stochastic differential equations (SDEs), delay differential equations (DDEs), differential-algebraic equations (DAEs), and more in Julia.
https://docs.sciml.ai/DiffEqDocs/stable/
Other
2.87k stars 230 forks source link

deleteat! on integrator in callback fails for stiffness detection algorithms #665

Open vlepori opened 4 years ago

vlepori commented 4 years ago

As reported here. Changing the size of the ODE system during integration with a callback throws an error when using some functions (eg resize! works but deleteat! does not) if one uses the default automatic stiffness detection algorithm. The issue is fixed when specifying the solver.

using DifferentialEquations

const α = -0.3
function f(du,u,p,t)
  for i in 1:length(u)
    du[i] = α*u[i]
  end
end

function condition(u,t,integrator) # Event when event_f(u,t) == 0
  minimum(u)-0.01
end

function affect!(integrator)
  u = integrator.u
  #resize!(integrator,length(u)-1) # This works
  deleteat!(integrator, length(u)) # This errors
  nothing
end

callback = ContinuousCallback(condition,affect!)
u0 = [5, 3, 1]
tspan = (0.0,20.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,callback=callback) # specifying the algorithm fixes the problem
ChrisRackauckas commented 4 years ago

Changed the title to reflect what was found in the Discourse thread.

evan-wehi commented 1 year ago

Just wasted a couple of hours on this, would be nice to get it fixed.