Skip to content

Commit

Permalink
fix sparse test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Dec 4, 2022
1 parent 8b74b25 commit 486bc1e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ function jacobian!(J::AbstractMatrix{<:Number}, cache)
else
isforward = alg_difftype(alg) === Val{:forward}
if isforward
forwardcache = get_tmp_cache(cache, alg, unwrap_cache(cache, true))[2]
uf(forwardcache, x)
uf(fx, x)
#cache.destats.nf += 1
tmp = jacobian_finitediff_forward!(J, uf, x, jac_config, forwardcache,
tmp = jacobian_finitediff_forward!(J, uf, x, jac_config, fx,
cache)
else # not forward difference
tmp = jacobian_finitediff!(J, uf, x, jac_config, cache)
end
cache.destats.nf += tmp
#cache.destats.nf += tmp
end
nothing
end
Expand Down
2 changes: 1 addition & 1 deletion src/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function perform_step!(cache::NewtonRaphsonCache{true})
jacobian!(J, cache)

# u = u - J \ fu
linres = dolinsolve(alg.precs, linsolve, A = J, b = fu, linu = du1,
linres = dolinsolve(alg.precs, linsolve, A = J, b = _vec(fu), linu = _vec(du1),
p = p, reltol = cache.abstol)
cache.linsolve = linres.cache
@. u = u - du1
Expand Down
14 changes: 10 additions & 4 deletions test/sparse.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NonlinearSolve, LinearAlgebra, SparseArrays
using NonlinearSolve, LinearAlgebra, SparseArrays, Symbolics

const N = 32
const xyd_brusselator = range(0,stop=1,length=N)
Expand Down Expand Up @@ -34,10 +34,16 @@ u0 = init_brusselator_2d(xyd_brusselator)
prob_brusselator_2d = NonlinearProblem(brusselator_2d_loop,u0,p)
sol = solve(prob_brusselator_2d, NewtonRaphson())

using Symbolics
du0 = copy(u0)
jac_sparsity = Symbolics.jacobian_sparsity((du,u)->brusselator_2d_loop(du,u,p),du0,u0)

f = NonlinearFunction(brusselator_2d_loop;jac_prototype=float.(jac_sparsity))
prob_brusselator_2d = NonlinearProblem(f,u0,p)
ff = NonlinearFunction(brusselator_2d_loop;jac_prototype=float.(jac_sparsity))
prob_brusselator_2d = NonlinearProblem(ff,u0,p)
sol = solve(prob_brusselator_2d, NewtonRaphson())
@test norm(sol.resid) < 1e-8

sol = solve(prob_brusselator_2d, NewtonRaphson(autodiff=false))
@test norm(sol.resid) < 1e-6

cache = init(prob_brusselator_2d, NewtonRaphson())
@test maximum(cache.jac_config.colorvec) == 12

0 comments on commit 486bc1e

Please sign in to comment.