# How to dynamically assign unique names to models in a loop with @named in ModelingToolkit

**URL:** <https://discourse.julialang.org/t/how-to-dynamically-assign-unique-names-to-models-in-a-loop-with-named-in-modelingtoolkit/121367>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit, symbolics\
**Created:** [October 16, 2024, 8:38am UTC](https://discourse.julialang.org/t/how-to-dynamically-assign-unique-names-to-models-in-a-loop-with-named-in-modelingtoolkit/121367 "2024-10-16T08:38:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanaO](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@DanaO](https://discourse.julialang.org/u/DanaO)\
**Post date:** [October 16, 2024, 8:38am UTC](https://discourse.julialang.org/t/how-to-dynamically-assign-unique-names-to-models-in-a-loop-with-named-in-modelingtoolkit/121367/1 "2024-10-16T08:38:16Z")

</div>

Hi everyone,

I’m working on a simulation of a gas network using `ModelingToolkit` in Julia, where I create multiple `Pipe` models inside a loop. My issue is that I need to assign unique names to each pipe created in the loop, but I am struggling to do so with `@named`, as it assigns the same name to every pipe, leading to name collisions as soon as I connect the differen pipe models.

Here is a the part of my code:

```julia
function create_pipes(lines)
    pipes = Dict()
    count = 0
    for line in eachrow(lines)
        count += 1
        n_pipe = Int64(line[:n])
        name = String(line[:name])
        pipe_name = Symbol(name)
        pipe_model = @named $(pipe_name) = GasNetwork.Pipe(; N=n_pipe)
        push!(pipes, name => MyPipe(
            name,
            number = count,
            node1 = String(line[:linedesc_Knoten_1]),
            node2 = String(line[:linedesc_Knoten_2]),
            length = Float64(line[:length]),
            diameter = Float64(line[:diameter]),
            n =n_pipe,
            model = pipe_model
            ))
            with_logger(file_logger) do
                @info "Pipe $name created with nodes $(pipes[name].node1) and $(pipes[name].node2) "
            end
    end
    return pipes
end

```

However, this code gives me the error:

```julia
ERROR: ParseError("The lhs must be a symbol (a) or a ref (a[1:10]). Got \$pipe_name.")

```

What would be the correct way to assign unique names to each model within a loop?

Is there a different approach or a recommended pattern for this scenario?

---

<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 16, 2024, 10:20am UTC](https://discourse.julialang.org/t/how-to-dynamically-assign-unique-names-to-models-in-a-loop-with-named-in-modelingtoolkit/121367/2 "2024-10-16T10:20:14Z")

</div>

> [@DanaO](#):
>
> `pipe_model = @named $(pipe_name) = GasNetwork.Pipe(; N=n_pipe)`

`pipe_model = GasNetwork.Pipe(; N=n_pipe, name=pipe_name)`
