Skip to content

Commit 1980ec8

Browse files
Merge pull request #3994 from AayushSabharwal/as/fix-incidence
fix: fix incorrect incidence after tearing some differential equations
2 parents bf8d242 + 091e68e commit 1980ec8

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/structural_transformation/symbolics_tearing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,14 @@ function codegen_equation!(eg::EquationGenerator,
773773
dx = D(simplify_shifts(fullvars[lv]))
774774

775775
neweq = make_differential_equation(var, dx, eq, total_sub)
776+
# We will add `neweq.lhs` to `total_sub`, so any equation involving it won't be
777+
# incident on it. Remove the edges incident on `iv` from the graph, and add
778+
# the replacement vertices from `ieq` so that the incidence is still correct.
776779
for e in 𝑑neighbors(graph, iv)
777780
e == ieq && continue
781+
for v in 𝑠neighbors(graph, ieq)
782+
add_edge!(graph, e, v)
783+
end
778784
rem_edge!(graph, e, iv)
779785
end
780786

src/systems/codegen.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,8 @@ end
455455
Return the sparsity pattern of the jacobian of `sys` as a matrix.
456456
"""
457457
function jacobian_sparsity(sys::System)
458-
# disable to fix https://github.com/SciML/ModelingToolkit.jl/issues/3871
459-
#sparsity = torn_system_jacobian_sparsity(sys)
460-
#sparsity === nothing || return sparsity
458+
sparsity = torn_system_jacobian_sparsity(sys)
459+
sparsity === nothing || return sparsity
461460

462461
Symbolics.jacobian_sparsity([eq.rhs for eq in full_equations(sys)],
463462
[dv for dv in unknowns(sys)])

test/jacobiansparsity.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,10 @@ end
153153
prob = ODEProblem(sys, [x => 1.0, y => 0.0], (0.0, 1.0); jac = true, sparse = true)
154154
sol = solve(prob, FBDF())
155155
@test SciMLBase.successful_retcode(sol)
156+
ts = ModelingToolkit.get_tearing_state(sys)
157+
for ieq in 1:2
158+
vars1 = ts.fullvars[ModelingToolkit.BipartiteGraphs.𝑠neighbors(ts.structure.graph, ieq)]
159+
vars2 = ModelingToolkit.vars(equations(sys)[ieq])
160+
@test issetequal(vars1, vars2)
161+
end
156162
end

0 commit comments

Comments
 (0)