Skip to content

Commit

Permalink
Add a note on the first value
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Nov 24, 2023
1 parent 354e080 commit 1b9c188
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
7 changes: 6 additions & 1 deletion docs/src/basics/Logging.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [Logging the Solve Process](@ logging_api)
# Logging the Solve Process

All NonlinearSolve.jl native solvers allow storing and displaying the trace of the nonlinear
solve process. This is controlled by 3 keyword arguments to `solve`:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/gaussnewton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/levenberg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/pseudotransient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/raphson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions src/trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -226,8 +228,8 @@ function update_trace_with_invJ!(trace::NonlinearSolveTrace{ShT, StT}, iter, u,
return trace

Check warning on line 228 in src/trace.jl

View check run for this annotation

Codecov / codecov/patch

src/trace.jl#L227-L228

Added lines #L227 - L228 were not covered by tests
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, α)
Expand Down
2 changes: 1 addition & 1 deletion src/trustRegion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,10 @@ end

# Define special concatenation for certain Array combinations
@inline _vcat(x, y) = vcat(x, y)

__zero(x::AbstractArray) = zero(x)
__zero(x) = x

Check warning on line 359 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L359

Added line #L359 was not covered by tests
LazyArrays.applied_eltype(::typeof(__zero), x) = eltype(x)
LazyArrays.applied_ndims(::typeof(__zero), x) = ndims(x)
LazyArrays.applied_size(::typeof(__zero), x) = size(x)
LazyArrays.applied_axes(::typeof(__zero), x) = axes(x)

Check warning on line 363 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L363

Added line #L363 was not covered by tests

0 comments on commit 1b9c188

Please sign in to comment.