Skip to content

Commit 909c6a8

Browse files
committed
add conversion to LightGraph.SimpleDiGraph
1 parent 1768e27 commit 909c6a8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/ModelingToolkit.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ using RecursiveArrayTools
1818
import SymbolicUtils
1919
import SymbolicUtils: to_symbolic, FnType
2020

21+
import LightGraphs: SimpleDiGraph
22+
2123
import TreeViews
2224

2325
"""

src/systems/dependency_graphs.jl

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ function asgraph(sys::AbstractSystem; variables=nothing, variablestoids=nothing)
4545
asgraph(eqdeps, vtois)
4646
end
4747

48-
4948
# for each variable determine the equations that modify it
5049
function variable_dependencies(sys::AbstractSystem; variables=states(sys), variablestoids=nothing)
5150
eqs = equations(sys)
@@ -62,10 +61,43 @@ function variable_dependencies(sys::AbstractSystem; variables=states(sys), varia
6261
fadjlist = [Vector{Int}() for i = 1:length(variables)]
6362
ne = 0
6463
for (eqidx,vidxs) in enumerate(badjlist)
65-
println(vidxs)
6664
foreach(vidx -> push!(fadjlist[vidx], eqidx), vidxs)
6765
ne += length(vidxs)
6866
end
6967

7068
BipartiteGraph(ne, fadjlist, badjlist)
71-
end
69+
end
70+
71+
# convert BipartiteGraph to LightGraph.SimpleDiGraph
72+
function digraph(g::BipartiteGraph, sys::AbstractSystem; variables = states(sys), equationsfirst = true)
73+
neqs = length(equations(sys))
74+
nvars = length(variables)
75+
76+
fadjlist = deepcopy(g.fadjlist)
77+
badjlist = deepcopy(g.badjlist)
78+
if equationsfirst
79+
# variable indices must be incremented by neqs
80+
for i = 1:neqs
81+
fadjlist[i] .+= neqs
82+
end
83+
84+
# variables do not connect to anything
85+
append!(fadjlist, [Vector{Int}() for i=1:nvars])
86+
87+
# eqs have nothing that mapped to them
88+
prepend!(badjlist, [Vector{Int}() for i=1:neqs])
89+
else
90+
# equation indices must be incremented by nvars
91+
for i = 1:nvars
92+
fadjlist[i] .+= nvars
93+
end
94+
95+
# equations do not connect to anything
96+
append!(fadjlist, [Vector{Int}() for i=1:neqs])
97+
98+
# vars have nothing that mapped to them
99+
prepend!(badjlist, [Vector{Int}() for i=1:nvars])
100+
end
101+
102+
SimpleDiGraph(g.ne, fadjlist, badjlist)
103+
end

0 commit comments

Comments
 (0)