Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Oct 27, 2023
1 parent 19fb541 commit d8d1c95
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/infeasible.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using LinearAlgebra, NonlinearSolve, StaticArrays, Test

# this is infeasible
function f1!(out, u, p)
μ = 3.986004415e14
x = 7000.0e3
y = -6.970561549987071e-9
z = -3.784706123246018e-9
v_x = 8.550491684548064e-12 + u[1]
v_y = 6631.60076191005 + u[2]
v_z = 3600.665431405663 + u[3]
r = @SVector [x, y, z]
v = @SVector [v_x, v_y, v_z]
h = cross(r, v)
ev = cross(v, h) / μ - r / norm(r)
i = acos(h[3] / norm(h))
e = norm(ev)
a = 1 / (2 / norm(r) - (norm(v)^2 / μ))
out .= [a - 42.0e6, e - 1e-5, i - 1e-5]
return nothing
end

# this is unfeasible
function f1(u, p)
μ = 3.986004415e14
x = 7000.0e3
y = -6.970561549987071e-9
z = -3.784706123246018e-9
v_x = 8.550491684548064e-12 + u[1]
v_y = 6631.60076191005 + u[2]
v_z = 3600.665431405663 + u[3]
r = @SVector [x, y, z]
v = @SVector [v_x, v_y, v_z]
h = cross(r, v)
ev = cross(v, h) / μ - r / norm(r)
i = acos(h[3] / norm(h))
e = norm(ev)
a = 1 / (2 / norm(r) - (norm(v)^2 / μ))
return [a - 42.0e6, e - 1e-5, i - 1e-5]
end

@testset "[IIP] Infeasible" begin
u0 = [0.0, 0.0, 0.0]
prob = NonlinearProblem(f1!, u0)
sol = solve(prob)

@test all(!isnan, sol.u)
@test !SciMLBase.successful_retcode(sol.retcode)
end

@testset "[OOP] Infeasible" begin
u0 = [0.0, 0.0, 0.0]
prob = NonlinearProblem(f1, u0)
sol = solve(prob)

@test all(!isnan, sol.u)
@test !SciMLBase.successful_retcode(sol.retcode)

try
u0 = @SVector [0.0, 0.0, 0.0]
prob = NonlinearProblem(f1, u0)
sol = solve(prob)

@test all(!isnan, sol.u)
@test !SciMLBase.successful_retcode(sol.retcode)
catch err
# Static Arrays has different default linearsolve which throws an error
@test err isa SingularException
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end
@time @safetestset "Polyalgs" include("polyalgs.jl")
@time @safetestset "Matrix Resizing" include("matrix_resizing.jl")
@time @safetestset "Nonlinear Least Squares" include("nonlinear_least_squares.jl")
@time @safetestset "Infeasible Problems" include("infeasible.jl")
end

if GROUP == "All" || GROUP == "23TestProblems"
Expand Down

0 comments on commit d8d1c95

Please sign in to comment.