MTK - simple case with `@mtkmodel` and standard library doesn't work

I’m experimenting with the new @mtkmodel in combination with the ModelingToolkitStandardLibrary. I get an error message…

My case is simple: a liquid tank “class” Tank_m with flow out given by gravity, encoded via @mtkmodel. [The _m part indicates “modified” – a modification of a model that works when I do not use ModelingToolkitStandardLibrary.]

using ModelingToolkit, ModelingToolkitStandardLibrary.Blocks

@variables t
Dt = Differential(t)

@mtkmodel Tank_m begin
    # Model parameters
    @parameters begin 
        ρ=1,    [description = "Liquid density"]
        A=5,    [description = "Cross sectional tank area"]
        K=5,    [description = "Effluent valve constant"]
        h_ς=3,  [description = "Scaling level in valve model"]
    # Model variables, with initial values needed
    @variables begin
        m(t)=1.5*ρ*A,   [description = "Liquid mass"]
        # ṁ_i(t),         [description = "Influent mass flow rate"]
        ṁ_e(t),         [description = "Effluent mass flow rate"]
        V(t),           [description = "Liquid volume"]
        h(t),           [description = "level"]
    # Components from ModelingToolkitStandardLibrary
    @components begin
        # We move ṁ_i from "variables" to "components"
        ṁ_i = RealInput()   # Influent mass flow rate
        y_ṁ_e = RealOutput()
    # Providing model equations
    @equations begin
        Dt(m) ~ ṁ_i.u - ṁ_e
        m ~ ρ*V
        V ~ A*h
        ṁ_e ~ K*sqrt(h/h_ς)
        y_ṁ_e.u ~ ṁ_e

Next, I make two copies of this liquid tank model and use MTK SL (standard library) to generate the input flow rate to the first tank, and then passes the output from the first tank as input to the second tank…

@mtkmodel Sys2Tank_m begin
    # Components used
    @components begin 
        ṁ = Constant(k=2)
        tank_1 = Tank_m()
        tank_2 = Tank_m()
    # Equations for connecting components
    @equations begin
        connect(ṁ.output, tank_1.ṁ_i)
        connect(tank_1.y_ṁ_e, tank_2.ṁ_i)

Executing these macros work, but then I get an error message:

@mtkbuild sys2tank_m_balanced = Sys2Tank_m()

leads to:

I’m sure it is a trivial thing I do incorrectly… Any ideas?
[Note: with fixes, the above code should now work.]

Could you make sure the MWE can be reproduced in a fresh Julia session? There are multiple undefined symbols in the MWE.

1 Like

Ahrg. Found the error. I’ll update the code above anyways. The key error was that I had forgotten to update the name of the class Tank to Tank_m in the Sys2Tank_m class.