-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add partial SimpleKlement Implementation
- Loading branch information
Showing
5 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
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::ImmutableNonlinearProblem, alg::SimpleKlement, args...; | ||
abstol = nothing, reltol = nothing, maxiters = 1000, | ||
alias_u0 = false, termination_condition = nothing, kwargs...) | ||
x = Utils.maybe_unaliased(prob.u0, alias_u0) | ||
T = eltype(x) | ||
|
||
abstol, reltol, tc_cache = NonlinearSolveBase.init_termination_cache( | ||
prob, abstol, reltol, fx, x, termination_condition, Val(:simple)) | ||
|
||
@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(iszero, J) && (J = Utils.identity_jacobian!!(J)) | ||
|
||
@bb @. δx = fprev / J | ||
|
||
@bb @. x = xo - δx | ||
fx = Utils.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 SciMLBase.build_solution(prob, alg, x, fx; retcode = ReturnCode.MaxIters) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module Utils | ||
|
||
using ADTypes: AbstractADType, AutoForwardDiff, AutoFiniteDiff, AutoPolyesterForwardDiff | ||
using ArrayInterface: ArrayInterface | ||
using DifferentiationInterface: DifferentiationInterface | ||
using FastClosures: @closure | ||
using LinearAlgebra: LinearAlgebra, I, diagind | ||
using NonlinearSolveBase: NonlinearSolveBase, ImmutableNonlinearProblem | ||
using SciMLBase: SciMLBase, NonlinearLeastSquaresProblem, NonlinearProblem, | ||
NonlinearFunction | ||
using StaticArraysCore: StaticArray, SArray, SMatrix, SVector | ||
|
||
const DI = DifferentiationInterface | ||
|
||
const safe_similar = NonlinearSolveBase.Utils.safe_similar | ||
|
||
pickchunksize(n::Int) = min(n, 12) | ||
|
||
can_dual(::Type{<:Real}) = true | ||
can_dual(::Type) = false | ||
|
||
maybe_unaliased(x::Union{Number, SArray}, ::Bool) = x | ||
function maybe_unaliased(x::T, alias::Bool) where {T <: AbstractArray} | ||
(alias || !ArrayInterface.can_setindex(T)) && return x | ||
return copy(x) | ||
end | ||
|
||
function get_concrete_autodiff(_, ad::AbstractADType) | ||
DI.check_available(ad) && return ad | ||
error("AD Backend $(ad) is not available. This could be because you haven't loaded the \ | ||
actual backend (See [Differentiation Inferface Docs](https://gdalle.github.io/DifferentiationInterface.jl/DifferentiationInterface/stable/) \ | ||
for more details) or the backend might not be supported by DifferentiationInferface.jl.") | ||
end | ||
function get_concrete_autodiff( | ||
prob, ad::Union{AutoForwardDiff{nothing}, AutoPolyesterForwardDiff{nothing}}) | ||
return get_concrete_autodiff(prob, | ||
ArrayInterface.parameterless_type(ad)(; | ||
chunksize = pickchunksize(length(prob.u0)), ad.tag)) | ||
end | ||
function get_concrete_autodiff(prob, ::Nothing) | ||
if can_dual(eltype(prob.u0)) && DI.check_available(AutoForwardDiff()) | ||
return AutoForwardDiff(; chunksize = pickchunksize(length(prob.u0))) | ||
end | ||
DI.check_available(AutoFiniteDiff()) && return AutoFiniteDiff() | ||
error("Default AD backends are not available. Please load either FiniteDiff or \ | ||
ForwardDiff for default AD selection to work. Else provide a specific AD \ | ||
backend (instead of `nothing`) to the solver.") | ||
end | ||
|
||
# NOTE: This doesn't initialize the `f(x)` but just returns a buffer of the same size | ||
function get_fx(prob::NonlinearLeastSquaresProblem, x) | ||
if SciMLBase.isinplace(prob) && prob.f.resid_prototype === nothing | ||
error("Inplace NonlinearLeastSquaresProblem requires a `resid_prototype` to be \ | ||
specified.") | ||
end | ||
return get_fx(prob.f, x, prob.p) | ||
end | ||
function get_fx(prob::Union{ImmutableNonlinearProblem, NonlinearProblem}, x) | ||
return get_fx(prob.f, x, prob.p) | ||
end | ||
function get_fx(f::NonlinearFunction, x, p) | ||
if SciMLBase.isinplace(f) | ||
f.resid_prototype === nothing && return eltype(x).(f.resid_prototype) | ||
return safe_similar(x) | ||
end | ||
return f(x, p) | ||
end | ||
|
||
function eval_f(prob, fx, x) | ||
SciMLBase.isinplace(prob) || return prob.f(x, prob.p) | ||
prob.f(fx, x, prob.p) | ||
return fx | ||
end | ||
|
||
function fixed_parameter_function(prob::AbstractNonlinearProblem) | ||
SciMLBase.isinplace(prob) && return @closure (du, u) -> prob.f(du, u, prob.p) | ||
return Base.Fix2(prob.f, prob.p) | ||
end | ||
|
||
# __init_identity_jacobian(u::Number, fu, α = true) = oftype(u, α) | ||
# function __init_identity_jacobian(u, fu, α = true) | ||
# J = __similar(u, promote_type(eltype(u), eltype(fu)), length(fu), length(u)) | ||
# fill!(J, zero(eltype(J))) | ||
# J[diagind(J)] .= eltype(J)(α) | ||
# return J | ||
# end | ||
# function __init_identity_jacobian(u::StaticArray, fu, α = true) | ||
# S1, S2 = length(fu), length(u) | ||
# J = SMatrix{S1, S2, eltype(u)}(I * α) | ||
# return J | ||
# end | ||
|
||
identity_jacobian!!(J::Number) = one(J) | ||
function identity_jacobian!!(J::AbstractVector) | ||
ArrayInterface.can_setindex(J) || return one.(J) | ||
fill!(J, true) | ||
return J | ||
end | ||
function identity_jacobian!!(J::AbstractMatrix) | ||
ArrayInterface.can_setindex(J) || return convert(typeof(J), I) | ||
J[diagind(J)] .= true | ||
return J | ||
end | ||
identity_jacobian!!(::SMatrix{S1, S2, T}) where {S1, S2, T} = SMatrix{S1, S2, T}(I) | ||
identity_jacobian!!(::SVector{S1, T}) where {S1, T} = ones(SVector{S1, T}) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
using TestItemRunner, InteractiveUtils | ||
|
||
@info sprint(InteractiveUtils.versioninfo) | ||
|
||
@run_package_tests |