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

Commit 93554e6

Browse files
Merge pull request #8 from utkarsh530/u/test
Add tests for solvers
2 parents 6a1a088 + b2c8373 commit 93554e6

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

test/autodiff.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SciMLNLSolve, SciMLBase
2+
# Test the autodiff technique
3+
function f!(fvec, x, p)
4+
fvec[1] = (x[1]+3)*(x[2]^3-7)+18
5+
fvec[2] = sin(x[2]*exp(x[1])-1)
6+
end
7+
8+
prob = NonlinearProblem{true}(f!,[ 0.1; 1.2])
9+
sol = solve(prob,NLSolveJL(autodiff=:forward))
10+
11+
du = zeros(2)
12+
f!(du, sol.u, nothing)
13+
@test maximum(du) < 1e-6

test/finite_difference.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SciMLNLSolve, SciMLBase
2+
# Test the finite differencing technique
3+
function f!(fvec, x, p)
4+
fvec[1] = (x[1]+3)*(x[2]^3-7)+18
5+
fvec[2] = sin(x[2]*exp(x[1])-1)
6+
end
7+
8+
prob = NonlinearProblem{true}(f!,[ 0.1; 1.2])
9+
sol = solve(prob,NLSolveJL(autodiff=:central))
10+
11+
du = zeros(2)
12+
f!(du, sol.u, nothing)
13+
@test maximum(du) < 1e-6

test/nlsolve.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using SciMLNLSolve, SciMLBase
2+
3+
# IIP Tests
4+
function f_iip(du, u, p, t)
5+
du[1] = 2 - 2u[1]
6+
du[2] = u[1] - 4u[2]
7+
end
8+
u0 = zeros(2)
9+
prob_iip = SteadyStateProblem(f_iip, u0)
10+
abstol = 1e-8
11+
12+
for alg in [NLSolveJL()]
13+
sol = solve(prob_iip, alg)
14+
@test sol.retcode == :Success
15+
p = nothing
16+
17+
du = zeros(2)
18+
f_iip(du, sol.u, nothing, 0)
19+
@test maximum(du) < 1e-6
20+
end
21+
22+
# OOP Tests
23+
f_oop(u, p, t) = [2 - 2u[1],u[1] - 4u[2]]
24+
u0 = zeros(2)
25+
prob_oop = SteadyStateProblem(f_oop, u0)
26+
27+
for alg in [NLSolveJL()]
28+
sol = solve(prob_oop, alg)
29+
@test sol.retcode == :Success
30+
31+
du = zeros(2)
32+
du = f_oop(sol.u, nothing, 0)
33+
@test maximum(du) < 1e-6
34+
end
35+
36+
# NonlinearProblem Tests
37+
38+
function f_iip(du, u, p)
39+
du[1] = 2 - 2u[1]
40+
du[2] = u[1] - 4u[2]
41+
end
42+
u0 = zeros(2)
43+
prob_iip = NonlinearProblem{true}(f_iip, u0)
44+
abstol = 1e-8
45+
for alg in [NLSolveJL()]
46+
local sol
47+
sol = solve(prob_iip, alg)
48+
@test sol.retcode == :Success
49+
p = nothing
50+
51+
du = zeros(2)
52+
f_iip(du, sol.u, nothing)
53+
@test maximum(du) < 1e-6
54+
end
55+
56+
# OOP Tests
57+
f_oop(u, p) = [2 - 2u[1],u[1] - 4u[2]]
58+
u0 = zeros(2)
59+
prob_oop = NonlinearProblem{false}(f_oop, u0)
60+
for alg in [NLSolveJL(),]
61+
local sol
62+
sol = solve(prob_oop, alg)
63+
@test sol.retcode == :Success
64+
65+
du = zeros(2)
66+
du = f_oop(sol.u, nothing)
67+
@test maximum(du) < 1e-6
68+
end

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using Pkg
22
using SafeTestsets
33
using Test
4-
using SciMLBase
54

65
#@test isempty(detect_ambiguities(SciMLBase))
76

87
const GROUP = get(ENV, "GROUP", "All")
98
const is_APPVEYOR = ( Sys.iswindows() && haskey(ENV,"APPVEYOR") )
109

1110
@time begin
12-
@time @safetestset "NLsolve.jl" begin include("nlsolve.jl") end
11+
@time @safetestset "NLsolve.jl Basic" begin include("nlsolve.jl") end
12+
@time @safetestset "NLsolve.jl Autodiff" begin include("autodiff.jl") end
13+
@time @safetestset "NLsolve.jl FiniteDiff" begin include("finite_difference.jl") end
1314
end

0 commit comments

Comments
 (0)