I’ve defined a system as follows:
using ModelingToolkit, ModelingToolkitStandardLibrary
using DifferentialEquations
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks
function Component(; name, R)
@named n = Pin()
@named p1 = Pin()
@named p2 = Pin()
@named R = Resistor(R=R)
@named C = Capacitor(C=R)
eqs = [
connect(p1, R.p)
connect(p2, C.p)
connect(n, R.n, C.n)
]
return ODESystem(eqs, t, [], [] ; name,
systems=[p1, p2, n, R, C])
end
function S(;name, num)
# construct the layers from the center out
comps = Vector{Any}(undef, num)
Ra_l = Vector{Any}(undef, num)
Ra_r = Vector{Any}(undef, num)
Rc_l = Vector{Any}(undef, num)
Rc_r = Vector{Any}(undef, num)
for i in 1:num
comps[i] = Component(; name=Symbol(:L_, i), R=1)
Ra_l[i] = Resistor(name=Symbol(:Ra_, i, :l), R=2)
Ra_r[i] = Resistor(name=Symbol(:Ra_, i, :r), R=3)
Rc_l[i] = Resistor(name=Symbol(:Rc_, i, :l), R=4)
Rc_r[i] = Resistor(name=Symbol(:Rc_, i, :r), R=5)
end
@named n = Pin()
@named p = Pin()
# connect everything
eqs = [
connect(Ra_l[1].p, Ra_r[1].p, p)
connect(Rc_l[1].n, Rc_r[1].n, n)
[connect(Ra_l[i].n, Ra_l[i+1].p) for i in 1:num-1]...
[connect(Ra_r[i].n, Ra_r[i+1].p) for i in 1:num-1]...
[connect(Rc_l[i].n, Rc_l[i+1].p) for i in 1:num-1]...
[connect(Rc_r[i].n, Rc_r[i+1].p) for i in 1:num-1]...
[connect(Ra_l[i].n, comps[i].p1) for i in 1:num]...
[connect(Ra_r[i].n, comps[i].p2) for i in 1:num]...
[connect(Rc_l[i].n, Rc_r[i].n, comps[i].n) for i in 1:num]...
]
return ODESystem(eqs, t, [], [] ; name,
systems=[p, n, comps..., Ra_l..., Ra_r..., Rc_l..., Rc_r...])
end
@variables t
@named source = Voltage()
@named c = Constant(k=0.01)
@named ground = Ground()
@named s = S(num=2)
rc_eqs = [
connect(c.output, source.V)
connect(source.p, s.p)
connect(s.n, source.n, ground.g)
]
@named rc_model = ODESystem(rc_eqs, t, systems=[s, c, source, ground])
sys = structural_simplify(rc_model)
No issues so far.
However, once I increase num
to 3:
@named s = S(num=3)
I’m told the system is unbalanced:
ExtraEquationsSystemException: The system is unbalanced. There are 41 highest order derivative variables and 42 equations.
More equations than variables, here are the potential extra equation(s):
0 ~ -s₊L_3₊n₊i(t) - s₊Ra_3l₊i(t) - s₊Ra_3r₊i(t)
I can’t really see what’s wrong here.
ModelingToolkit is v8.36.0 and ModelingToolkitStandardLibrary is v1.10.0
Thanks