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..0c371aa 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)) @@ -9,5 +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 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