Skip to content

Commit

Permalink
Pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Jan 5, 2024
1 parent 3cd445d commit edd6dae
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 30 deletions.
29 changes: 0 additions & 29 deletions src/NonlinearSolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,6 @@ const False = Val(false)

# __reinit_internal!(::AbstractNonlinearSolveCache; kwargs...) = nothing

# function Base.show(io::IO, alg::AbstractNonlinearSolveAlgorithm)
# str = "$(nameof(typeof(alg)))("
# modifiers = String[]
# if __getproperty(alg, Val(:ad)) !== nothing
# push!(modifiers, "ad = $(nameof(typeof(alg.ad)))()")
# end
# if __getproperty(alg, Val(:linsolve)) !== nothing
# push!(modifiers, "linsolve = $(nameof(typeof(alg.linsolve)))()")
# end
# if __getproperty(alg, Val(:linesearch)) !== nothing
# ls = alg.linesearch
# if ls isa LineSearch
# ls.method !== nothing &&
# push!(modifiers, "linesearch = $(nameof(typeof(ls.method)))()")
# else
# push!(modifiers, "linesearch = $(nameof(typeof(alg.linesearch)))()")
# end
# end
# append!(modifiers, __alg_print_modifiers(alg))
# if __getproperty(alg, Val(:radius_update_scheme)) !== nothing
# push!(modifiers, "radius_update_scheme = $(alg.radius_update_scheme)")
# end
# str = str * join(modifiers, ", ")
# print(io, "$(str))")
# return nothing
# end

# __alg_print_modifiers(_) = String[]

include("abstract_types.jl")
include("internal/helpers.jl")

Expand Down
26 changes: 26 additions & 0 deletions src/abstract_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ Abstract Type for all Line Search Algorithms used in NonlinearSolve.jl.
"""
abstract type AbstractNonlinearSolveLineSearchAlgorithm end

function Base.show(io::IO, alg::AbstractNonlinearSolveLineSearchAlgorithm)
print(io, "$(nameof(typeof(alg)))()")

Check warning on line 109 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L108-L109

Added lines #L108 - L109 were not covered by tests
end

abstract type AbstractNonlinearSolveLineSearchCache end

"""
Expand Down Expand Up @@ -179,8 +183,25 @@ end

abstract type AbstractJacobianInitialization end

function Base.show(io::IO, alg::AbstractJacobianInitialization)
modifiers = String[]
hasfield(typeof(alg), :structure) &&

Check warning on line 188 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L186-L188

Added lines #L186 - L188 were not covered by tests
push!(modifiers, "structure = $(nameof(typeof(alg.structure)))()")
print(io, "$(nameof(typeof(alg)))($(join(modifiers, ", ")))")
return nothing

Check warning on line 191 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L190-L191

Added lines #L190 - L191 were not covered by tests
end

abstract type AbstractApproximateJacobianUpdateRule{INV} end

function Base.show(io::IO, alg::AbstractApproximateJacobianUpdateRule{INV}) where {INV}
if INV
print(io, "$(nameof(typeof(alg)))(stores_inverse = true)")

Check warning on line 198 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L196-L198

Added lines #L196 - L198 were not covered by tests
else
print(io, "$(nameof(typeof(alg)))()")

Check warning on line 200 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L200

Added line #L200 was not covered by tests
end
return nothing

Check warning on line 202 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L202

Added line #L202 was not covered by tests
end

store_inverse_jacobian(::AbstractApproximateJacobianUpdateRule{INV}) where {INV} = INV

Check warning on line 205 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L205

Added line #L205 was not covered by tests

abstract type AbstractApproximateJacobianUpdateRuleCache{INV} end
Expand All @@ -189,6 +210,11 @@ store_inverse_jacobian(::AbstractApproximateJacobianUpdateRuleCache{INV}) where

abstract type AbstractResetCondition end

function Base.show(io::IO, alg::AbstractResetCondition)
print(io, "$(nameof(typeof(alg)))()")
return nothing

