Skip to content

Commit 2d1f2ff

Browse files
Merge pull request #3949 from hersle/jac_observed_derivative
Fix Jacobian sparsity pattern with observed derivatives
2 parents d5fd2b6 + 16c9c0d commit 2d1f2ff

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/systems/codegen.jl

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

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

test/jacobiansparsity.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,18 @@ end
139139
@test J == prob.f.jac(prob.u0, prob.p, 1.0)
140140
@test J prob.f.jac(prob.u0, prob.p, 1.0)
141141
end
142+
143+
# https://github.com/SciML/ModelingToolkit.jl/issues/3871
144+
@testset "Issue#3871: Sparsity with observed derivatives" begin
145+
t = ModelingToolkit.t_nounits
146+
D = ModelingToolkit.D_nounits
147+
@variables x(t) y(t)
148+
@mtkcompile sys = System([D(x) ~ x * D(y), D(y) ~ x - y], t)
149+
@test ModelingToolkit.jacobian_sparsity(sys) == [1 1; 1 1] # all nonzero
150+
J1 = calculate_jacobian(sys)
151+
J2 = isequal(unknowns(sys)[1], x) ? [2x-y -x; 1 -1] : [-1 1; -x 2x-y] # analytical result
152+
@test isequal(J1, J2)
153+
prob = ODEProblem(sys, [x => 1.0, y => 0.0], (0.0, 1.0); jac = true, sparse = true)
154+
sol = solve(prob, FBDF())
155+
@test SciMLBase.successful_retcode(sol)
156+
end

0 commit comments

Comments
 (0)