Skip to content

Commit 4015a53

Browse files
authored
Merge pull request #2044 from SciML/myb/scope
Apply scope recursively
2 parents c19fb0a + 998b801 commit 4015a53

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/systems/abstractsystem.jl

+38-6
Original file line numberDiff line numberDiff line change
@@ -357,30 +357,54 @@ function Base.setproperty!(sys::AbstractSystem, prop::Symbol, val)
357357
end
358358
end
359359

360+
function apply_to_variables(f::F, ex) where {F}
361+
ex = value(ex)
362+
if isvariable(ex)
363+
return f(ex)
364+
end
365+
istree(ex) || return ex
366+
similarterm(ex, apply_to_variables(f, operation(ex)),
367+
map(Base.Fix1(apply_to_variables, f), arguments(ex)),
368+
metadata = metadata(ex))
369+
end
370+
360371
abstract type SymScope end
361372

362373
struct LocalScope <: SymScope end
363-
LocalScope(sym::Union{Num, Symbolic}) = setmetadata(sym, SymScope, LocalScope())
374+
function LocalScope(sym::Union{Num, Symbolic})
375+
apply_to_variables(sym) do sym
376+
setmetadata(sym, SymScope, LocalScope())
377+
end
378+
end
364379

365380
struct ParentScope <: SymScope
366381
parent::SymScope
367382
end
368383
function ParentScope(sym::Union{Num, Symbolic})
369-
setmetadata(sym, SymScope, ParentScope(getmetadata(value(sym), SymScope, LocalScope())))
384+
apply_to_variables(sym) do sym
385+
setmetadata(sym, SymScope,
386+
ParentScope(getmetadata(value(sym), SymScope, LocalScope())))
387+
end
370388
end
371389

372390
struct DelayParentScope <: SymScope
373391
parent::SymScope
374392
N::Int
375393
end
376394
function DelayParentScope(sym::Union{Num, Symbolic}, N)
377-
setmetadata(sym, SymScope,
378-
DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N))
395+
apply_to_variables(sym) do sym
396+
setmetadata(sym, SymScope,
397+
DelayParentScope(getmetadata(value(sym), SymScope, LocalScope()), N))
398+
end
379399
end
380400
DelayParentScope(sym::Union{Num, Symbolic}) = DelayParentScope(sym, 1)
381401

382402
struct GlobalScope <: SymScope end
383-
GlobalScope(sym::Union{Num, Symbolic}) = setmetadata(sym, SymScope, GlobalScope())
403+
function GlobalScope(sym::Union{Num, Symbolic})
404+
apply_to_variables(sym) do sym
405+
setmetadata(sym, SymScope, GlobalScope())
406+
end
407+
end
384408

385409
renamespace(sys, eq::Equation) = namespace_equation(eq, sys)
386410

@@ -996,7 +1020,15 @@ end
9961020

9971021
function default_to_parentscope(v)
9981022
uv = unwrap(v)
999-
uv isa Symbolic && !hasmetadata(uv, SymScope) ? ParentScope(v) : v
1023+
uv isa Symbolic || return v
1024+
apply_to_variables(v) do sym
1025+
if !hasmetadata(uv, SymScope)
1026+
setmetadata(sym, SymScope,
1027+
ParentScope(getmetadata(value(sym), SymScope, LocalScope())))
1028+
else
1029+
sym
1030+
end
1031+
end
10001032
end
10011033

10021034
function _config(expr, namespace)

test/structural_transformation/bareiss.jl

+9-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ function det_bareiss!(M)
1616
end
1717

1818
@testset "bareiss tests" begin
19-
# copy gives a dense matrix
20-
@testset "bareiss tests: $T" for T in (copy, sparse)
21-
# matrix determinent pairs
22-
for (M, d) in ((BigInt[9 1 8 0; 0 0 8 7; 7 6 8 3; 2 9 7 7], -1),
23-
(BigInt[1 big(2)^65+1; 3 4], 4 - 3 * (big(2)^65 + 1)))
24-
# test that the determinent was correctly computed
25-
@test det_bareiss!(T(M)) == d
19+
# copy gives a dense matrix
20+
@testset "bareiss tests: $T" for T in (copy, sparse)
21+
# matrix determinent pairs
22+
for (M, d) in ((BigInt[9 1 8 0; 0 0 8 7; 7 6 8 3; 2 9 7 7], -1),
23+
(BigInt[1 big(2)^65+1; 3 4], 4 - 3 * (big(2)^65 + 1)))
24+
# test that the determinent was correctly computed
25+
@test det_bareiss!(T(M)) == d
26+
end
2627
end
27-
end end
28+
end

test/variable_scope.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using ModelingToolkit
2+
using ModelingToolkit: SymScope
3+
using Symbolics: arguments, value
24
using Test
35

46
@parameters t
@@ -13,6 +15,9 @@ LocalScope(e.val)
1315
ParentScope(e.val)
1416
GlobalScope(e.val)
1517

18+
ie = ParentScope(1 / e)
19+
@test getmetadata(arguments(value(ie))[2], SymScope) === ParentScope(LocalScope())
20+
1621
eqs = [0 ~ a
1722
0 ~ b
1823
0 ~ c

0 commit comments

Comments
 (0)