From d8d1c95c031d18cb2a3b124dda4d0aed343b8dad Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Fri, 27 Oct 2023 15:35:25 -0400 Subject: [PATCH] Add tests --- test/infeasible.jl | 70 ++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 71 insertions(+) create mode 100644 test/infeasible.jl diff --git a/test/infeasible.jl b/test/infeasible.jl new file mode 100644 index 000000000..001ce1f6e --- /dev/null +++ b/test/infeasible.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index d4f817d0a..a5365cf38 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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"