MTK - instantiate *vectors* of models?

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:

@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:

@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:

@mtkbuild f = Field()

I get an error message:

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?

This is documented in the @mtkmodel macro page:

Components and Connectors · ModelingToolkit.jl?

    @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
1 Like