Check warning on line 215 in src/abstract_types.jl

View check run for this annotation

Codecov / codecov/patch

src/abstract_types.jl#L213-L215

Added lines #L213 - L215 were not covered by tests
end

abstract type AbstractTrustRegionMethod end

abstract type AbstractTrustRegionMethodCache end
Expand Down
13 changes: 13 additions & 0 deletions src/core/approximate_jacobian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
initialization
end

function Base.show(io::IO,

Check warning on line 12 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L12

Added line #L12 was not covered by tests
alg::ApproximateJacobianSolveAlgorithm{concrete_jac, name}) where {concrete_jac, name}
modifiers = String[]
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
__is_present(alg.trustregion) && push!(modifiers, "trustregion = $(alg.trustregion)")
push!(modifiers, "descent = $(alg.descent)")
push!(modifiers, "update_rule = $(alg.update_rule)")
push!(modifiers, "reinit_rule = $(alg.reinit_rule)")
push!(modifiers, "max_resets = $(alg.max_resets)")
push!(modifiers, "initialization = $(alg.initialization)")
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")

Check warning on line 22 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L14-L22

Added lines #L14 - L22 were not covered by tests
end

function ApproximateJacobianSolveAlgorithm(; concrete_jac = nothing,

Check warning on line 25 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L25

Added line #L25 was not covered by tests
name::Symbol = :unknown, kwargs...)
return ApproximateJacobianSolveAlgorithm{concrete_jac, name}(; kwargs...)

Check warning on line 27 in src/core/approximate_jacobian.jl

View check run for this annotation

Codecov / codecov/patch

src/core/approximate_jacobian.jl#L27

Added line #L27 was not covered by tests
Expand Down
12 changes: 12 additions & 0 deletions src/core/generalized_first_order.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
reverse_ad
end

function Base.show(io::IO,

Check warning on line 11 in src/core/generalized_first_order.jl

View check run for this annotation

Codecov / codecov/patch

src/core/generalized_first_order.jl#L11

Added line #L11 was not covered by tests
alg::GeneralizedFirstOrderAlgorithm{concrete_jac, name}) where {concrete_jac, name}
modifiers = String[]
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
__is_present(alg.trustregion) && push!(modifiers, "trustregion = $(alg.trustregion)")
push!(modifiers, "descent = $(alg.descent)")
__is_present(alg.jacobian_ad) && push!(modifiers, "jacobian_ad = $(alg.jacobian_ad)")
__is_present(alg.forward_ad) && push!(modifiers, "forward_ad = $(alg.forward_ad)")
__is_present(alg.reverse_ad) && push!(modifiers, "reverse_ad = $(alg.reverse_ad)")
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")

Check warning on line 20 in src/core/generalized_first_order.jl

View check run for this annotation

Codecov / codecov/patch

src/core/generalized_first_order.jl#L13-L20

Added lines #L13 - L20 were not covered by tests
end

function GeneralizedFirstOrderAlgorithm(; concrete_jac = nothing,

Check warning on line 23 in src/core/generalized_first_order.jl

View check run for this annotation

Codecov / codecov/patch

src/core/generalized_first_order.jl#L23

Added line #L23 was not covered by tests
name::Symbol = :unknown, kwargs...)
return GeneralizedFirstOrderAlgorithm{concrete_jac, name}(; kwargs...)

Check warning on line 25 in src/core/generalized_first_order.jl

View check run for this annotation

Codecov / codecov/patch

src/core/generalized_first_order.jl#L25

Added line #L25 was not covered by tests
Expand Down
9 changes: 9 additions & 0 deletions src/core/spectral_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
σ_1
end

function Base.show(io::IO, alg::GeneralizedDFSane{name}) where {name}
modifiers = String[]
__is_present(alg.linesearch) && push!(modifiers, "linesearch = $(alg.linesearch)")
push!(modifiers, "σ_min = $(alg.σ_min)")
push!(modifiers, "σ_max = $(alg.σ_max)")
push!(modifiers, "σ_1 = $(alg.σ_1)")
print(io, "$(name)(\n $(join(modifiers, ",\n "))\n)")

