Newbie: Variables are missing from the variable map: PMSM model

Based from the RC example I am trying to build something different to learn more about ModelingToolkit.jl. But I am struggling with an error message about “[Ψd(t), Ψq(t), ω(t)] are missing from the variable map” and I am not able to figure out why I get this.

Here is my code so far. It’s not doing much yet, just a model for a simple electric machine:

using ModelingToolkit;

const Zp = 5.0;
const R = 0.031#u"Ω";
const Lq = 0.000015#u"H";
const Ld = 0.000015#u"H";
const kt = 0.07#u"N*m/A";
Ψpm = 2/3 * Zp * kt
const Mw = 0.1#u"N*m";
const Θ = 1.0
Ud = 0.0
Uq = 1.0

@variables t

function PMSM(;name, Ld = 0.00001, Lq = 0.00001, R = 0.001, Zp = 5, Ψpm = 5.0, Mw = 0, Θ = 0, Ud=0.0, Uq=1.0 )
   ps = @parameters Ld=Ld Lq=Lq R=R Zp=Zp Ψpm=Ψpm Θ=Θ
   sts = @variables Id(t) Iq(t) ω(t) Ψq(t) Ψd(t) ωel(t) M(t)
   D = Differential(t)
   eqs = [
          D(Ψd) ~ Ud - R*Id + ωel*Ψq
          D(Ψq) ~ Uq - R*Iq - ωel*Ψd
          Ψd ~ Ψpm + Ld*Id
          Ψq ~ Lq*Iq
          M ~ 3/2*Zp*(Ψpm*Iq - (Ld-Lq)*Id*Iq)
          Θ*D(ω) ~ M - Mw
          ωel ~ Zp * ω
         ]
   ODESystem(eqs, t, sts, ps; name=name)
end

@named motor_ = PMSM(Ld=Ld, Lq=Lq, R=R, Zp=Zp, Ψpm=Ψpm, Mw=Mw, Θ=Θ, Ud=Ud, Uq=Uq)
motor = structural_simplify(motor_)

u0 =
[
   motor.Id=>0.0
   motor.Iq=>0.0
   motor.ω=>0.0
   motor.ωel=>0.0
   motor.Ψq=>0.0
   motor.Ψd=>0.0
   motor.M=>0.0
]

prob = ODAEProblem(motor, u0, (0, 10.0))

And this is what I get:

ERROR: LoadError: ArgumentError: Term{Real, Base.ImmutableDict{DataType, Any}}[Ψd(t), Ψq(t), ω(t)] are missing from the variable map.
Stacktrace:
 [1] throw_missingvars(vars::Vector{Term{Real, Base.ImmutableDict{DataType, Any}}})
   @ ModelingToolkit C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\variables.jl:77
 [2] _varmap_to_vars(varmap::Dict{Num, Float64}, varlist::Vector{Term{Real, Base.ImmutableDict{DataType, Any}}}; defaults::Dict{Any, Any}, check::Bool, toterm::typeof(Symbolics.diff2term))
   @ ModelingToolkit C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\variables.jl:69
 [3] varmap_to_vars(varmap::Vector{Pair{Num, Float64}}, varlist::Vector{Term{Real, Base.ImmutableDict{DataType, Any}}}; defaults::Dict{Any, Any}, check::Bool, toterm::Function)     
   @ ModelingToolkit C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\variables.jl:43
 [4] ODAEProblem{true}(sys::ODESystem, u0map::Vector{Pair{Num, Float64}}, tspan::Tuple{Int64, Float64}, parammap::SciMLBase.NullParameters; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit.StructuralTransformations C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\structural_transformation\codegen.jl:349
 [5] ODAEProblem (repeats 2 times)
   @ C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\structural_transformation\codegen.jl:343 [inlined]
 [6] ODAEProblem(::ODESystem, ::Vararg{Any, N} where N; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ModelingToolkit.StructuralTransformations C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\structural_transformation\codegen.jl:335
 [7] ODAEProblem(::ODESystem, ::Vararg{Any, N} where N)
   @ ModelingToolkit.StructuralTransformations C:\Users\jku\.julia\packages\ModelingToolkit\3q7jk\src\structural_transformation\codegen.jl:335
 [8] top-level scope
   @ c:\JKU_local\code\julia\scripts\motor_performance.jl:49
in expression starting at c:\JKU_local\code\julia\scripts\motor_performance.jl:49

I figured it out.

The right idea came from StackOverFlow Error in SIR model using ModelingTookit - #18 by contradict

Actually I needed to do 2 things:

  1. After structural_simplify the “states” ωel and M are removed.
  2. The motor. namespace somehow seems to confuse

The code now works like this:

using ModelingToolkit;
using DifferentialEquations;
#using Unitful

const Zp = 5.0;
const R = 0.031#u"Ω";
const Lq = 0.000015#u"H";
const Ld = 0.000015#u"H";
const kt = 0.07#u"N*m/A";
Ψpm = 2/3 * Zp * kt
const Mw = 0.1#u"N*m";
const Θ = 1.0
Ud = 0.0
Uq = 1.0

@variables t


function PMSM(;name, Ld = 0.00001, Lq = 0.00001, R = 0.001, Zp = 5, Ψpm = 5.0, Mw = 0, Θ = 0, Ud=0.0, Uq=1.0 )
   ps = @parameters Ld=Ld Lq=Lq R=R Zp=Zp Ψpm=Ψpm Θ=Θ
   sts = @variables Id(t) Iq(t) Ψd(t) Ψq(t) ω(t) ωel(t) M(t)
   D = Differential(t)
   eqs = [
          D(Ψd) ~ Ud - R*Id + ωel*Ψq
          D(Ψq) ~ Uq - R*Iq - ωel*Ψd
          Ψd ~ Ψpm + Ld*Id
          Ψq ~ Lq*Iq
          M ~ 3/2*Zp*(Ψpm*Iq - (Ld-Lq)*Id*Iq)
          Θ*D(ω) ~ M - Mw
          ωel ~ Zp * ω
         ]
   ODESystem(eqs, t, sts, ps; name=name)
end

@named motor_ = PMSM(Ld=Ld, Lq=Lq, R=R, Zp=Zp, Ψpm=Ψpm, Mw=Mw, Θ=Θ, Ud=Ud, Uq=Uq)
motor = structural_simplify(motor_)

@nonamespace u0 =
[
   motor.Id=>0.0
   motor.Iq=>0.0
   motor.Ψd=>0.0
   motor.Ψq=>0.0
   # motor.ωel=>0.0
   motor.ω=>0.0
   # motor.M=>0.0
]


prob = ODAEProblem(motor, u0, (0, 10.0))
sol = solve(prob, Tsit5())

Another remark from myself:-)

The code above produces garbage results. By changing the equation above from: Θ*D(ω) = M - Mw to D(ω) = 1/Θ*(M - Mw) it suddenly works.

I wonder whether this should be a bug in DifferentialEquations.jl or ModelingToolkit.jl?