Skip to content

Commit 113ed20

Browse files
authored
Merge pull request #1891 from SciML/myb/inf
Handle self-alias in a differentiation chain more carefully
2 parents 9a44828 + a5c5b8f commit 113ed20

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/systems/alias_elimination.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ function alias_eliminate_graph!(graph, var_to_diff, mm_orig::SparseMatrixCLIL)
732732
c, dr = reach₌[idx]
733733
@assert c == 1
734734
end
735+
dr in stem_set && break
735736
var_to_diff[prev_r] = dr
736737
push!(updated_diff_vars, prev_r)
737738
prev_r = dr
@@ -901,10 +902,14 @@ function alias_eliminate_graph!(graph, var_to_diff, mm_orig::SparseMatrixCLIL)
901902
# RHS or its derivaitves must still exist in the system to be valid aliases.
902903
needs_update = false
903904
function contains_v_or_dv(var_to_diff, graph, v)
905+
counter = 0
904906
while true
905907
isempty(𝑑neighbors(graph, v)) || return true
906908
v = var_to_diff[v]
907909
v === nothing && return false
910+
counter += 1
911+
counter > 10_000 &&
912+
error("Internal error: there's an infinite loop in the `var_to_diff` graph.")
908913
end
909914
end
910915
for (v, (c, a)) in ag

test/reduction.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,10 @@ eqs = [x ~ 0
295295
ss = alias_elimination(sys)
296296
@test isempty(equations(ss))
297297
@test sort(observed(ss), by = string) == ([D(x), x, y] .~ 0)
298+
299+
eqs = [D(D(x)) ~ -x]
300+
@named sys = ODESystem(eqs, t, [x], [])
301+
ss = alias_elimination(sys)
302+
@test length(equations(ss)) == length(states(ss)) == 1
303+
ss = structural_simplify(sys)
304+
@test length(equations(ss)) == length(states(ss)) == 2

0 commit comments

Comments
 (0)