Skip to content

Commit ed51a45

Browse files
committed
If a variable is present, all lower diff variables are present
1 parent 835eb6f commit ed51a45

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/structural_transformation/partial_state_selection.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
267267
end
268268
dummy_derivatives_set = BitSet(dummy_derivatives)
269269

270+
irreducible_set = BitSet()
270271
if ag !== nothing
271272
function isreducible(x)
272273
# `k` is reducible if all lower differentiated variables are.
@@ -284,7 +285,6 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
284285
end
285286
isred
286287
end
287-
irreducible_set = BitSet()
288288
for (k, (c, v)) in ag
289289
isreducible(k) || push!(irreducible_set, k)
290290
isreducible(k) || push!(irreducible_set, k)
@@ -293,14 +293,30 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
293293
end
294294
end
295295

296-
is_not_present = v -> isempty(𝑑neighbors(graph, v)) &&
297-
(ag === nothing || !haskey(ag, v) || !(v in irreducible_set))
296+
is_not_present_non_rec = let graph = graph, irreducible_set = irreducible_set
297+
v -> begin
298+
not_in_eqs = isempty(𝑑neighbors(graph, v))
299+
isirreducible = false
300+
if ag !== nothing
301+
isirreducible = haskey(ag, v) && (v in irreducible_set)
302+
end
303+
not_in_eqs && !isirreducible
304+
end
305+
end
306+
307+
is_not_present = let var_to_diff = var_to_diff
308+
v -> while true
309+
# if a higher derivative is present, then it's present
310+
is_not_present_non_rec(v) || return false
311+
v = var_to_diff[v]
312+
v === nothing && return true
313+
end
314+
end
298315

299316
# Derivatives that are either in the dummy derivatives set or ended up not
300317
# participating in the system at all are not considered differential
301318
is_some_diff = let dummy_derivatives_set = dummy_derivatives_set
302-
v -> !(v in dummy_derivatives_set) &&
303-
!(var_to_diff[v] === nothing && is_not_present(v))
319+
v -> !(v in dummy_derivatives_set) && !is_not_present(v)
304320
end
305321

306322
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
@@ -312,7 +328,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
312328
# We can eliminate variables that are not a selected state (differential
313329
# variables). Selected states are differentiated variables that are not
314330
# dummy derivatives.
315-
can_eliminate = let var_to_diff = var_to_diff
331+
can_eliminate = let var_to_diff = var_to_diff, ag = ag
316332
v -> begin
317333
if ag !== nothing
318334
haskey(ag, v) && return false

0 commit comments

Comments
 (0)