MTK: connection of flow variable forces it to zero

Hello,
I encountered some problem in MTK which I could recreate in the following MWE:

using Pkg
pkg"activate --temp"
pkg"add ModelingToolkit"
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as Dt

@connector Connector begin
    potential(t)
    flow(t), [connect=Flow]
end
@mtkmodel Storage begin
    @components begin
        pin = Connector()
    end
    @variables begin
        potential(t)
    end
    @equations begin
        Dt(potential) ~ pin.flow
    end
end

@named storage = Storage()
@named pin = Connector()
@parameters begin
    inputflow
end
@variables begin
    outputpotential(t)
end
eqs = [inputflow ~ pin.flow,
       outputpotential ~ pin.potential,
       connect(pin, storage.pin)]
@named sys = ODESystem(eqs, t; systems=[pin, storage])

structural_simplify(sys)

Which leads to an error of structural_simplify because the system is overconstraint:

ERROR: ExtraEquationsSystemException: The system is unbalanced. There are 6 highest order derivative variables and 7 equations.
More equations than variables, here are the potential extra equation(s):
 0 ~ pin₊flow(t)

It even points out an equation, but I don’t know where this equation is coming from in the first place. I think I have some deep misunderstand about how flow connectors work. I thought I would model the following system, which should cleanly reduce to just a single ODE:

What am I doing wrong here? Where does the 0 ~ pin₊flow(t) constraint come from?

@mtkmodel Fixed begin
    @components begin
        pin = Connector()
    end
    @parameters begin
        inputflow
    end
    @variables begin
        outputpotential(t)
    end
    @equations begin
        inputflow ~ pin.flow
        outputpotential ~ pin.potential
    end
end
@named storage = Storage()
@named fixed = Fixed()
eqs = [connect(storage.pin, fixed.pin)]
@named sys = ODESystem(eqs, t; systems=[storage, fixed])
simp = structural_simplify(sys)
full_equations(simp)

Interestingly, once I define another subsystem and don’t mix connections and equations it works…

Open an issue. I at least cannot eyeball it.

1 Like

Allright thanks, issue opened `connect` can lead to wrong flow equations · Issue #3008 · SciML/ModelingToolkit.jl · GitHub