|
1 | 1 | module SimpleNonlinearSolve |
2 | 2 |
|
3 | | -using Reexport |
4 | | -using FiniteDiff, ForwardDiff |
5 | | -using ForwardDiff: Dual |
6 | | -using StaticArraysCore |
7 | | -using LinearAlgebra |
8 | | -import ArrayInterface |
9 | | -using DiffEqBase |
10 | | - |
11 | | -@reexport using SciMLBase |
| 3 | +import PrecompileTools: @compile_workload, @setup_workload, @recompile_invalidations |
| 4 | + |
| 5 | +@recompile_invalidations begin |
| 6 | + using ADTypes, |
| 7 | + ArrayInterface, ConcreteStructs, DiffEqBase, Reexport, LinearAlgebra, SciMLBase |
| 8 | + |
| 9 | + import DiffEqBase: AbstractNonlinearTerminationMode, |
| 10 | + AbstractSafeNonlinearTerminationMode, AbstractSafeBestNonlinearTerminationMode, |
| 11 | + NonlinearSafeTerminationReturnCode, get_termination_mode, |
| 12 | + NONLINEARSOLVE_DEFAULT_NORM |
| 13 | + using FiniteDiff, ForwardDiff |
| 14 | + import ForwardDiff: Dual |
| 15 | + import MaybeInplace: @bb, setindex_trait, CanSetindex, CannotSetindex |
| 16 | + import SciMLBase: AbstractNonlinearAlgorithm, build_solution, isinplace |
| 17 | + import StaticArraysCore: StaticArray, SVector, SMatrix, SArray, MArray, MMatrix, Size |
| 18 | +end |
12 | 19 |
|
13 | | -const NNlibExtLoaded = Ref{Bool}(false) |
| 20 | +@reexport using ADTypes, SciMLBase |
14 | 21 |
|
15 | | -abstract type AbstractSimpleNonlinearSolveAlgorithm <: SciMLBase.AbstractNonlinearAlgorithm end |
| 22 | +abstract type AbstractSimpleNonlinearSolveAlgorithm <: AbstractNonlinearAlgorithm end |
16 | 23 | abstract type AbstractBracketingAlgorithm <: AbstractSimpleNonlinearSolveAlgorithm end |
17 | | -abstract type AbstractNewtonAlgorithm{CS, AD, FDT} <: AbstractSimpleNonlinearSolveAlgorithm end |
18 | | -abstract type AbstractImmutableNonlinearSolver <: AbstractSimpleNonlinearSolveAlgorithm end |
19 | | -abstract type AbstractBatchedNonlinearSolveAlgorithm <: |
20 | | - AbstractSimpleNonlinearSolveAlgorithm end |
| 24 | +abstract type AbstractNewtonAlgorithm <: AbstractSimpleNonlinearSolveAlgorithm end |
21 | 25 |
|
22 | 26 | include("utils.jl") |
23 | | -include("bisection.jl") |
24 | | -include("falsi.jl") |
25 | | -include("raphson.jl") |
26 | | -include("broyden.jl") |
27 | | -include("lbroyden.jl") |
28 | | -include("klement.jl") |
29 | | -include("trustRegion.jl") |
30 | | -include("ridder.jl") |
31 | | -include("brent.jl") |
32 | | -include("dfsane.jl") |
33 | | -include("ad.jl") |
34 | | -include("halley.jl") |
35 | | -include("alefeld.jl") |
36 | | -include("itp.jl") |
37 | 27 |
|
38 | | -# Batched Solver Support |
39 | | -include("batched/utils.jl") |
40 | | -include("batched/raphson.jl") |
41 | | -include("batched/dfsane.jl") |
42 | | -include("batched/broyden.jl") |
| 28 | +## Nonlinear Solvers |
| 29 | +include("nlsolve/raphson.jl") |
| 30 | +include("nlsolve/broyden.jl") |
| 31 | +include("nlsolve/lbroyden.jl") |
| 32 | +include("nlsolve/klement.jl") |
| 33 | +include("nlsolve/trustRegion.jl") |
| 34 | +include("nlsolve/halley.jl") |
| 35 | +include("nlsolve/dfsane.jl") |
| 36 | + |
| 37 | +## Interval Nonlinear Solvers |
| 38 | +include("bracketing/bisection.jl") |
| 39 | +include("bracketing/falsi.jl") |
| 40 | +include("bracketing/ridder.jl") |
| 41 | +include("bracketing/brent.jl") |
| 42 | +include("bracketing/alefeld.jl") |
| 43 | +include("bracketing/itp.jl") |
| 44 | + |
| 45 | +# AD |
| 46 | +include("ad.jl") |
43 | 47 |
|
44 | 48 | ## Default algorithm |
45 | 49 |
|
46 | 50 | # Set the default bracketing method to ITP |
47 | | - |
48 | 51 | function SciMLBase.solve(prob::IntervalNonlinearProblem; kwargs...) |
49 | | - SciMLBase.solve(prob, ITP(); kwargs...) |
| 52 | + return solve(prob, ITP(); kwargs...) |
50 | 53 | end |
51 | 54 |
|
52 | 55 | function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Nothing, |
53 | 56 | args...; kwargs...) |
54 | | - SciMLBase.solve(prob, ITP(), args...; kwargs...) |
| 57 | + return solve(prob, ITP(), args...; kwargs...) |
55 | 58 | end |
56 | 59 |
|
57 | | -import PrecompileTools |
58 | | - |
59 | | -PrecompileTools.@compile_workload begin |
| 60 | +@setup_workload begin |
60 | 61 | for T in (Float32, Float64) |
61 | | - prob_no_brack = NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2)) |
62 | | - for alg in (SimpleNewtonRaphson, SimpleHalley, Broyden, Klement, SimpleTrustRegion, |
63 | | - SimpleDFSane) |
64 | | - solve(prob_no_brack, alg(), abstol = T(1e-2)) |
65 | | - end |
| 62 | + prob_no_brack_scalar = NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2)) |
| 63 | + prob_no_brack_iip = NonlinearProblem{true}((du, u, p) -> du .= u .* u .- p, |
| 64 | + T.([1.0, 1.0, 1.0]), T(2)) |
| 65 | + prob_no_brack_oop = NonlinearProblem{false}((u, p) -> u .* u .- p, |
| 66 | + T.([1.0, 1.0, 1.0]), T(2)) |
| 67 | + |
| 68 | + algs = [SimpleNewtonRaphson(), SimpleBroyden(), SimpleKlement(), SimpleDFSane(), |
| 69 | + SimpleTrustRegion(), SimpleLimitedMemoryBroyden(; threshold = 2)] |
| 70 | + |
| 71 | + algs_no_iip = [SimpleHalley()] |
| 72 | + |
| 73 | + @compile_workload begin |
| 74 | + for alg in algs |
| 75 | + solve(prob_no_brack_scalar, alg, abstol = T(1e-2)) |
| 76 | + solve(prob_no_brack_iip, alg, abstol = T(1e-2)) |
| 77 | + solve(prob_no_brack_oop, alg, abstol = T(1e-2)) |
| 78 | + end |
66 | 79 |
|
67 | | - #= |
68 | | - for alg in (SimpleNewtonRaphson,) |
69 | | - for u0 in ([1., 1.], StaticArraysCore.SA[1.0, 1.0]) |
70 | | - u0 = T.(.1) |
71 | | - probN = NonlinearProblem{false}((u,p) -> u .* u .- p, u0, T(2)) |
72 | | - solve(probN, alg(), tol = T(1e-2)) |
| 80 | + for alg in algs_no_iip |
| 81 | + solve(prob_no_brack_scalar, alg, abstol = T(1e-2)) |
| 82 | + solve(prob_no_brack_oop, alg, abstol = T(1e-2)) |
73 | 83 | end |
74 | 84 | end |
75 | | - =# |
76 | 85 |
|
77 | 86 | prob_brack = IntervalNonlinearProblem{false}((u, p) -> u * u - p, |
78 | | - T.((0.0, 2.0)), |
79 | | - T(2)) |
80 | | - for alg in (Bisection, Falsi, Ridder, Brent, Alefeld, ITP) |
81 | | - solve(prob_brack, alg(), abstol = T(1e-2)) |
| 87 | + T.((0.0, 2.0)), T(2)) |
| 88 | + algs = [Bisection(), Falsi(), Ridder(), Brent(), Alefeld(), ITP()] |
| 89 | + @compile_workload begin |
| 90 | + for alg in algs |
| 91 | + solve(prob_brack, alg, abstol = T(1e-2)) |
| 92 | + end |
82 | 93 | end |
83 | 94 | end |
84 | 95 | end |
85 | 96 |
|
86 | | -export Bisection, Brent, Broyden, LBroyden, SimpleDFSane, Falsi, SimpleHalley, Klement, |
87 | | - Ridder, SimpleNewtonRaphson, SimpleTrustRegion, Alefeld, ITP, SimpleGaussNewton |
88 | | -export BatchedBroyden, BatchedSimpleNewtonRaphson, BatchedSimpleDFSane |
| 97 | +export SimpleBroyden, SimpleDFSane, SimpleGaussNewton, SimpleHalley, SimpleKlement, |
| 98 | + SimpleLimitedMemoryBroyden, SimpleNewtonRaphson, SimpleTrustRegion |
| 99 | +export Alefeld, Bisection, Brent, Falsi, ITP, Ridder |
89 | 100 |
|
90 | 101 | end # module |
0 commit comments