Skip to content

Commit

Permalink
upstream fix to SparseDiffTools.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Oct 6, 2023
1 parent cb105fc commit b5b9298
Showing 1 changed file with 1 addition and 25 deletions.
26 changes: 1 addition & 25 deletions src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f, u, p, ::Val{ii
if has_analytic_jac
f.jac_prototype === nothing ? undefmatrix(u) : f.jac_prototype
else
if f.jac_prototype === nothing
__safe_init_jacobian(jac_cache)
else
f.jac_prototype
end
f.jac_prototype === nothing ? init_jacobian(jac_cache) : f.jac_prototype
end
end

Expand All @@ -102,26 +98,6 @@ function jacobian_caches(alg::AbstractNonlinearSolveAlgorithm, f, u, p, ::Val{ii
return uf, linsolve, J, fu, jac_cache, du
end

@generated function __getfield(c::T, ::Val{S}) where {T, S}
hasfield(T, S) && return :(c.$(S))
return :(nothing)
end

function __safe_init_jacobian(c::SparseDiffTools.AbstractMaybeSparseJacobianCache)
T = promote_type(eltype(c.fx), eltype(c.x))
return __safe_init_jacobian(__getfield(c, Val(:jac_prototype)), T, c.fx, c.x)
end
function __safe_init_jacobian(::Nothing, ::Type{T}, fx, x) where {T}
return similar(fx, T, length(fx), length(x))
end
function __safe_init_jacobian(J::SparseMatrixCSC, ::Type{T}, fx, x) where {T}
@assert size(J, 1) == length(fx) && size(J, 2) == length(x)
return T.(J)
end
function __safe_init_jacobian(J, ::Type{T}, fx, x) where {T}
return similar(fx, T, length(fx), length(x)) # This is not safe for sparse jacobians
end

__get_nonsparse_ad(::AutoSparseForwardDiff) = AutoForwardDiff()
__get_nonsparse_ad(::AutoSparseFiniteDiff) = AutoFiniteDiff()
__get_nonsparse_ad(::AutoSparseZygote) = AutoZygote()

Check warning on line 103 in src/jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/jacobian.jl#L101-L103

Added lines #L101 - L103 were not covered by tests
Expand Down

0 comments on commit b5b9298

Please sign in to comment.