Skip to content

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

@vlepori

Description

@vlepori

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions