# Initial and final affect for timed periodic event in ModelingToolkeit

**URL:** https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [September 25, 2024, 9:02am UTC](https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849 "2024-09-25T09:02:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jmaedler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmaedler/32/212218_2.png) [@jmaedler](https://discourse.julialang.org/u/jmaedler)
#### Post date: [September 25, 2024, 9:02am UTC](https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849/1 "2024-09-25T09:02:09Z")

</div>

Hi everyone,

I am working on an adapter for virtual commissioning using [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl). More specifically, I use discrete callbacks (timed → periodic) to read inputs from and write output to a soft-controller. Is there a way to set the `initial_affect` and the `final_affect` arguments from [DiffEqCallbacks.jl](https://github.com/SciML/DiffEqCallbacks.jl) using [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl)? Here is my MWE:

```julia
using ModelingToolkit, DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as D

function affect!(integ, u, p, ctx)
    println("Time: $(integ.t)")    
end

@mtkmodel SimpleSystem begin
    @parameters begin
        m = 1.0
    end
    @variables begin
        y(t) = 0.0
    end
    @equations begin
        D(y) ~ m
    end
    @discrete_events begin
        1 => (affect!,[],[],[],nothing)
    end
end

@mtkbuild sys = SimpleSystem()
prob = ODEProblem(sys, Pair[], (0, 10.0))
sol = solve(prob)

```

If you run this code the event will not be triggered for t = 0.0 nor 10.0. If explicitly add additional events like this:

```julia
    @discrete_events begin
        [0,10] => (affect!,[],[],[],nothing)
        1 => (affect!,[],[],[],nothing)
    end

```

both events will be called twise. I guess this is related to the solver starting at some \Delta t in the first case (and thus overstepping 0.0) and being forced to start with 0.0 in the second case. Is there currently a way arround this behavior in [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl)?

---

<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: [September 25, 2024, 9:51am UTC](https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849/2 "2024-09-25T09:51:11Z")

</div>

Can you open an issue? There’s explicit handling in events for DiffEq for initial and final, but I’m not sure we’ve made those into options with MTK yet.

---

<div class="post-metadata">

### Author: ![jmaedler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmaedler/32/212218_2.png) [@jmaedler](https://discourse.julialang.org/u/jmaedler)
#### Post date: [September 25, 2024, 9:52am UTC](https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849/3 "2024-09-25T09:52:22Z")

</div>

Ok, I will.

---

<div class="post-metadata">

### Author: ![jmaedler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmaedler/32/212218_2.png) [@jmaedler](https://discourse.julialang.org/u/jmaedler)
#### Post date: [September 25, 2024, 9:57am UTC](https://discourse.julialang.org/t/initial-and-final-affect-for-timed-periodic-event-in-modelingtoolkeit/119849/4 "2024-09-25T09:57:30Z")

</div>

Done! Thanks, Chris!

> <https://github.com/SciML/ModelingToolkit.jl/issues/3074>
>
> \*\*Question❓\*\*
> 
> Hi everyone,
> 
> I am working on an adapter for virtual commissi…oning using \[ModelingToolkit.jl\](https://github.com/SciML/ModelingToolkit.jl). More specifically, I use discrete callbacks (timed --\> periodic) to read inputs from and write output to a soft-controller. Is there a way to set the \`initial\_affect\` and the \`final\_affect\` arguments from \[DiffEqCallbacks.jl\](https://github.com/SciML/DiffEqCallbacks.jl) using \[ModelingToolkit.jl\](https://github.com/SciML/ModelingToolkit.jl)? Here is my MWE:
> 
> \`\`\`julia
> using ModelingToolkit, DifferentialEquations
> using ModelingToolkit: t\_nounits as t, D\_nounits as D
> 
> function affect!(integ, u, p, ctx)
> println("Time: $(integ.t)")    
> end
> 
> @mtkmodel SimpleSystem begin
> @parameters begin
> m = 1.0
> end
> @variables begin
> y(t) = 0.0
> end
> @equations begin
> D(y) ~ m
> end
> @discrete\_events begin
> 1 =\> (affect!,\[\],\[\],\[\],nothing)
> end
> end
> 
> @mtkbuild sys = SimpleSystem()
> prob = ODEProblem(sys, Pair\[\], (0, 10.0))
> sol = solve(prob)
> \`\`\`
> If you run this code the event will not be triggered for t = 0.0 nor 10.0. If you explicitly add additional events like this:
> 
> \`\`\`julia
> @discrete\_events begin
> \[0,10\] =\> (affect!,\[\],\[\],\[\],nothing)
> 1 =\> (affect!,\[\],\[\],\[\],nothing)
> end
> \`\`\`
> 
> both events will be called twise. I guess this is related to the solver starting at some $\\Delta t$ in the first case (and thus overstepping 0.0) and being forced to start with 0.0 in the second case. Is there currently a way arround this behavior in \[ModelingToolkit.jl\](https://github.com/SciML/ModelingToolkit.jl)?
