From 24852cc97bb65892d46f2207f9d8e05b4db5bb54 Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Thu, 4 Jan 2024 20:23:30 -0500 Subject: [PATCH 1/2] make RobustMultiNewton always respect autodiff choice --- src/default.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/default.jl b/src/default.jl index 6986299f0..abc4397d1 100644 --- a/src/default.jl +++ b/src/default.jl @@ -207,7 +207,7 @@ function RobustMultiNewton(::Type{T} = Float64; concrete_jac = nothing, linsolve # Let's atleast have something here for complex numbers algs = (NewtonRaphson(; concrete_jac, linsolve, precs, autodiff),) else - algs = (TrustRegion(; concrete_jac, linsolve, precs), + algs = (TrustRegion(; concrete_jac, linsolve, precs, autodiff), TrustRegion(; concrete_jac, linsolve, precs, autodiff, radius_update_scheme = RadiusUpdateSchemes.Bastin), NewtonRaphson(; concrete_jac, linsolve, precs, linesearch = BackTracking(), From 8614ebe8653c8a00f989d7dc38b66c454f9aea5a Mon Sep 17 00:00:00 2001 From: oscarddssmith Date: Fri, 5 Jan 2024 14:37:43 -0500 Subject: [PATCH 2/2] format and add tests --- docs/src/solvers/FixedPointSolvers.md | 2 +- ext/NonlinearSolveSIAMFANLEquationsExt.jl | 7 ++++--- test/misc/no_ad.jl | 23 +++++++++++++++++++++++ test/runtests.jl | 1 + test/wrappers/fixedpoint.jl | 6 ++++-- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 test/misc/no_ad.jl diff --git a/docs/src/solvers/FixedPointSolvers.md b/docs/src/solvers/FixedPointSolvers.md index 704561905..0d5a6f826 100644 --- a/docs/src/solvers/FixedPointSolvers.md +++ b/docs/src/solvers/FixedPointSolvers.md @@ -50,4 +50,4 @@ robust. ### SIAMFANLEquations.jl - - `SIAMFANLEquationsJL(; method = :anderson)`: Anderson acceleration for fixed point problems. \ No newline at end of file + - `SIAMFANLEquationsJL(; method = :anderson)`: Anderson acceleration for fixed point problems. diff --git a/ext/NonlinearSolveSIAMFANLEquationsExt.jl b/ext/NonlinearSolveSIAMFANLEquationsExt.jl index 6ebe82268..27da9dd81 100644 --- a/ext/NonlinearSolveSIAMFANLEquationsExt.jl +++ b/ext/NonlinearSolveSIAMFANLEquationsExt.jl @@ -43,7 +43,8 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SIAMFANLEquationsJL, arg rtol = NonlinearSolve.DEFAULT_TOLERANCE(reltol, T) if prob.u0 isa Number - f = method == :anderson ? (du, u) -> (du = prob.f(u, prob.p)) : ((u) -> prob.f(u, prob.p)) + f = method == :anderson ? (du, u) -> (du = prob.f(u, prob.p)) : + ((u) -> prob.f(u, prob.p)) if method == :newton sol = nsolsc(f, prob.u0; maxit = maxiters, atol, rtol, printerr = ShT) @@ -55,7 +56,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SIAMFANLEquationsJL, arg elseif method == :anderson f, u = NonlinearSolve.__construct_f(prob; alias_u0, make_fixed_point = Val(true), can_handle_arbitrary_dims = Val(true)) - sol = aasol(f, [prob.u0], m, __zeros_like(u, 1, 2*m+4); maxit = maxiters, + sol = aasol(f, [prob.u0], m, __zeros_like(u, 1, 2 * m + 4); maxit = maxiters, atol, rtol, beta = beta) end @@ -110,7 +111,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SIAMFANLEquationsJL, arg elseif method == :anderson f!, u = NonlinearSolve.__construct_f(prob; alias_u0, can_handle_arbitrary_dims = Val(true), make_fixed_point = Val(true)) - sol = aasol(f!, u, m, zeros(T, N, 2*m+4), atol = atol, rtol = rtol, + sol = aasol(f!, u, m, zeros(T, N, 2 * m + 4), atol = atol, rtol = rtol, maxit = maxiters, beta = beta) end else diff --git a/test/misc/no_ad.jl b/test/misc/no_ad.jl new file mode 100644 index 000000000..4dc8a1a8e --- /dev/null +++ b/test/misc/no_ad.jl @@ -0,0 +1,23 @@ +using LinearAlgebra, NonlinearSolve, Test + +@testset "[IIP] no AD" begin + f_iip = Base.Experimental.@opaque (du, u, p) -> du .= u .* u .- p + u0 = [0.0] + prob = NonlinearProblem(f_iip, u0, 1.0) + for alg in [RobustMultiNewton(autodiff = AutoFiniteDiff()())] + sol = solve(prob, alg) + @test isapprox(only(sol.u), 1.0) + @test SciMLBase.successful_retcode(sol.retcode) + end +end + +@testset "[OOP] no AD" begin + f_oop = Base.Experimental.@opaque (u, p) -> u .* u .- p + u0 = [0.0] + prob = NonlinearProblem{false}(f_oop, u0, 1.0) + for alg in [RobustMultiNewton(autodiff = AutoFiniteDiff())] + sol = solve(prob, alg) + @test isapprox(only(sol.u), 1.0) + @test SciMLBase.successful_retcode(sol.retcode) + end +end diff --git a/test/runtests.jl b/test/runtests.jl index 2e1a241ed..f48303249 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -35,6 +35,7 @@ end @time @safetestset "Matrix Resizing" include("misc/matrix_resizing.jl") @time @safetestset "Infeasible Problems" include("misc/infeasible.jl") @time @safetestset "Banded Matrices" include("misc/banded_matrices.jl") + @time @safetestset "No AD" include("misc/no_ad.jl") end if GROUP == "GPU" diff --git a/test/wrappers/fixedpoint.jl b/test/wrappers/fixedpoint.jl index a66a2eaba..282c8c124 100644 --- a/test/wrappers/fixedpoint.jl +++ b/test/wrappers/fixedpoint.jl @@ -1,4 +1,5 @@ -using NonlinearSolve, FixedPointAcceleration, SpeedMapping, NLsolve, SIAMFANLEquations, LinearAlgebra, Test +using NonlinearSolve, + FixedPointAcceleration, SpeedMapping, NLsolve, SIAMFANLEquations, LinearAlgebra, Test # Simple Scalar Problem @testset "Simple Scalar Problem" begin @@ -29,7 +30,8 @@ end @test maximum(abs.(solve(prob, SpeedMappingJL()).resid)) ≤ 1e-10 @test maximum(abs.(solve(prob, SpeedMappingJL(; orders = [3, 2])).resid)) ≤ 1e-10 @test maximum(abs.(solve(prob, SpeedMappingJL(; stabilize = true)).resid)) ≤ 1e-10 - @test maximum(abs.(solve(prob, SIAMFANLEquationsJL(; method = :anderson)).resid)) ≤ 1e-10 + @test maximum(abs.(solve(prob, SIAMFANLEquationsJL(; method = :anderson)).resid)) ≤ + 1e-10 @test_broken maximum(abs.(solve(prob, NLsolveJL(; method = :anderson)).resid)) ≤ 1e-10 end