Skip to content

ODEProblem from ODESystem does not sub nested constants #2969

@aml5600

Description

@aml5600

Describe the bug 🐞

When building an ODEProblem from an ODESystem that contains nested systems, the function generation step does not fully substitute all nested constants in the nested systems.

Expected behavior

We can evaluate prob.f without error.

Minimal Reproducible Example 👇

Without MRE, we would only be able to help you to a limited extent, and attention to the issue would be limited. to know more about MRE refer to wikipedia and stackoverflow.

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D

@mtkmodel ModelA begin
    @constants begin
        a = 1.234
    end
    @variables begin
        x(t)
    end
    @equations begin
        D(x) ~ -a * x
    end
end

@mtkmodel ModelB begin
    @constants begin
        b = 5.678
    end
    @variables begin
        y(t)
    end
    @components begin
        modela = ModelA()
    end
    @equations begin
        D(y) ~ -b * y
    end
end

##
@named sys = ModelB()
sys = complete(sys)

u0 = [sys.y => 1.0, sys.modela.x => -1.0]
p = defaults(sys)

prob = ODEProblem(sys, u0, (0.0, 1.0), p)

# evaluate
u0_v, p_v, _ = ModelingToolkit.get_u0_p(sys, u0, p)
prob.f(u0_v, p_v, 0.0)

Error & Stacktrace ⚠️

ERROR: UndefVarError: `modela₊a` not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/SymbolicUtils/ZwKM7/src/code.jl:480 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:163 [inlined]
 [3] macro expansion
   @ ./none:0 [inlined]
 [4] generated_callfunc
   @ ./none:0 [inlined]
 [5] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{…})(::Vector{…}, ::SciMLBase.NullParameters, ::Float64)
   @ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150
 [6] (::ModelingToolkit.var"#f#777"{})(u::Vector{…}, p::SciMLBase.NullParameters, t::Float64)
   @ ModelingToolkit ~/workspace/ModelingToolkit.jl/src/systems/diffeqs/abstractodesystem.jl:343
 [7] (::ODEFunction{…})(::Vector{…}, ::Vararg{…})
   @ SciMLBase ~/.julia/packages/SciMLBase/MfzUF/src/scimlfunctions.jl:2335
 [8] top-level scope
   @ REPL[52]:1

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
  [961ee093] ModelingToolkit v9.33.0 `..`
  [16a59e39] ModelingToolkitStandardLibrary v2.11.0
  [1dea7af3] OrdinaryDiffEq v6.87.0
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
  • Output of versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 8 × Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 8 default, 0 interactive, 4 GC (on 6 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 8

Additional context

I have a fix, will make a PR =]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions