# Continuous event detection with @mtkmodel - multiple events

**URL:** <https://discourse.julialang.org/t/continuous-event-detection-with-mtkmodel-multiple-events/130037>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [June 19, 2025, 5:31pm UTC](https://discourse.julialang.org/t/continuous-event-detection-with-mtkmodel-multiple-events/130037 "2025-06-19T17:31:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RThul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rthul/32/209774_2.png) [@RThul](https://discourse.julialang.org/u/RThul)\
**Post date:** [June 19, 2025, 5:31pm UTC](https://discourse.julialang.org/t/continuous-event-detection-with-mtkmodel-multiple-events/130037/1 "2025-06-19T17:31:23Z")

</div>

I’m trying to detect multiple events in a system of ODEs. Basically, as soon as any variable crosses a threshold, the value of that variable is changed. I know I can achieve this by explicitly coding up these conditions within @continuous\_events. This is fine for a small number of equations (there are 3 in the MWE below), but it’s not feasible for large systems with hundreds of equations. I tried a for-loop like

```julia
for i in 1:N
      [x[i] ~ 0.5] => [x[i] ~ 1.0]
end

```

in the @continuous\_events block, but that gave an error message. I’m hoping that these event conditions can be vectorised in the same way as the equations in @equations. Does anyone know how to achieve this?

Here is the MWE

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

A = [1.0 -2.0 3.0;
     -4.0 2.3 5.2;
     2.1 3.5 -1.7]
As = -A'*A
N = size(As,1)

@mtkmodel LinODEsys begin
    @parameters begin
        Ω[1:N,1:N] 
    end
    @variables begin
        x(t)[1:N]
    end
    @equations begin
        collect(D.(x) .~ Ω*x)
    end
     @continuous_events begin
        [x[1] ~ 0.5] => [x[1] ~ 1.0]
        [x[2] ~ 0.5] => [x[2] ~ 1.0]
        [x[3] ~ 0.5] => [x[3] ~ 1.0]
     end
end

@mtkcompile linODEsys = LinODEsys()
u0 = [1.0, 1.0, 1.0]
u0_p = [linODEsys.x => u0]
Ω_p = [linODEsys.Ω[i,j] => As[i,j] for i in 1:N, j in 1:N] |> vec
u0_params = vcat(u0_p, Ω_p)

prob_sys = ODEProblem(linODEsys, u0_params, (0.0, 1.0))
sol_sys = solve(prob_sys)

```

---

<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:** [June 19, 2025, 7:13pm UTC](https://discourse.julialang.org/t/continuous-event-detection-with-mtkmodel-multiple-events/130037/2 "2025-06-19T19:13:12Z")

</div>

If you want to use an imperative construct like that you can’t use the macro.

---

<div class="post-metadata">

**Author:** ![RThul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rthul/32/209774_2.png) [@RThul](https://discourse.julialang.org/u/RThul)\
**Post date:** [June 20, 2025, 8:11am UTC](https://discourse.julialang.org/t/continuous-event-detection-with-mtkmodel-multiple-events/130037/3 "2025-06-20T08:11:57Z")

</div>

Ok. Thank you.
