From 2f49790a3e0c5d6d1b4363a6bc06bf680753a787 Mon Sep 17 00:00:00 2001 From: Utkarsh Date: Tue, 18 Jan 2022 14:15:12 +0000 Subject: [PATCH 1/2] Add tests for solvers --- test/autodiff.jl | 13 ++++++++ test/finite_difference.jl | 13 ++++++++ test/nlsolve.jl | 68 +++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 3 +- 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/autodiff.jl create mode 100644 test/finite_difference.jl diff --git a/test/autodiff.jl b/test/autodiff.jl new file mode 100644 index 0000000..dd01812 --- /dev/null +++ b/test/autodiff.jl @@ -0,0 +1,13 @@ +using SciMLNLSolve, SciMLBase +# Test the autodiff technique +function f!(fvec, x, p) + fvec[1] = (x[1]+3)*(x[2]^3-7)+18 + fvec[2] = sin(x[2]*exp(x[1])-1) +end + +prob = NonlinearProblem{true}(f!,[ 0.1; 1.2]) +sol = solve(prob,NLSolveJL(autodiff=:forward)) + +du = zeros(2) +f!(du, sol.u, nothing) +@test maximum(du) < 1e-6 \ No newline at end of file diff --git a/test/finite_difference.jl b/test/finite_difference.jl new file mode 100644 index 0000000..29b3e46 --- /dev/null +++ b/test/finite_difference.jl @@ -0,0 +1,13 @@ +using SciMLNLSolve, SciMLBase +# Test the finite differencing technique +function f!(fvec, x, p) + fvec[1] = (x[1]+3)*(x[2]^3-7)+18 + fvec[2] = sin(x[2]*exp(x[1])-1) +end + +prob = NonlinearProblem{true}(f!,[ 0.1; 1.2]) +sol = solve(prob,NLSolveJL(autodiff=:central)) + +du = zeros(2) +f!(du, sol.u, nothing) +@test maximum(du) < 1e-6 \ No newline at end of file diff --git a/test/nlsolve.jl b/test/nlsolve.jl index e69de29..a40f9d9 100644 --- a/test/nlsolve.jl +++ b/test/nlsolve.jl @@ -0,0 +1,68 @@ +using SciMLNLSolve, SciMLBase + +# IIP Tests +function f_iip(du, u, p, t) + du[1] = 2 - 2u[1] + du[2] = u[1] - 4u[2] +end +u0 = zeros(2) +prob_iip = SteadyStateProblem(f_iip, u0) +abstol = 1e-8 + +for alg in [NLSolveJL()] + sol = solve(prob_iip, alg) + @test sol.retcode == :Success + p = nothing + + du = zeros(2) + f_iip(du, sol.u, nothing, 0) + @test maximum(du) < 1e-6 +end + +# OOP Tests +f_oop(u, p, t) = [2 - 2u[1],u[1] - 4u[2]] +u0 = zeros(2) +prob_oop = SteadyStateProblem(f_oop, u0) + +for alg in [NLSolveJL()] + sol = solve(prob_oop, alg) + @test sol.retcode == :Success + + du = zeros(2) + du = f_oop(sol.u, nothing, 0) + @test maximum(du) < 1e-6 +end + +# NonlinearProblem Tests + +function f_iip(du, u, p) + du[1] = 2 - 2u[1] + du[2] = u[1] - 4u[2] +end +u0 = zeros(2) +prob_iip = NonlinearProblem{true}(f_iip, u0) +abstol = 1e-8 +for alg in [NLSolveJL()] + local sol + sol = solve(prob_iip, alg) + @test sol.retcode == :Success + p = nothing + + du = zeros(2) + f_iip(du, sol.u, nothing) + @test maximum(du) < 1e-6 +end + +# OOP Tests +f_oop(u, p) = [2 - 2u[1],u[1] - 4u[2]] +u0 = zeros(2) +prob_oop = NonlinearProblem{false}(f_oop, u0) +for alg in [NLSolveJL(),] + local sol + sol = solve(prob_oop, alg) + @test sol.retcode == :Success + + du = zeros(2) + du = f_oop(sol.u, nothing) + @test maximum(du) < 1e-6 +end diff --git a/test/runtests.jl b/test/runtests.jl index 854fa7c..52e1654 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,7 +1,6 @@ using Pkg using SafeTestsets using Test -using SciMLBase #@test isempty(detect_ambiguities(SciMLBase)) @@ -10,4 +9,6 @@ const is_APPVEYOR = ( Sys.iswindows() && haskey(ENV,"APPVEYOR") ) @time begin @time @safetestset "NLsolve.jl" begin include("nlsolve.jl") end +@time @safetestset "NLsolve.jl" begin include("autodiff.jl") end +@time @safetestset "NLsolve.jl" begin include("finite_difference.jl") end end From b2c8373ccf1b748a538b971a58d7330add6ee480 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 18 Jan 2022 09:20:38 -0500 Subject: [PATCH 2/2] Update runtests.jl --- test/runtests.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 52e1654..0c371aa 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,7 @@ const GROUP = get(ENV, "GROUP", "All") const is_APPVEYOR = ( Sys.iswindows() && haskey(ENV,"APPVEYOR") ) @time begin -@time @safetestset "NLsolve.jl" begin include("nlsolve.jl") end -@time @safetestset "NLsolve.jl" begin include("autodiff.jl") end -@time @safetestset "NLsolve.jl" begin include("finite_difference.jl") end +@time @safetestset "NLsolve.jl Basic" begin include("nlsolve.jl") end +@time @safetestset "NLsolve.jl Autodiff" begin include("autodiff.jl") end +@time @safetestset "NLsolve.jl FiniteDiff" begin include("finite_difference.jl") end end