I’m trying to use ModelingToolkit.jl to generate a system of linear algebraic equations. It’s quite simple: my system is a resistor network with a constant voltage source. I use ModelingToolkit to create a topology, and I would like to extract the equations and variables from rnet_model.
using ModelingToolkit, Plots, DifferentialEquations
using ModelingToolkit: t_nounits as t, D_nounits as D
using Symbolics
@connector Pin begin
v(t)
i(t), [connect = Flow]
end
@mtkmodel Ground begin
@components begin
g = Pin()
end
@equations begin
g.v ~ 0
end
end
@mtkmodel OnePort begin
@components begin
p = Pin()
n = Pin()
end
@variables begin
v(t)
i(t)
end
@equations begin
v ~ p.v - n.v
0 ~ p.i + n.i
i ~ p.i
end
end
@mtkmodel Resistor begin
@extend OnePort()
@parameters begin
R = 1.0 # Sets the default resistance
end
@equations begin
v ~ i * R
end
end
@mtkmodel ConstantVoltage begin
@extend OnePort()
@parameters begin
V = 1.0
end
@equations begin
V ~ v
end
end
@mtkmodel RNetModel begin
@components begin
R1 = Resistor(R = 1.0)
R2 = Resistor(R = 1.0)
R3 = Resistor(R = 1.0)
source = ConstantVoltage(V = 1.0)
ground = Ground()
end
@equations begin
connect(source.p, R1.p)
connect(R1.p,R2.p)
connect(R2.n,R3.p)
connect(R3.n, ground.g)
connect(R1.n, ground.g)
end
end
@mtkbuild rnet_model = RNetModel()
println("equations", equations(expand_connections(rnet_model) ))
println("unknowns: ", unknowns(rnet_model) )
println("observed: ", observed(rnet_model) )
solution =Symbolics.solve_for(equations(expand_connections(rnet_model)), unknowns(rnet_model))
println(solution)
I see that the equations are there, rnet_model.observed, rnet_model.eqs, and the variables as well in rnet_model. My goal is to get the equation
AX=B
, which is not overdetermined, and then solve the system either symbolically or numerically. How can I get a system of linearly independent equations from rnet_model?