Skip to content
This repository has been archived by the owner on Dec 10, 2023. It is now read-only.

Add tests for solvers #8

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/autodiff.jl
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/finite_difference.jl
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions test/nlsolve.jl
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Pkg
using SafeTestsets
using Test
using SciMLBase

#@test isempty(detect_ambiguities(SciMLBase))

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