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

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:

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:

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?

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