Skip to content

Commit 8bf5d9e

Browse files
feat: add kwarg to allow disabling sorting of equations in structural_simplify
1 parent ea0acd4 commit 8bf5d9e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/systems/systems.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ topological sort of the observed equations in `sys`.
2424
+ When `simplify=true`, the `simplify` function will be applied during the tearing process.
2525
+ `allow_symbolic=false`, `allow_parameter=true`, and `conservative=false` limit the coefficient types during tearing. In particular, `conservative=true` limits tearing to only solve for trivial linear systems where the coefficient has the absolute value of ``1``.
2626
+ `fully_determined=true` controls whether or not an error will be thrown if the number of equations don't match the number of inputs, outputs, and equations.
27+
+ `sort_eqs=true` controls whether equations are sorted lexicographically before simplification or not.
2728
"""
2829
function structural_simplify(
2930
sys::AbstractSystem, io = nothing; additional_passes = [], simplify = false, split = true,
@@ -69,10 +70,11 @@ function __structural_simplify(sys::SDESystem, args...; kwargs...)
6970
return __structural_simplify(ODESystem(sys), args...; kwargs...)
7071
end
7172

72-
function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = false,
73+
function __structural_simplify(
74+
sys::AbstractSystem, io = nothing; simplify = false, sort_eqs = true,
7375
kwargs...)
7476
sys = expand_connections(sys)
75-
state = TearingState(sys)
77+
state = TearingState(sys; sort_eqs)
7678

7779
@unpack structure, fullvars = state
7880
@unpack graph, var_to_diff, var_types = structure

src/systems/systemstructure.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function is_time_dependent_parameter(p, iv)
260260
(args = arguments(p); length(args)) == 1 && isequal(only(args), iv))
261261
end
262262

263-
function TearingState(sys; quick_cancel = false, check = true)
263+
function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true)
264264
sys = flatten(sys)
265265
ivs = independent_variables(sys)
266266
iv = length(ivs) == 1 ? ivs[1] : nothing
@@ -381,11 +381,13 @@ function TearingState(sys; quick_cancel = false, check = true)
381381
neqs = length(eqs)
382382
symbolic_incidence = symbolic_incidence[eqs_to_retain]
383383

384-
# sort equations lexicographically to reduce simplification issues
385-
# depending on order due to NP-completeness of tearing.
386-
sortidxs = Base.sortperm(eqs, by = string)
387-
eqs = eqs[sortidxs]
388-
symbolic_incidence = symbolic_incidence[sortidxs]
384+
if sort_eqs
385+
# sort equations lexicographically to reduce simplification issues
386+
# depending on order due to NP-completeness of tearing.
387+
sortidxs = Base.sortperm(eqs, by = string)
388+
eqs = eqs[sortidxs]
389+
symbolic_incidence = symbolic_incidence[sortidxs]
390+
end
389391

390392
### Handle discrete variables
391393
lowest_shift = Dict()

0 commit comments

Comments
 (0)