Nested loop in @mtkmodel / Matrix manipulation

madtorresga
Mon, 23 Jun 2025 13:48:43 +0000
Modelling & Simulations

Hi
I’m trying to automate the battery cells’ creation from an already defined ECM model:

const Qs = [4.8  4.4  4.2 ; 3.7  3.5  3.3]
@mtkmodel Series_Connection begin
    @variables begin
        v_max
        v_min
    end
    @components begin
        cells = [ECM(focv=focv, Q=Qs[i]) for i in eachindex(Qs)]
        #...
    end
    @equations begin
        # Series connect all cells
        [connect(cells[j+(col*i)].n, cells[j+1+(col*i)].p) for i in 0:row-1, j in 1:col-1] #error

        #Connect in parallel
        connect(source.n, voltage.n, cells[1+col*0].p, cells[1+col*1].p)
        connect(source.p, voltage.p, cells[col*1].n, cells[end].n, ground.g)

        v_max ~ maximum([cells[i].v for i in eachindex(Qs)])
        v_min ~ minimum([cells[i].v for i in eachindex(Qs)])
        #...
    end
end

With ERROR: MethodError: Cannot `convert` an object of type Matrix{Equation} to an object of type Union{Equation, Vector{Equation}}

How could I implement this otherwise? Even better would be to be able to manipulate matrices instead of arrays.

3 posts - 3 participants