Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SIAMFANLEquations wrapper #333

Merged
merged 19 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ FixedPointAcceleration = "817d07cb-a79a-5c30-9a31-890123675176"
LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891"
MINPACK = "4854310b-de5a-5eb6-a2a5-c1dee2bd17f9"
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
SIAMFANLEquations = "084e46ad-d928-497d-ad5e-07fa361a48c4"
SpeedMapping = "f1835b91-879b-4a3f-a438-e4baacf14412"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
Expand All @@ -47,6 +48,7 @@ NonlinearSolveFixedPointAccelerationExt = "FixedPointAcceleration"
NonlinearSolveLeastSquaresOptimExt = "LeastSquaresOptim"
NonlinearSolveMINPACKExt = "MINPACK"
NonlinearSolveNLsolveExt = "NLsolve"
NonlinearSolveSIAMFANLEquationsExt = "SIAMFANLEquations"
NonlinearSolveSpeedMappingExt = "SpeedMapping"
NonlinearSolveSymbolicsExt = "Symbolics"
NonlinearSolveZygoteExt = "Zygote"
Expand All @@ -55,8 +57,8 @@ NonlinearSolveZygoteExt = "Zygote"
ADTypes = "0.2.5"
Aqua = "0.8"
ArrayInterface = "7.7"
BandedMatrices = "1.3"
BenchmarkTools = "1"
BandedMatrices = "1.4"
BenchmarkTools = "1.4"
ConcreteStructs = "0.2"
DiffEqBase = "6.144"
EnumX = "1"
Expand Down Expand Up @@ -86,6 +88,7 @@ Reexport = "1.2"
SafeTestsets = "0.1"
SciMLBase = "2.11"
SciMLOperators = "0.3.7"
SIAMFANLEquations = "1.0.1"
SimpleNonlinearSolve = "1.0.2"
SparseArrays = "1.9"
SparseDiffTools = "2.14"
Expand Down Expand Up @@ -118,6 +121,7 @@ OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SIAMFANLEquations = "084e46ad-d928-497d-ad5e-07fa361a48c4"
SparseDiffTools = "47a9eef4-7e08-11e9-0b38-333d64bd3804"
SpeedMapping = "f1835b91-879b-4a3f-a438-e4baacf14412"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Expand All @@ -127,4 +131,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[targets]
test = ["Aqua", "Enzyme", "BenchmarkTools", "SafeTestsets", "Pkg", "Test", "ForwardDiff", "StaticArrays", "Symbolics", "LinearSolve", "Random", "LinearAlgebra", "Zygote", "SparseDiffTools", "NonlinearProblemLibrary", "LeastSquaresOptim", "FastLevenbergMarquardt", "NaNMath", "BandedMatrices", "DiffEqBase", "StableRNGs", "MINPACK", "NLsolve", "OrdinaryDiffEq", "SpeedMapping", "FixedPointAcceleration"]
test = ["Aqua", "Enzyme", "BenchmarkTools", "SafeTestsets", "Pkg", "Test", "ForwardDiff", "StaticArrays", "Symbolics", "LinearSolve", "Random", "LinearAlgebra", "Zygote", "SparseDiffTools", "NonlinearProblemLibrary", "LeastSquaresOptim", "FastLevenbergMarquardt", "NaNMath", "BandedMatrices", "DiffEqBase", "StableRNGs", "MINPACK", "NLsolve", "OrdinaryDiffEq", "SpeedMapping", "FixedPointAcceleration", "SIAMFANLEquations"]
3 changes: 2 additions & 1 deletion docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pages = ["index.md",
"api/leastsquaresoptim.md",
"api/fastlevenbergmarquardt.md",
"api/speedmapping.md",
"api/fixedpointacceleration.md"],
"api/fixedpointacceleration.md",
"api/siamfanlequations.md"],
"Release Notes" => "release_notes.md",
]
2 changes: 1 addition & 1 deletion docs/src/api/fastlevenbergmarquardt.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# FastLevenbergMarquardt.jl

This is a extension for importing solvers from FastLevenbergMarquardt.jl into the SciML
This is an extension for importing solvers from FastLevenbergMarquardt.jl into the SciML
interface. Note that these solvers do not come by default, and thus one needs to install
the package before using these solvers:

Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/leastsquaresoptim.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# LeastSquaresOptim.jl

This is a extension for importing solvers from LeastSquaresOptim.jl into the SciML
This is an extension for importing solvers from LeastSquaresOptim.jl into the SciML
interface. Note that these solvers do not come by default, and thus one needs to install
the package before using these solvers:

Expand Down
17 changes: 17 additions & 0 deletions docs/src/api/siamfanlequations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SIAMFANLEquations.jl

