Skip to content

No method matching operation(::Sym{Real, Nothing}) error during index reduction #998

@JKRT

Description

@JKRT

Hi!

I have the following model (not sure if it is an error but I have gotten similar models to work using DifferentialEquations.jl (bare bones) ):

using ModelingToolkit
using DiffEqBase
using DifferentialEquations
function SimpleCircuitModel(tspan = (0.0, 1.0))
  pars = ModelingToolkit.@parameters((f, A, C, R1, L, R2, t)) 
  vars = ModelingToolkit.@variables((u2(t), i2(t), u4, u3, i1, u, u1, i))
  der = Differential(t)
  eqs = [
    der(u2) ~ i1 * (C^-1),
    der(i2) ~ u4 * (L^-1),
    0 ~ u - (u4 + u3),
    0 ~ u3 - R2 * i2,
    0 ~ u1 - R1 * i1,
    0 ~ u - A * sin(((2.0 * 3.1415) * f) * t),
    0 ~ u - (u1 + u2),
    0 ~ i - (i2 + i1),
  ]
  nonLinearSystem = ModelingToolkit.ODESystem(
    eqs,
    t,
    vars,
    pars,
    name = :($(Symbol("SimpleCircuit"))),
  )
  params = Dict(
    f => 1.0,
    A => 12.0,
    C => 1.0,
    R1 => 10.0,
    L => 0.01,
    R2 => 100.0,
    t => tspan[1],
  )
  initialValues = [
    u4 => 0.007,
    u3 => 0.0,
    i1 => 0.0,
    u => 0.0,
    u1 => 0.0,
    i => 0.0,
    u2 => 0.0,
    i2 => 0.0,
  ]
  firstOrderSystem = ModelingToolkit.ode_order_lowering(nonLinearSystem)
  reducedSystem = ModelingToolkit.dae_index_lowering(firstOrderSystem)
  problem = ModelingToolkit.ODEProblem(reducedSystem , initialValues, tspan, params)
  return problem
end
function SimpleCircuitSimulate(tspan = (0.0, 10.0))
  problem = SimpleCircuitModel(tspan)
  return solve(problem, Tsit5())
end
SimpleCircuitSimulate()

Currently running this results in the following error:


julia> @time include("SimpleCircuit.jl")
ERROR: LoadError: MethodError: no method matching operation(::Sym{Real, Nothing})
Closest candidates are:
  operation(::Term) at C:\Users\John\.julia\packages\SymbolicUtils\9iQGH\src\types.jl:338
  operation(::SymbolicUtils.Add) at C:\Users\John\.julia\packages\SymbolicUtils\9iQGH\src\types.jl:600
  operation(::SymbolicUtils.Mul) at C:\Users\John\.julia\packages\SymbolicUtils\9iQGH\src\types.jl:743

If I make a small change that is,

  firstOrderSystem = ModelingToolkit.ode_order_lowering(nonLinearSystem)
#  reducedSystem = ModelingToolkit.dae_index_lowering(firstOrderSystem)
  problem = ModelingToolkit.ODEProblem(firstOrderSystem, initialValues, tspan, params)
  return problem

It seems that I am able to generate the problem, however the simulation results are wrong

image

Edit:

Another model with a similar problem:

using ModelingToolkit
using DiffEqBase
using DifferentialEquations
function SimpleMechanicalSystemModel(tspan = (0.0, 1.0))
  pars = ModelingToolkit.@parameters((C, J2, J1, t))
  vars = ModelingToolkit.@variables((
    omega_1(t),
    Phi_1(t),
    Phi_2(t),
    omega_2(t),
    tau_3,
    tau_1,
    tau_2,
    u,
  ))
  der = Differential(t)
  eqs = [
    der(omega_1) ~ (J1^-1) * (tau_1 + tau_2),
    der(Phi_1) ~ omega_1,
    der(Phi_2) ~ omega_2,
    der(omega_2) ~ tau_3 * (J2^-1),
    0 ~ tau_3 - -tau_2,
    0 ~ tau_1 - u,
    0 ~ tau_2 - C * (Phi_2 - Phi_1),
    0 ~ u - t,
  ]
  nonLinearSystem = ModelingToolkit.ODESystem(
    eqs,
    t,
    vars,
    pars,
    name = :($(Symbol("SimpleMechanicalSystem"))),
  )
  pars = Dict(C => 1.0, J2 => 1.0, J1 => 1.0, t => tspan[1])
  initialValues = [
    tau_3 => 0.0,
    tau_1 => 0.0,
    tau_2 => 0.0,
    u => 0.0,
    omega_1 => 0.0,
    Phi_1 => 0.0,
    Phi_2 => 0.0,
    omega_2 => 0.0,
  ]
  firstOrderSystem = ModelingToolkit.ode_order_lowering(nonLinearSystem)
  reducedSystem = ModelingToolkit.dae_index_lowering(firstOrderSystem)
  problem = ModelingToolkit.ODEProblem(reducedSystem, initialValues, tspan, pars)
  return problem
end
function SimpleMechanicalSystemSimulate(tspan = (0.0, 1.0))
  problem = SimpleMechanicalSystemModel(tspan)
  return solve(problem, Rodas5())
end

Cheers,
John

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions