Hello everyone,

I’ve run into a challenge with using ModelingToolkit, where I have a lots of (similar) systems, each with lots of inputs from a number of other systems connected to it.

Because of the large number of systems and connections involved, I need to generate these somehow procedurally.

Problem is, I cannot specify how many connections each subsystem has manually.

Say I have a system with variable `X(t)`

and I want to add `Y(t)`

to it from another system.

This works:

```
using ModelingToolkit, OrdinaryDiffEq
@variables t
D = Differential(t)
sys = ODESystem(Equation[], t; name=:System)
function subsystem(;name)
states = @variables X(t)=0. In(t)=0.
eqs = [X ~ In]
ODESystem(eqs, t, states, []; name=name)
end
@named sub = subsystem(;name=:Sub)
sys = compose(sys, sub; name=:System)
function input_factory(;name)
states = @variables Y(t)
eq = [
D(Y) ~ -Y
]
ODESystem(eq, t, states, []; name=name)
end
function add_input(system, i)
input = input_factory(name=Symbol(:input, i))
sub = getproperty(sys, :Sub; namespace=false)
eq = [
sub.In ~ input.Y
]
@named connection = ODESystem(eq, t)
connected_input = compose(connection, input; name=Symbol(:input, i))
compose(connected_input, system.systems; name=system.name)
end
julia> sys = add_input(sys, 1)
Model System with 3 equations
States (3):
Sub₊In(t) [defaults to 0.0]
input1₊Y(t)
Sub₊X(t) [defaults to 0.0]
Parameters (0):
julia> equations(sys)
3-element Vector{Equation}:
Sub₊In(t) ~ input1₊Y(t)
Differential(t)(input1₊Y(t)) ~ -input1₊Y(t)
Sub₊X(t) ~ Sub₊In(t)
```

But how to handle the case where I’d like to say `X(t) = Y_1(t) + Y_2(t)`

, for example, where each `Y_i(t)`

originates from a different system?

Using `add_input`

again fails, presumably because the `In`

variable of `sys`

gets overridden:

```
julia> sys = add_input(sys, 2)
julia> states(sys)
4-element Vector{Term{Real, Base.ImmutableDict{DataType, Any}}}:
Sub₊In(t)
input2₊Y(t)
input1₊Y(t)
Sub₊X(t)
julia> equations(sys)
4-element Vector{Equation}:
Sub₊In(t) ~ input2₊Y(t)
Differential(t)(input2₊Y(t)) ~ -input2₊Y(t)
Differential(t)(input1₊Y(t)) ~ -input1₊Y(t)
Sub₊X(t) ~ Sub₊In(t)
```

Any advice on how to circumvent this problem?

Thanks a lot in advance!