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

`structural_simplify` messes with `hasproperty` for parameters #3016

Open hexaeder opened 3 weeks ago

hexaeder commented 3 weeks ago

Describe the bug 🐞 I'm really sorry for spamming your issue tracker, but I'll provide MWEs at least...

A simplified system has weird behaviour for hasproperty and getproperty: namely it might lie about properties being present or not present!

Expected behavior

hasproperty true  => getproperty does not error
hasproperty false => getproperty errors

Minimal Reproducible Example 👇

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as Dt
@mtkmodel Inner begin
    @parameters begin
        p
    end
end
@mtkmodel Outer begin
    @components begin
        inner = Inner()
    end
    @variables begin
        x(t)
    end
    @equations begin
        x ~ inner.p
    end
end
@named outer = Outer()
simp = structural_simplify(outer)

hasproperty(outer, :inner)   # true
outer.inner # works
hasproperty(outer, :inner₊p) # false
outer.inner₊p # errors as expected

hasproperty(simp, :inner)   # false
simp.inner # still works
hasproperty(simp, :inner₊p) # true
inner.inner₊p # errors despite hasproperty true

Error & Stacktrace ⚠️

julia> outer.inner₊p # errors despite hasproperty
ERROR: ArgumentError: System outer: variable inner₊p does not exist
Stacktrace:
 [1] getvar(sys::ODESystem, name::Symbol; namespace::Bool)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/yfT8s/src/systems/abstractsystem.jl:1066
 [2] getproperty(sys::ODESystem, name::Symbol; namespace::Bool)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/yfT8s/src/systems/abstractsystem.jl:1009
 [3] getproperty(sys::ODESystem, name::Symbol)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/yfT8s/src/systems/abstractsystem.jl:1005
 [4] top-level scope
   @ none:1

Environment (please complete the following information):

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