ArgumentError: Cannot find the parent of

I’m trying to get started using modeling toolkit, but I’m hitting an ArgumentError that I can’t interpret. This is my code:

``````using Plots; pyplot()
using ControlSystemsBase, RobustAndOptimalControl, LinearAlgebra
using ModelingToolkit
@parameters Li::Real Ri::Real Lg::Real Rg::Real ωn::Real Vn::Real kp::Real kq::Real ωf::Real
j=1im

Ap = -(Ri+Rg)/(Li+Lg)-j*ωn
A1 = (Rg*Li-Ri*Lg)/(Lg+Li)
A2 = [Lg/(Lg+Li) Li/(Lg+Li)]
A3 = 1/(Li+Lg)
kp = 0.2*2π/1000
kq = 20/1000
@variables t P(t)::Real=0 Q(t)::Real=0 Pf(t)::Real=0 Qf(t)::Real=0 vo(t)::Complex=0 ig(t)::Complex=0 vi(t)::Complex=0 vamp(t)::Real=0 theta(t)::Real=0 ω(t)::Real=0 vg(t)::Complex=0
D = Differential(t)
eqs = [P ~ 3/2*real(vo*conj(ig))    # P calc
D(Pf) ~ ωf*(P-Pf)            # low pass filter
Q ~ 3/2*imag(vo*conj(ig))    # Q calc
D(Qf) ~ ωf*(Q-Qf)             # low pass filter
vi ~ vamp*exp(j*theta)       # output voltage generation
D(theta) ~ ω                 # angle integration
vo ~ A1*ig+(A2*[vi; vg])[1] # output admittance
D(ig) ~ Ap*ig+A3*vi-A3*vg # output admittance
vamp ~ Vn-kq*Qf              # Q-droop
ω ~ -kp*Pf]                  # P-droop
@named sys = ODESystem(eqs, t, [Pf Qf ig theta], [Li Ri Lg Rg ωn Vn kp kq ωf], tspan = [0,1])
``````

Complex variables should be supported, right?

in the past I had a similar problem.
Below my approach (not perfect, but sufficient for me):

• substitute `real(cVAR)` and `imag(cVAR)` by `cVAR_re` and `cVAR_im`
``````using Plots; pyplot()
using ControlSystemsBase, RobustAndOptimalControl, LinearAlgebra
using ModelingToolkit
@parameters Li::Real Ri::Real Lg::Real Rg::Real ωn::Real Vn::Real kp::Real kq::Real ωf::Real
j=1im

function NewVars(eqName::String, CCX::Vector{Complex{Num}})::Tuple{Expr, Expr}
vari::String = "newVariables = @variables "
substi::String = ""
for cx in CCX
stcx = string(cx)[1:end-3]  # without '(t)'
vari = string(vari,stcx*"₋re(t) ",stcx*"₋im(t) ")
substi = string(substi,"\$eqName = substitute(\$eqName,(real(\$stcx)=>\$(stcx)₋re)); ")
substi = string(substi,"\$eqName = substitute(\$eqName,(imag(\$stcx)=>\$(stcx)₋im)); ")
end
return (Meta.parse(vari), Meta.parse(substi))
end

Ap = -(Ri+Rg)/(Li+Lg)-j*ωn
A1 = (Rg*Li-Ri*Lg)/(Lg+Li)
A2 = [Lg/(Lg+Li) Li/(Lg+Li)]
A3 = 1/(Li+Lg)
kp = 0.2*2π/1000
kq = 20/1000

@variables begin
t
P(t)::Real=0
Q(t)::Real=0
Pf(t)::Real=0
Qf(t)::Real=0
vamp(t)::Real=0
theta(t)::Real=0
ω(t)::Real=0
end

CX = @variables begin   # Complex VARS
vo(t)::Complex
ig(t)::Complex
vi(t)::Complex
vg(t)::Complex
end

D = Differential(t)
eqs = [P ~ 3/2*real(vo*conj(ig))    # P calc
D(Pf) ~ ωf*(P-Pf)            # low pass filter
Q ~ 3/2*imag(vo*conj(ig))    # Q calc
D(Qf) ~ ωf*(Q-Qf)             # low pass filter
vi ~ vamp*exp(j*theta)       # output voltage generation
D(theta) ~ ω                 # angle integration
vo ~ A1*ig+(A2*[vi; vg])[1] # output admittance
D(ig) ~ Ap*ig+A3*vi-A3*vg # output admittance
vamp ~ Vn-kq*Qf              # Q-droop
ω ~ -kp*Pf]                  # P-droop

eqs1=deepcopy(eqs)

(Bvaria, Bsubst) = NewVars("eqs1",CX)
eval(Bvaria) # create new variables
eval(Bsubst) # substitute

# @named sys = ODESystem(eqs, t, [Pf Qf ig theta], [Li Ri Lg Rg ωn Vn kp kq ωf], tspan = [0,1])
@named model = ODESystem(eqs1, t)
# sys = structural_simplify(model)

``````

Check your system again: `Model model with 13 equations, States (15):`