Description
I'm curious if it is possible to add/remove a specific Callback
from a CallbackSet
during runtime in the body of an affect!
function. For example, I might have a large list of Callback
objects that I only need to run a finite (possibly unknown) number of times before knowing for certain I don't need to apply them again. If these have expensive condition
functions, I would like to ensure I don't run them unnecessarily. It's possible I could have condition
check some kind of global array before running, but (a) I would rather not create unnecessary global variables and (b) removing a Callback
from a CallbackSet
sounds like a much cleaner solution.
As a simple example, here's a Callback
that removes the k
th Callback
from an (assumed) CallbackSet
:
function removeKthCB(tk::Float64, k::Int64)
return ContinuousCallback(
function condition(u, t, integrator)
return t - tk
end,
function affect!(integrator)
integrator.opts.callback.remove(k) # This line is the important line
end
)
end
I know this isn't a perfect solution, or if there's a more "proper" way of going about this, but something along these lines would be great to have!