Skip to content

Commit f2c74fb

Browse files
feat: implement sorted_incidence_matrix(sys)
1 parent 658af34 commit f2c74fb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using ModelingToolkit: System, AbstractSystem, var_from_nested_derivative, Diffe
2626
filter_kwargs, lower_varname_with_unit,
2727
lower_shift_varname_with_unit, setio, SparseMatrixCLIL,
2828
get_fullvars, has_equations, observed,
29-
Schedule, schedule
29+
Schedule, schedule, iscomplete, get_schedule
3030

3131
using ModelingToolkit.BipartiteGraphs
3232
import .BipartiteGraphs: invview, complete

src/structural_transformation/utils.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,27 @@ function sorted_incidence_matrix(ts::TransformationState, val = true; only_algeq
205205
sparse(I, J, val, nsrcs(graph), ndsts(graph))
206206
end
207207

208+
"""
209+
$(TYPEDSIGNATURES)
210+
211+
Obtain the incidence matrix of the system sorted by the SCCs. Requires that the system is
212+
simplified and has a `schedule`.
213+
"""
214+
function sorted_incidence_matrix(sys::AbstractSystem)
215+
if !iscomplete(sys) || get_tearing_state(sys) === nothing ||
216+
get_schedule(sys) === nothing
217+
error("A simplified `System` is required. Call `mtkcompile` on the system before creating an `SCCNonlinearProblem`.")
218+
end
219+
sched = get_schedule(sys)
220+
var_sccs = sched.var_sccs
221+
222+
ts = get_tearing_state(sys)
223+
imat = Graphs.incidence_matrix(ts.structure.graph)
224+
buffer = similar(imat)
225+
permute!(buffer, imat, 1:size(imat, 2), reduce(vcat, var_sccs))
226+
buffer
227+
end
228+
208229
###
209230
### Structural and symbolic utilities
210231
###

0 commit comments

Comments
 (0)