# Auxiliary variable in MTK connector

**URL:** <https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [October 22, 2024, 8:18am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572 "2024-10-22T08:18:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [October 22, 2024, 8:18am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/1 "2024-10-22T08:18:50Z")

</div>

Consider the MTK Pin example.

```julia
@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](https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKLanguage/#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`).

```julia
@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)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 22, 2024, 8:58am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/2 "2024-10-22T08:58:19Z")

</div>

> [@cstjean](#):
>
> `p(t), [connect = Flow]`

Try

```julia
p(t), [output = true]

```

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

---

<div class="post-metadata">

**Author:** ![jmaedler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmaedler/32/212218_2.png) [@jmaedler](https://discourse.julialang.org/u/jmaedler)\
**Post date:** [October 22, 2024, 1:41pm UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/3 "2024-10-22T13:41:00Z")

</div>

If I am not mistaken

```julia
p(t), [connect = Stream]

```

should do the job.  
(At least according to the doc: [Symbolic Metadata · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/basics/Variable_metadata/#Connect))

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 10, 2024, 4:34am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/4 "2024-12-10T04:34:51Z")

</div>

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

```julia
    @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.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 10, 2024, 5:12am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/5 "2024-12-10T05:12:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [December 10, 2024, 5:20am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/6 "2024-12-10T05:20:12Z")

</div>

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:

```julia
@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`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [December 10, 2024, 6:29am UTC](https://discourse.julialang.org/t/auxiliary-variable-in-mtk-connector/121572/7 "2024-12-10T06:29:33Z")

</div>

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.
