# Change parameters of array components in ModelingToolkit.jl

**URL:** <https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358>\
**Category:** Performance\
**Tags:** modelingtoolkit\
**Created:** [April 22, 2024, 8:29pm UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358 "2024-04-22T20:29:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [April 22, 2024, 8:29pm UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/1 "2024-04-22T20:29:53Z")

</div>

Hello all,

I am looking for a way of setting the individual parameters of an array of components in ModelingToolkit.

Consider the following example (inspired by the [ModelingToolkit docs](https://docs.sciml.ai/ModelingToolkit/stable/basics/MTKModel_Connector/)):

```julia
using ModelingToolkit
using ModelingToolkit: t

@mtkmodel ModelA begin
    @parameters begin
        k
        k_array[1:2]
    end
end

@mtkmodel ModelB begin
    @parameters begin
        p1 = 1.0, [description = "Parameter of ModelB"]
        p2 = 1.0, [description = "Parameter of ModelB"]
    end
end

@mtkmodel ModelC begin
    @icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
    @constants begin
        c::Int = 1, [description = "Example constant."]
    end
    @structural_parameters begin
        f = sin
        N = 2
    end
    begin
        v_var = 1.0
    end
    @variables begin
        v(t) = v_var
        v_array(t)[1:2, 1:3]
        v_for_defaults(t)
    end
    @extend ModelB(; p1)
    @components begin
	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
    @equations begin
        sum([m.k for m in model_array_a]) ~ f(v)
    end
    @defaults begin
        v_for_defaults => 2.0
    end
end

@mtkbuild model_c = ModelC()

```

In this case, the `k` variables of `model_array_b` are set in the for loop.  
But in my use case I want to run run my model hundreds of times with slightly different parameters each time.  
To do this efficiently, I have to change the parameters each time, for example using the `setp` function.

But the only way I have found to access these parameters, is by doing something like `@mtkbuild model_c2 = ModelC(; model_array_a.k = 11.0)`.  
This works, but does not allow me to set the `k`’s of `model_array_a` to individually different values, using the parameter interface (i.e. not a for loop).

Does anyone know of a way of doing this?

---

<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:** [April 23, 2024, 10:49am UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/2 "2024-04-23T10:49:57Z")

</div>

> [@Antomek](#):
>
> This works, but does not allow me to set the `k`’s of `model_array_a` to individually different values, using the parameter interface (i.e. not a for loop).

If you use a parameter array then you have to set it with an array of the same size.

---

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [April 23, 2024, 11:06am UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/3 "2024-04-23T11:06:04Z")

</div>

I can’t quite get it to work.

When I do something like `@mtkbuild model_c = ModelC(; model_array_a.k = [i for i in 1:2])` I get an error saying `TypeError: in keyword argument k, expected Union{Nothing, Real}, got a value of type Vector{Int64}`.

Adding something along the lines of

```julia
@mtkmodel ModelC begin
    @icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
    @constants begin
        c::Int = 1, [description = "Example constant."]
    end
	@parameters begin
		new_k_array[1:2]::Float64
	end
    @structural_parameters begin
        f = sin
        N = 2
    end
    begin
        v_var = 1.0
    end
    @variables begin
        v(t) = v_var
        v_array(t)[1:2, 1:3]
        v_for_defaults(t)
    end
    @extend ModelB(; p1)
    @components begin
		model_array_a = [ModelA(; k = new_k_array[i]) for i in 1:N]
        model_array_b = for i in 1:N
            k = i^2
            ModelA(; k)
        end
    end
    @equations begin
        sum([m.k for m in model_array_a]) ~ f(v)
    end
    @defaults begin
        v_for_defaults => 2.0
    end
end

@mtkbuild model_c1 = ModelC(; new_k_array = [Float64(i) for i in 1:2])

prob = ODEProblem(model_c1, [], (0., 100.0), [])

```

yields the error `MethodError: Cannot `convert` an object of type SymbolicUtils.BasicSymbolic{Float64} to an object of type Float64`.

Am I doing something wrong?

---

<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:** [April 23, 2024, 3:29pm UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/4 "2024-04-23T15:29:59Z")

</div>

> [@Antomek](#):
>
> `new_k_array[1:2]::Float64`

This is not a Float64

---

<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:** [April 23, 2024, 3:30pm UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/5 "2024-04-23T15:30:09Z")

</div>

@ven-k do we have a good issue to point to?

---

<div class="post-metadata">

**Author:** ![Antomek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antomek/32/9406_2.png) [@Antomek](https://discourse.julialang.org/u/Antomek)\
**Post date:** [April 23, 2024, 7:00pm UTC](https://discourse.julialang.org/t/change-parameters-of-array-components-in-modelingtoolkit-jl/113358/6 "2024-04-23T19:00:35Z")

</div>

Correct, I’m following the instructions from the ModelingToolkit docs, which says to use the `eltype` of the array, [as described here](https://docs.sciml.ai/ModelingToolkit/stable/basics/FAQ/#How-do-I-use-non-numeric/array-valued-parameters?).
