Skip to content

Commit e9aa6e3

Browse files
Merge pull request #3072 from AayushSabharwal/as/scope-discovery
fix: check scope when discovering variables from equations
2 parents c8db319 + 98889f8 commit e9aa6e3

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ end
483483

484484
function collect_var!(unknowns, parameters, var, iv)
485485
isequal(var, iv) && return nothing
486+
getmetadata(var, SymScope, LocalScope()) == LocalScope() || return nothing
486487
if iscalledparameter(var)
487488
callable = getcalledparameter(var)
488489
push!(parameters, callable)

test/variable_scope.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using ModelingToolkit
2-
using ModelingToolkit: SymScope
3-
using Symbolics: arguments, value
2+
using ModelingToolkit: SymScope, t_nounits as t, D_nounits as D
3+
using Symbolics: arguments, value, getname
44
using Test
55

6-
@independent_variables t
76
@variables a b(t) c d e(t)
87

98
b = ParentScope(b)
@@ -52,7 +51,6 @@ end
5251
@test renamed([:foo :bar :baz], c) == Symbol("foo₊c")
5352
@test renamed([:foo :bar :baz], d) == :d
5453

55-
@independent_variables t
5654
@parameters a b c d e f
5755
p = [a
5856
ParentScope(b)
@@ -84,3 +82,22 @@ arr1 = ODESystem(Equation[], t, [], []; name = :arr1) ∘ arr0
8482
arr_ps = ModelingToolkit.getname.(parameters(arr1))
8583
@test isequal(arr_ps[1], Symbol("xx"))
8684
@test isequal(arr_ps[2], Symbol("arr0₊xx"))
85+
86+
function Foo(; name, p = 1)
87+
@parameters p = p
88+
@variables x(t)
89+
return ODESystem(D(x) ~ p, t; name)
90+
end
91+
function Bar(; name, p = 2)
92+
@parameters p = p
93+
@variables x(t)
94+
@named foo = Foo(; p)
95+
return ODESystem(D(x) ~ p + t, t; systems = [foo], name)
96+
end
97+
@named bar = Bar()
98+
bar = complete(bar)
99+
@test length(parameters(bar)) == 2
100+
@test sort(getname.(parameters(bar))) == [:foo₊p, :p]
101+
defs = ModelingToolkit.defaults(bar)
102+
@test defs[bar.p] == 2
103+
@test isequal(defs[bar.foo.p], bar.p)

0 commit comments

Comments
 (0)