Skip to content

new ExtraEquationsSystemException in 8.7.0 #1526

@visr

Description

@visr

I upgdraded to the newly released MTK 8.7.0 and noticed my system started failing at structural_simplify. This happens since #1525.

It's possible that this is a usage error, but I haven't been able to find it. Here is my simplified system, a linear reservoir that is both draining by itself, and has an extraction that stops if storage gets low.

using ModelingToolkit

@variables t

"S [m³]: storage"
@connector function Storage(; name, S0 = 0.0)
    vars = @variables S(t) = S0
    ODESystem(Equation[], t, vars, []; name)
end

"Q [m3 s⁻¹]: volumetric flux"
@connector function Discharge(; name, Q0 = 0.0)
    vars = @variables Q(t) = Q0 [connect = Flow]
    ODESystem(Equation[], t, vars, []; name)
end

function Bucket(; name, S0, α)
    @named storage = Storage(; S0)
    @named x = Discharge()
    @named o = Discharge()
    (; S) = storage

    pars = @parameters α = α
    D = Differential(t)

    eqs = Equation[
        # Q(S) rating curve
        -o.Q ~ α * S
        # storage / balance
        D(S) ~ x.Q + o.Q
    ]
    compose(ODESystem(eqs, t, [], pars; name), storage, x, o)
end

"Extract water if there is storage left"
function User(; name, demand)
    @named x = Discharge(Q0 = demand)
    @named storage = Storage()
    pars = @parameters demand = demand
    (; Q) = x
    (; S) = storage

    eqs = Equation[
        # smoothly reduce demand to 0 around S=1 with smoothness 0.1
        Q ~ demand * (0.5 * tanh((S - 1.0) / 0.01) + 0.5)
    ]
    compose(ODESystem(eqs, t, [], pars; name), x, storage)
end

@named bucket = Bucket= 2.0, S0 = 3.0)
@named user = User(demand = 3.0)

eqs = [
    connect(user.x, bucket.x)
    connect(user.storage, bucket.storage)
]

@named _sys = ODESystem(eqs, t, [], [])
@named sys = compose(_sys, [bucket, user])
sim = structural_simplify(sys)

On MTK 8.6.0 this results in a 1 equation system:

Model sys with 1 equations
States (1):
  bucket₊storage₊S(t) [defaults to 3.0]
Parameters (2):
  bucket₊α [defaults to 2.0]   
  user₊demand [defaults to 3.0]
Incidence matrix:sparse([1, 1], [1, 2], Num[×, ×], 1, 2)

But on MTK 8.7.0 I get:

ERROR: ExtraEquationsSystemException: The system is unbalanced. There are 2 highest order derivative variables and 3 equations.
More equations than variables, here are the potential extra equation(s):

stacktrace and version info
Stacktrace:
 [1] error_reporting(state::TearingState{ODESystem}, bad_idxs::Vector{Int64}, n_highest_vars::Int64, iseqs::Bool)
   @ ModelingToolkit.StructuralTransformations C:\Users\visser_mn\.julia\packages\ModelingToolkit\XinBN\src\structural_transformation\utils.jl:27
 [2] check_consistency(state::TearingState{ODESystem})
   @ ModelingToolkit.StructuralTransformations C:\Users\visser_mn\.julia\packages\ModelingToolkit\XinBN\src\structural_transformation\utils.jl:66
 [3] structural_simplify(sys::ODESystem; simplify::Bool)
   @ ModelingToolkit C:\Users\visser_mn\.julia\packages\ModelingToolkit\XinBN\src\systems\abstractsystem.jl:928
 [4] structural_simplify(sys::ODESystem)
   @ ModelingToolkit C:\Users\visser_mn\.julia\packages\ModelingToolkit\XinBN\src\systems\abstractsystem.jl:925

julia> versioninfo()
Julia Version 1.8.0-beta3
Commit 3e092a2521 (2022-03-29 15:42 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 3 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 3

(8.7) pkg> st
Status `D:\repo\bach\8.7\Project.toml`
  [0c46a032] DifferentialEquations v7.1.0
  [961ee093] ModelingToolkit v8.7.0
  [1dea7af3] OrdinaryDiffEq v6.9.0
  [91a5bcdd] Plots v1.27.5
  [0c5d862f] Symbolics v4.4.1
  [37e2e46d] LinearAlgebra

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions