It seems that parameters modified by callback events are not captured as time dependent in the ODE solution.
Using a slight modification of the example in here, parameter k
changes from 1.0
to 5.0
at time t=5
. However the solution reports only the last value of the parameter irrespective of the time.
Many thanks.
In Julia 1.9.4
[479239e8] Catalyst v13.5.1
[0c46a032] DifferentialEquations v7.13.0
⌅ [961ee093] ModelingToolkit v8.75.0
⌅ [1dea7af3] OrdinaryDiffEq v6.66.0
[91a5bcdd] Plots v1.40.4
⌅ [2efcf032] SymbolicIndexingInterface v0.3.1
using Catalyst, DifferentialEquations, Plots
@parameters k
rn = @reaction_network begin
(k,1), X1 <--> X2
end
u0 = [:X1 => 10.0,:X2 => 0.0]
tspan = (0.0, 20.0)
p = [:k => 1.0]
oprob = ODEProblem(rn, u0, tspan, p)
condition = [5.0]
# this is the version that does not work (at least for me)
# affect!(integrator) = integrator[:k] = 5.0
# however this works
affect!(integrator) = setp(integrator.f.sys, :k)(integrator, 5.0)
ps_cb = PresetTimeCallback(condition, affect!)
sol = solve(deepcopy(oprob); callback = ps_cb)
println( "k = 5 if t > 5 so for t = 6 it k should be 1 ----- sol(6,idx=k)) is ", sol(6,idxs=k), " OK " )
println( "k = 1 if t < 5 so for t = 4 it k should be 1 ----- sol(4,idx=k)) is ", sol(4,idxs=k), " NOT OK !!!" )
plot(sol)
# the following does not work !!!
plot(sol, idxs=[:X1; :X2; :k])