Skip to content

Commit b85ce1d

Browse files
committed
fix: calculate_jacobian(sparse) should use W-sparsity)
1 parent e74e754 commit b85ce1d

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/systems/diffeqs/abstractodesystem.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ function calculate_jacobian(sys::AbstractODESystem;
7373

7474
if sparse
7575
jac = sparsejacobian(rhs, dvs, simplify = simplify)
76+
W_s = W_sparsity(sys)
77+
(Is, Js, Vs) = findnz(W_s)
78+
# Add nonzeros of W as non-structural zeros of the Jacobian (to ensure equal results for oop and iip Jacobian.)
79+
for (i, j) in zip(Is, Js)
80+
iszero(jac[i, j]) && begin
81+
jac[i, j] = 1
82+
jac[i, j] = 0
83+
end
84+
end
7685
else
7786
jac = jacobian(rhs, dvs, simplify = simplify)
7887
end
@@ -294,12 +303,7 @@ function jacobian_dae_sparsity(sys::AbstractODESystem)
294303
end
295304

296305
function W_sparsity(sys::AbstractODESystem)
297-
if has_tearing_state(sys)
298-
jac_sparsity = jacobian_sparsity(sys)
299-
else
300-
jac = calculate_jacobian(sys; sparse = true)
301-
jac_sparsity = SparseMatrixCSC{Bool, Int64}((!iszero).(jac))
302-
end
306+
jac_sparsity = jacobian_sparsity(sys)
303307
(n, n) = size(jac_sparsity)
304308
M = calculate_massmatrix(sys)
305309
M_sparsity = M isa UniformScaling ? sparse(I(n)) : SparseMatrixCSC{Bool, Int64}((!iszero).(M))

src/systems/diffeqs/sdesystem.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ struct SDESystem <: AbstractODESystem
164164
"""
165165
is_dde::Bool
166166
isscheduled::Bool
167+
tearing_state::Any
167168

168169
function SDESystem(tag, deqs, neqs, iv, dvs, ps, tspan, var_to_name, ctrls, observed,
169170
tgrad, jac, ctrl_jac, Wfact, Wfact_t, name, description, systems, defaults,
@@ -173,7 +174,8 @@ struct SDESystem <: AbstractODESystem
173174
metadata = nothing, gui_metadata = nothing, namespacing = true,
174175
complete = false, index_cache = nothing, parent = nothing, is_scalar_noise = false,
175176
is_dde = false,
176-
isscheduled = false;
177+
isscheduled = false,
178+
tearing_state = nothing;
177179
checks::Union{Bool, Int} = true)
178180
if checks == true || (checks & CheckComponents) > 0
179181
check_independent_variables([iv])
@@ -198,7 +200,7 @@ struct SDESystem <: AbstractODESystem
198200
ctrl_jac, Wfact, Wfact_t, name, description, systems,
199201
defaults, guesses, initializesystem, initialization_eqs, connector_type, cevents,
200202
devents, parameter_dependencies, assertions, metadata, gui_metadata, namespacing,
201-
complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled)
203+
complete, index_cache, parent, is_scalar_noise, is_dde, isscheduled, tearing_state)
202204
end
203205
end
204206

src/systems/systems.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function __structural_simplify(sys::AbstractSystem, io = nothing; simplify = fal
155155
get_iv(ode_sys), unknowns(ode_sys), parameters(ode_sys);
156156
name = nameof(ode_sys), is_scalar_noise, observed = observed(ode_sys), defaults = defaults(sys),
157157
parameter_dependencies = parameter_dependencies(sys), assertions = assertions(sys),
158-
guesses = guesses(sys), initialization_eqs = initialization_equations(sys))
158+
guesses = guesses(sys), initialization_eqs = initialization_equations(sys), tearing_state = get_tearing_state(ode_sys))
159159
end
160160
end
161161

0 commit comments

Comments
 (0)