11"""
22 SimpleDFSane(; σ_min::Real = 1e-10, σ_max::Real = 1e10, σ_1::Real = 1.0,
3- M::Int = 10 , γ::Real = 1e-4, τ_min::Real = 0.1, τ_max::Real = 0.5,
3+ M::Union{ Int, Val} = Val(10) , γ::Real = 1e-4, τ_min::Real = 0.1, τ_max::Real = 0.5,
44 nexp::Int = 2, η_strategy::Function = (f_1, k, x, F) -> f_1 ./ k^2)
55
66A low-overhead implementation of the df-sane method for solving large-scale nonlinear
@@ -42,21 +42,26 @@ see the paper [1].
4242information for solving large-scale nonlinear systems of equations, Mathematics of
4343Computation, 75, 1429-1448.
4444"""
45- @kwdef @concrete struct SimpleDFSane <: AbstractSimpleNonlinearSolveAlgorithm
46- σ_min = 1e-10
47- σ_max = 1e10
48- σ_1 = 1.0
49- M:: Int = 10
50- γ = 1e-4
51- τ_min = 0.1
52- τ_max = 0.5
53- nexp:: Int = 2
54- η_strategy = (f_1, k, x, F) -> f_1 ./ k^ 2
45+ @concrete struct SimpleDFSane{M} <: AbstractSimpleNonlinearSolveAlgorithm
46+ σ_min
47+ σ_max
48+ σ_1
49+ γ
50+ τ_min
51+ τ_max
52+ nexp:: Int
53+ η_strategy
5554end
5655
57- function SciMLBase. __solve (prob:: NonlinearProblem , alg:: SimpleDFSane , args... ;
56+ function SimpleDFSane (; σ_min:: Real = 1e-10 , σ_max:: Real = 1e10 , σ_1:: Real = 1.0 ,
57+ M:: Union{Int, Val} = Val (10 ), γ:: Real = 1e-4 , τ_min:: Real = 0.1 , τ_max:: Real = 0.5 ,
58+ nexp:: Int = 2 , η_strategy:: F = (f_1, k, x, F) -> f_1 ./ k^ 2 ) where {F}
59+ return SimpleDFSane {SciMLBase._unwrap_val(M)} (σ_min, σ_max, σ_1, γ, τ_min, τ_max, nexp, η_strategy)
60+ end
61+
62+ function SciMLBase. __solve (prob:: NonlinearProblem , alg:: SimpleDFSane{M} , args... ;
5863 abstol = nothing , reltol = nothing , maxiters = 1000 , alias_u0 = false ,
59- termination_condition = nothing , kwargs... )
64+ termination_condition = nothing , kwargs... ) where {M}
6065 x = __maybe_unaliased (prob. u0, alias_u0)
6166 fx = _get_fx (prob, x)
6267 T = eltype (x)
@@ -65,7 +70,7 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane, args...;
6570 σ_max = T (alg. σ_max)
6671 σ_k = T (alg. σ_1)
6772
68- (; M, nexp, η_strategy) = alg
73+ (; nexp, η_strategy) = alg
6974 γ = T (alg. γ)
7075 τ_min = T (alg. τ_min)
7176 τ_max = T (alg. τ_max)
@@ -77,7 +82,11 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane, args...;
7782 α_1 = one (T)
7883 f_1 = fx_norm
7984
80- history_f_k = fill (fx_norm, M)
85+ history_f_k = if x isa SArray
86+ ones (SVector{M, T}) * fx_norm
87+ else
88+ fill (fx_norm, M)
89+ end
8190
8291 # Generate the cache
8392 @bb x_cache = similar (x)
@@ -143,7 +152,11 @@ function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleDFSane, args...;
143152 fx_norm = fx_norm_new
144153
145154 # Store function value
146- history_f_k[mod1 (k, M)] = fx_norm_new
155+ if history_f_k isa SVector
156+ history_f_k = Base. setindex (history_f_k, fx_norm_new, mod1 (k, M))
157+ else
158+ history_f_k[mod1 (k, M)] = fx_norm_new
159+ end
147160 k += 1
148161 end
149162
0 commit comments