Skip to content

Commit e903cf8

Browse files
committed
Variables in the irreducible_set implicitly appear in equations
1 parent 447f4af commit e903cf8

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/structural_transformation/partial_state_selection.jl

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

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+
270294
# Derivatives that are either in the dummy derivatives set or ended up not
271295
# 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
273301

274302
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
275303
# 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)
280306
end
281307

282308
# We can eliminate variables that are not a selected state (differential
283309
# variables). Selected states are differentiated variables that are not
284310
# 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
288312
v -> begin
289313
if ag !== nothing
290314
haskey(ag, v) && return false

0 commit comments

Comments
 (0)