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

deleteat! on integrator in callback fails for stiffness detection algorithms

Open vlepori opened this issue 5 years ago • 2 comments

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

vlepori avatar Sep 04 '20 15:09 vlepori

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

ChrisRackauckas avatar Sep 04 '20 16:09 ChrisRackauckas

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

evan-wehi avatar Aug 22 '23 00:08 evan-wehi