# ModelingToolkit @mtkmodel and loop on equations

**URL:** https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374
**Category:** Modelling & Simulations
**Created:** [April 23, 2024, 7:43am UTC](https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374 "2024-04-23T07:43:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![julien-dc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julien-dc/32/208704_2.png) [@julien-dc](https://discourse.julialang.org/u/julien-dc)
#### Post date: [April 23, 2024, 7:43am UTC](https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374/1 "2024-04-23T07:43:08Z")

</div>

Greetings Julia community,

I’m currently exploring the possibility of transitioning from a Modelica/Python workflow to a fully Julia-based workflow for my projects. As I’m relatively new to Julia, I’ve encountered an issue while working with `@mtkmodel`.

The following code snippet demonstrates the problem:

```julia
@mtkmodel Circuit1 begin

    @components begin
        pipe = [Pipe() for i in 1:8]
    end
    
    @equations begin
        for i in 1:7
            connect(pipe[i].c_out, pipe[i+1].c_in)
        end
    end
    
end

@mtkbuild circuit_model = Circuit1()

```

While the above code throws an error: MethodError: Cannot `convert` an object of type Nothing to an object of type Equation

The following code, which manually lists each connection, works as expected:

```julia
@mtkmodel Circuit2 begin

    @components begin
        pipe = [Pipe() for i in 1:8]
    end
    
    @equations begin
        connect(pipe[1].c_out, pipe[2].c_in)
        connect(pipe[2].c_out, pipe[3].c_in)
        connect(pipe[3].c_out, pipe[4].c_in)
        connect(pipe[4].c_out, pipe[5].c_in)
        connect(pipe[5].c_out, pipe[6].c_in)
        connect(pipe[6].c_out, pipe[7].c_in)
        connect(pipe[7].c_out, pipe[8].c_in)
    end
    
end

@mtkbuild circuit_model = Circuit2()

```

I’d greatly appreciate any insights or suggestions on how to properly loop over the connections within `@equations` using `@mtkmodel`.

Thank you in advance for your help!

---

<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:48am UTC](https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374/2 "2024-04-23T10:48:48Z")

</div>

If you want to loop over constructions then you likely want to direct build the ODESystem.

---

<div class="post-metadata">

### Author: ![TimoSchubert](https://avatars.discourse-cdn.com/v4/letter/t/258eb7/32.png) [@TimoSchubert](https://discourse.julialang.org/u/TimoSchubert)
#### Post date: [April 27, 2024, 1:39pm UTC](https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374/3 "2024-04-27T13:39:58Z")

</div>

Hi @ChrisRackauckas , can you give an example? The example above is a typical way of how to create discretized volumes like pipes in Modelica. I wonder what the best way to do this with MTK would look like.

---

<div class="post-metadata">

### Author: ![ven-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ven-k/32/11915_2.png) [@ven-k](https://discourse.julialang.org/u/ven-k)
#### Post date: [April 29, 2024, 5:16am UTC](https://discourse.julialang.org/t/modelingtoolkit-mtkmodel-and-loop-on-equations/113374/4 "2024-04-29T05:16:56Z")

</div>

```julia
@mtkmodel Circuit1 begin

    @components begin
        pipe = [Pipe() for i in 1:8]
    end
    
    @equations begin
        [connect(pipe[i].c_out, pipe[i+1].c_in) for i in 1:7]...
    end
    
end

@mtkbuild circuit_model = Circuit1()

```

This works.

The `for...end` syntax returns `nothing` resulting the above error; maybe we should add a parser for that as well. For now, please use `[<> for <>]...` syntax.
