Skip to content

Commit b88327e

Browse files
committed
MatchedSystemStructure: Print derivative/primal in separate columns
This makes it a bit easier to scan and follow derivative relationships: ``` # ∂ₜ eq # ∂ₜ v 1 ∅ | 1 7↑ [3, (5)] 2 ∅ | 2 9↑ ∅ 3 [1, 5, (11)] | 3 8↑ [4, (6)] 4 [3, 5, (12)] | 4 10↑ ∅ 5 7↑ [(1), 6] | 5 [3, 4] 6 8↑ [(3), 6] | 6 13↑ ∫ [5, 6, 7, 8, 9, 10] 7 9↑ 5↓ [6, (7), 13] | 7 11↑ 1↓ [(7)] 8 10↑ 6↓ [6, (8), 13] | 8 12↑ 3↓ [(8)] 9 7↓ [6, 11, 13, 14] | 9 2↓ ∅ 10 8↓ [6, 12, 13, 14] | 10 4↓ ∅ ⋅ | 11 7↓ [(3), 9] ⋅ | 12 8↓ [(4), 10] ⋅ | 13 14↑ 6↓ ∫ [7, 8, 9, 10] ⋅ | 14 13↓ [9, 10] ``` It also makes the diff more useful by showing when, e.g. the derivative of variable `x` is still the same but a derivative of `x` has been newly added to the system.
1 parent f0e245b commit b88327e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

ext/MTKDeepDiffsExt.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module MTKDeepDiffsExt
22

33
using DeepDiffs, ModelingToolkit
4-
using ModelingToolkit.BipartiteGraphs: Label, BipartiteAdjacencyList, unassigned
4+
using ModelingToolkit.BipartiteGraphs: Label, BipartiteAdjacencyList, unassigned,
5+
HighlightInt
56
using ModelingToolkit.SystemStructures: SystemStructure, MatchedSystemStructure,
6-
SystemStructurePrintMatrix, HighlightInt
7+
SystemStructurePrintMatrix
78

89
"""
910
A utility struct for displaying the difference between two HighlightInts.
@@ -162,7 +163,7 @@ end
162163

163164
function Base.getindex(ssdpm::SystemStructureDiffPrintMatrix, i::Integer, j::Integer)
164165
checkbounds(ssdpm, i, j)
165-
if i > 1 && (j == 3 || j == 7)
166+
if i > 1 && (j == 4 || j == 9)
166167
old = new = BipartiteAdjacencyList(nothing, nothing, unassigned)
167168
(i <= size(ssdpm.new, 1)) && (new = ssdpm.new[i, j])
168169
(i <= size(ssdpm.old, 1)) && (old = ssdpm.old[i, j])

src/systems/systemstructure.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,30 +405,31 @@ function SystemStructurePrintMatrix(s::SystemStructure)
405405
complete(s.eq_to_diff),
406406
nothing)
407407
end
408-
Base.size(bgpm::SystemStructurePrintMatrix) = (max(nsrcs(bgpm.bpg), ndsts(bgpm.bpg)) + 1, 7)
409-
function compute_diff_label(diff_graph, i)
408+
Base.size(bgpm::SystemStructurePrintMatrix) = (max(nsrcs(bgpm.bpg), ndsts(bgpm.bpg)) + 1, 9)
409+
function compute_diff_label(diff_graph, i, symbol)
410410
di = i - 1 <= length(diff_graph) ? diff_graph[i - 1] : nothing
411-
ii = i - 1 <= length(invview(diff_graph)) ? invview(diff_graph)[i - 1] : nothing
412-
return Label(string(di === nothing ? "" : string(di, ''),
413-
di !== nothing && ii !== nothing ? " " : "",
414-
ii === nothing ? "" : string(ii, '')))
411+
return di === nothing ? Label("") : Label(string(di, symbol))
415412
end
416413
function Base.getindex(bgpm::SystemStructurePrintMatrix, i::Integer, j::Integer)
417414
checkbounds(bgpm, i, j)
418415
if i <= 1
419-
return (Label.(("#", "∂ₜ", " eq", "", "#", "∂ₜ", " v")))[j]
420-
elseif j == 4
416+
return (Label.(("#", "∂ₜ", " ", " eq", "", "#", "∂ₜ", " ", " v")))[j]
417+
elseif j == 5
421418
colors = Base.text_colors
422419
return Label("|", :light_black)
423420
elseif j == 2
424-
return compute_diff_label(bgpm.eq_to_diff, i)
425-
elseif j == 6
426-
return compute_diff_label(bgpm.var_to_diff, i)
421+
return compute_diff_label(bgpm.eq_to_diff, i, '')
422+
elseif j == 3
423+
return compute_diff_label(invview(bgpm.eq_to_diff), i, '')
424+
elseif j == 7
425+
return compute_diff_label(bgpm.var_to_diff, i, '')
426+
elseif j == 8
427+
return compute_diff_label(invview(bgpm.var_to_diff), i, '')
427428
elseif j == 1
428429
return Label((i - 1 <= length(bgpm.eq_to_diff)) ? string(i - 1) : "")
429-
elseif j == 5
430+
elseif j == 6
430431
return Label((i - 1 <= length(bgpm.var_to_diff)) ? string(i - 1) : "")
431-
elseif j == 3
432+
elseif j == 4
432433
return BipartiteAdjacencyList(i - 1 <= nsrcs(bgpm.bpg) ?
433434
𝑠neighbors(bgpm.bpg, i - 1) : nothing,
434435
bgpm.highlight_graph !== nothing &&
@@ -438,7 +439,7 @@ function Base.getindex(bgpm::SystemStructurePrintMatrix, i::Integer, j::Integer)
438439
bgpm.var_eq_matching !== nothing &&
439440
(i - 1 <= length(invview(bgpm.var_eq_matching))) ?
440441
invview(bgpm.var_eq_matching)[i - 1] : unassigned)
441-
elseif j == 7
442+
elseif j == 9
442443
match = unassigned
443444
if bgpm.var_eq_matching !== nothing && i - 1 <= length(bgpm.var_eq_matching)
444445
match = bgpm.var_eq_matching[i - 1]

0 commit comments

Comments
 (0)