# ArgumentError: Cannot find the parent of

**URL:** <https://discourse.julialang.org/t/argumenterror-cannot-find-the-parent-of/110514>\
**Category:** General Usage\
**Tags:** modelingtoolkit\
**Created:** [February 21, 2024, 1:53pm UTC](https://discourse.julialang.org/t/argumenterror-cannot-find-the-parent-of/110514 "2024-02-21T13:53:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maltee1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maltee1/32/204977_2.png) [@maltee1](https://discourse.julialang.org/u/maltee1)\
**Post date:** [February 21, 2024, 1:53pm UTC](https://discourse.julialang.org/t/argumenterror-cannot-find-the-parent-of/110514/1 "2024-02-21T13:53:28Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![johhell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johhell/32/46165_2.png) [@johhell](https://discourse.julialang.org/u/johhell)\
**Post date:** [February 22, 2024, 6:16pm UTC](https://discourse.julialang.org/t/argumenterror-cannot-find-the-parent-of/110514/2 "2024-02-22T18:16:42Z")

</div>

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`

```julia
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):`
