|
1 | 1 | # V-nodes `[x_1, x_2, x_3, ..., dx_1, dx_2, ..., y_1, y_2, ...]` where `x`s are |
2 | 2 | # differential variables and `y`s are algebraic variables. |
3 | 3 | function get_vnodes(sys) |
4 | | - diffnodes = Operation[] |
| 4 | + dxvars = Operation[] |
5 | 5 | edges = map(_->Int[], 1:length(sys.eqs)) |
6 | 6 | for (i, eq) in enumerate(sys.eqs) |
7 | 7 | if !(eq.lhs isa Constant) |
8 | 8 | # Make sure that the LHS is a first order derivative of a var. |
9 | 9 | @assert eq.lhs.op isa Differential |
10 | 10 | @assert !(eq.lhs.args[1] isa Differential) # first order |
11 | 11 |
|
12 | | - push!(diffnodes, eq.lhs) |
| 12 | + push!(dxvars, eq.lhs) |
13 | 13 | # For efficiency we note down the diff edges here |
14 | | - push!(edges[i], length(diffnodes)) |
| 14 | + push!(edges[i], length(dxvars)) |
15 | 15 | end |
16 | 16 | end |
17 | 17 |
|
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 |
21 | 21 | end |
22 | 22 |
|
23 | 23 | 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 |
26 | 30 |
|
27 | 31 | for (i, eq) in enumerate(sys.eqs) |
28 | 32 | # T or D(x): |
29 | 33 | # We assume no derivatives appear on the RHS at this point |
30 | 34 | vs = vars(eq.rhs) |
31 | 35 | 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 |
32 | 41 | for (j, target_v) in enumerate(algvars) |
33 | 42 | if v == target_v |
34 | | - push!(edges[i], j+varnumber_offset) |
| 43 | + push!(edges[i], j+algvar_offset) |
35 | 44 | end |
36 | 45 | end |
37 | 46 | end |
38 | 47 | 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 |
40 | 52 | end |
41 | 53 |
|
42 | 54 | print_bigraph(sys, vars, edges) = print_bigraph(stdout, sys, vars, edges) |
|
0 commit comments