SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.41k stars 204 forks source link

Incorrect results using `Complex` variable in `@mtkmodel` #3014

Open hexaeder opened 2 weeks ago

hexaeder commented 2 weeks ago

Describe the bug 🐞 followup on #2895

The recently introduced handling of complex variables in @mtkmodel #2935 leads to incorrect results. See MWE with different results within and without @mtkmodel

Expected behavior

Expansion of complex equations should yield the same result regardless of construction method...

Minimal Reproducible Example 👇

# wrong results in @mtkmodel
@independent_variables t
@mtkmodel ComplexModel begin
    @variables begin
        x(t)
        y(t)
        z(t)::Complex
    end
    @equations begin
        z ~ x + im*y
    end
end
@named mixed = ComplexModel()
full_equations(mixed)
# 2-element Vector{Equation}:
#  z(t) ~ x(t)
#  0 ~ y(t)

# correct results in ode system
@independent_variables t
@variables x(t) y(t) z(t)::Complex
eqs = z ~ x + im*y # notice how complex equation returns Vector{Equation}
sys = ODESystem(eqs, t; name=:ComplexModel)
full_equations(sys)
# 2-element Vector{Equation}:
#  real(z(t)) ~ x(t)
#  imag(z(t)) ~ y(t)

Environment (please complete the following information):

(jl_QTt44V) pkg> st
Status `/tmp/jl_QTt44V/Project.toml`
  [961ee093] ModelingToolkit v9.36.0
julia> versioninfo()
Julia Version 1.11.0-rc3
Commit 616e45539db (2024-08-26 15:46 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × AMD Ryzen 9 8945HS w/ Radeon 780M Graphics
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 16 default, 0 interactive, 8 GC (on 16 virtual cores)
Environment:
  JULIA_NUM_THREADS = auto
ChrisRackauckas commented 2 weeks ago

@ven-k can we make sure this at least errors if not fixed?