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 1bf9805
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 1 deletion.
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)))()")
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) &&
push!(modifiers, "structure = $(nameof(typeof(alg.structure)))()")
print(io, "$(nameof(typeof(alg)))($(join(modifiers, ", ")))")
return nothing
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)")
else
print(io, "$(nameof(typeof(alg)))()")
end
return nothing
end

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

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
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,
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)")
end

function ApproximateJacobianSolveAlgorithm(; concrete_jac = nothing,
name::Symbol = :unknown, kwargs...)
return ApproximateJacobianSolveAlgorithm{concrete_jac, name}(; kwargs...)
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,
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)")
end

function GeneralizedFirstOrderAlgorithm(; concrete_jac = nothing,
name::Symbol = :unknown, kwargs...)
return GeneralizedFirstOrderAlgorithm{concrete_jac, name}(; kwargs...)
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)")
end

concrete_jac(::GeneralizedDFSane) = nothing

@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, ", ")))")
end

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

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,
"Dogleg(newton_descent = $(d.newton_descent), steepest_descent = $(d.steepest_descent))")
end

supports_trust_region(::Dogleg) = true

function Dogleg(; linsolve = nothing, precs = DEFAULT_PRECS, damping = False,
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 = ",
"$(alg.finite_diff_step_geodesic), α = $(alg.α))")
end

supports_trust_region(::GeodesicAcceleration) = true

@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, ", ")))")
end

supports_line_search(::NewtonDescent) = true

@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, ", ")))")
end

supports_line_search(::SteepestDescent) = true

@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 &&
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, ", "), ")")
end

LineSearchesJL(method; kwargs...) = LineSearchesJL(; method, kwargs...)
function LineSearchesJL(; method = LineSearches.Static(), autodiff = nothing, α = true)
return LineSearchesJL(method, α, autodiff)
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

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

0 comments on commit 1bf9805

Please sign in to comment.