Skip to content

Commit 447f4af

Browse files
KenoYingboMa
authored andcommitted
dummy_derivatives: Make robust against present, but unused derivatives
Like #1842, but for dummy_derivatives.
1 parent 0162555 commit 447f4af

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/structural_transformation/partial_state_selection.jl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
266266
@warn "The number of dummy derivatives ($n_dummys) does not match the number of differentiated equations ($n_diff_eqs)."
267267
end
268268
dummy_derivatives_set = BitSet(dummy_derivatives)
269+
270+
# Derivatives that are either in the dummy derivatives set or ended up not
271+
# 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)))
273+
274+
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
275+
# 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
280+
end
281+
269282
# We can eliminate variables that are not a selected state (differential
270283
# variables). Selected states are differentiated variables that are not
271284
# dummy derivatives.
@@ -277,16 +290,12 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
277290
haskey(ag, v) && return false
278291
end
279292
dv = var_to_diff[v]
280-
dv === nothing || dv in dummy_derivatives_set
293+
dv === nothing && return true
294+
is_some_diff(dv) || return true
295+
return false
281296
end
282297
end
283298

284-
# We don't want tearing to give us `y_t ~ D(y)`, so we skip equations with
285-
# actually differentiated variables.
286-
isdiffed = let diff_to_var = diff_to_var, dummy_derivatives_set = dummy_derivatives_set
287-
v -> diff_to_var[v] !== nothing && !(v in dummy_derivatives_set)
288-
end
289-
290299
var_eq_matching = tear_graph_modia(structure, isdiffed,
291300
Union{Unassigned, SelectedState};
292301
varfilter = can_eliminate)
@@ -295,7 +304,7 @@ function dummy_derivative_graph!(structure::SystemStructure, var_eq_matching, ja
295304
continue
296305
end
297306
dv = var_to_diff[v]
298-
(dv === nothing || dv in dummy_derivatives_set) && continue
307+
(dv === nothing || !is_some_diff(dv)) && continue
299308
var_eq_matching[v] = SelectedState()
300309
end
301310

0 commit comments

Comments
 (0)