From 486bc1e3242cf4781f11795f979a6b3838ced2a1 Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Sun, 4 Dec 2022 02:43:57 +0100 Subject: [PATCH] fix sparse test --- src/jacobian.jl | 7 +++---- src/raphson.jl | 2 +- test/sparse.jl | 14 ++++++++++---- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/jacobian.jl b/src/jacobian.jl index a8ee4050a..e8f6fb2f9 100644 --- a/src/jacobian.jl +++ b/src/jacobian.jl @@ -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 diff --git a/src/raphson.jl b/src/raphson.jl index 327b9a94d..9e7ae6c88 100644 --- a/src/raphson.jl +++ b/src/raphson.jl @@ -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 diff --git a/test/sparse.jl b/test/sparse.jl index 24c16408a..22642fa59 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -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) @@ -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