# Adding a sensor breaks MTK model

**URL:** https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964
**Category:** General Usage
**Tags:** modelingtoolkit
**Created:** [January 9, 2026, 2:47pm UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964 "2026-01-09T14:47:58Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [January 9, 2026, 2:47pm UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/1 "2026-01-09T14:47:58Z")

</div>

For some reason, adding a `ForceSensor` from `ModelingToolkitStandardLibrary` breaks the following model.

```julia-auto
using ModelingToolkit
import ModelingToolkit: connect, t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary
using DifferentialEquations

@named mass = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Mass(m=1)
@named spring = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Spring(k=1)
@named fix = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Fixed()
@named force = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Force()
@named fsensor = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.ForceSensor()

eqs = Equation[
    connect(mass.flange, spring.flange_b)
    connect(fix.flange, spring.flange_a)
]

@named model = ODESystem(eqs, t; systems=[mass, spring, fix])
sys = mtkcompile(model)
initial = Dict{Num,Any}([
    mass.s => 1.
    mass.v => 0.
]
)

prob = ODEProblem(sys, initial, (0.0, 100.0)) # Works
sol_mass_nosens = solve(prob, Rodas5())

eqs = Equation[
    connect(mass.flange, spring.flange_b)
    connect(fix.flange, spring.flange_a)
    connect(fix.flange, fsensor.flange_a)
    connect(fsensor.flange_b, mass.flange)
]

@named model = ODESystem(eqs, t; systems=[mass, spring, fix, fsensor])
sys = mtkcompile(model)
initial = Dict{Num,Any}([
    mass.s => 1.
    mass.v => 0.
]
)

prob = ODEProblem(sys, initial, (0.0, 100.0)) # Fails on initialization or returns nothing depending on initial_positions
sol_mass_sens = solve(prob, Rodas5())

```

I figured adding a sensor should only add some “slave” equations but not over constrain the system. Is this a bug in the `ModelingToolkitStandardLibrary` or am I missing something ?

---

<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: [January 11, 2026, 1:29am UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/2 "2026-01-11T01:29:07Z")

</div>

What is the error or warning?

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [January 12, 2026, 9:39am UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/3 "2026-01-12T09:39:57Z")

</div>

It’s a warning, stating that the system is overdetermined. So I’m guessing that the sensor enforces some variable (probably through its `Flange`s). But it leads to an empty solution.

> ┌ Warning: Initialization system is overdetermined. 2 equations for 0 unknowns. Initialization will default to using least squares. `SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. To suppress this warning pass warn\_initialize\_determined = false. To make this warning into an error, pass fully\_determined = true  
> └ @ ModelingToolkit D:\bamboo.julia\packages\ModelingToolkit\Ay2JZ\src\problems\initializationproblem.jl:106  
> ODEProblem with uType Nothing and tType Float64. In-place: true  
> Initialization status: OVERDETERMINED  
> Non-trivial mass matrix: false  
> timespan: (0.0, 100.0)  
> u0: nothing

If I input empty initial conditions, there is no warning, but the solution remains empty.

---

<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: [January 12, 2026, 11:02am UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/4 "2026-01-12T11:02:26Z")

</div>

> [@BambOoxX](#):
>
> If I input empty initial conditions, there is no warning, but the solution remains empty.

empty doesn’t mean anything bad? It could just mean that it simplified everything and can be interpolated at any time.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [January 12, 2026, 4:41pm UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/5 "2026-01-12T16:41:53Z")

</div>

Maybe I’m seeing this with an overly practical viewpoint, but generally a sensor should not change how a system behaves. In my example, I basically just want to monitor the force in the spring of a spring-mass system, using a dedicated sensor. This should not change the dynamics of the system. Or maybe I’m misunderstanding the use of sensors in MTK.  
This situation does not show with e.g `AccelerationSensor` for instance, to monitor the mass acceleration.

About the empty solution, initial and final points are the same, so nothing happens. If I come back to the previous initial conditions showing a warning, the simulation fails with an `InitialFailure`

> julia\> sol\_mass\_sens = solve(prob, Rodas5())  
> retcode: InitialFailure  
> Interpolation: 1st order linear  
> t: 2-element Vector{Float64}:  
> 0.0  
> 100.0  
> u: 2-element Vector{Vector{Float64}}:

which doesn’t make more sense.

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [January 12, 2026, 4:59pm UTC](https://discourse.julialang.org/t/adding-a-sensor-breaks-mtk-model/134964/6 "2026-01-12T16:59:37Z")

</div>

Ok, you can call me dumb.  
For some reason, I thought that to measure the force through the spring I should connect the `ForceSensor` to the same flanges as the spring. But no, like for a true force sensor, this should be done is series…

This works perfectly fine… Thanks for the help.

```julia-auto
using MIGALE
using ModelingToolkit
import ModelingToolkit: connect, t_nounits as t, D_nounits as D
using ModelingToolkitStandardLibrary
using DifferentialEquations

@named mass = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Mass(m=1)
@named spring = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Spring(k=1)
@named fix = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Fixed()
@named force = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.Force()
@named fsensor = ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition.ForceSensor()

eqs = Equation[
    connect(mass.flange, spring.flange_b)
    connect(fix.flange, spring.flange_a)
]

@named model = ODESystem(eqs, t; systems=[mass, spring, fix])
sys = mtkcompile(model)
initial = Dict{Num,Any}([
    mass.s => 1.
    mass.v => 0.
]
)

prob = ODEProblem(sys, initial, (0.0, 100.0)) # Works
sol_mass_nosens = solve(prob, Rodas5())

eqs = Equation[
    connect(fix.flange, spring.flange_a)
    connect(spring.flange_b, fsensor.flange_a)
    connect(fsensor.flange_b, mass.flange)
]

@named model = ODESystem(eqs, t; systems=[mass, spring, fix, fsensor])
sys = mtkcompile(model)
initial = Dict{Num,Any}([
    mass.s => 1.
    mass.v => 0.
]
)

prob = ODEProblem(sys, initial, (0.0, 100.0))
sol_mass_sens = solve(prob, Rodas5())

plot(sol_mass_nosens)
plot!(sol_mass_sens, color=:red, linestyle=:dash)

```
