# Reinit! doesn't work on MTK integrators

**URL:** <https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577>\
**Category:** Modelling & Simulations\
**Tags:** question, modelingtoolkit, differentialequation\
**Created:** [March 31, 2025, 10:51pm UTC](https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577 "2025-03-31T22:51:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [March 31, 2025, 10:51pm UTC](https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577/1 "2025-03-31T22:51:40Z")

</div>

It seems like `reinit!` doesn’t work for ModelingToolkit systems. Is this true, what is the workaround and why don’t I get an error or warning? Trying to run this mwe shows that the integrator state is unchanged after `reinit!`.

```julia
using OrdinaryDiffEq
using LinearAlgebra
using ModelingToolkit

# Create a simple ODE system
function simple_system()
    @independent_variables t
    D = Differential(t)
    
    @variables x(t) y(t) z(t)
    @parameters p
    
    eqs = [
        D(x) ~ y + p,
        D(y) ~ z,
        D(z) ~ -x - 0.1*y
    ]
    
    @named sys = ODESystem(eqs, t, [x, y, z], [p])
    sys = structural_simplify(sys)
    return sys
end

function demonstrate_issue()
    sys = simple_system()
    
    # Create two different initial states
    initial_u0 = [1.0, 0.0, 0.0]
    new_u0 = [5.0, 2.0, 3.0]
    
    # Create the problem with initial u0
    prob = ODEProblem(sys, initial_u0, (0.0, 10.0), [0.5])
    
    # Initialize with FBDF solver
    println("Testing with FBDF solver:")
    fbdf_integrator = init(prob, FBDF(); dt=0.1, abstol=1e-6, reltol=1e-6)
    
    println("Initial state: ", fbdf_integrator.u)
    
    # Try to reinitialize with new_u0
    reinit!(fbdf_integrator, new_u0)
    
    println("After reinit!: ", fbdf_integrator.u)
    println("Expected: ", new_u0)
    println("Are equal? ", fbdf_integrator.u == new_u0)
    
    # Compare with Tsit5 solver
    println("\nTesting with Tsit5 solver:")
    tsit_integrator = init(prob, Tsit5(); dt=0.1, abstol=1e-6, reltol=1e-6)
    
    println("Initial state: ", tsit_integrator.u)
    
    # Try to reinitialize with new_u0
    reinit!(tsit_integrator, new_u0)
    
    println("After reinit!: ", tsit_integrator.u)
    println("Expected: ", new_u0)
    println("Are equal? ", tsit_integrator.u == new_u0)
    
    # Reset cache and history
    println("\nTesting FBDF with reset cache:")
    reinit!(fbdf_integrator, new_u0; reset_dt=true, reinit_cache=true, erase_sol=true)
    
    println("After reset cache: ", fbdf_integrator.u)
    println("Expected: ", new_u0)
    println("Are equal? ", fbdf_integrator.u == new_u0)
end

demonstrate_issue()

```

Gives the output:

```julia
julia> include("mwe/reinit_problem.jl")
Initial state: [1.0, 0.0, 0.0]
After reinit!: [1.0, 0.0, 0.0]
Expected: [5.0, 2.0, 3.0]
Are equal? false

Testing with Tsit5 solver:
Initial state: [1.0, 0.0, 0.0]
After reinit!: [1.0, 0.0, 0.0]
Expected: [5.0, 2.0, 3.0]
Are equal? false

Testing FBDF with reset cache:
After reset cache: [1.0, 0.0, 0.0]
Expected: [5.0, 2.0, 3.0]
Are equal? false

```

---

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [April 1, 2025, 10:05am UTC](https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577/2 "2025-04-01T10:05:54Z")

</div>

Using `setu` works, but still, it would be nice to get some kind of warning when trying to use `reinit!`.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 1, 2025, 10:52am UTC](https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577/3 "2025-04-01T10:52:24Z")

</div>

> <https://github.com/SciML/ModelingToolkit.jl/issues/3451>
>
> \*\*Minimal Reproducible Example 👇\*\*
> 
> \`\`\`julia
> using ModelingToolkit, OrdinaryDiff…EqDefault
> 
> @independent\_variables t
> D = Differential(t)
> @mtkmodel Roessler begin
> @parameters begin
> a = 0.2
> b = 0.2
> c = 5.7
> end
> @variables begin
> x(t) = 1.0
> y(t) = 0.0
> z(t) = 0.0
> nlt(t) # nonlinear term
> end
> @equations begin
> D(x) ~ -y -z
> D(y) ~ x + a\*y
> D(z) ~ b + nlt
> nlt ~ z\*(x - c)
> end
> end
> 
> @mtkbuild roessler\_model = Roessler()
> 
> prob = ODEProblem(roessler\_model)
> integ = OrdinaryDiffEqDefault.init(prob)
> reinit!(integ, SVector(5, 5, 0.0))
> integ
> \`\`\`
> 
> \`\`\`
> 
> julia\> integ
> t: 0.0
> u: 3-element SVector{3, Float64} with indices SOneTo(3):
> 1.0
> 0.0
> 0.0
> \`\`\`
> 
> There are no error messages. The \`reinit!\` function simply now ignores the second argument and does not re-initialize the integrator at the given state. This is breaking for DynamicalSystems.jl, so I hope there is an easy fix!
> 
> Disclaimer: I have not checked whether this problem persists even for normally created \`ODEProblems\`.
> 
> - Output of \`using Pkg; Pkg.status()\`
> 
> \`\`\`julia
> pkg\> st -m ModelingToolkit OrdinaryDiffEqDefault
> Status \`C:\\Users\\datse\\.julia\\environments\\NLD\\Manifest.toml\`
> \[961ee093\] ModelingToolkit v9.65.0
> \[50262376\] OrdinaryDiffEqDefault v1.3.0
> \`\`\`
> 
> - Output of \`versioninfo()\`
> 
> 1.10.8.
> 
> Add any other context about the problem here.

I agree with you, this is a problem.

---

<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:** [April 3, 2025, 7:07pm UTC](https://discourse.julialang.org/t/reinit-doesnt-work-on-mtk-integrators/127577/4 "2025-04-03T19:07:47Z")

</div>

handled now.
