Skip to content

Commit e4f0c5e

Browse files
Pass the cache to nlsolve! in PDIRK44's out-of-place branch (#4352)
1 parent 53cc394 commit e4f0c5e

6 files changed

Lines changed: 45 additions & 6 deletions

File tree

lib/OrdinaryDiffEqNonlinearSolve/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OrdinaryDiffEqNonlinearSolve"
22
uuid = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8"
33
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>", "Yingbo Ma <mayingbo5@gmail.com>"]
4-
version = "2.9.0"
4+
version = "2.9.1"
55

66
[deps]
77
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"

lib/OrdinaryDiffEqNonlinearSolve/src/nlsolve.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ function nlsolve!(
112112
# Initialize γW for JET
113113
γW = one(integrator.dt)
114114
if isnewton(nlsolver)
115+
# Checking the type, not just `nothing`: passing something else entirely
116+
# in this slot went unnoticed because `update_W!` happens not to read it
117+
# on the out-of-place path.
118+
cache isa Union{OrdinaryDiffEqCore.OrdinaryDiffEqCache, Nothing} ||
119+
throw(
120+
ArgumentError(
121+
"`nlsolve!` expects the integrator cache in its third argument, got a " *
122+
"$(typeof(cache))"
123+
)
124+
)
115125
cache === nothing &&
116126
throw(ArgumentError("cache is not passed to `nlsolve!` when using NLNewton"))
117127
if nlsolver.method === DIRK

lib/OrdinaryDiffEqPDIRK/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OrdinaryDiffEqPDIRK"
22
uuid = "5dd0a6cf-3d4b-4314-aa06-06d4e299bc89"
33
authors = ["ParamThakkar123 <paramthakkar864@gmail.com>"]
4-
version = "2.4.0"
4+
version = "2.4.1"
55

66
[deps]
77
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"

lib/OrdinaryDiffEqPDIRK/src/pdirk_perform_step.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@ function initialize!(integrator, cache::PDIRK44ConstantCache) end
4545
_nlsolver.γ = γs[1]
4646
_nlsolver.c = cs[1]
4747
markfirststage!(_nlsolver)
48-
k11 = nlsolve!(_nlsolver, integrator, γs[1] * dt, repeat_step)
48+
k11 = nlsolve!(_nlsolver, integrator, cache, repeat_step)
4949
nlsolvefail(_nlsolver) && return
5050
_nlsolver.z = zero(u)
5151
_nlsolver.tmp = uprev
5252
_nlsolver.γ = γs[2]
5353
_nlsolver.c = cs[2]
5454
markfirststage!(_nlsolver)
55-
k12 = nlsolve!(_nlsolver, integrator, γs[2] * dt, repeat_step)
55+
k12 = nlsolve!(_nlsolver, integrator, cache, repeat_step)
5656
nlsolvefail(_nlsolver) && return
5757
_nlsolver.z = zero(u)
5858
_nlsolver.tmp = uprev + α1[1] * k11 + α2[1] * k12
5959
_nlsolver.γ = γs[1]
6060
_nlsolver.c = cs[3]
6161
markfirststage!(_nlsolver)
62-
k21 = nlsolve!(_nlsolver, integrator, γs[1] * dt, repeat_step)
62+
k21 = nlsolve!(_nlsolver, integrator, cache, repeat_step)
6363
nlsolvefail(_nlsolver) && return
6464
_nlsolver.z = zero(u)
6565
_nlsolver.tmp = uprev + α1[2] * k11 + α2[2] * k12
6666
_nlsolver.γ = γs[2]
6767
_nlsolver.c = cs[4]
6868
markfirststage!(_nlsolver)
69-
k22 = nlsolve!(_nlsolver, integrator, γs[2] * dt, repeat_step)
69+
k22 = nlsolve!(_nlsolver, integrator, cache, repeat_step)
7070
nlsolvefail(_nlsolver) && return
7171
integrator.u = uprev + b1 * k11 + b2 * k21 + b3 * k12 + b4 * k22
7272
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using OrdinaryDiffEqPDIRK
2+
using OrdinaryDiffEqCore
3+
using OrdinaryDiffEqNonlinearSolve: nlsolve!
4+
using SciMLBase
5+
using Test
6+
7+
# The out-of-place branch used to pass `γ * dt` where `nlsolve!` takes the
8+
# integrator cache. Nothing complained: the `NLNewton` guard only rejected
9+
# `nothing`, and `update_W!` happens not to read that argument on the
10+
# out-of-place path. Both halves are covered here.
11+
@testset "PDIRK44 out-of-place agrees with in-place" begin
12+
oop = ODEProblem((u, p, t) -> -u * (1 + 0.1u), 1.0, (0.0, 1.0))
13+
iip = ODEProblem((du, u, p, t) -> (du[1] = -u[1] * (1 + 0.1u[1]); nothing), [1.0], (0.0, 1.0))
14+
for threading in (false, true)
15+
a = solve(oop, PDIRK44(; threading); dt = 0.05, adaptive = false)
16+
b = solve(iip, PDIRK44(; threading); dt = 0.05, adaptive = false)
17+
@test SciMLBase.successful_retcode(a)
18+
@test a.u[end] b.u[end][1] rtol = 1.0e-10
19+
end
20+
end
21+
22+
@testset "nlsolve! rejects a third argument that is not a cache" begin
23+
prob = ODEProblem((du, u, p, t) -> (du[1] = -u[1]; nothing), [1.0], (0.0, 1.0))
24+
integrator = init(prob, PDIRK44(threading = false); dt = 0.1, adaptive = false)
25+
nlsolver = first(integrator.cache.nlsolver)
26+
@test_throws ArgumentError nlsolve!(nlsolver, integrator, 0.5, false)
27+
@test_throws ArgumentError nlsolve!(nlsolver, integrator, nothing, false)
28+
end

lib/OrdinaryDiffEqPDIRK/test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ if (TEST_GROUP == "QA" || TEST_GROUP == "ALL") && isempty(VERSION.prerelease)
1818
end
1919

2020
@time @safetestset "Convergence Tests" include("pdirk_convergence_tests.jl")
21+
@time @safetestset "nlsolve! Arguments" include("nlsolve_argument_tests.jl")

0 commit comments

Comments
 (0)