Skip to content

Commit ad8730c

Browse files
fix: better handle allow_algebraic=false
1 parent 76d9045 commit ad8730c

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

src/structural_transformation/partial_state_selection.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ function partial_state_selection_graph!(structure::SystemStructure, var_eq_match
170170
end
171171

172172
function dummy_derivative_graph!(state::TransformationState, jac = nothing;
173-
state_priority = nothing, log = Val(false), allow_symbolic = false, kwargs...)
173+
state_priority = nothing, log = Val(false), allow_symbolic = false, allow_algebraic = true, kwargs...)
174174
state.structure.solvable_graph === nothing &&
175-
find_solvables!(state; allow_symbolic, kwargs...)
175+
find_solvables!(state; allow_symbolic, allow_algebraic, kwargs...)
176176
complete!(state.structure)
177-
var_eq_matching = complete(pantelides!(state; allow_symbolic, kwargs...))
177+
var_eq_matching = complete(pantelides!(
178+
state; allow_symbolic, allow_algebraic, kwargs...))
178179
dummy_derivative_graph!(
179-
state.structure, var_eq_matching, jac, state_priority, log; allow_symbolic)
180+
state.structure, var_eq_matching, jac, state_priority, log; allow_symbolic, allow_algebraic)
180181
end
181182

182183
struct DummyDerivativeSummary
@@ -186,7 +187,8 @@ end
186187

187188
function dummy_derivative_graph!(
188189
structure::SystemStructure, var_eq_matching, jac = nothing,
189-
state_priority = nothing, ::Val{log} = Val(false); allow_symbolic = false) where {log}
190+
state_priority = nothing, ::Val{log} = Val(false); allow_symbolic = false,
191+
allow_algebraic = true) where {log}
190192
@unpack eq_to_diff, var_to_diff, graph = structure
191193
diff_to_eq = invview(eq_to_diff)
192194
diff_to_var = invview(var_to_diff)
@@ -345,7 +347,8 @@ function dummy_derivative_graph!(
345347
end
346348

347349
dummy_derivatives_set = BitSet(dummy_derivatives)
348-
make_differential_denominators_unsolvable!(structure, dummy_derivatives_set)
350+
make_differential_denominators_unsolvable!(
351+
structure, dummy_derivatives_set; allow_algebraic)
349352

350353
ret = tearing_with_dummy_derivatives(structure, dummy_derivatives_set)
351354
if log

src/structural_transformation/symbolics_tearing.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ end
926926
function tearing(state::TearingState; kwargs...)
927927
state.structure.solvable_graph === nothing && find_solvables!(state; kwargs...)
928928
complete!(state.structure)
929-
make_differential_denominators_unsolvable!(state.structure)
929+
make_differential_denominators_unsolvable!(
930+
state.structure; allow_algebraic = get(kwargs, :allow_algebraic, true))
930931
tearing_with_dummy_derivatives(state.structure, ())
931932
end
932933

@@ -939,7 +940,7 @@ instead, which calls this function internally.
939940
"""
940941
function tearing(sys::AbstractSystem, state = TearingState(sys); mm = nothing,
941942
simplify = false, array_hack = true, kwargs...)
942-
var_eq_matching, full_var_eq_matching = tearing(state)
943+
var_eq_matching, full_var_eq_matching = tearing(state; kwargs...)
943944
invalidate_cache!(tearing_reassemble(
944945
state, var_eq_matching, full_var_eq_matching; mm, simplify, array_hack))
945946
end

src/structural_transformation/utils.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,11 @@ differential variables that should be considered as algebraic for the purpose of
284284
transformation.
285285
"""
286286
function make_differential_denominators_unsolvable!(
287-
structure::SystemStructure, additional_algevars = ())
287+
structure::SystemStructure, additional_algevars = (); allow_algebraic)
288288
for ((eqi, vari), denoms) in structure.denominators
289-
all(i -> isalgvar(structure, i) || i in additional_algevars, denoms) && continue
289+
if allow_algebraic && all(i -> isalgvar(structure, i) || i in additional_algevars, denoms)
290+
continue
291+
end
290292
rem_edge!(structure.solvable_graph, eqi, vari)
291293
end
292294
end

src/systems/systemstructure.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,18 +722,23 @@ function _structural_simplify!(state::TearingState, io; simplify = false,
722722
end
723723
if fully_determined && dummy_derivative
724724
sys = ModelingToolkit.dummy_derivative(
725-
sys, state; simplify, mm, check_consistency, allow_symbolic, kwargs...)
725+
sys, state; simplify, mm, check_consistency,
726+
allow_symbolic, allow_algebraic, kwargs...)
726727
elseif fully_determined
727-
var_eq_matching = pantelides!(state; finalize = false, kwargs...)
728-
StructuralTransformations.make_differential_denominators_unsolvable!(state.structure)
728+
var_eq_matching = pantelides!(state; finalize = false, allow_algebraic, kwargs...)
729+
StructuralTransformations.make_differential_denominators_unsolvable!(
730+
state.structure; allow_algebraic)
729731
sys = pantelides_reassemble(state, var_eq_matching)
730732
state = TearingState(sys)
731-
sys, mm = ModelingToolkit.alias_elimination!(state; allow_symbolic, kwargs...)
733+
sys, mm = ModelingToolkit.alias_elimination!(
734+
state; allow_symbolic, allow_algebraic, kwargs...)
732735
sys = ModelingToolkit.dummy_derivative(
733-
sys, state; simplify, mm, check_consistency, allow_symbolic, kwargs...)
736+
sys, state; simplify, mm, check_consistency,
737+
allow_symbolic, allow_algebraic, kwargs...)
734738
else
735739
sys = ModelingToolkit.tearing(
736-
sys, state; simplify, mm, check_consistency, allow_symbolic, kwargs...)
740+
sys, state; simplify, mm, check_consistency,
741+
allow_symbolic, allow_algebraic, kwargs...)
737742
end
738743
fullunknowns = [map(eq -> eq.lhs, observed(sys)); unknowns(sys)]
739744
@set! sys.observed = ModelingToolkit.topsort_equations(observed(sys), fullunknowns)

0 commit comments

Comments
 (0)