Hi everyone,
I am wondering how to correctly change the right hand side inside a DifferentialEquations callback.
If I don’t reinit the system runs through but gives wrong results (compared to just running new differential equations). If I reinit the integrator I get an infinite loop.
Any pointers?
mutable struct ode_switch
original_ode
fault_ode
fault::Bool
end
function (pgs::ode_switch)(u, t, integrator)
println("time $t") # To make sure the call back is really called
if ! pgs.fault
integrator.f = rhs(pgs.fault_ode)
integrator.u = find_valid_ic(integrator.f, integrator.u) # This takes care of the DAE/Massmatrix aspect
# reinit!(integrator, t0=t) # If I use reinit, I get an infinite loop with the callback being called over and over.
# step!(integrator) # I tried to do a step in order for that not to happen but it didn't help
pgs.fault = true
else
integrator.f = rhs(pgs.original_ode)
integrator.u = find_valid_ic(integrator.f, integrator.u) # This takes care of the DAE/Massmatrix aspect
# reinit!(integrator, t0=t) # If I use reinit, I get an infinite loop with the callback being called over and over.
# step!(integrator) # I tried to do a step in order for that not to happen but it didn't help
pgs.fault = false
end
end
timespan = (0., 10.)
faulttime = (1., 2.)
pgs = ode_switch(func1,func2)
cb = FunctionCallingCallback(pgs; funcat=faultime, func_start = false)
prob = ODEProblem(func1, x0, timespan, callback=cb)
solve(prob, Rodas4(autodiff=false))