# Parameters modified by events are not captured in ODE the solution

**URL:** <https://discourse.julialang.org/t/parameters-modified-by-events-are-not-captured-in-ode-the-solution/114592>\
**Category:** General Usage\
**Tags:** modelingtoolkit, events, catalyst\
**Created:** [May 22, 2024, 9:00pm UTC](https://discourse.julialang.org/t/parameters-modified-by-events-are-not-captured-in-ode-the-solution/114592 "2024-05-22T21:00:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![valerdam](https://avatars.discourse-cdn.com/v4/letter/v/4491bb/32.png) [@valerdam](https://discourse.julialang.org/u/valerdam)\
**Post date:** [May 22, 2024, 9:00pm UTC](https://discourse.julialang.org/t/parameters-modified-by-events-are-not-captured-in-ode-the-solution/114592/1 "2024-05-22T21:00:18Z")

</div>

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](https://discourse.julialang.org/t/callback-function-modifying-model-parameter/114478), 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.

```julia
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

```

```julia
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]) 

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 23, 2024, 9:23pm UTC](https://discourse.julialang.org/t/parameters-modified-by-events-are-not-captured-in-ode-the-solution/114592/2 "2024-05-23T21:23:23Z")

</div>

We have a whole set of PRs currently dedicated to giving a good solution to this problem. I expect it to land in the next week or so. You can follow the final pull request here:

> <https://github.com/SciML/ModelingToolkit.jl/pull/2728>
>
> Requires:
> \- https://github.com/SciML/SymbolicIndexingInterface.jl/pull/75
> \- ht…tps://github.com/SciML/RecursiveArrayTools.jl/pull/372
> \- https://github.com/SciML/SciMLBase.jl/pull/645
> \- https://github.com/SciML/ModelingToolkit.jl/pull/2676
> - PR is based off of this branch right now, will rebase on \`master\` once the above PR is merged
> 
> \## Checklist
> 
> \- \[\] Appropriate tests were added
> \- \[\] Any code changes were done in a way that does not break public API
> \- \[\] All documentation related to code changes were updated
> \- \[\] The new code follows the
> \[contributor guidelines\](https://github.com/SciML/.github/blob/master/CONTRIBUTING.md), in particular the \[SciML Style Guide\](https://github.com/SciML/SciMLStyle) and
> \[COLPRAC\](https://github.com/SciML/COLPRAC).
> \- \[\] Any new documentation only uses public API
>   
> \## Additional context
> 
> Add any other context about the problem here.