Check warning on line 17 in src/core/spectral_methods.jl

View check run for this annotation

Codecov / codecov/patch

src/core/spectral_methods.jl#L11-L17

Added lines #L11 - L17 were not covered by tests
end

concrete_jac(::GeneralizedDFSane) = nothing

Check warning on line 20 in src/core/spectral_methods.jl

View check run for this annotation

Codecov / codecov/patch

src/core/spectral_methods.jl#L20

Added line #L20 was not covered by tests

@concrete mutable struct GeneralizedDFSaneCache{iip} <: AbstractNonlinearSolveCache{iip}
Expand Down
9 changes: 9 additions & 0 deletions src/descent/damped_newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Based on the formulation we expect the damping factor returned to be a non-negat
damping_fn
end

function Base.show(io::IO, d::DampedNewtonDescent)
modifiers = String[]
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
push!(modifiers, "initial_damping = $(d.initial_damping)")
push!(modifiers, "damping_fn = $(d.damping_fn)")
print(io, "DampedNewtonDescent($(join(modifiers, ", ")))")

Check warning on line 27 in src/descent/damped_newton.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/damped_newton.jl#L21-L27

Added lines #L21 - L27 were not covered by tests
end

supports_line_search(::DampedNewtonDescent) = true
supports_trust_region(::DampedNewtonDescent) = true

Check warning on line 31 in src/descent/damped_newton.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/damped_newton.jl#L30-L31

Added lines #L30 - L31 were not covered by tests

