Skip to content

Commit

Permalink
Merge pull request #375 from SciML/ap/format
Browse files Browse the repository at this point in the history
Run Formatter
  • Loading branch information
avik-pal authored Feb 13, 2024
2 parents 9e46f00 + 8d0578a commit 8f9a432
Show file tree
Hide file tree
Showing 30 changed files with 235 additions and 135 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Documenter, DocumenterCitations
using NonlinearSolve,
SimpleNonlinearSolve, Sundials, SteadyStateDiffEq, SciMLBase, DiffEqBase
SimpleNonlinearSolve, Sundials, SteadyStateDiffEq, SciMLBase, DiffEqBase

cp(joinpath(@__DIR__, "Manifest.toml"), joinpath(@__DIR__, "src/assets/Manifest.toml"),
force = true)
Expand Down
2 changes: 1 addition & 1 deletion docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ pages = [
"devdocs/operators.md",
"devdocs/algorithm_helpers.md"],
"Release Notes" => "release_notes.md",
"References" => "references.md",
"References" => "references.md"
]
12 changes: 8 additions & 4 deletions docs/src/basics/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ myfun(x, lv) = x * sin(x) - lv
function f(out, levels, u0)
for i in 1:N
out[i] = solve(IntervalNonlinearProblem{false}(IntervalNonlinearFunction{false}(myfun),
u0, levels[i]), Falsi()).u
out[i] = solve(
IntervalNonlinearProblem{false}(IntervalNonlinearFunction{false}(myfun),
u0, levels[i]),
Falsi()).u
end
end
function f2(out, levels, u0)
for i in 1:N
out[i] = solve(NonlinearProblem{false}(NonlinearFunction{false}(myfun),
u0, levels[i]), SimpleNewtonRaphson()).u
out[i] = solve(
NonlinearProblem{false}(NonlinearFunction{false}(myfun),
u0, levels[i]),
SimpleNewtonRaphson()).u
end
end
Expand Down
9 changes: 6 additions & 3 deletions docs/src/basics/sparsity_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ NonlinearSolve will automatically perform matrix coloring and use sparse differe
Now you can help the solver further by providing the color vector. This can be done by

```julia
prob = NonlinearProblem(NonlinearFunction(nlfunc; sparsity = jac_prototype,
prob = NonlinearProblem(
NonlinearFunction(nlfunc; sparsity = jac_prototype,
colorvec = colorvec), x0)
# OR
prob = NonlinearProblem(NonlinearFunction(nlfunc; jac_prototype = jac_prototype,
prob = NonlinearProblem(
NonlinearFunction(nlfunc; jac_prototype = jac_prototype,
colorvec = colorvec), x0)
```

