No method matching get_connection_type when connecting components

Hello,

I’m trying to get a feeling for MTK by converting the electric circuit example for acasual component based modeling to thermal components. Unfortunately I get this error when I’m using connect:

ht_eqs = [
    connect(capacitorA.heatPort, resistor.heatPortA)
    connect(resistor.heatPortB, capacitorB.heatPort)
    ]

ERROR: LoadError: MethodError: no method matching get_connection_type(::Type{HeatPort})

I’m not too experienced in Julia so maybe I’m even missing something obvious. Thanks in advance.

Here is my complete example as intended

HeatPort Example
using ModelingToolkit, Plots, DifferentialEquations

@variables t

@connector function HeatPort(;name)
    sts = @variables T(t) q(t) [connect = Flow]
    ODESystem(Equation[], t, sts, []; name=name, defaults=[T=>298.15, q=>0.0])
end

function HeatCapacitor(;name, m=1.0, cp=2.0)
    @named heatPort = HeatPort()
    @unpack T = heatPort
    ps = @parameters m=m cp=cp
    D = Differential(t)
    eqs = [
        m*cp*D(T) ~ heatPort.q
    ]
    compose(ODESystem(eqs, t, [], ps; name=name), heatPort)
end

function HeatResistor(;name, R=1e2)
    @named heatPortA = HeatPort()
    @named heatPortB = HeatPort()
    ps = @parameters R=R
    eqs = [
        0 ~ heatPortA.q + heatPortB.q
        heatPortA.T - heatPortB.T ~ heatPortA.q * R
    ]
    compose(ODESystem(eqs, t, [], ps; name=name), heatPortA, heatPortB)
end

@named capacitorA = HeatCapacitor(m=1.5, cp=2.1)
@named capacitorB = HeatCapacitor(m=0.75, cp=3.2)
@named resistor = HeatResistor(R=1e-1)

ht_eqs = [
    connect(capacitorA.heatPort, resistor.heatPortA)
    connect(resistor.heatPortB, capacitorB.heatPort)
    ]

@named _ht_model = ODESystem(ht_eqs, t)
@named ht_model = compose(_ht_model, [capacitorA, capacitorB, resistor])

sys = structural_simplify(ht_model)

u0 = [
    capacitorA.T = 50+273.15
    capacitorB.T = 25+273.15
]

prob = ODAEProblem(sys, u0, (0, 100.0))
sol = solve(prob, Tsit5())
plot(sol)

On a side note: Is there any ambition or ongoing project to build something similar to the Modelica Standard Library based on MTK?

1 Like

So the error was linked to my Julia installation. The code, safe minor carelessness, now runs!
There seems to be a modeling error though as the temperatures of the HeatCapacitor stay on the initial Temperatures. :thinking:

New Code
using ModelingToolkit, DifferentialEquations

@variables t

@connector function HeatPort(;name)
    sts = @variables T(t) q(t) [connect=Flow]
    ODESystem(Equation[], t, sts, []; name=name, defaults=[T=>298.15, q=>0.0])
end

function HeatCapacitor(;name, m=1.0, cp=2.0e3)
    @named heatPort = HeatPort()
    ps = @parameters m=m cp=cp
    sts = @variables T(t)
    D = Differential(t)
    eqs = [
        heatPort.T ~ T
        m*cp*D(T) ~ heatPort.q
    ]
    compose(ODESystem(eqs, t, sts, ps; name=name), heatPort)
end

function HeatResistor(;name, R=1e-2)
    @named heatPortA = HeatPort()
    @named heatPortB = HeatPort()
    ps = @parameters R=R
    eqs = [
        0 ~ heatPortA.q - heatPortB.q
        heatPortB.T - heatPortA.T ~ heatPortA.q * R
    ]
    compose(ODESystem(eqs, t, [], ps; name=name), heatPortA, heatPortB)
end

@named capacitorA = HeatCapacitor(m=1.5, cp=2.1e3)
@named capacitorB = HeatCapacitor(m=0.75, cp=3.2e3)
@named resistor = HeatResistor(R=1e0)

ht_eqs = [
    connect(capacitorA.heatPort, resistor.heatPortA)
    connect(resistor.heatPortB, capacitorB.heatPort)
    ]

@named _ht_model = ODESystem(ht_eqs, t)
@named ht_model = compose(_ht_model, [capacitorA, capacitorB, resistor])

sys = structural_simplify(ht_model)

u0 = [
    capacitorA.T => 50+273.15
    capacitorB.T => 25+273.15
]

prob = ODAEProblem(sys, u0, (0, 100.0))
sol = solve(prob, Tsit5())
#sol = solve(prob, Rodas4())

#plot(sol)
using UnicodePlots

lineplot(sol.t, sol[capacitorA.T])