Skip to content

Commit 75f4ec6

Browse files
committed
update collecting vars with scoping
1 parent 4daf884 commit 75f4ec6

File tree

3 files changed

+56
-9
lines changed

3 files changed

+56
-9
lines changed

src/systems/abstractsystem.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,14 @@ One property to note is that if a system is complete, the system will no longer
923923
namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
924924
"""
925925
function complete(sys::AbstractSystem; split = true, flatten = true)
926-
if !(sys isa JumpSystem)
927-
newunknowns = OrderedSet()
928-
newparams = OrderedSet()
929-
iv = has_iv(sys) ? get_iv(sys) : nothing
930-
collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1)
931-
# don't update unknowns to not disturb `structural_simplify` order
932-
# `GlobalScope`d unknowns will be picked up and added there
933-
@set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams)))
934-
end
926+
newunknowns = OrderedSet()
927+
newparams = OrderedSet()
928+
iv = has_iv(sys) ? get_iv(sys) : nothing
929+
collect_scoped_vars!(newunknowns, newparams, sys, iv; depth = -1)
930+
# don't update unknowns to not disturb `structural_simplify` order
931+
# `GlobalScope`d unknowns will be picked up and added there
932+
@set! sys.ps = unique!(vcat(get_ps(sys), collect(newparams)))
933+
935934
if flatten
936935
eqs = equations(sys)
937936
if eqs isa AbstractArray && eltype(eqs) <: Equation

src/systems/jumps/jumpsystem.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ function JumpSystem(eqs, iv, unknowns, ps;
217217
end
218218

219219
##### MTK dispatches for JumpSystems #####
220+
eqtype_supports_collect_vars(j::MassActionJump) = true
220221
function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0,
221222
op = Differential)
222223
collect_vars!(unknowns, parameters, j.scaled_rates, iv; depth, op)
@@ -228,6 +229,7 @@ function collect_vars!(unknowns, parameters, j::MassActionJump, iv; depth = 0,
228229
return nothing
229230
end
230231

232+
eqtype_supports_collect_vars(j::Union{ConstantRateJump,VariableRateJump}) = true
231233
function collect_vars!(unknowns, parameters, j::Union{ConstantRateJump,VariableRateJump},
232234
iv; depth = 0, op = Differential)
233235
collect_vars!(unknowns, parameters, j.rate, iv; depth, op)

test/jumpsystem.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,49 @@ let
376376
@test issetequal(us, [x1, x5, x2])
377377
@test issetequal(ps, [p4, p5])
378378
end
379+
380+
# scoping tests
381+
let
382+
@variables x1(t) x2(t) x3(t) x4(t) x5(t)
383+
x2 = ParentScope(x2)
384+
x3 = ParentScope(ParentScope(x3))
385+
x4 = DelayParentScope(x4, 2)
386+
x5 = GlobalScope(x5)
387+
@parameters p1 p2 p3 p4 p5
388+
p2 = ParentScope(p2)
389+
p3 = ParentScope(ParentScope(p3))
390+
p4 = DelayParentScope(p4, 2)
391+
p5 = GlobalScope(p5)
392+
393+
j1 = ConstantRateJump(p1, [x1 ~ x1 + 1])
394+
j2 = MassActionJump(p2, [x2 => 1], [x3 => -1])
395+
j3 = VariableRateJump(p3, [x3 ~ x3 + 1, x4 ~ x4 + 1])
396+
j4 = MassActionJump(p4*p5, [x1 => 1, x5 => 1], [x1 => -1, x5 => -1, x2 => 1])
397+
@named js = JumpSystem([j1, j2, j3, j4], t, [x1, x2, x3, x4, x5], [p1, p2, p3, p4, p5])
398+
399+
us = Set(); ps = Set()
400+
iv = t
401+
MT.collect_scoped_vars!(us, ps, js, iv)
402+
@test issetequal(us, [x2])
403+
@test issetequal(ps, [p2])
404+
405+
empty!.((us,ps))
406+
MT.collect_scoped_vars!(us, ps, js, iv; depth = 0)
407+
@test issetequal(us, [x1])
408+
@test issetequal(ps, [p1])
409+
410+
empty!.((us,ps))
411+
MT.collect_scoped_vars!(us, ps, js, iv; depth = 1)
412+
@test issetequal(us, [x2])
413+
@test issetequal(ps, [p2])
414+
415+
empty!.((us,ps))
416+
MT.collect_scoped_vars!(us, ps, js, iv; depth = 2)
417+
@test issetequal(us, [x3, x4])
418+
@test issetequal(ps, [p3, p4])
419+
420+
empty!.((us,ps))
421+
MT.collect_scoped_vars!(us, ps, js, iv; depth = -1)
422+
@test issetequal(us, [x5])
423+
@test issetequal(ps, [p5])
424+
end

0 commit comments

Comments
 (0)