Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/systems/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,9 @@ end
Return the sparsity pattern of the jacobian of `sys` as a matrix.
"""
function jacobian_sparsity(sys::System)
sparsity = torn_system_jacobian_sparsity(sys)
sparsity === nothing || return sparsity
# disable to fix https://github.com/SciML/ModelingToolkit.jl/issues/3871
#sparsity = torn_system_jacobian_sparsity(sys)
#sparsity === nothing || return sparsity
Comment on lines -458 to +460
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AayushSabharwal is there a reason the torn sparsity would have false zeros here? That seems like it would give issues in other places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, we've just used torn_system_jacobian_sparsity since the beginning of time iirc. Maybe this doesn't handle the W sparsity correctly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I would think that if this sparsity pattern is wrong, then the incidence matrix is wrong. Can you make a note to follow up on this? For now I want to merge so that these are right, but I'd like to make sure something isn't hiding here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do


Symbolics.jacobian_sparsity([eq.rhs for eq in full_equations(sys)],
[dv for dv in unknowns(sys)])
Expand Down
15 changes: 15 additions & 0 deletions test/jacobiansparsity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,18 @@ end
@test J == prob.f.jac(prob.u0, prob.p, 1.0)
@test J ≈ prob.f.jac(prob.u0, prob.p, 1.0)
end

# https://github.com/SciML/ModelingToolkit.jl/issues/3871
@testset "Issue#3871: Sparsity with observed derivatives" begin
t = ModelingToolkit.t_nounits
D = ModelingToolkit.D_nounits
@variables x(t) y(t)
@mtkcompile sys = System([D(x) ~ x * D(y), D(y) ~ x - y], t)
@test ModelingToolkit.jacobian_sparsity(sys) == [1 1; 1 1] # all nonzero
J1 = calculate_jacobian(sys)
J2 = isequal(unknowns(sys)[1], x) ? [2x-y -x; 1 -1] : [-1 1; -x 2x-y] # analytical result
@test isequal(J1, J2)
prob = ODEProblem(sys, [x => 1.0, y => 0.0], (0.0, 1.0); jac = true, sparse = true)
sol = solve(prob, FBDF())
@test SciMLBase.successful_retcode(sol)
end
Loading