Skip to content

Commit

Permalink
Merge pull request #100 from avik-pal/ap/cleanup
Browse files Browse the repository at this point in the history
Port over improvements from NonlinearSolve
  • Loading branch information
ChrisRackauckas authored Nov 28, 2023
2 parents 31b8a57 + 32c20f1 commit 14b65b4
Show file tree
Hide file tree
Showing 43 changed files with 2,114 additions and 2,709 deletions.
5 changes: 4 additions & 1 deletion lib/SimpleNonlinearSolve/.JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
style = "sciml"
style = "sciml"
format_markdown = true
annotate_untyped_fields_with_any = false
format_docstrings = true
15 changes: 4 additions & 11 deletions lib/SimpleNonlinearSolve/Project.toml
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
name = "SimpleNonlinearSolve"
uuid = "727e6d20-b764-4bd8-a329-72de5adea6c7"
authors = ["SciML"]
version = "0.1.25"
version = "0.2.0"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MaybeInplace = "bb5d69b7-63fc-4a16-80bd-7e42200c7bdb"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"

[weakdeps]
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"

[extensions]
SimpleNonlinearSolveNNlibExt = "NNlib"

[compat]
ArrayInterface = "7"
DiffEqBase = "6.126"
FiniteDiff = "2"
ForwardDiff = "0.10.3"
LinearAlgebra = "1.9"
NNlib = "0.8, 0.9"
PrecompileTools = "1"
Reexport = "1"
SciMLBase = "2.7"
StaticArraysCore = "1.4"
julia = "1.9"

