@@ -294,9 +294,13 @@ function jacobian_dae_sparsity(sys::AbstractODESystem)
294294end
295295
296296function W_sparsity (sys:: AbstractODESystem )
297- jac_sparsity = jacobian_sparsity (sys)
297+ if has_tearing_state (sys)
298+ jac_sparsity = jacobian_sparsity (sys)
299+ else
300+ jac = calculate_jacobian (sys; sparse = true )
301+ jac_sparsity = sparse ((! iszero). (jac))
302+ end
298303 (n, n) = size (jac_sparsity)
299-
300304 M = calculate_massmatrix (sys)
301305 M_sparsity = M isa UniformScaling ? sparse (I (n)) : sparse ((! iszero). (M))
302306 jac_sparsity .|| M_sparsity
@@ -408,14 +412,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem,
408412
409413 if sparse
410414 uElType = u0 === nothing ? Float64 : eltype (u0)
411- if jac
412- jac_prototype = similar (calculate_jacobian (sys; sparse), uElType)
413- else
414- jac_prototype = similar (jacobian_sparsity (sys), uElType)
415- end
416415 W_prototype = similar (W_sparsity (sys), uElType)
417416 else
418- jac_prototype = nothing
419417 W_prototype = nothing
420418 end
421419
@@ -427,9 +425,8 @@ function DiffEqBase.ODEFunction{iip, specialize}(sys::AbstractODESystem,
427425 tgrad = _tgrad === nothing ? nothing : _tgrad,
428426 mass_matrix = _M,
429427 jac_prototype = W_prototype,
430- W_prototype = W_prototype,
431428 observed = observedfun,
432- sparsity = sparsity ? jacobian_sparsity (sys) : nothing ,
429+ sparsity = sparsity ? W_sparsity (sys) : nothing ,
433430 analytic = analytic,
434431 initialization_data)
435432end
0 commit comments