# Automatically naming N similar elements in MTK

**URL:** <https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [December 20, 2022, 6:30am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888 "2022-12-20T06:30:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)\
**Post date:** [December 20, 2022, 6:30am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/1 "2022-12-20T06:30:40Z")

</div>

Hi,

I’d like a simple way of creating a “vector” of similar elements.

I.e. I’d like:

```julia
@named R = [Resistor(R=n) for n in 1:10]

```

to create a vector of `Resistor`s with resistances 1 to 10. These could be named e.g. `R_1`…`R_10`

It’s not difficult to achieve this but it’s a hack:

```julia
R = [Resistor(name=Symbol(:R_, n), R=n) for n in 1:10]

```

What do you think?

I _think_ it’s not too difficult to achieve with Julia’s `macro`s.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 22, 2022, 1:07am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/2 "2022-12-22T01:07:13Z")

</div>

The hack seems fine. The `name` field is exposed to take in symbols so that people can programmatically name things. I personally think such a macro extension would be overkill, but no reason to not make one.

---

<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 22, 2022, 6:59am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/3 "2022-12-22T06:59:44Z")

</div>

> [@dodoplus](#):
>
> `@named R = [Resistor(R=n) for n in 1:10]`

There is this syntax for creating several similar components

```julia
@named R 1:10 i -> Resistor(R=i)

```

it’s described in the docstring for `@named`, which also mentions that it’s not recommended to use it 🙂 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![dodoplus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dodoplus/32/30148_2.png) [@dodoplus](https://discourse.julialang.org/u/dodoplus)\
**Post date:** [December 23, 2022, 6:29am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/4 "2022-12-23T06:29:35Z")

</div>

> [@baggepinnen](#):
>
> There is this syntax for creating several similar components
> 
> ```julia
> @named R 1:10 i -> Resistor(R=i)
> 
> ```
> 
> it’s described in the docstring for `@named`, which also mentions that it’s not recommended to use it 🙂 🤷‍♂️

Didn’t know that…  
Seems that creating identical components is fine, though:

```julia-repl
julia> @named y[1:3] = foo(x)
3-element Vector{NamedTuple{(:i, :name), Tuple{Int64, Symbol}}}:
 (i = 41, name = :y_1)
 (i = 41, name = :y_2)
 (i = 41, name = :y_3)

```

But as you say:

```julia
    @named y 1:10 i -> foo(x*i) # This is not recommended

```

What’s the issue with this form?

---

<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 23, 2022, 7:01am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/5 "2022-12-23T07:01:34Z")

</div>

Maybe @YingboMa knows

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 23, 2022, 11:41am UTC](https://discourse.julialang.org/t/automatically-naming-n-similar-elements-in-mtk/91888/6 "2022-12-23T11:41:24Z")

</div>

It’s just kind of a hack given how the `@named` macro is implemented. It’s kind of surprising it works, since it just grabs the `args` at the highest level and slaps `name = $y` in there.
