Skip to content

Commit 2cc41f7

Browse files
committed
test: add tests
1 parent 0ced8d4 commit 2cc41f7

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

lib/ImplicitDiscreteSolve/src/solve.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function initialize!(integrator, cache::IDSolveCache)
2222
else
2323
(u_next, p) -> f(u_next, p.u, p.p, p.t_next)
2424
end
25-
nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) == length(integrator.u))
25+
nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) != length(integrator.u))
26+
@show nlls
2627

2728
prob = if nlls
2829
NonlinearLeastSquaresProblem{isinplace(f)}(NonlinearFunction(_f; resid_prototype = f.resid_prototype), cache.state.u, cache.state)
@@ -49,7 +50,7 @@ function _initialize_dae!(integrator, prob::ImplicitDiscreteProblem,
4950
(u_next, p) -> f(u_next, p.u, p.p, p.t_next)
5051
end
5152

52-
nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) == length(integrator.u))
53+
nlls = !isnothing(f.resid_prototype) && (length(f.resid_prototype) != length(integrator.u))
5354
prob = if nlls
5455
NonlinearLeastSquaresProblem{isinplace(f)}(NonlinearFunction(_f; resid_prototype = f.resid_prototype), u, initstate)
5556
else

lib/ImplicitDiscreteSolve/test/runtests.jl

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using OrdinaryDiffEqSDIRK
2020
tspan = (0., 0.5)
2121

2222
idprob = ImplicitDiscreteProblem(f!, u0, tspan, []; dt = 0.01)
23-
idsol = solve(idprob, SimpleIDSolve())
23+
idsol = solve(idprob, IDSolve())
2424

2525
oprob = ODEProblem(lotkavolterra, u0, tspan)
2626
osol = solve(oprob, ImplicitEuler())
@@ -43,7 +43,7 @@ using OrdinaryDiffEqSDIRK
4343
tspan = (0, 0.2)
4444

4545
idprob = ImplicitDiscreteProblem(g!, u0, tspan, []; dt = 0.01)
46-
idsol = solve(idprob, SimpleIDSolve())
46+
idsol = solve(idprob, IDSolve())
4747

4848
oprob = ODEProblem(ff, u0, tspan)
4949
osol = solve(oprob, ImplicitEuler())
@@ -60,7 +60,7 @@ end
6060
tsteps = 15
6161
u0 = [1., 3.]
6262
idprob = ImplicitDiscreteProblem(periodic!, u0, (0, tsteps), [])
63-
integ = init(idprob, SimpleIDSolve())
63+
integ = init(idprob, IDSolve())
6464
@test integ.u[1]^2 + integ.u[2]^2 16
6565

6666
for ts in 1:tsteps
@@ -77,5 +77,31 @@ end
7777
tsteps = 5
7878
u0 = nothing
7979
idprob = ImplicitDiscreteProblem(empty, u0, (0, tsteps), [])
80-
@test_nowarn integ = init(idprob, SimpleIDSolve())
80+
@test_nowarn integ = init(idprob, IDSolve())
81+
end
82+
83+
@testset "Create NonlinearLeastSquaresProblem" begin
84+
function over(u_next, u, p, t)
85+
[u_next[1] - 1, u_next[2] - 1, u_next[1] - u_next[2]]
86+
end
87+
88+
tsteps = 5
89+
u0 = [1., 1.]
90+
idprob = ImplicitDiscreteProblem(ImplicitDiscreteFunction(over, resid_prototype = zeros(3)), u0, (0, tsteps), [])
91+
integ = init(idprob, IDSolve())
92+
@test integ.cache.prob isa NonlinearLeastSquaresProblem
93+
94+
function under(u_next, u, p, t)
95+
[u_next[1] - u_next[2] - 1]
96+
end
97+
idprob = ImplicitDiscreteProblem(ImplicitDiscreteFunction(under; resid_prototype = zeros(1)), u0, (0, tsteps), [])
98+
integ = init(idprob, IDSolve())
99+
@test integ.cache.prob isa NonlinearLeastSquaresProblem
100+
101+
function full(u_next, u, p, t)
102+
[u_next[1]^2 - 3, u_next[2] - u[1]]
103+
end
104+
idprob = ImplicitDiscreteProblem(ImplicitDiscreteFunction(full; resid_prototype = zeros(2)), u0, (0, tsteps), [])
105+
integ = init(idprob, IDSolve())
106+
@test integ.cache.prob isa NonlinearProblem
81107
end

0 commit comments

Comments
 (0)