Skip to content

Commit d8373f0

Browse files
committed
add JumpSystem dependency graph tests
1 parent 0ddb5fb commit d8373f0

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export calculate_hessian, generate_hessian
131131
export calculate_massmatrix, generate_diffusion_function
132132

133133
export BipartiteGraph, equation_dependencies, variable_dependencies
134-
export eqeq_dendencies, varvar_dependencies
134+
export eqeq_dependencies, varvar_dependencies
135135
export asgraph, asdigraph
136136

137137
export simplified_expr, rename, get_variables

src/systems/dependency_graphs.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ function asgraph(eqdeps, vtois)
3737
BipartiteGraph(ne, fadjlist, badjlist)
3838
end
3939

40+
function Base.isequal(bg1::BipartiteGraph{T}, bg2::BipartiteGraph{T}) where {T<:Integer}
41+
iseq = (bg1.ne == bg2.ne)
42+
iseq &= (bg1.fadjlist == bg2.fadjlist)
43+
iseq &= (bg1.badjlist == bg2.badjlist)
44+
iseq
45+
end
46+
4047
# could be made to directly generate graph and save memory
4148
function asgraph(sys::AbstractSystem; variables=nothing, variablestoids=nothing)
4249
vs = isnothing(variables) ? states(sys) : variables
@@ -89,7 +96,7 @@ function asdigraph(g::BipartiteGraph, sys::AbstractSystem; variables = states(sy
8996
end
9097

9198
# maps the i'th eq to equations that depend on it
92-
function eqeq_dendencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer}
99+
function eqeq_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer}
93100
g = SimpleDiGraph{T}(length(eqdeps.fadjlist))
94101

95102
for (eqidx,sidxs) in enumerate(vardeps.badjlist)
@@ -104,4 +111,4 @@ function eqeq_dendencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T})
104111
end
105112

106113
# maps the i'th variable to variables that depend on it
107-
varvar_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer} = eqeq_dendencies(vardeps, eqdeps)
114+
varvar_dependencies(eqdeps::BipartiteGraph{T}, vardeps::BipartiteGraph{T}) where {T <: Integer} = eqeq_dependencies(vardeps, eqdeps)

test/dep_graphs.jl

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using ModelingToolkit, LightGraphs
2+
3+
# use a ReactionSystem to generate systems for testing
4+
@parameters k1 k2 t
5+
@variables S(t) I(t) R(t)
6+
7+
rxs = [Reaction(k1, nothing, [S]),
8+
Reaction(k1, [S], nothing),
9+
Reaction(k2, [S,I], [I], [1,1], [2]),
10+
Reaction(k2, [S,R], [S], [2,1], [2]),
11+
Reaction(k1*I, nothing, [R]),
12+
Reaction(k1*k2/(1+t), [S], [R])]
13+
rs = ReactionSystem(rxs, t, [S,I,R], [k1,k2])
14+
15+
16+
#################################
17+
# testing for Jumps
18+
#################################
19+
js = convert(JumpSystem, rs)
20+
S = convert(Variable,S); I = convert(Variable,I); R = convert(Variable,R)
21+
k1 = convert(Variable,k1); k2 = convert(Variable,k2)
22+
23+
# eq to vars they depend on
24+
eq_sdeps = [Variable[], [S], [S,I], [S,R], [I], [S]]
25+
eq_sidepsf = [Int[], [1], [1,2], [1,3], [2], [1]]
26+
eq_sidepsb = [[2,3,4,6], [3,5],[4]]
27+
deps = equation_dependencies(js)
28+
@test all(i -> isequal(Set(eq_sdeps[i]),Set(deps[i])), 1:length(rxs))
29+
depsbg = asgraph(js)
30+
@test depsbg.fadjlist == eq_sidepsf
31+
@test depsbg.badjlist == eq_sidepsb
32+
33+
# eq to params they depend on
34+
eq_pdeps = [[k1],[k1],[k2],[k2],[k1],[k1,k2]]
35+
eq_pidepsf = [[1],[1],[2],[2],[1],[1,2]]
36+
eq_pidepsb = [[1,2,5,6],[3,4,6]]
37+
deps = equation_dependencies(js, variables=parameters(js))
38+
@test all(i -> isequal(Set(eq_pdeps[i]),Set(deps[i])), 1:length(rxs))
39+
depsbg2 = asgraph(js, variables=parameters(js))
40+
@test depsbg2.fadjlist == eq_pidepsf
41+
@test depsbg2.badjlist == eq_pidepsb
42+
43+
# var to eqs that modify them
44+
s_eqdepsf = [[1,2,3,6],[3],[4,5,6]]
45+
s_eqdepsb = [[1],[1],[1,2],[3],[3],[1,3]]
46+
ne = 8
47+
bg = BipartiteGraph(ne, s_eqdepsf, s_eqdepsb)
48+
deps2 = variable_dependencies(js)
49+
@test isequal(bg,deps2)
50+
51+
# eq to eqs that depend on them
52+
eq_eqdeps = [[2,3,4,6],[2,3,4,6],[2,3,4,5,6],[4],[4],[2,3,4,6]]
53+
dg = SimpleDiGraph(6)
54+
for (eqidx,eqdeps) in enumerate(eq_eqdeps)
55+
for eqdepidx in eqdeps
56+
add_edge!(dg, eqidx, eqdepidx)
57+
end
58+
end
59+
dg3 = eqeq_dependencies(depsbg,deps2)
60+
@test dg == dg3
61+
62+
# var to vars they depend on
63+
var_vardeps = [[1,2,3],[1,2,3],[3]]
64+
ne = 7
65+
dg = SimpleDiGraph(3)
66+
for (vidx,vdeps) in enumerate(var_vardeps)
67+
for vdepidx in vdeps
68+
add_edge!(dg, vidx, vdepidx)
69+
end
70+
end
71+
dg4 = varvar_dependencies(depsbg,deps2)
72+
@test dg == dg4
73+
74+

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ using SafeTestsets, Test
1919
@safetestset "PDE Construction Test" begin include("pde.jl") end
2020
@safetestset "Lowering Integration Test" begin include("lowering_solving.jl") end
2121
@safetestset "Test Big System Usage" begin include("bigsystem.jl") end
22+
@safetestset "Depdendency Graph Test" begin include("dep_graphs.jl")
2223
#@testset "Latexify recipes Test" begin include("latexify.jl") end
2324
@testset "Distributed Test" begin include("distributed.jl") end

0 commit comments

Comments
 (0)