From b0aa0afb46f9c05d442600388f8beb250cce5e11 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 18 Aug 2023 12:37:44 -0400 Subject: [PATCH 1/3] Newton Raphson ignores jac-prototype --- .gitignore | 2 ++ Project.toml | 2 +- src/raphson.jl | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 525e271af..aa4ff57e3 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ docs/site/ # environment. Manifest.toml docs/src/assets/Project.toml + +.vscode diff --git a/Project.toml b/Project.toml index 14ae59a09..8e204f043 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "NonlinearSolve" uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" authors = ["SciML"] -version = "1.9.0" +version = "1.10.0" [deps] ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" diff --git a/src/raphson.jl b/src/raphson.jl index 437af65c3..48f224959 100644 --- a/src/raphson.jl +++ b/src/raphson.jl @@ -106,7 +106,11 @@ end function jacobian_caches(alg::NewtonRaphson, f, u, p, ::Val{true}) uf = JacobianWrapper(f, p) - J = ArrayInterface.undefmatrix(u) + J = if f.jac_prototype === nothing + ArrayInterface.undefmatrix(u) + else + f.jac_prototype + end linprob = LinearProblem(J, _vec(zero(u)); u0 = _vec(zero(u))) weight = similar(u) From 011f790af8dc6e997e21ccd93dbf2f165b6a82c5 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 18 Aug 2023 15:38:05 -0400 Subject: [PATCH 2/3] Check jac_prototype usage --- test/sparse.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/sparse.jl b/test/sparse.jl index 577a1c7be..bb1d72ed8 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -41,8 +41,10 @@ sol = solve(prob_brusselator_2d, NewtonRaphson()) du0 = copy(u0) jac_sparsity = Symbolics.jacobian_sparsity((du, u) -> brusselator_2d_loop(du, u, p), du0, u0) +jac_prototype = float.(jac_sparsity) +fill!(jac_prototype, 0) -ff = NonlinearFunction(brusselator_2d_loop; jac_prototype = float.(jac_sparsity)) +ff = NonlinearFunction(brusselator_2d_loop; jac_prototype) prob_brusselator_2d = NonlinearProblem(ff, u0, p) sol = solve(prob_brusselator_2d, NewtonRaphson()) @test norm(sol.resid) < 1e-8 From 503d579b2465a472a41e07521df8bc58d55e7ad8 Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 18 Aug 2023 17:44:18 -0400 Subject: [PATCH 3/3] Check jac_prototype usage --- test/sparse.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/sparse.jl b/test/sparse.jl index bb1d72ed8..1f4d07161 100644 --- a/test/sparse.jl +++ b/test/sparse.jl @@ -43,11 +43,13 @@ jac_sparsity = Symbolics.jacobian_sparsity((du, u) -> brusselator_2d_loop(du, u, u0) jac_prototype = float.(jac_sparsity) fill!(jac_prototype, 0) +@test all(iszero, jac_prototype) ff = NonlinearFunction(brusselator_2d_loop; jac_prototype) prob_brusselator_2d = NonlinearProblem(ff, u0, p) sol = solve(prob_brusselator_2d, NewtonRaphson()) @test norm(sol.resid) < 1e-8 +@test !all(iszero, jac_prototype) sol = solve(prob_brusselator_2d, NewtonRaphson(autodiff = false)) @test norm(sol.resid) < 1e-6