# How to use discrete events with an affect

**URL:** https://discourse.julialang.org/t/how-to-use-discrete-events-with-an-affect/108503
**Category:** Modelling & Simulations
**Tags:** question, modelingtoolkit
**Created:** [January 8, 2024, 9:51am UTC](https://discourse.julialang.org/t/how-to-use-discrete-events-with-an-affect/108503 "2024-01-08T09:51:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 8, 2024, 9:51am UTC](https://discourse.julialang.org/t/how-to-use-discrete-events-with-an-affect/108503/1 "2024-01-08T09:51:09Z")

</div>

I am trying to add an affect function to a discrete event. Not working:

```julia
function bb_affect!(integ, u, p, ctx)
    println(integ.u)
end

discrete_events = [[1.0, 4.0] => bb_affect!]            
@named sys = ODESystem(eqs, t; discrete_events = discrete_events)

```

Error message:

```julia
ERROR: MethodError: no method matching compile_affect(::var"#bb_affect!#17", ::ODESystem, ::Vector{SymbolicUtils.BasicSymbolic{Real}}, ::Vector{SymbolicUtils.BasicSymbolic{Real}}; expression::DataType, postprocess_affect_expr!::Nothing)

Closest candidates are:
  compile_affect(::ModelingToolkit.FunctionalAffect, ::Any, ::Any, ::Any; kwargs...)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:452
  compile_affect(::Vector{Equation}, ::Any, ::Any, ::Any; outputidxs, expression, checkvars, postprocess_affect_expr!, kwargs...)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:303
  compile_affect(::ModelingToolkit.SymbolicContinuousCallback, ::Any...; kwargs...)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:282

Stacktrace:
  [1] generate_timed_callback(cb::ModelingToolkit.SymbolicDiscreteCallback, sys::ODESystem, dvs::Vector{SymbolicUtils.BasicSymbolic{…}}, ps::Vector{SymbolicUtils.BasicSymbolic{…}}; postprocess_affect_expr!::Nothing, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:459
  [2] generate_timed_callback
    @ ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:456 [inlined]
  [3] generate_discrete_callback(cb::ModelingToolkit.SymbolicDiscreteCallback, sys::ODESystem, dvs::Vector{SymbolicUtils.BasicSymbolic{…}}, ps::Vector{SymbolicUtils.BasicSymbolic{…}}; postprocess_affect_expr!::Nothing, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:473
  [4] generate_discrete_callback
    @ ~/.julia/packages/ModelingToolkit/Gpzyo/src/systems/callbacks.jl:470 [inlined]
  [5] #331

```

What do I miss?

See: [Event Handling and Callback Functions · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Periodic-and-preset-time-events)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [January 8, 2024, 10:40am UTC](https://discourse.julialang.org/t/how-to-use-discrete-events-with-an-affect/108503/2 "2024-01-08T10:40:53Z")

</div>

OK, this works:

```julia
function bb_affect!(integ, u, p, ctx)
    println(integ.u)
end

discrete_events = [[1.0, 4.0] => (bb_affect!, [], [], nothing)]            
@named sys = ODESystem(eqs, t; discrete_events = discrete_events)

```

But in this tuple:

```julia
(bb_affect!, [], [], nothing)

```

what is the purpose of the second, third and forth element?

EDIT: Ok, the second element is a vector of symbolic state variables and the third a vector of the symbolic parameters. Which leaves two questions:

1. how can I access state variables that are part of the system, but not of the simplified system?
2. what is the forth element?

To answer my own questions:

1. This is not possible, but what IS possible is to keep variables of the system in the simplified system by using the following notation:

```julia
@variables Pr(t)=0 [irreducible = true]

```

Important is that the variable IS initialized, the value does not matter.

1. this is a free parameter for passing a context to the callback function which can be used if required.