[extras]
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
19 changes: 16 additions & 3 deletions lib/SimpleNonlinearSolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
[![codecov](https://codecov.io/gh/SciML/SimpleNonlinearSolve.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/SciML/SimpleNonlinearSolve.jl)
[![Build Status](https://github.com/SciML/SimpleNonlinearSolve.jl/workflows/CI/badge.svg)](https://github.com/SciML/SimpleNonlinearSolve.jl/actions?query=workflow%3ACI)

[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor%27s%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![SciML Code Style](https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826)](https://github.com/SciML/SciMLStyle)

Fast implementations of root finding algorithms in Julia that satisfy the SciML common interface.
SimpleNonlinearSolve.jl focuses on low-dependency implementations of very fast methods for
very small and simple problems. For the full set of solvers, see
very small and simple problems. For the full set of solvers, see
[NonlinearSolve.jl](https://github.com/SciML/NonlinearSolve.jl), of which
SimpleNonlinearSolve.jl is just one solver set.

Expand All @@ -25,7 +25,7 @@ the documentation which contains the unreleased features.
```julia
using SimpleNonlinearSolve, StaticArrays

f(u,p) = u .* u .- 2
f(u, p) = u .* u .- 2
u0 = @SVector[1.0, 1.0]
probN = NonlinearProblem{false}(f, u0)
solver = solve(probN, SimpleNewtonRaphson(), abstol = 1e-9)
Expand All @@ -39,3 +39,16 @@ sol = solve(probB, ITP())
```

For more details on the bracketing methods, refer to the [Tutorials](https://docs.sciml.ai/NonlinearSolve/stable/tutorials/nonlinear/#Using-Bracketing-Methods) and detailed [APIs](https://docs.sciml.ai/NonlinearSolve/stable/api/simplenonlinearsolve/#Solver-API)

## Breaking Changes in v2

- Batched solvers have been removed in favor of `BatchedArrays.jl`. Stay tuned for detailed
tutorials on how to use `BatchedArrays.jl` with `NonlinearSolve` & `SimpleNonlinearSolve`
solvers.
- The old style of specifying autodiff with `chunksize`, `standardtag`, etc. has been
deprecated in favor of directly specifying the autodiff type, like `AutoForwardDiff`.
- `Broyden` and `Klement` have been renamed to `SimpleBroyden` and `SimpleKlement` to
avoid conflicts with `NonlinearSolve.jl`'s `GeneralBroyden` and `GeneralKlement`, which
will be renamed to `Broyden` and `Klement` in the future.
- `LBroyden` has been renamed to `SimpleLimitedMemoryBroyden` to make it consistent with
`NonlinearSolve.jl`'s `LimitedMemoryBroyden`.
81 changes: 0 additions & 81 deletions lib/SimpleNonlinearSolve/ext/SimpleNonlinearSolveNNlibExt.jl

This file was deleted.

129 changes: 70 additions & 59 deletions lib/SimpleNonlinearSolve/src/SimpleNonlinearSolve.jl
Original file line number Diff line number Diff line change
@@ -1,90 +1,101 @@
module SimpleNonlinearSolve

using Reexport
using FiniteDiff, ForwardDiff
using ForwardDiff: Dual
using StaticArraysCore
using LinearAlgebra
import ArrayInterface
using DiffEqBase

@reexport using SciMLBase
import PrecompileTools: @compile_workload, @setup_workload, @recompile_invalidations

@recompile_invalidations begin
using ADTypes,
ArrayInterface, ConcreteStructs, DiffEqBase, Reexport, LinearAlgebra, SciMLBase

import DiffEqBase: AbstractNonlinearTerminationMode,
AbstractSafeNonlinearTerminationMode, AbstractSafeBestNonlinearTerminationMode,
NonlinearSafeTerminationReturnCode, get_termination_mode,
NONLINEARSOLVE_DEFAULT_NORM
using FiniteDiff, ForwardDiff
import ForwardDiff: Dual
import MaybeInplace: @bb, setindex_trait, CanSetindex, CannotSetindex
import SciMLBase: AbstractNonlinearAlgorithm, build_solution, isinplace
import StaticArraysCore: StaticArray, SVector, SMatrix, SArray, MArray, MMatrix, Size
end

const NNlibExtLoaded = Ref{Bool}(false)
@reexport using ADTypes, SciMLBase

abstract type AbstractSimpleNonlinearSolveAlgorithm <: SciMLBase.AbstractNonlinearAlgorithm end
abstract type AbstractSimpleNonlinearSolveAlgorithm <: AbstractNonlinearAlgorithm end
abstract type AbstractBracketingAlgorithm <: AbstractSimpleNonlinearSolveAlgorithm end
abstract type AbstractNewtonAlgorithm{CS, AD, FDT} <: AbstractSimpleNonlinearSolveAlgorithm end
abstract type AbstractImmutableNonlinearSolver <: AbstractSimpleNonlinearSolveAlgorithm end
abstract type AbstractBatchedNonlinearSolveAlgorithm <:
AbstractSimpleNonlinearSolveAlgorithm end
abstract type AbstractNewtonAlgorithm <: AbstractSimpleNonlinearSolveAlgorithm end

include("utils.jl")
include("bisection.jl")
include("falsi.jl")
include("raphson.jl")
include("broyden.jl")
include("lbroyden.jl")
include("klement.jl")
include("trustRegion.jl")
include("ridder.jl")
include("brent.jl")
include("dfsane.jl")
include("ad.jl")
include("halley.jl")
include("alefeld.jl")
include("itp.jl")

# Batched Solver Support
include("batched/utils.jl")
include("batched/raphson.jl")
include("batched/dfsane.jl")
include("batched/broyden.jl")
## Nonlinear Solvers
include("nlsolve/raphson.jl")
include("nlsolve/broyden.jl")
include("nlsolve/lbroyden.jl")
include("nlsolve/klement.jl")
include("nlsolve/trustRegion.jl")
include("nlsolve/halley.jl")
include("nlsolve/dfsane.jl")

## Interval Nonlinear Solvers
include("bracketing/bisection.jl")
include("bracketing/falsi.jl")
include("bracketing/ridder.jl")
include("bracketing/brent.jl")
include("bracketing/alefeld.jl")
include("bracketing/itp.jl")

# AD
include("ad.jl")

## Default algorithm

# Set the default bracketing method to ITP

function SciMLBase.solve(prob::IntervalNonlinearProblem; kwargs...)
SciMLBase.solve(prob, ITP(); kwargs...)
return solve(prob, ITP(); kwargs...)
end

function SciMLBase.solve(prob::IntervalNonlinearProblem, alg::Nothing,
args...; kwargs...)
SciMLBase.solve(prob, ITP(), args...; kwargs...)
return solve(prob, ITP(), args...; kwargs...)
end

import PrecompileTools

PrecompileTools.@compile_workload begin
@setup_workload begin
for T in (Float32, Float64)
prob_no_brack = NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2))
for alg in (SimpleNewtonRaphson, SimpleHalley, Broyden, Klement, SimpleTrustRegion,
SimpleDFSane)
solve(prob_no_brack, alg(), abstol = T(1e-2))
end
prob_no_brack_scalar = NonlinearProblem{false}((u, p) -> u .* u .- p, T(0.1), T(2))
prob_no_brack_iip = NonlinearProblem{true}((du, u, p) -> du .= u .* u .- p,
T.([1.0, 1.0, 1.0]), T(2))
prob_no_brack_oop = NonlinearProblem{false}((u, p) -> u .* u .- p,
T.([1.0, 1.0, 1.0]), T(2))

algs = [SimpleNewtonRaphson(), SimpleBroyden(), SimpleKlement(), SimpleDFSane(),
SimpleTrustRegion(), SimpleLimitedMemoryBroyden(; threshold = 2)]

algs_no_iip = [SimpleHalley()]

@compile_workload begin
for alg in algs
solve(prob_no_brack_scalar, alg, abstol = T(1e-2))
solve(prob_no_brack_iip, alg, abstol = T(1e-2))
solve(prob_no_brack_oop, alg, abstol = T(1e-2))
end

#=
for alg in (SimpleNewtonRaphson,)
for u0 in ([1., 1.], StaticArraysCore.SA[1.0, 1.0])
u0 = T.(.1)
probN = NonlinearProblem{false}((u,p) -> u .* u .- p, u0, T(2))
solve(probN, alg(), tol = T(1e-2))
for alg in algs_no_iip
solve(prob_no_brack_scalar, alg, abstol = T(1e-2))
solve(prob_no_brack_oop, alg, abstol = T(1e-2))
end
end
=#

prob_brack = IntervalNonlinearProblem{false}((u, p) -> u * u - p,
T.((0.0, 2.0)),
T(2))
for alg in (Bisection, Falsi, Ridder, Brent, Alefeld, ITP)
solve(prob_brack, alg(), abstol = T(1e-2))
T.((0.0, 2.0)), T(2))
algs = [Bisection(), Falsi(), Ridder(), Brent(), Alefeld(), ITP()]
@compile_workload begin
for alg in algs
solve(prob_brack, alg, abstol = T(1e-2))
end
end
end
end

export Bisection, Brent, Broyden, LBroyden, SimpleDFSane, Falsi, SimpleHalley, Klement,
Ridder, SimpleNewtonRaphson, SimpleTrustRegion, Alefeld, ITP, SimpleGaussNewton
export BatchedBroyden, BatchedSimpleNewtonRaphson, BatchedSimpleDFSane
export SimpleBroyden, SimpleDFSane, SimpleGaussNewton, SimpleHalley, SimpleKlement,
SimpleLimitedMemoryBroyden, SimpleNewtonRaphson, SimpleTrustRegion
export Alefeld, Bisection, Brent, Falsi, ITP, Ridder

end # module
Loading

0 comments on commit 14b65b4

Please sign in to comment.