# Terminate simulation on event using ModelingToolkit

**URL:** https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243
**Category:** General Usage
**Tags:** question, error, modelingtoolkit, events
**Created:** [October 17, 2025, 2:03pm UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243 "2025-10-17T14:03:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![klinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klinders/32/219046_2.png) [@klinders](https://discourse.julialang.org/u/klinders)
#### Post date: [October 17, 2025, 2:03pm UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243/1 "2025-10-17T14:03:34Z")

</div>

Hi all,

First of all, thank you all for your immense efforts on Julia and ModelingToolkit. I’ve been using it for over 2 years and I keep being impressed with the power and capabilities.

I’m trying to terminate an MTK simulation using an event description.

I have a minimal example below:

```julia-auto
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

function affect!(mod,obs,ctx,int)
    ModelingToolkit.terminate!(int)
end

function FOL(;name)
    @parameters begin
        τ = 3.0 # parameters
    end
    @variables begin
        x(t) = 0.0 # dependent variables
    end
    eq=[
        D(x) ~ (1 - x) / τ
    ]

    events = [x ~ 0.6]=>(affect!,(;))

    return System(eq, t; name=name, continuous_events=events)
end

using OrdinaryDiffEq
@mtkbuild fol = FOL()
prob = ODEProblem(fol, [], (0.0, 10.0))
sol = solve(prob)

using Plots
plot(sol)

```

I get the following error. The full description did not fit, so I just pasted the summary.

```julia-auto
MethodError: no method matching _generated_writeback(::OrdinaryDiffEqCore.ODEIntegrator{}, ::@NamedTuple{}, ::Vector{Float64})

Closest candidates are:
  _generated_writeback(::Any, ::NamedTuple{NS1}, !Matched::NamedTuple{NS2}) where {NS1, NS2}
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/80wTI/src/systems/imperative_affect.jl:148

```

I am running Julia v1.10.10 and MTK v10.26.0.

The problem is very trivial so I am surely missing something obvious…

Kind regards,

Koen Linders

---

<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: [October 23, 2025, 10:25am UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243/2 "2025-10-23T10:25:30Z")

</div>

Open an issue. I’m surprised that didn’t just work but we haven’t tested using terminations yet.

---

<div class="post-metadata">

### Author: ![klinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klinders/32/219046_2.png) [@klinders](https://discourse.julialang.org/u/klinders)
#### Post date: [October 23, 2025, 10:54am UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243/3 "2025-10-23T10:54:17Z")

</div>

Thank you for the response Chris! I’ll make an issue 👍

---

<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: [October 23, 2025, 11:02am UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243/4 "2025-10-23T11:02:40Z")

</div>

Actually it was a small easy bug

> <https://github.com/SciML/MethodOfLines.jl/pull/484>
>
> \## Summary
> Fixes MethodError in staggered\_discretize.jl by passing \`prob.p\` inst…ead of \`nothing\` to \`prob.f\` calls.
> 
> This resolves the error \`MethodError: no method matching view(::Nothing, ::UnitRange{Int64})\` that occurred when using staggered grids with periodic boundary conditions.
> 
> \## Problem
> The \`symbolic\_trace\` function in \`src/discretization/staggered\_discretize.jl\` was calling \`prob.f\` with \`nothing\` as the parameters argument, but the generated \`ODEFunction\` expected actual parameters to perform view operations on parameter arrays.
> 
> \## Solution
> Changed lines 33-34 to pass \`prob.p\` instead of \`nothing\`:
> \`\`\`julia
> du1 = prob.f(tracevec\_1, prob.p, 0.0);
> du2 = prob.f(tracevec\_2, prob.p, 0.0);
> \`\`\`
> 
> \## Test plan
> \- \[x\] Tested with the example from the discourse post
> \- \[x\] Verified that discretization completes successfully
> \- \[x\] Verified that the solve runs without errors
> 
> Fixes: https://discourse.julialang.org/t/methodoflines-staggered-grids-methoderror-by-function-discretize/133312
> 
> 🤖 Generated with \[Claude Code\](https://claude.com/claude-code)

---

<div class="post-metadata">

### Author: ![klinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klinders/32/219046_2.png) [@klinders](https://discourse.julialang.org/u/klinders)
#### Post date: [November 5, 2025, 10:57am UTC](https://discourse.julialang.org/t/terminate-simulation-on-event-using-modelingtoolkit/133243/5 "2025-11-05T10:57:05Z")

</div>

Just for good bookkeeping: The issue was solved by @cryptic.ax on Github.

The solution is returning an empty NamedTuple like so

```julia
function affect!(mod,obs,ctx,int)
    ModelingToolkit.terminate!(int)

    return (;)
end

```

Thank you all for your help