This is an extension for importing solvers from [SIAMFANLEquations.jl](https://github.com/ctkelley/SIAMFANLEquations.jl) into the SciML
interface. Note that these solvers do not come by default, and thus one needs to install
the package before using these solvers:

```julia
using Pkg
Pkg.add("SIAMFANLEquations")
using SIAMFANLEquations, NonlinearSolve
```

## Solver API

```@docs
SIAMFANLEquationsJL
```
137 changes: 137 additions & 0 deletions ext/NonlinearSolveSIAMFANLEquationsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module NonlinearSolveSIAMFANLEquationsExt

using NonlinearSolve, SciMLBase
using SIAMFANLEquations
import ConcreteStructs: @concrete
import UnPack: @unpack

function SciMLBase.__solve(prob::NonlinearProblem, alg::SIAMFANLEquationsJL, args...; abstol = nothing,
reltol = nothing, alias_u0::Bool = false, maxiters = 1000, termination_condition = nothing, kwargs...)
@assert (termination_condition === nothing) || (termination_condition isa AbsNormTerminationMode) "SIAMFANLEquationsJL does not support termination conditions!"

@unpack method, show_trace, delta, linsolve = alg

iip = SciMLBase.isinplace(prob)
T = eltype(prob.u0)

atol = abstol === nothing ? real(oneunit(T)) * (eps(real(one(T))))^(4 // 5) : abstol
rtol = reltol === nothing ? real(oneunit(T)) * (eps(real(one(T))))^(4 // 5) : reltol

if prob.u0 isa Number
f! = if iip
function (u)
du = similar(u)
prob.f(du, u, prob.p)
return du

Check warning on line 25 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L22-L25

Added lines #L22 - L25 were not covered by tests
end
else
u -> prob.f(u, prob.p)
end

if method == :newton
sol = nsolsc(f!, prob.u0; maxit = maxiters, atol = atol, rtol = rtol, printerr = show_trace)
elseif method == :pseudotransient
sol = ptcsolsc(f!, prob.u0; delta0 = delta, maxit = maxiters, atol = atol, rtol=rtol, printerr = show_trace)
elseif method == :secant
sol = secant(f!, prob.u0; maxit = maxiters, atol = atol, rtol = rtol, printerr = show_trace)
end

if sol.errcode == 0
retcode = ReturnCode.Success
elseif sol.errcode == 10
retcode = ReturnCode.MaxIters
elseif sol.errcode == 1
retcode = ReturnCode.Failure
elseif sol.errcode == -1
retcode = ReturnCode.Default

Check warning on line 46 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L41-L46

Added lines #L41 - L46 were not covered by tests
end
stats = method == :pseudotransient ? nothing : (SciMLBase.NLStats(sum(sol.stats.ifun), sum(sol.stats.ijac), 0, 0, sum(sol.stats.iarm)))
return SciMLBase.build_solution(prob, alg, sol.solution, sol.history; retcode, stats, original = sol)
else
u = NonlinearSolve.__maybe_unaliased(prob.u0, alias_u0)
end

if iip
f! = function (du, u)
prob.f(du, u, prob.p)
return du
end
else
f! = function (du, u)
du .= prob.f(u, prob.p)
return du
end
end

# Allocate ahead for function
N = length(u)
FS = zeros(T, N)

# Jacobian free Newton Krylov
if linsolve !== nothing
# Allocate ahead for Krylov basis
JVS = linsolve == :gmres ? zeros(T, N, 3) : zeros(T, N)

Check warning on line 73 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L73

Added line #L73 was not covered by tests
# `linsolve` as a Symbol to keep unified interface with other EXTs, SIAMFANLEquations directly use String to choose between different linear solvers
linsolve_alg = String(linsolve)

Check warning on line 75 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L75

Added line #L75 was not covered by tests

if method == :newton
sol = nsoli(f!, u, FS, JVS; lsolver = linsolve_alg, maxit = maxiters, atol = atol, rtol = rtol, printerr = show_trace)
elseif method == :pseudotransient
sol = ptcsoli(f!, u, FS, JVS; lsolver = linsolve_alg, maxit = maxiters, atol = atol, rtol = rtol, printerr = show_trace)

Check warning on line 80 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L77-L80

Added lines #L77 - L80 were not covered by tests
end

if sol.errcode == 0
retcode = ReturnCode.Success
elseif sol.errcode == 10
retcode = ReturnCode.MaxIters
elseif sol.errcode == 1
retcode = ReturnCode.Failure
elseif sol.errcode == -1
retcode = ReturnCode.Default

Check warning on line 90 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L83-L90

Added lines #L83 - L90 were not covered by tests
end
stats = method == :pseudotransient ? nothing : (SciMLBase.NLStats(sum(sol.stats.ifun), sum(sol.stats.ijac), 0, 0, sum(sol.stats.iarm)))
return SciMLBase.build_solution(prob, alg, sol.solution, sol.history; retcode, stats, original = sol)

Check warning on line 93 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L92-L93

Added lines #L92 - L93 were not covered by tests
end

# Allocate ahead for Jacobian
FPS = zeros(T, N, N)
if prob.f.jac === nothing
# Use the built-in Jacobian machinery
if method == :newton
sol = nsol(f!, u, FS, FPS;
sham=1, atol = atol, rtol = rtol, maxit = maxiters,
printerr = show_trace)
elseif method == :pseudotransient

Check warning on line 104 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L104

Added line #L104 was not covered by tests
sol = ptcsol(f!, u, FS, FPS;
atol = atol, rtol = rtol, maxit = maxiters,
delta0 = delta, printerr = show_trace)
end
else
AJ!(J, u, x) = prob.f.jac(J, x, prob.p)
if method == :newton
sol = nsol(f!, u, FS, FPS, AJ!;
sham=1, atol = atol, rtol = rtol, maxit = maxiters,
printerr = show_trace)
elseif method == :pseudotransient
sol = ptcsol(f!, u, FS, FPS, AJ!;
atol = atol, rtol = rtol, maxit = maxiters,
delta0 = delta, printerr = show_trace)
end
end

if sol.errcode == 0
retcode = ReturnCode.Success
elseif sol.errcode == 10
retcode = ReturnCode.MaxIters
elseif sol.errcode == 1
retcode = ReturnCode.Failure
elseif sol.errcode == -1
retcode = ReturnCode.Default

Check warning on line 129 in ext/NonlinearSolveSIAMFANLEquationsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/NonlinearSolveSIAMFANLEquationsExt.jl#L124-L129

Added lines #L124 - L129 were not covered by tests
end

# pseudo transient continuation has a fixed cost per iteration, iteration statistics are not interesting here.
stats = method == :pseudotransient ? nothing : (SciMLBase.NLStats(sum(sol.stats.ifun), sum(sol.stats.ijac), 0, 0, sum(sol.stats.iarm)))
return SciMLBase.build_solution(prob, alg, sol.solution, sol.history; retcode, stats, original = sol)
end

end
2 changes: 1 addition & 1 deletion src/NonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export RadiusUpdateSchemes
export NewtonRaphson, TrustRegion, LevenbergMarquardt, DFSane, GaussNewton, PseudoTransient,
Broyden, Klement, LimitedMemoryBroyden
export LeastSquaresOptimJL,
FastLevenbergMarquardtJL, CMINPACK, NLsolveJL, FixedPointAccelerationJL, SpeedMappingJL
FastLevenbergMarquardtJL, CMINPACK, NLsolveJL, FixedPointAccelerationJL, SpeedMappingJL, SIAMFANLEquationsJL
export NonlinearSolvePolyAlgorithm,
RobustMultiNewton, FastShortcutNonlinearPolyalg, FastShortcutNLLSPolyalg

Expand Down
32 changes: 32 additions & 0 deletions src/extension_algs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
end

"""

SpeedMappingJL(; σ_min = 0.0, stabilize::Bool = false, check_obj::Bool = false,
orders::Vector{Int} = [3, 3, 2], time_limit::Real = 1000)

Expand Down Expand Up @@ -322,3 +323,34 @@
return FixedPointAccelerationJL(algorithm, extrapolation_period, replace_invalids,
dampening, m, condition_number_threshold)
end

"""

SIAMFANLEquationsJL(; method = :newton, autodiff = :central, show_trace = false, delta = 1e-3, linsolve = nothing)

### Keyword Arguments

- `method`: the choice of method for solving the nonlinear system.
- `show_trace`: whether to show the trace.
- `delta`: initial pseudo time step, default is 1e-3.
- `linsolve` : JFNK linear solvers, choices are `gmres` and `bicgstab`.

### Submethod Choice

- `:newton`: Classical Newton method.
- `:pseudotransient`: Pseudo transient method.
- `:secant`: Secant method for scalar equations.
"""
@concrete struct SIAMFANLEquationsJL <: AbstractNonlinearAlgorithm
method::Symbol
show_trace::Bool
delta
linsolve::Union{Symbol, Nothing}
end

function SIAMFANLEquationsJL(; method = :newton, show_trace = false, delta = 1e-3, linsolve = nothing)
if Base.get_extension(@__MODULE__, :NonlinearSolveSIAMFANLEquationsExt) === nothing
error("SIAMFANLEquationsJL requires SIAMFANLEquations.jl to be loaded")

Check warning on line 353 in src/extension_algs.jl

View check run for this annotation

Codecov / codecov/patch

src/extension_algs.jl#L353

Added line #L353 was not covered by tests
end
return SIAMFANLEquationsJL(method, show_trace, delta, linsolve)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ end
if GROUP == "All" || GROUP == "Wrappers"
@time @safetestset "MINPACK" include("minpack.jl")
@time @safetestset "NLsolve" include("nlsolve.jl")
@time @safetestset "SIAMFANLEquations" include("siamfanlequations.jl")
@time @safetestset "SpeedMapping" include("speedmapping.jl")
@time @safetestset "FixedPointAcceleration" include("fixed_point_acceleration.jl")
end
Expand Down
Loading
Loading