Skip to content

Commit 9c05aa4

Browse files
committed
test: test W_sparsity
1 parent 1e6b296 commit 9c05aa4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ end
132132

133133
function assert_jac_length_header(sys)
134134
W = W_sparsity(sys)
135-
(W_Is, W_Js, W_Vs) = findnz(W)
136135
identity, expr -> Func([expr.args...], [], LiteralExpr(quote
137-
J_Is, J_Js, J_Vs = $(findnz)($(expr.args[1]))
138-
@assert (J_Is, J_Js) == ($W_Is, $W_Js)
136+
@assert $(findnz)($(expr.args[1]))[1:2] == $(findnz)($W)[1:2]
139137
expr.body
140138
end))
141139
end
@@ -294,7 +292,7 @@ end
294292

295293
function W_sparsity(sys::AbstractODESystem)
296294
jac_sparsity = jacobian_sparsity(sys)
297-
M_sparsity = sparse(iszero.(calculate_massmatrix(sys)))
295+
M_sparsity = sparse((!iszero).(calculate_massmatrix(sys)))
298296
jac_sparsity .|| M_sparsity
299297
end
300298

test/jacobiansparsity.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using OrdinaryDiffEq, ModelingToolkit, Test, SparseArrays
1+
using OrdinaryDiffEq, ModelingToolkit, SparseArrays
22

33
N = 3
44
xyd_brusselator = range(0, stop = 1, length = N)
@@ -82,3 +82,26 @@ prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = false)
8282

8383
prob = ODEProblem(sys, u0, (0, 11.5), sparse = true, jac = true)
8484
@test eltype(prob.f.jac_prototype) == Float32
85+
86+
@testset "W matrix sparsity" begin
87+
@parameters g
88+
@variables x(t) y(t) [state_priority = 10] λ(t)
89+
eqs = [D(D(x)) ~ λ * x
90+
D(D(y)) ~ λ * y - g
91+
x^2 + y^2 ~ 1]
92+
@mtkbuild pend = ODESystem(eqs, t)
93+
94+
u0 = [x => 1, y => 0]
95+
prob = ODEProblem(pend, u0, (0, 11.5), [g => 1], guesses ==> 1], sparse = true, jac = true)
96+
jac, jac! = generate_jacobian(pend; expression = Val{false}, sparse = true)
97+
jac_prototype = ModelingToolkit.jacobian_sparsity(pend)
98+
W_prototype = ModelingToolkit.W_sparsity(pend)
99+
@test nnz(W_prototype) == nnz(jac_prototype) + 2
100+
101+
@test findnz(prob.f.jac_prototype)[1:2] == findnz(W_prototype)[1:2]
102+
103+
u = zeros(5)
104+
p = prob.p
105+
t = 0.0
106+
@test_throws AssertionError jac!(similar(jac_prototype, Float64), u, p, t)
107+
end

0 commit comments

Comments
 (0)