From 4d100c94e48c0e55108ae21ea7e834a910148daa Mon Sep 17 00:00:00 2001 From: Avik Pal Date: Thu, 23 Nov 2023 14:42:30 -0500 Subject: [PATCH] Add a note on the first value --- docs/src/basics/Logging.md | 5 +++++ src/gaussnewton.jl | 2 +- src/levenberg.jl | 2 +- src/pseudotransient.jl | 2 +- src/raphson.jl | 2 +- src/trace.jl | 14 ++++++++------ src/trustRegion.jl | 2 +- src/utils.jl | 5 +++++ 8 files changed, 23 insertions(+), 11 deletions(-) diff --git a/docs/src/basics/Logging.md b/docs/src/basics/Logging.md index 093532c8f..7cb757967 100644 --- a/docs/src/basics/Logging.md +++ b/docs/src/basics/Logging.md @@ -49,6 +49,11 @@ sol = solve(prob; trace_level = TraceAll(), store_trace = Val(true)); sol.trace ``` +!!! note + + For `iteration == 0` only the `norm(fu, Inf)` is guaranteed to be meaningful. The other + values being meaningful are solver dependent. + ## API ```@docs diff --git a/src/gaussnewton.jl b/src/gaussnewton.jl index 6010618cf..ea1855e68 100644 --- a/src/gaussnewton.jl +++ b/src/gaussnewton.jl @@ -109,7 +109,7 @@ function SciMLBase.__init(prob::NonlinearLeastSquaresProblem{uType, iip}, alg_:: abstol, reltol, tc_cache_1 = init_termination_cache(abstol, reltol, fu1, u, termination_condition) _, _, tc_cache_2 = init_termination_cache(abstol, reltol, fu1, u, termination_condition) - trace = init_nonlinearsolve_trace(alg, u, fu1, J, du; kwargs...) + trace = init_nonlinearsolve_trace(alg, u, fu1, ApplyArray(__zero, J), du; kwargs...) return GaussNewtonCache{iip}(f, alg, u, copy(u), fu1, fu2, zero(fu1), du, p, uf, linsolve, J, JᵀJ, Jᵀf, jac_cache, false, maxiters, internalnorm, ReturnCode.Default, diff --git a/src/levenberg.jl b/src/levenberg.jl index 9b11151fa..dcc07d85e 100644 --- a/src/levenberg.jl +++ b/src/levenberg.jl @@ -221,7 +221,7 @@ function SciMLBase.__init(prob::Union{NonlinearProblem{uType, iip}, tc_cache_2 = nothing end - trace = init_nonlinearsolve_trace(alg, u, fu1, J, du; kwargs...) + trace = init_nonlinearsolve_trace(alg, u, fu1, ApplyArray(__zero, J), du; kwargs...) if _unwrap_val(linsolve_with_JᵀJ) mat_tmp = zero(JᵀJ) diff --git a/src/pseudotransient.jl b/src/pseudotransient.jl index bb5594526..3873202c4 100644 --- a/src/pseudotransient.jl +++ b/src/pseudotransient.jl @@ -95,7 +95,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg_::PseudoTransi abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fu1, u, termination_condition) - trace = init_nonlinearsolve_trace(alg, u, fu1, J, du; kwargs...) + trace = init_nonlinearsolve_trace(alg, u, fu1, ApplyArray(__zero, J), du; kwargs...) return PseudoTransientCache{iip}(f, alg, u, copy(u), fu1, fu2, du, p, alpha, res_norm, uf, linsolve, J, jac_cache, false, maxiters, internalnorm, ReturnCode.Default, diff --git a/src/raphson.jl b/src/raphson.jl index 7ee75e050..594b893e5 100644 --- a/src/raphson.jl +++ b/src/raphson.jl @@ -89,7 +89,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg_::NewtonRaphso termination_condition) ls_cache = init_linesearch_cache(alg.linesearch, f, u, p, fu1, Val(iip)) - trace = init_nonlinearsolve_trace(alg, u, fu1, J, du; kwargs...) + trace = init_nonlinearsolve_trace(alg, u, fu1, ApplyArray(__zero, J), du; kwargs...) return NewtonRaphsonCache{iip}(f, alg, u, copy(u), fu1, fu2, du, p, uf, linsolve, J, jac_cache, false, maxiters, internalnorm, ReturnCode.Default, abstol, reltol, prob, diff --git a/src/trace.jl b/src/trace.jl index cdc76361a..c458c7d07 100644 --- a/src/trace.jl +++ b/src/trace.jl @@ -146,7 +146,9 @@ __copy(x) = x trace_level::Tr end -reset!(trace::NonlinearSolveTrace) = resize!(trace.history, 0) +function reset!(trace::NonlinearSolveTrace) + (trace.history !== nothing && resize!(trace.history, 0)) +end function Base.show(io::IO, trace::NonlinearSolveTrace) for entry in trace.history @@ -166,7 +168,7 @@ function init_nonlinearsolve_trace(alg, ::Val{show_trace}, trace_level::AbstractNonlinearSolveTraceLevel, ::Val{store_trace}, u, fu, J, δu, ::Val{uses_jac_inverse}) where {show_trace, store_trace, uses_jac_inverse} if show_trace - Base.printstyled("\nAlgorithm: "; italic = true) + print("\nAlgorithm: ") Base.printstyled(alg, "\n\n"; color = :green, bold = true) end J_ = uses_jac_inverse ? (trace_level isa TraceMinimal ? J : inv(J)) : J @@ -205,8 +207,8 @@ function update_trace!(trace::NonlinearSolveTrace{ShT, StT}, iter, u, fu, J, δu return trace end - show_now = ShT && (iter % trace.trace_level.print_frequency == 0) - store_now = StT && (iter % trace.trace_level.store_frequency == 0) + show_now = ShT && (iter % trace.trace_level.print_frequency == 1) + store_now = StT && (iter % trace.trace_level.store_frequency == 1) (show_now || store_now) && (entry = __trace_entry(trace.trace_level, iter, u, fu, J, δu, α)) store_now && push!(trace.history, entry) @@ -226,8 +228,8 @@ function update_trace_with_invJ!(trace::NonlinearSolveTrace{ShT, StT}, iter, u, return trace end - show_now = ShT && (iter % trace.trace_level.print_frequency == 0) - store_now = StT && (iter % trace.trace_level.store_frequency == 0) + show_now = ShT && (iter % trace.trace_level.print_frequency == 1) + store_now = StT && (iter % trace.trace_level.store_frequency == 1) if show_now || store_now J_ = trace.trace_level isa TraceMinimal ? J : inv(J) entry = __trace_entry(trace.trace_level, iter, u, fu, J_, δu, α) diff --git a/src/trustRegion.jl b/src/trustRegion.jl index 53758787d..8b4041b75 100644 --- a/src/trustRegion.jl +++ b/src/trustRegion.jl @@ -340,7 +340,7 @@ function SciMLBase.__init(prob::NonlinearProblem{uType, iip}, alg_::TrustRegion, abstol, reltol, tc_cache = init_termination_cache(abstol, reltol, fu1, u, termination_condition) - trace = init_nonlinearsolve_trace(alg, u, fu1, J, du; kwargs...) + trace = init_nonlinearsolve_trace(alg, u, fu1, ApplyArray(__zero, J), du; kwargs...) return TrustRegionCache{iip}(f, alg, u_prev, u, fu_prev, fu1, fu2, p, uf, linsolve, J, jac_cache, false, maxiters, internalnorm, ReturnCode.Default, abstol, reltol, prob, diff --git a/src/utils.jl b/src/utils.jl index c5161df7c..c9c9cf61d 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -354,3 +354,8 @@ end # Define special concatenation for certain Array combinations @inline _vcat(x, y) = vcat(x, y) + +__zero(x::AbstractArray) = zero(x) +__zero(x) = x +LazyArrays.applied_eltype(::typeof(__zero), x) = eltype(x) +LazyArrays.applied_ndims(::typeof(__zero), x) = ndims(x)