Auxiliary variable in MTK connector

Consider the MTK Pin example.

@connector Pin begin
    v(t) = 0.0, [description = "Voltage"]
    i(t), [connect = Flow]
end

Suppose I wanted to also define the auxiliary variable p(t) = v * i (for power), such that I could refer to pin.p. I can’t figure out how to do that. The docs for connector say that connectors can be complex:

Similar to @mtkmodel, @connector accepts begin blocks of @components, @equations, @extend, @parameters, @structural_parameters, @variables.

But @variables seems to be a no-op (same effect as when defining directly with @variables).

@connector Pin begin
    v(t) = 0.0, [description = "Voltage"]
    i(t), [connect = Flow]
    p(t), [connect = Flow]
    @equations begin
       p = v * i
    end
end

seems to work, but

  • A) It yields a warning about unbalanced connectors
  • B) It will generate a lot of redundant connecting equations. p really is an auxiliary variable, no need to “connect” it (and I shouldn’t have to specify its connect property)

Try

p(t), [output = true]

instead, this indicates that p is a computed variable and I believe this should remove the warnings about unbalanced connectors.

1 Like

If I am not mistaken

p(t), [connect = Stream]

should do the job.
(At least according to the doc: Symbolic Metadata · ModelingToolkit.jl)

Thank you for the suggestion. If I start from a system that runs and add p(t), [output = true] to the connector, it still runs (which seems reasonable to me; I’m not using p at all), but when I add

    @equations begin
       p = 5
    end

in the connector, I get a ExtraEquationsSystemException:. This doesn’t feel right: I just added a dummy variable whose value is 5 to each connector. It should have no impact.

Variables in connectors have implicit equations associated with them, added when you connect two connectors together. If you add an equation to the connector, these are then added too many times.

But then coming back to the original question: is there any way to define an auxiliary variable in a connector? To take an even sillier example:

@connector Pin begin
    v(t)
    i(t), [connect = Flow]
    v_in_millivolt = v * 1000
end

I just want MTK to treat pin.v_in_millivolt as shorthand for pin.v * 1000. Is that not possible? Obviously, I don’t want any connection equations associated to v_in_millivolt.

I don’t think that’s possible, if it’s not a connector variable it probably has to be defined outside of the connector. I guess an AbstractConnectType for “no connection” could be added to support such a use case.

1 Like