# About @connectors

**URL:** <https://discourse.julialang.org/t/about-connectors/65083>\
**Category:** Modelling & Simulations\
**Created:** [July 22, 2021, 4:00am UTC](https://discourse.julialang.org/t/about-connectors/65083 "2021-07-22T04:00:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![juliascrub](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@juliascrub](https://discourse.julialang.org/u/juliascrub)\
**Post date:** [July 22, 2021, 4:00am UTC](https://discourse.julialang.org/t/about-connectors/65083/1 "2021-07-22T04:00:25Z")

</div>

Ive got a model defined as a connector

```julia
@connector function Pin(iv::Num; name)
   ...
end

```

And another model composed of these connectors:

```julia
function ModelWithConnectors(iv::Num; name)
    @named a = Pin(iv)
    @named b = Pin(iv)
     ... 
end

```

Is there any way to get info on the connectors (similar to the way you can query model states, parameters etc)? Id like to be able to find out how many connectors, connector names, etc

What exactly does the @connector do anyways? I cant seem to find anything on them in the docs

---

<div class="post-metadata">

**Author:** ![juliascrub](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@juliascrub](https://discourse.julialang.org/u/juliascrub)\
**Post date:** [July 23, 2021, 1:57am UTC](https://discourse.julialang.org/t/about-connectors/65083/2 "2021-07-23T01:57:42Z")

</div>

In order to get the connector names inside of a vector I ended up doing this:

```julia
function get_connections(system::ModelingToolkit.AbstractSystem)::Vector{String}
    connections::Vector{String} = []
    for subsystem in ModelingToolkit.get_systems(system)
        if isequal(subsystem.connection_type, Pin)
            push!(connections, string(subsystem.name))
        end
    end
    return connections
end

```

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [July 23, 2021, 1:58pm UTC](https://discourse.julialang.org/t/about-connectors/65083/3 "2021-07-23T13:58:16Z")

</div>

You can always put `@macroexpand` before a macro call in the REPL to see exactly what it’s doing. Here is the result of that on for the `Pin` case from the MTK docs (I deleted line number comments to make it more readable):

```julia
julia> @macroexpand @connector function Pin(;name)
           sts = @variables v(t)=1.0 i(t)=1.0
           ODESystem(Equation[], t, sts, []; name=name)
       end
quote
    struct Pin
        var"##275"->begin
                1
            end
    end
    function Pin(; name)
        function f()
            begin
                sts = begin
                        v = (identity)((Symbolics.wrap)((Symbolics.setdefaultval)(((Sym){(SymbolicUtils.FnType){NTuple{1, Any}, Real}}(:v))((Symbolics.value)(t)), begin
                                            1.0
                                        end)))
                        i = (identity)((Symbolics.wrap)((Symbolics.setdefaultval)(((Sym){(SymbolicUtils.FnType){NTuple{1, Any}, Real}}(:i))((Symbolics.value)(t)), begin
                                            1.0
                                        end)))
                        [v, i]
                    end
                ODESystem(Equation[], t, sts, []; name = name)
            end
        end
        res = f()
        if (isdefined)(res, :connection_type)
            var"#131#lens" = (identity)((Setfield.compose)((Setfield.PropertyLens){:connection_type}()))
            res = (Setfield.set)(res, var"#131#lens", Pin)
        else
            res
        end
    end
end

```

So it’s creating a struct called `Pin`, doing some fancy trickery to delete it’s default constructor (I think?), then creating a `Pin` method that calls your code and then sets the `connection_type` of the output to the type `Pin`.

This can be especially useful if your model has different types of connections. Let’s say it has electrical `Pin`s as defined above, but also rotational mechanical `Flange`s that are basically the same, but with angular velocity instead of current and torque instead of voltage. This might look like:

```julia
@connector function Flange(;name)
     sts = @variables τ(t)=1.0 ω(t)=1.0
     ODESystem(Equation[], t, sts, []; name=name)
end 

```

Now you can write pretty generic code that takes advantage of [circuit analogies](https://en.wikipedia.org/wiki/Mechanical%E2%80%93electrical_analogies#Other_energy_domains) for other domains. For example, you can have generic functions to get the flow and effort variables from a connector as:

```julia
flow_variable(connector) = flow_variable(ModelingToolkit.get_connection_type(connector), connector)
flow_variable(::Type{Pin}, connector) = connector.i
flow_variable(::Type{Flange}, connector) = connector.ω

effort_variable(connector) = effort_variable(ModelingToolkit.get_connection_type(connector), connector)
effort_variable(::Type{Pin}, connector) = connector.v
effort_variable(::Type{Flange}, connector) = connector.τ 

```

---

<div class="post-metadata">

**Author:** ![juliascrub](https://avatars.discourse-cdn.com/v4/letter/j/5daacb/32.png) [@juliascrub](https://discourse.julialang.org/u/juliascrub)\
**Post date:** [July 26, 2021, 12:47am UTC](https://discourse.julialang.org/t/about-connectors/65083/4 "2021-07-26T00:47:08Z")

</div>

Thank you! This was very helpful
