# MTK - instantiate \*vectors\* of models?

**URL:** https://discourse.julialang.org/t/mtk-instantiate-vectors-of-models/113487
**Category:** Modelling & Simulations
**Created:** [April 25, 2024, 7:58am UTC](https://discourse.julialang.org/t/mtk-instantiate-vectors-of-models/113487 "2024-04-25T07:58:46Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [April 25, 2024, 7:58am UTC](https://discourse.julialang.org/t/mtk-instantiate-vectors-of-models/113487/1 "2024-04-25T07:58:46Z")

</div>

Is it possible to instantiate _vectors_ of models in MTK?

What I mean is the following. I have created a re-usable “class” which I have named `Well`. In the past, I have instantiated several copies of this model and used the instances in equations, example:

```julia
@mtkmodel Field begin
    # Components used
    @components begin 
        ...
        w1 = Well()
        w2 = Well()
        ...
    end
    # Equations for connecting components
    @equations begin
        w1.p_i ~ ...
        w2.p_i ~ ...
        ...
        x ~ w1.p_i + w2.p_i + ...
    end
end

```

This works. However, with many instances of `Well`, the code is ugly.

What I would like to do is something similar to:

```julia
@mtkmodel Field begin
    # Components used
    @components begin 
        ...
        w[1:5] = Well()
        ...
    end
    # Equations for connecting components
    @equations begin
        w[1:5].p_i .~ ...
        ...
        x ~ sum(w[1:5].p_i)
    end
end

```

When I do this, and try to build the model:

```julia
@mtkbuild f = Field()

```

I get an error message:

```julia
type Array has no field p_i

```

**Question** :

1. Is it _possible_ to use arrays to instantiate models?
2. If yes, how do I need to modify my code?

---

<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: [June 15, 2024, 10:34am UTC](https://discourse.julialang.org/t/mtk-instantiate-vectors-of-models/113487/2 "2024-06-15T10:34:15Z")

</div>

This is documented in the `@mtkmodel` macro page:

[Components and Connectors · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKModel_Connector/#What-can-an-MTK-Model-definition-have)?

```julia
    @components begin
        model_a = ModelA(; k_array)
        model_array_a = [ModelA(; k = i) for i in 1:N]
        model_array_b = for i in 1:N
            k = i^2
            ModelA(; k)
        end
    end

```
