@@ -267,6 +267,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
267
267
end
268
268
dummy_derivatives_set = BitSet (dummy_derivatives)
269
269
270
+ irreducible_set = BitSet ()
270
271
if ag != = nothing
271
272
function isreducible (x)
272
273
# `k` is reducible if all lower differentiated variables are.
@@ -284,7 +285,6 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
284
285
end
285
286
isred
286
287
end
287
- irreducible_set = BitSet ()
288
288
for (k, (c, v)) in ag
289
289
isreducible (k) || push! (irreducible_set, k)
290
290
isreducible (k) || push! (irreducible_set, k)
@@ -293,14 +293,30 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
293
293
end
294
294
end
295
295
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
298
315
299
316
# Derivatives that are either in the dummy derivatives set or ended up not
300
317
# participating in the system at all are not considered differential
301
318
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)
304
320
end
305
321
306
322
# 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
312
328
# We can eliminate variables that are not a selected state (differential
313
329
# variables). Selected states are differentiated variables that are not
314
330
# dummy derivatives.
315
- can_eliminate = let var_to_diff = var_to_diff
331
+ can_eliminate = let var_to_diff = var_to_diff, ag = ag
316
332
v -> begin
317
333
if ag != = nothing
318
334
haskey (ag, v) && return false
0 commit comments