@@ -267,24 +267,48 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
267
267
end
268
268
dummy_derivatives_set = BitSet (dummy_derivatives)
269
269
270
+ if ag != = nothing
271
+ function isreducible (x)
272
+ # `k` is reducible if all lower differentiated variables are.
273
+ isred = true
274
+ while isred
275
+ if x in dummy_derivatives_set
276
+ break
277
+ end
278
+ x = diff_to_var[x]
279
+ x === nothing && break
280
+ if ! haskey (ag, x)
281
+ isred = false
282
+ end
283
+ end
284
+ isred
285
+ end
286
+ irreducible_set = BitSet ()
287
+ for (k, (_, v)) in ag
288
+ isreducible (k) || push! (irreducible_set, k)
289
+ isreducible (k) || push! (irreducible_set, k)
290
+ push! (irreducible_set, v)
291
+ end
292
+ end
293
+
270
294
# Derivatives that are either in the dummy derivatives set or ended up not
271
295
# participating in the system at all are not considered differential
272
- is_some_diff (v) = ! (v in dummy_derivatives_set) && ! (var_to_diff[v] === nothing && isempty (𝑑neighbors (graph, v)))
296
+ is_some_diff = let dummy_derivatives_set = dummy_derivatives_set
297
+ v -> ! (v in dummy_derivatives_set) &&
298
+ ! (var_to_diff[v] === nothing && isempty (𝑑neighbors (graph, v)) &&
299
+ (ag === nothing || ! (v in irreducible_set)))
300
+ end
273
301
274
302
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
275
303
# actually differentiated variables.
276
- isdiffed = let diff_to_var = diff_to_var, dummy_derivatives_set = dummy_derivatives_set
277
- function (v)
278
- diff_to_var[v] != = nothing && is_some_diff (v)
279
- end
304
+ isdiffed = let diff_to_var = diff_to_var
305
+ v -> diff_to_var[v] != = nothing && is_some_diff (v)
280
306
end
281
307
282
308
# We can eliminate variables that are not a selected state (differential
283
309
# variables). Selected states are differentiated variables that are not
284
310
# dummy derivatives.
285
- can_eliminate = let var_to_diff = var_to_diff,
286
- dummy_derivatives_set = dummy_derivatives_set
287
-
311
+ can_eliminate = let var_to_diff = var_to_diff
288
312
v -> begin
289
313
if ag != = nothing
290
314
haskey (ag, v) && return false
0 commit comments