Skip to content

Commit b83baaa

Browse files
avik-palChrisRackauckas
authored andcommitted
feat: allow simple newton steps
1 parent 769b890 commit b83baaa

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Sundials"
22
uuid = "c3572dad-4567-51f8-b174-8c6c989267f4"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "4.27.0"
4+
version = "4.28.0"
55

66
[deps]
77
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"

src/common_interface/algorithms.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ KINSOL(;
726726
prec_side = 0,
727727
krylov_dim = 0,
728728
globalization_strategy = :None
729+
maxsetupcalls=0
729730
)
730731
```
731732
@@ -754,6 +755,11 @@ The choices for globalization strategy are:
754755
755756
- `:None`: No globalization strategy
756757
- `:LineSearch`: A line search globalization strategy
758+
759+
Other options:
760+
761+
- `maxsetupcalls`: Maximum number of nonlinear iterations that can be performed between
762+
calls to the preconditioner or Jacobian setup function.
757763
"""
758764
struct KINSOL{LinearSolver} <: SundialsNonlinearSolveAlgorithm{LinearSolver}
759765
jac_upper::Int
@@ -762,6 +768,7 @@ struct KINSOL{LinearSolver} <: SundialsNonlinearSolveAlgorithm{LinearSolver}
762768
prec_side::Int
763769
krylov_dim::Int
764770
globalization_strategy::Symbol
771+
maxsetupcalls::Int
765772
end
766773

767774
Base.@pure function KINSOL(;
@@ -771,7 +778,8 @@ Base.@pure function KINSOL(;
771778
userdata = nothing,
772779
prec_side = 0,
773780
krylov_dim = 0,
774-
globalization_strategy = :None)
781+
globalization_strategy = :None,
782+
maxsetupcalls = 0)
775783
if !(linear_solver in (:None,
776784
:Dense,
777785
:LapackDense,
@@ -789,7 +797,7 @@ Base.@pure function KINSOL(;
789797
error("Globalization strategy not accepted.")
790798
end
791799
KINSOL{linear_solver}(jac_upper, jac_lower, userdata, prec_side, krylov_dim,
792-
globalization_strategy)
800+
globalization_strategy, maxsetupcalls)
793801
end
794802

795803
method_choice(alg::SundialsODEAlgorithm{Method}) where {Method} = Method

src/common_interface/solve.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ function DiffEqBase.__solve(prob::Union{
7676
alg.prec_side,
7777
alg.krylov_dim,
7878
maxiters,
79-
strategy = alg.globalization_strategy)
79+
strategy = alg.globalization_strategy,
80+
alg.maxsetupcalls)
8081

8182
f!(resid, u)
8283
retcode = interpret_sundials_retcode(flag)

src/simple.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ function ___kinsol(f,
5555
krylov_dim::Int = 0,
5656
jac_prototype = nothing,
5757
maxiters = 1000,
58-
strategy = :None)
58+
strategy = :None,
59+
maxsetupcalls=0)
5960
# f, Function to be optimized of the form f(y::Vector{Float64}, fy::Vector{Float64})
6061
# where `y` is the input vector, and `fy` is the result of the function
6162
# y0, Vector of initial values
@@ -111,6 +112,7 @@ function ___kinsol(f,
111112
flag = @checkflag KINSetLinearSolver(kmem, LS, A) true
112113
flag = @checkflag KINSetUserData(kmem, userfun) true
113114
flag = @checkflag KINSetNumMaxIters(kmem, maxiters) true
115+
flag = @checkflag KINSetMaxSetupCalls(kmem, maxsetupcalls) true
114116
## Solve problem
115117
scale = ones(length(y0))
116118
if strategy == :None

test/kinsol_nonlinear_solve.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ prob_oop = NonlinearProblem{false}(f_oop, u0)
3636
du = zeros(2)
3737
f_oop(sol.u, nothing)
3838
@test maximum(abs, du) < 1e-6
39+
40+
# Pure Newton Steps
41+
alg = KINSOL(; linear_solver, globalization_strategy, maxsetupcalls = 1)
42+
sol = solve(prob_oop, alg; abstol)
43+
@test SciMLBase.successful_retcode(sol.retcode)
44+
45+
du = zeros(2)
46+
f_oop(sol.u, nothing)
47+
@test maximum(abs, du) < 1e-6
3948
end
4049

4150
# Scalar

0 commit comments

Comments
 (0)