Thank you for your reply!
Unfortunately, it doesn’t work. I tried to solve it like an ODE:
prob = ODEProblem(rnet_model, u0, (0, 10.0))
sol = solve(prob)
However, when I call, for example, sol(0, idxs=rnet_model.R1.i)
, it returns NaN, and prob[rnet_model.R2.i]
returns 0. It’s not so bad for me, I’m interested in the feature of MTK - building symbolic equations from a circuit diagram.
I’ve played with the following circuit.
I would expect that for this specific case, the minimal set of equations is n, but it’s not so. There is an example with a circuit where n = 3.
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 = 0.16373498241757348)
R2 = Resistor(R = 0.16373498241757348)
R3 = Resistor(R = 0.7409669292221475)
R4 = Resistor(R = 0.7409669292221475)
R5 = Resistor(R = 0.25021155859159183)
R6 = Resistor(R = 0.25021155859159183)
source = ConstantVoltage(V = 1.0)
ground = Ground()
end
@equations begin
connect(source.p, R1.p)
connect(R1.n, R2.p)
connect(source.n, R2.n)
connect(source.n, ground.g)
connect(R1.n,R3.p)
connect(R3.n,R4.p)
connect(R4.n,R2.n)
connect(R3.n,R5.p)
connect(R5.n,R6.p)
connect(R6.n,R4.n)
end
end
#include("GeneratedCircuit.jl")
@mtkbuild rnet_model = RNetModel()
println("equations: ", equations(expand_connections(rnet_model) ))
println("unknowns: ", size(unknowns(rnet_model)) )
println("observed: ", size(observed(rnet_model) ))
#u0 = [0]
#prob = ODEProblem(rnet_model, u0, (0, 10.0))
#sol = solve(prob)
unknown_var = unknowns(rnet_model)
observed_var = observed(rnet_model)
observed_dict = Dict()
[observed_dict[x.lhs]=x.rhs for x in observed_var]
sol_eqs=equations(expand_connections(rnet_model))
sol_eqs_sub=Symbolics.fixpoint_sub(sol_eqs,observed_dict)
sym_sol=Symbolics.solve_for(sol_eqs_sub,unknown_var)
If I create the model with @mtkbuild, I see that there are only 2 unknowns, and the others are observed. I’ve tried different configurations, n=5, n=10; for all cases, n is not equal to the number of unknowns. Why is it so? Is there something wrong with my construct, or am I missing something?
My goal is to extract from the circuit the equation in the form of Ax=b
. The vector x is x= (i2, i4, …, i2n) or x = (u2, u4, …, u2n), and the matrix A should be n * n.