Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array paramter scoping #2352

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
abstract type SymScope end

struct LocalScope <: SymScope end
function LocalScope(sym::Union{Num, Symbolic})
function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N}

Check warning on line 397 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L397

Added line #L397 was not covered by tests
apply_to_variables(sym) do sym
setmetadata(sym, SymScope, LocalScope())
end
Expand All @@ -403,7 +403,7 @@
struct ParentScope <: SymScope
parent::SymScope
end
function ParentScope(sym::Union{Num, Symbolic})
function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N}

Check warning on line 406 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L406

Added line #L406 was not covered by tests
apply_to_variables(sym) do sym
setmetadata(sym, SymScope,
ParentScope(getmetadata(value(sym), SymScope, LocalScope())))
Expand All @@ -414,16 +414,18 @@
parent::SymScope
N::Int
end
function DelayParentScope(sym::Union{Num, Symbolic}, N)
function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, M}}, N) where {M}

Check warning on line 417 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L417

Added line #L417 was not covered by tests
apply_to_variables(sym) do sym
setmetadata(sym, SymScope,
DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N))
end
end
DelayParentScope(sym::Union{Num, Symbolic}) = DelayParentScope(sym, 1)
function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N}
DelayParentScope(sym, 1)

Check warning on line 424 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L423-L424

Added lines #L423 - L424 were not covered by tests
end

struct GlobalScope <: SymScope end
function GlobalScope(sym::Union{Num, Symbolic})
function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N}

Check warning on line 428 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L428

Added line #L428 was not covered by tests
apply_to_variables(sym) do sym
setmetadata(sym, SymScope, GlobalScope())
end
Expand Down Expand Up @@ -503,8 +505,13 @@
return O
elseif istree(O)
T = typeof(O)
op = operation(O)
args = arguments(O)
if op == getindex && hasmetadata(O, SymScope)
args[1] = setmetadata(args[1], SymScope, getmetadata(O, SymScope))

Check warning on line 511 in src/systems/abstractsystem.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/abstractsystem.jl#L511

Added line #L511 was not covered by tests
end
renamed = let sys = sys, n = n, T = T
map(a -> namespace_expr(a, sys, n; ivs)::Any, arguments(O))
map(a -> namespace_expr(a, sys, n; ivs)::Any, args)
end
if isvariable(O)
# Use renamespace so the scope is correct, and make sure to use the
Expand Down
8 changes: 8 additions & 0 deletions test/variable_scope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ ps = ModelingToolkit.getname.(parameters(level3))
@test isequal(ps[4], :level2₊level0₊d)
@test isequal(ps[5], :level1₊level0₊e)
@test isequal(ps[6], :f)

@parameters xx[1:2]
arr_p = [ParentScope(xx[1]), xx[2]]
arr0 = ODESystem(Equation[], t, [], arr_p; name = :arr0)
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"))
Loading