Skip to content

Commit 472ffa5

Browse files
Merge pull request #67 from JuliaComputing/as/fix-dummy-der-scc-order
fix: [AI] consider variable derivatives when inserting dummy derivative variables into SCCs
2 parents c037c3a + e04aa67 commit 472ffa5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/ModelingToolkitTearing/src/reassemble.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,20 @@ function generate_derivative_variables!(
243243
push!(scc_del_idxs, j_)
244244
end
245245
# `dv` still needs to be present in some SCC. Since we solve for `dv` from
246-
# `0 ~ D(x) - x_t`, it is in its own SCC. This new singleton SCC is solved
247-
# immediately before the one that `dv` used to be in (`i`)
248-
sccs_to_insert[k] = (i, [dv])
246+
# `0 ~ D(x) - x_t`, it is in its own SCC. This new singleton SCC must run
247+
# before the SCC containing var_to_diff[dv] (the 2nd-order derivative), because
248+
# `generate_system_equations!` populates `total_sub[D(x)] = x_t` when processing
249+
# this singleton, and the DerivativeDict substitution for D(D(x)) requires D(x)
250+
# to already be in `total_sub`. If the original SelectedState SCC for `dv` was
251+
# placed after the 2nd-order derivative SCC in BLT order (e.g. when the dynamics
252+
# equation has no incidence on `dv`), inserting at position `i` is too late.
253+
ddv = var_to_diff[dv]
254+
i_insert = if ddv isa Int && ddv <= length(v_to_scc)
255+
min(i, v_to_scc[ddv][1])
256+
else
257+
i
258+
end
259+
sccs_to_insert[k] = (i_insert, [dv])
249260
end
250261
sort!(sccs_to_insert, by = first)
251262
# remove the idxs we need to remove

0 commit comments

Comments
 (0)