Skip to content

parameter default given by other parameter leads to incorrect codegen #2813

@baggepinnen

Description

@baggepinnen
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using OrdinaryDiffEq
using JuliaSimCompiler
function inner(; a = 0, name)
    @parameters a = a
    @variables x(t) = 0
    ODESystem([D(x) ~ a], t; name)
end

function outer(; name)
    @parameters a = 1
    systems = @named begin
        i = inner(a=a)
    end
    @variables y(t) = 0
    ODESystem([D(y) ~ a], t; name, systems)
end

@named o = outer()
o = complete(o)
prob = ODEProblem(structural_simplify(o), [], (0, 1))
sol = solve(prob, Tsit5())
@assert sol[o.y]  sol[o.i.x]
julia> sol = solve(prob, Tsit5())
ERROR: UndefVarError: `i₊a` not defined
Stacktrace:
...
  [7] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{…})(::Vector{…}, ::Vector{…}, ::Vector{…}, ::Float64)
    @ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150
  [8] f
    @ ~/.julia/packages/ModelingToolkit/353ne/src/systems/diffeqs/abstractodesystem.jl:343 [inlined]
...

The generated code contains a symbolic variable i₊a that should have been extracted from the parameter argument

julia> prob.f.f.f_oop
RuntimeGeneratedFunction(#=in ModelingToolkit=#, #=using ModelingToolkit=#, :((ˍ₋arg1, ˍ₋arg2, t)->begin
          begin
              #= /home/fredrikb/.julia/packages/SymbolicUtils/0opve/src/code.jl:468 =#
              (SymbolicUtils.Code.create_array)(typeof(ˍ₋arg1), nothing, Val{1}(), Val{(2,)}(), ˍ₋arg2[1], i₊a)
          end
      end))

This is likely a namespacing issue, since if a different name is used for the outer parameter, it works fine. However, passing i = inner(a=ParentScope(a)) does not resolve the issue. Using a different name is not desirable since oftentimes this pattern appears when simply "forwarding" parameters to inner components

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