This repository was archived by the owner on May 15, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathklement.jl
More file actions
48 lines (36 loc) · 1.38 KB
/
klement.jl
File metadata and controls
48 lines (36 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
SimpleKlement()
A low-overhead implementation of `Klement` [klement2014using](@citep). This
method is non-allocating on scalar and static array problems.
"""
struct SimpleKlement <: AbstractSimpleNonlinearSolveAlgorithm end
function SciMLBase.__solve(prob::NonlinearProblem, alg::SimpleKlement, args...;
abstol = nothing, reltol = nothing, maxiters = 1000, alias_u0 = false,
termination_condition = nothing, kwargs...)
x = __maybe_unaliased(prob.u0, alias_u0)
T = eltype(x)
fx = _get_fx(prob, x)
abstol, reltol, tc_cache = init_termination_cache(prob, abstol, reltol, fx, x,
termination_condition)
@bb δx = copy(x)
@bb fprev = copy(fx)
@bb xo = copy(x)
@bb d = copy(x)
J = one.(x)
@bb δx² = similar(x)
for _ in 1:maxiters
any(any(iszero, J)) && (J = __init_identity_jacobian!!(J))
@bb @. δx = fprev / J
@bb @. x = xo - δx
fx = __eval_f(prob, fx, x)
# Termination Checks
tc_sol = check_termination(tc_cache, fx, x, xo, prob, alg)
tc_sol !== nothing && return tc_sol
@bb δx .*= -1
@bb @. δx² = δx^2 * J^2
@bb @. J += (fx - fprev - J * δx) / ifelse(iszero(δx²), T(1e-5), δx²) * δx * (J^2)
@bb copyto!(fprev, fx)
@bb copyto!(xo, x)
end
return build_solution(prob, alg, x, fx; retcode = ReturnCode.MaxIters)
end