Skip to content

Commit 6e6cded

Browse files
committed
Add variable association list
1 parent 96040b9 commit 6e6cded

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/systems/diffeqs/index_reduction.jl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
11
# V-nodes `[x_1, x_2, x_3, ..., dx_1, dx_2, ..., y_1, y_2, ...]` where `x`s are
22
# differential variables and `y`s are algebraic variables.
33
function get_vnodes(sys)
4-
diffnodes = Operation[]
4+
dxvars = Operation[]
55
edges = map(_->Int[], 1:length(sys.eqs))
66
for (i, eq) in enumerate(sys.eqs)
77
if !(eq.lhs isa Constant)
88
# Make sure that the LHS is a first order derivative of a var.
99
@assert eq.lhs.op isa Differential
1010
@assert !(eq.lhs.args[1] isa Differential) # first order
1111

12-
push!(diffnodes, eq.lhs)
12+
push!(dxvars, eq.lhs)
1313
# For efficiency we note down the diff edges here
14-
push!(edges[i], length(diffnodes))
14+
push!(edges[i], length(dxvars))
1515
end
1616
end
1717

18-
diffvars = (first var_from_nested_derivative).(diffnodes)
19-
algvars = setdiff(states(sys), diffvars)
20-
return diffnodes, edges, algvars
18+
xvars = (first var_from_nested_derivative).(dxvars)
19+
algvars = setdiff(states(sys), xvars)
20+
return xvars, dxvars, edges, algvars
2121
end
2222

2323
function sys2bigraph(sys)
24-
diffvars, edges, algvars = get_vnodes(sys)
25-
varnumber_offset = length(diffvars)
24+
xvars, dxvars, edges, algvars = get_vnodes(sys)
25+
xvar_offset = length(xvars)
26+
algvar_offset = 2xvar_offset
27+
for edge in edges
28+
isempty(edge) || (edge .+= xvar_offset)
29+
end
2630

2731
for (i, eq) in enumerate(sys.eqs)
2832
# T or D(x):
2933
# We assume no derivatives appear on the RHS at this point
3034
vs = vars(eq.rhs)
3135
for v in vs
36+
for (j, target_v) in enumerate(xvars)
37+
if v == target_v
38+
push!(edges[i], j)
39+
end
40+
end
3241
for (j, target_v) in enumerate(algvars)
3342
if v == target_v
34-
push!(edges[i], j+varnumber_offset)
43+
push!(edges[i], j+algvar_offset)
3544
end
3645
end
3746
end
3847
end
39-
vcat(diffvars, algvars), edges
48+
49+
fullvars = [xvars; dxvars; algvars] # full list of variables
50+
vars_asso = [(1:xvar_offset) .+ xvar_offset; zeros(Int, length(fullvars) - xvar_offset)] # variable association list
51+
return edges, fullvars, vars_asso
4052
end
4153

4254
print_bigraph(sys, vars, edges) = print_bigraph(stdout, sys, vars, edges)

0 commit comments

Comments
 (0)