Expand Down
5 changes: 5 additions & 0 deletions src/descent/dogleg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ See also [`SteepestDescent`](@ref), [`NewtonDescent`](@ref), [`DampedNewtonDesce
steepest_descent
end

function Base.show(io::IO, d::Dogleg)
print(io,

Check warning on line 28 in src/descent/dogleg.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/dogleg.jl#L27-L28

Added lines #L27 - L28 were not covered by tests
"Dogleg(newton_descent = $(d.newton_descent), steepest_descent = $(d.steepest_descent))")
end

supports_trust_region(::Dogleg) = true

Check warning on line 32 in src/descent/dogleg.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/dogleg.jl#L32

Added line #L32 was not covered by tests

function Dogleg(; linsolve = nothing, precs = DEFAULT_PRECS, damping = False,

Check warning on line 34 in src/descent/dogleg.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/dogleg.jl#L34

Added line #L34 was not covered by tests
Expand Down
5 changes: 5 additions & 0 deletions src/descent/geodesic_acceleration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ algorithm for nonlinear least-squares minimization." arXiv preprint arXiv:1201.5
α
end

function Base.show(io::IO, alg::GeodesicAcceleration)
print(io, "GeodesicAcceleration(descent = $(alg.descent), finite_diff_step_geodesic = ",

Check warning on line 24 in src/descent/geodesic_acceleration.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/geodesic_acceleration.jl#L23-L24

Added lines #L23 - L24 were not covered by tests
"$(alg.finite_diff_step_geodesic), α = $(alg.α))")
end

supports_trust_region(::GeodesicAcceleration) = true

Check warning on line 28 in src/descent/geodesic_acceleration.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/geodesic_acceleration.jl#L28

Added line #L28 was not covered by tests

@concrete mutable struct GeodesicAccelerationCache <: AbstractDescentCache
Expand Down
7 changes: 7 additions & 0 deletions src/descent/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ See also [`Dogleg`](@ref), [`SteepestDescent`](@ref), [`DampedNewtonDescent`](@r
precs = DEFAULT_PRECS
end

function Base.show(io::IO, d::NewtonDescent)
modifiers = String[]
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
print(io, "NewtonDescent($(join(modifiers, ", ")))")

Check warning on line 30 in src/descent/newton.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/newton.jl#L26-L30

Added lines #L26 - L30 were not covered by tests
end

supports_line_search(::NewtonDescent) = true

Check warning on line 33 in src/descent/newton.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/newton.jl#L33

Added line #L33 was not covered by tests

@concrete mutable struct NewtonDescentCache{pre_inverted, normalform} <:
Expand Down
7 changes: 7 additions & 0 deletions src/descent/steepest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ See also [`Dogleg`](@ref), [`NewtonDescent`](@ref), [`DampedNewtonDescent`](@ref
precs = DEFAULT_PRECS
end

function Base.show(io::IO, d::SteepestDescent)
modifiers = String[]
d.linsolve !== nothing && push!(modifiers, "linsolve = $(d.linsolve)")
d.precs !== DEFAULT_PRECS && push!(modifiers, "precs = $(d.precs)")
print(io, "SteepestDescent($(join(modifiers, ", ")))")

Check warning on line 31 in src/descent/steepest.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/steepest.jl#L27-L31

Added lines #L27 - L31 were not covered by tests
end

supports_line_search(::SteepestDescent) = true

Check warning on line 34 in src/descent/steepest.jl

View check run for this annotation

Codecov / codecov/patch

src/descent/steepest.jl#L34

Added line #L34 was not covered by tests

@concrete mutable struct SteepestDescentCache{pre_inverted} <: AbstractDescentCache
Expand Down
12 changes: 11 additions & 1 deletion src/globalization/line_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ differentiation for fast Vector Jacobian Products.
to `AutoFiniteDiff()`, which means that finite differencing is used to compute the VJP.
`AutoZygote()` will be faster in most cases, but it requires `Zygote.jl` to be manually
installed and loaded.
- `alpha`: the initial step size to use. Defaults to `true` (which is equivalent to `1`).
- `α`: the initial step size to use. Defaults to `true` (which is equivalent to `1`).
"""
@concrete struct LineSearchesJL <: AbstractNonlinearSolveLineSearchAlgorithm
method
initial_alpha
autodiff
end

function Base.show(io::IO, alg::LineSearchesJL)
str = "$(nameof(typeof(alg)))("
modifiers = String[]
alg.autodiff !== nothing &&

Check warning on line 47 in src/globalization/line_search.jl

View check run for this annotation

Codecov / codecov/patch

src/globalization/line_search.jl#L44-L47

Added lines #L44 - L47 were not covered by tests
push!(modifiers, "autodiff = $(nameof(typeof(alg.autodiff)))()")
alg.initial_alpha != true && push!(modifiers, "initial_alpha = $(alg.initial_alpha)")
push!(modifiers, "method = $(alg.method)")
print(io, str, join(modifiers, ", "), ")")

Check warning on line 51 in src/globalization/line_search.jl

View check run for this annotation

Codecov / codecov/patch

src/globalization/line_search.jl#L49-L51

Added lines #L49 - L51 were not covered by tests
end

LineSearchesJL(method; kwargs...) = LineSearchesJL(; method, kwargs...)
function LineSearchesJL(; method = LineSearches.Static(), autodiff = nothing, α = true)
return LineSearchesJL(method, α, autodiff)

Check warning on line 56 in src/globalization/line_search.jl

View check run for this annotation

Codecov / codecov/patch

src/globalization/line_search.jl#L54-L56

Added lines #L54 - L56 were not covered by tests
Expand Down
5 changes: 5 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,8 @@ LazyArrays.applied_axes(::typeof(__zero), x) = axes(x)
@inline __get_nonsparse_ad(::AutoSparseFiniteDiff) = AutoFiniteDiff()
@inline __get_nonsparse_ad(::AutoSparseZygote) = AutoZygote()
@inline __get_nonsparse_ad(ad) = ad

Check warning on line 83 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L79-L83

Added lines #L79 - L83 were not covered by tests

# Simple Checks
@inline __is_present(::Nothing) = false
@inline __is_present(::Missing) = false
@inline __is_present(::Any) = true

Check warning on line 88 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L86-L88

Added lines #L86 - L88 were not covered by tests

0 comments on commit edd6dae

Please sign in to comment.