# Structural\_simplify unexpected ERROR when applied twice in a row

**URL:** <https://discourse.julialang.org/t/structural-simplify-unexpected-error-when-applied-twice-in-a-row/95540>\
**Category:** Modelling & Simulations\
**Tags:** bug, error, modelingtoolkit\
**Created:** [March 4, 2023, 1:20pm UTC](https://discourse.julialang.org/t/structural-simplify-unexpected-error-when-applied-twice-in-a-row/95540 "2023-03-04T13:20:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![halejulia](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@halejulia](https://discourse.julialang.org/u/halejulia)\
**Post date:** [March 4, 2023, 1:20pm UTC](https://discourse.julialang.org/t/structural-simplify-unexpected-error-when-applied-twice-in-a-row/95540/1 "2023-03-04T13:20:17Z")

</div>

I mentioned this on slack, but I took me a while to create an MWE, so I’m posting it here again.

Here is the MWE:

```julia
using ModelingToolkit
@variables t u(t)[1:2] 
D=Differential(t)               
eqs = [u[1] ~ 1
        D(u[2]) ~ u[1]-u[2]]

@named sys = ODESystem(eqs) #Model sys with 2 equations
sys = structural_simplify(sys) #Model sys with 1 equations
sys = structural_simplify(sys) #ERROR: ExtraVariablesSystemException...

```

The error is

```julia
ERROR: ExtraVariablesSystemException: The system is unbalanced. There are 2 highest order derivative variables and 1 equations.
More variables than equations, here are the potential extra variable(s):
 (u(t))[2]
 (u(t))[1]
Stacktrace:
 [5] structural_simplify(sys::ODESystem, io::Nothing; simplify::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit C:\Users\*\.julia\packages\ModelingToolkit\QxF9d\src\systems\systems.jl:39 [6] structural_simplify (repeats 2 times)
   @ C:\Users\*\.julia\packages\ModelingToolkit\QxF9d\src\systems\systems.jl:19 [inlined]
 [7] top-level scope

```

I was informed on slack that this error is not expected, but is it a problem? I guess there isnt really a reason to use structural\_simplify in this way, should I open an Issue anyway?

---

<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:** [March 4, 2023, 2:00pm UTC](https://discourse.julialang.org/t/structural-simplify-unexpected-error-when-applied-twice-in-a-row/95540/2 "2023-03-04T14:00:08Z")

</div>

Open an issue.

---

<div class="post-metadata">

**Author:** ![halejulia](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@halejulia](https://discourse.julialang.org/u/halejulia)\
**Post date:** [March 4, 2023, 2:47pm UTC](https://discourse.julialang.org/t/structural-simplify-unexpected-error-when-applied-twice-in-a-row/95540/3 "2023-03-04T14:47:18Z")

</div>

Okay thanks.

> <https://github.com/SciML/ModelingToolkit.jl/issues/2108>
>
> \`\`\`
> using ModelingToolkit
> @variables t u(t)\[1:2\] 
> D=Differential(t) …     
> eqs = \[u\[1\] ~ 1
> D(u\[2\]) ~ u\[1\]-u\[2\]\]
> 
> @named sys = ODESystem(eqs) #Model sys with 2 equations
> sys = structural\_simplify(sys) #Model sys with 1 equations
> sys = structural\_simplify(sys) #ERROR: ExtraVariablesSystemException...
> \`\`\`
> 
> 
> This produces the following error:
> 
> \`\`\`
> ERROR: ExtraVariablesSystemException: The system is unbalanced. There are 2 highest order derivative variables and 1 equations.
> More variables than equations, here are the potential extra variable(s):
> (u(t))\[2\]
> (u(t))\[1\]
> Stacktrace:
> \[1\] error\_reporting(state::TearingState{ODESystem}, bad\_idxs::Vector{Int64}, n\_highest\_vars::Int64, iseqs::Bool, orig\_inputs::Set{Any})
> @ ModelingToolkit.StructuralTransformations C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\structural\_transformation\\utils.jl:44
> \[2\] check\_consistency(state::TearingState{ODESystem}, ag::ModelingToolkit.AliasGraph, orig\_inputs::Set{Any})
> @ ModelingToolkit.StructuralTransformations C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\structural\_transformation\\utils.jl:76
> \[3\] \_structural\_simplify!(state::TearingState{ODESystem}, io::Nothing; simplify::Bool, check\_consistency::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
> @ ModelingToolkit.SystemStructures C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\systems\\systemstructure.jl:540
> \[4\] structural\_simplify!(state::TearingState{ODESystem}, io::Nothing; simplify::Bool, check\_consistency::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
> @ ModelingToolkit.SystemStructures C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\systems\\systemstructure.jl:496
> \[5\] structural\_simplify(sys::ODESystem, io::Nothing; simplify::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
> @ ModelingToolkit C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\systems\\systems.jl:39
> \[6\] structural\_simplify (repeats 2 times)
> @ C:\\Users\\name\\.julia\\packages\\ModelingToolkit\\QxF9d\\src\\systems\\systems.jl:19 \[inlined\]
> \[7\] top-level scope
> @ c:\\Users\\name\\MWE\\MWE.jl:11
> \`\`\`
