From 5bc895fdaa9cb60f4c8d3bd9df6e8d85ac08a4c0 Mon Sep 17 00:00:00 2001 From: contradict Date: Sat, 11 Nov 2023 20:10:09 -0800 Subject: [PATCH 1/4] Add test for scoping of array paramters --- test/variable_scope.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/variable_scope.jl b/test/variable_scope.jl index 2049f0fd69..a435f142b7 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -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")) From 2f13d13257088e9002d7cf73b6cc8d59589c3ef1 Mon Sep 17 00:00:00 2001 From: contradict Date: Sat, 11 Nov 2023 20:10:43 -0800 Subject: [PATCH 2/4] Allow attaching scope metadata to arrays --- src/systems/abstractsystem.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index fb65274828..440431a0e2 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -394,7 +394,7 @@ end 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 apply_to_variables(sym) do sym setmetadata(sym, SymScope, LocalScope()) end @@ -403,7 +403,7 @@ end struct ParentScope <: SymScope parent::SymScope end -function ParentScope(sym::Union{Num, Symbolic}) +function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N apply_to_variables(sym) do sym setmetadata(sym, SymScope, ParentScope(getmetadata(value(sym), SymScope, LocalScope()))) @@ -414,16 +414,16 @@ struct DelayParentScope <: SymScope 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 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) +DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N = DelayParentScope(sym, 1) struct GlobalScope <: SymScope end -function GlobalScope(sym::Union{Num, Symbolic}) +function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N apply_to_variables(sym) do sym setmetadata(sym, SymScope, GlobalScope()) end From 560dbc0d38e24f95f412bc621e9e6cce18936803 Mon Sep 17 00:00:00 2001 From: contradict Date: Sat, 11 Nov 2023 20:10:59 -0800 Subject: [PATCH 3/4] Don't drop metadata during array indexing operation Attach scope metatdata on indexing operation to array so renaming works correctly. --- src/systems/abstractsystem.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 440431a0e2..2851d2fe02 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -503,8 +503,13 @@ function namespace_expr(O, sys, n = nameof(sys); ivs = independent_variables(sys 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)) + 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 From be221565adfeaf735592ae5bebb9b117c8ff2c11 Mon Sep 17 00:00:00 2001 From: contradict Date: Sun, 12 Nov 2023 09:39:57 -0800 Subject: [PATCH 4/4] Formatting --- src/systems/abstractsystem.jl | 12 +++++++----- test/variable_scope.jl | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 2851d2fe02..69e0e86545 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -394,7 +394,7 @@ end abstract type SymScope end struct LocalScope <: SymScope end -function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N +function LocalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N} apply_to_variables(sym) do sym setmetadata(sym, SymScope, LocalScope()) end @@ -403,7 +403,7 @@ end struct ParentScope <: SymScope parent::SymScope end -function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N +function ParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N} apply_to_variables(sym) do sym setmetadata(sym, SymScope, ParentScope(getmetadata(value(sym), SymScope, LocalScope()))) @@ -414,16 +414,18 @@ struct DelayParentScope <: SymScope parent::SymScope N::Int end -function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, M}}, N) where M +function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, M}}, N) where {M} apply_to_variables(sym) do sym setmetadata(sym, SymScope, DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N)) end end -DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N = DelayParentScope(sym, 1) +function DelayParentScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N} + DelayParentScope(sym, 1) +end struct GlobalScope <: SymScope end -function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where N +function GlobalScope(sym::Union{Num, Symbolic, Symbolics.Arr{Num, N}}) where {N} apply_to_variables(sym) do sym setmetadata(sym, SymScope, GlobalScope()) end diff --git a/test/variable_scope.jl b/test/variable_scope.jl index a435f142b7..7efdc43028 100644 --- a/test/variable_scope.jl +++ b/test/variable_scope.jl @@ -75,8 +75,8 @@ ps = ModelingToolkit.getname.(parameters(level3)) @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 +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"))