diff --git a/src/utils.jl b/src/utils.jl index 4426c8a799..25731f495a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -483,6 +483,7 @@ end function collect_var!(unknowns, parameters, var, iv) isequal(var, iv) && return nothing + getmetadata(var, SymScope, LocalScope()) == LocalScope() || return nothing if iscalledparameter(var) callable = getcalledparameter(var) push!(parameters, callable) diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 8c6e358c23..853d5ce757 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -1,9 +1,8 @@ using ModelingToolkit -using ModelingToolkit: SymScope -using Symbolics: arguments, value +using ModelingToolkit: SymScope, t_nounits as t, D_nounits as D +using Symbolics: arguments, value, getname using Test -@independent_variables t @variables a b(t) c d e(t) b = ParentScope(b) @@ -52,7 +51,6 @@ end @test renamed([:foo :bar :baz], c) == Symbol("foo₊c") @test renamed([:foo :bar :baz], d) == :d -@independent_variables t @parameters a b c d e f p = [a ParentScope(b) @@ -84,3 +82,22 @@ arr1 = ODESystem(Equation[], t, [], []; name = :arr1) ∘ arr0 arr_ps = ModelingToolkit.getname.(parameters(arr1)) @test isequal(arr_ps[1], Symbol("xx")) @test isequal(arr_ps[2], Symbol("arr0₊xx")) + +function Foo(; name, p = 1) + @parameters p = p + @variables x(t) + return ODESystem(D(x) ~ p, t; name) +end +function Bar(; name, p = 2) + @parameters p = p + @variables x(t) + @named foo = Foo(; p) + return ODESystem(D(x) ~ p + t, t; systems = [foo], name) +end +@named bar = Bar() +bar = complete(bar) +@test length(parameters(bar)) == 2 +@test sort(getname.(parameters(bar))) == [:foo₊p, :p] +defs = ModelingToolkit.defaults(bar) +@test defs[bar.p] == 2 +@test isequal(defs[bar.foo.p], bar.p)