Skip to content

Commit

Permalink
Merge pull request #3072 from AayushSabharwal/as/scope-discovery
Browse files Browse the repository at this point in the history
fix: check scope when discovering variables from equations
  • Loading branch information
ChrisRackauckas authored Sep 25, 2024
2 parents c8db319 + 98889f8 commit e9aa6e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 21 additions & 4 deletions test/variable_scope.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit e9aa6e3

Please sign in to comment.