Expand All @@ -47,7 +49,8 @@ algorithm you want to use, then you can create your problem as follows:
prob = NonlinearProblem(NonlinearFunction(nlfunc; sparsity = SymbolicsSparsityDetection()),
x0) # Remember to have Symbolics.jl loaded
# OR
prob = NonlinearProblem(NonlinearFunction(nlfunc; sparsity = ApproximateJacobianSparsity()),
prob = NonlinearProblem(
NonlinearFunction(nlfunc; sparsity = ApproximateJacobianSparsity()),
x0)
```

Expand Down
12 changes: 8 additions & 4 deletions docs/src/tutorials/large_systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,14 @@ for the exact sparsity detection case, we left out the time it takes to perform
sparsity detection. Let's compare the two by setting the sparsity detection algorithms.

```@example ill_conditioned_nlprob
prob_brusselator_2d_exact = NonlinearProblem(NonlinearFunction(brusselator_2d_loop;
sparsity = SymbolicsSparsityDetection()), u0, p; abstol = 1e-10, reltol = 1e-10)
prob_brusselator_2d_approx = NonlinearProblem(NonlinearFunction(brusselator_2d_loop;
sparsity = ApproximateJacobianSparsity()), u0, p; abstol = 1e-10, reltol = 1e-10)
prob_brusselator_2d_exact = NonlinearProblem(
NonlinearFunction(brusselator_2d_loop;
sparsity = SymbolicsSparsityDetection()),
u0, p; abstol = 1e-10, reltol = 1e-10)
prob_brusselator_2d_approx = NonlinearProblem(
NonlinearFunction(brusselator_2d_loop;
sparsity = ApproximateJacobianSparsity()),
u0, p; abstol = 1e-10, reltol = 1e-10)
@btime solve(prob_brusselator_2d_exact, NewtonRaphson());
@btime solve(prob_brusselator_2d_approx, NewtonRaphson());
Expand Down
6 changes: 3 additions & 3 deletions docs/src/tutorials/modelingtoolkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ u0 = [x => 1.0,
z => 0.0]
ps = [σ => 10.0
ρ => 26.0
β => 8 / 3]
ρ => 26.0
β => 8 / 3]
prob = NonlinearProblem(ns, u0, ps)
sol = solve(prob, NewtonRaphson())
Expand Down Expand Up @@ -65,7 +65,7 @@ eqs = [
0 ~ u2 - cos(u1),
0 ~ u3 - hypot(u1, u2),
0 ~ u4 - hypot(u2, u3),
0 ~ u5 - hypot(u4, u1),
0 ~ u5 - hypot(u4, u1)
]
@named sys = NonlinearSystem(eqs, [u1, u2, u3, u4, u5], [])
```
Expand Down
3 changes: 2 additions & 1 deletion ext/NonlinearSolveMINPACKExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using MINPACK, NonlinearSolve, SciMLBase
import FastClosures: @closure

function SciMLBase.__solve(prob::Union{NonlinearLeastSquaresProblem,
NonlinearProblem}, alg::CMINPACK, args...; abstol = nothing, maxiters = 1000,
NonlinearProblem},
alg::CMINPACK, args...; abstol = nothing, maxiters = 1000,
alias_u0::Bool = false, show_trace::Val{ShT} = Val(false),
store_trace::Val{StT} = Val(false), termination_condition = nothing,
kwargs...) where {ShT, StT}
Expand Down
47 changes: 28 additions & 19 deletions src/NonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import PrecompileTools: @recompile_invalidations, @compile_workload, @setup_work

@recompile_invalidations begin
using ADTypes, ConcreteStructs, DiffEqBase, FastBroadcast, FastClosures, LazyArrays,
LineSearches, LinearAlgebra, LinearSolve, MaybeInplace, Preferences, Printf,
SciMLBase, SimpleNonlinearSolve, SparseArrays, SparseDiffTools
LineSearches, LinearAlgebra, LinearSolve, MaybeInplace, Preferences, Printf,
SciMLBase, SimpleNonlinearSolve, SparseArrays, SparseDiffTools

import ArrayInterface: undefmatrix, can_setindex, restructure, fast_scalar_indexing
import DiffEqBase: AbstractNonlinearTerminationMode,
AbstractSafeNonlinearTerminationMode, AbstractSafeBestNonlinearTerminationMode,
NonlinearSafeTerminationReturnCode, get_termination_mode
AbstractSafeNonlinearTerminationMode,
AbstractSafeBestNonlinearTerminationMode,
NonlinearSafeTerminationReturnCode, get_termination_mode
import FiniteDiff
import ForwardDiff
import ForwardDiff: Dual
import LinearSolve: ComposePreconditioner, InvPreconditioner, needs_concrete_A
import RecursiveArrayTools: recursivecopy!, recursivefill!

import SciMLBase: AbstractNonlinearAlgorithm, JacobianWrapper, AbstractNonlinearProblem,
AbstractSciMLOperator, NLStats, _unwrap_val, has_jac, isinplace
AbstractSciMLOperator, NLStats, _unwrap_val, has_jac, isinplace
import SparseDiffTools: AbstractSparsityDetection, AutoSparseEnzyme
import StaticArraysCore: StaticArray, SVector, SArray, MArray, Size, SMatrix, MMatrix
end
Expand Down Expand Up @@ -95,20 +96,28 @@ include("default.jl")
probs_nlls = NonlinearLeastSquaresProblem[]
nlfuncs = ((NonlinearFunction{false}((u, p) -> (u .^ 2 .- p)[1:1]), [0.1, 0.0]),
(NonlinearFunction{false}((u, p) -> vcat(u .* u .- p, u .* u .- p)), [0.1, 0.1]),
(NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
resid_prototype = zeros(1)), [0.1, 0.0]),
(NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
resid_prototype = zeros(4)), [0.1, 0.1]))
(
NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
resid_prototype = zeros(1)),
[0.1, 0.0]),
(
NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
resid_prototype = zeros(4)),
[0.1, 0.1]))
for (fn, u0) in nlfuncs
push!(probs_nlls, NonlinearLeastSquaresProblem(fn, u0, 2.0))
end
nlfuncs = ((NonlinearFunction{false}((u, p) -> (u .^ 2 .- p)[1:1]), Float32[0.1, 0.0]),
(NonlinearFunction{false}((u, p) -> vcat(u .* u .- p, u .* u .- p)),
Float32[0.1, 0.1]),
(NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
resid_prototype = zeros(Float32, 1)), Float32[0.1, 0.0]),
(NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
resid_prototype = zeros(Float32, 4)), Float32[0.1, 0.1]))
(
NonlinearFunction{true}((du, u, p) -> du[1] = u[1] * u[1] - p,
resid_prototype = zeros(Float32, 1)),
Float32[0.1, 0.0]),
(
NonlinearFunction{true}((du, u, p) -> du .= vcat(u .* u .- p, u .* u .- p),
resid_prototype = zeros(Float32, 4)),
Float32[0.1, 0.1]))
for (fn, u0) in nlfuncs
push!(probs_nlls, NonlinearLeastSquaresProblem(fn, u0, 2.0f0))
end
Expand All @@ -132,18 +141,18 @@ end
export NewtonRaphson, PseudoTransient, Klement, Broyden, LimitedMemoryBroyden, DFSane
export GaussNewton, LevenbergMarquardt, TrustRegion
export NonlinearSolvePolyAlgorithm,
RobustMultiNewton, FastShortcutNonlinearPolyalg, FastShortcutNLLSPolyalg
RobustMultiNewton, FastShortcutNonlinearPolyalg, FastShortcutNLLSPolyalg

# Extension Algorithms
export LeastSquaresOptimJL, FastLevenbergMarquardtJL, CMINPACK, NLsolveJL,
FixedPointAccelerationJL, SpeedMappingJL, SIAMFANLEquationsJL
FixedPointAccelerationJL, SpeedMappingJL, SIAMFANLEquationsJL

# Advanced Algorithms -- Without Bells and Whistles
export GeneralizedFirstOrderAlgorithm, ApproximateJacobianSolveAlgorithm, GeneralizedDFSane

# Descent Algorithms
export NewtonDescent, SteepestDescent, Dogleg, DampedNewtonDescent,
GeodesicAcceleration
GeodesicAcceleration

# Globalization
## Line Search Algorithms
Expand All @@ -153,9 +162,9 @@ export RadiusUpdateSchemes

# Export the termination conditions from DiffEqBase
export SteadyStateDiffEqTerminationMode, SimpleNonlinearSolveTerminationMode,
NormTerminationMode, RelTerminationMode, RelNormTerminationMode, AbsTerminationMode,
AbsNormTerminationMode, RelSafeTerminationMode, AbsSafeTerminationMode,
RelSafeBestTerminationMode, AbsSafeBestTerminationMode
NormTerminationMode, RelTerminationMode, RelNormTerminationMode, AbsTerminationMode,
AbsNormTerminationMode, RelSafeTerminationMode, AbsSafeTerminationMode,
RelSafeBestTerminationMode, AbsSafeBestTerminationMode

# Tracing Functionality
export TraceAll, TraceMinimal, TraceWithJacobianConditionNumber
Expand Down
6 changes: 4 additions & 2 deletions src/abstract_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ squares solver.
### `__internal_init` specification
```julia
__internal_init(prob::NonlinearProblem{uType, iip}, alg::AbstractDescentAlgorithm, J, fu, u;
__internal_init(
prob::NonlinearProblem{uType, iip}, alg::AbstractDescentAlgorithm, J, fu, u;
pre_inverted::Val{INV} = Val(false), linsolve_kwargs = (;), abstol = nothing,
reltol = nothing, alias_J::Bool = true, shared::Val{N} = Val(1),
kwargs...) where {INV, N, uType, iip} --> AbstractDescentCache
Expand Down Expand Up @@ -232,7 +233,8 @@ Abstract Type for Damping Functions in DampedNewton.
### `__internal_init` specification
```julia
__internal_init(prob::AbstractNonlinearProblem, f::AbstractDampingFunction, initial_damping,
__internal_init(
prob::AbstractNonlinearProblem, f::AbstractDampingFunction, initial_damping,
J, fu, u, args...; internal_norm = DEFAULT_NORM,
kwargs...) --> AbstractDampingFunctionCache
```
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/broyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ search.
useful for specific problems, but whether it will work may depend strongly on the
problem
"""
function Broyden(; max_resets = 100, linesearch = NoLineSearch(), reset_tolerance = nothing,
function Broyden(;
max_resets = 100, linesearch = NoLineSearch(), reset_tolerance = nothing,
init_jacobian::Val{IJ} = Val(:identity), autodiff = nothing, alpha = nothing,
update_rule::Val{UR} = Val(:good_broyden)) where {IJ, UR}
if IJ === :identity
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/dfsane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ For other keyword arguments, see [`RobustNonMonotoneLineSearch`](@ref).
function DFSane(; σ_min = 1 // 10^10, σ_max = 1e10, σ_1 = 1, M::Int = 10, γ = 1 // 10^4,
τ_min = 1 // 10, τ_max = 1 // 2, n_exp::Int = 2, max_inner_iterations::Int = 100,
η_strategy::ETA = (fn_1, n, x_n, f_n) -> fn_1 / n^2) where {ETA}
linesearch = RobustNonMonotoneLineSearch(; gamma = γ, sigma_1 = σ_1, M, tau_min = τ_min,
linesearch = RobustNonMonotoneLineSearch(;
gamma = γ, sigma_1 = σ_1, M, tau_min = τ_min,
tau_max = τ_max, n_exp, η_strategy, maxiters = max_inner_iterations)
return GeneralizedDFSane{:DFSane}(linesearch, σ_min, σ_max, nothing)
end
43 changes: 25 additions & 18 deletions src/algorithms/extension_algs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,21 @@ function CMINPACK(; show_trace = missing, tracing = missing, method::Symbol = :a
end

if show_trace !== missing
Base.depwarn("`show_trace` for CMINPACK has been deprecated and will be removed \
in v4. Use the `show_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :CMINPACK)
Base.depwarn(
"`show_trace` for CMINPACK has been deprecated and will be removed \
in v4. Use the `show_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :CMINPACK)
else
show_trace = false
end

if tracing !== missing
Base.depwarn("`tracing` for CMINPACK has been deprecated and will be removed \
in v4. Use the `store_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :CMINPACK)
Base.depwarn(
"`tracing` for CMINPACK has been deprecated and will be removed \
in v4. Use the `store_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :CMINPACK)
else
tracing = false
end
Expand Down Expand Up @@ -243,25 +245,29 @@ function NLsolveJL(; method = :trust_region, autodiff = :central, store_trace =
Base.depwarn("`show_trace` for NLsolveJL has been deprecated and will be removed \
in v4. Use the `show_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :NLsolveJL)
instead.",
:NLsolveJL)
else
show_trace = false
end

if store_trace !== missing
Base.depwarn("`store_trace` for NLsolveJL has been deprecated and will be removed \
in v4. Use the `store_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.", :NLsolveJL)
Base.depwarn(
"`store_trace` for NLsolveJL has been deprecated and will be removed \
in v4. Use the `store_trace` keyword argument via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ \
instead.",
:NLsolveJL)
else
store_trace = false
end

if extended_trace !== missing
Base.depwarn("`extended_trace` for NLsolveJL has been deprecated and will be \
removed in v4. Use the `trace_level = TraceAll()` keyword argument \
via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ instead.",
Base.depwarn(
"`extended_trace` for NLsolveJL has been deprecated and will be \
removed in v4. Use the `trace_level = TraceAll()` keyword argument \
via the logging API \
https://docs.sciml.ai/NonlinearSolve/stable/basics/Logging/ instead.",
:NLsolveJL)
else
extended_trace = false
Expand Down Expand Up @@ -317,7 +323,8 @@ function SpeedMappingJL(; σ_min = 0.0, stabilize::Bool = false, check_obj::Bool
if time_limit !== missing
Base.depwarn("`time_limit` keyword argument to `SpeedMappingJL` has been \
deprecated and will be removed in v4. Pass `maxtime = <value>` to \
`SciMLBase.solve`.", :SpeedMappingJL)
`SciMLBase.solve`.",
:SpeedMappingJL)
else
time_limit = 1000
end
Expand Down
5 changes: 3 additions & 2 deletions src/algorithms/klement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ function Klement(; max_resets::Int = 100, linsolve = nothing, alpha = nothing,
linesearch = NoLineSearch(), precs = DEFAULT_PRECS, autodiff = nothing,
init_jacobian::Val{IJ} = Val(:identity)) where {IJ}
if !(linesearch isa AbstractNonlinearSolveLineSearchAlgorithm)
Base.depwarn("Passing in a `LineSearches.jl` algorithm directly is deprecated. \
Please use `LineSearchesJL` instead.", :Klement)
Base.depwarn(
"Passing in a `LineSearches.jl` algorithm directly is deprecated. \
Please use `LineSearchesJL` instead.", :Klement)
linesearch = LineSearchesJL(; method = linesearch)
end

Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/lbroyden.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function __internal_init(prob::AbstractNonlinearProblem,
internalnorm)
end

function (cache::InitializedApproximateJacobianCache)(alg::BroydenLowRankInitialization, fu,
function (cache::InitializedApproximateJacobianCache)(
alg::BroydenLowRankInitialization, fu,
u)
α = __initial_alpha(alg.alpha, u, fu, cache.internalnorm)
cache.J.idx = 0
Expand Down
6 changes: 4 additions & 2 deletions src/algorithms/levenberg_marquardt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function LevenbergMarquardt(; concrete_jac = missing, linsolve = nothing,
Base.depwarn("The `concrete_jac` keyword argument is deprecated and will be \
removed in v0.4. This kwarg doesn't make sense (and is currently \
ignored) for LM since it needs to materialize the Jacobian to \
compute the Damping Term", :LevenbergMarquardt)
compute the Damping Term",
:LevenbergMarquardt)
end

descent = DampedNewtonDescent(; linsolve, precs, initial_damping = damping_initial,
Expand All @@ -49,7 +50,8 @@ function LevenbergMarquardt(; concrete_jac = missing, linsolve = nothing,
descent = GeodesicAcceleration(descent, finite_diff_step_geodesic, α_geodesic)
end
trustregion = LevenbergMarquardtTrustRegion(b_uphill)
return GeneralizedFirstOrderAlgorithm(; concrete_jac = true, name = :LevenbergMarquardt,
return GeneralizedFirstOrderAlgorithm(;
concrete_jac = true, name = :LevenbergMarquardt,
trustregion, descent, jacobian_ad = autodiff)
end

Expand Down
6 changes: 4 additions & 2 deletions src/core/approximate_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,13 @@ function __step!(cache::ApproximateJacobianSolveCache{INV, GB, iip};
@static_timeit cache.timer "descent" begin
if cache.trustregion_cache !== nothing &&
hasfield(typeof(cache.trustregion_cache), :trust_region)
δu, descent_success, descent_intermediates = __internal_solve!(cache.descent_cache,
δu, descent_success, descent_intermediates = __internal_solve!(
cache.descent_cache,
J, cache.fu, cache.u; new_jacobian,
trust_region = cache.trustregion_cache.trust_region)
else
δu, descent_success, descent_intermediates = __internal_solve!(cache.descent_cache,
δu, descent_success, descent_intermediates = __internal_solve!(
cache.descent_cache,
J, cache.fu, cache.u; new_jacobian)
end
end
Expand Down
Loading

0 comments on commit 8f9a432

Please sign in to comment.