Callback function modifying model parameter

valerdam
Mon, 20 May 2024 15:30:20 +0000
General Usage

I need to modify a parameter in an ODE problem in a callback. I found an example that does exactly what I want in here. (on the page search for PresetTimeCallback and it is the code including the first occurrence). Copied here for convenience:

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]
affect!(integrator) = integrator[:k] = 5.0
ps_cb = PresetTimeCallback(condition, affect!)

sol = solve(deepcopy(oprob); callback = ps_cb)
plot(sol)

When trying to run int in REPL it fails with the error message:

ERROR: LoadError: Parameter indexing is deprecated. Use `setp(sys, k)(integrator, 5.0)` to set parameter value.

The error seems to be in trying to modify the parameter value using integrator[:k].

Many thanks for your help.

16 posts - 4 participants