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 23, 2023
1 parent 354e080 commit 4d100c9
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
5 changes: 5 additions & 0 deletions docs/src/basics/Logging.md
Original file line number Diff line number Diff line change
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)

Check warning on line 172 in src/trace.jl

View check run for this annotation

Codecov / codecov/patch

src/trace.jl#L171-L172

Added lines #L171 - L172 were not covered by tests
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

Check warning on line 207 in src/trace.jl

View check run for this annotation

Codecov / codecov/patch

src/trace.jl#L206-L207

Added lines #L206 - L207 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)
(show_now || store_now) && (entry = __trace_entry(trace.trace_level, iter, u, fu, J,

Check warning on line 212 in src/trace.jl

View check run for this annotation

Codecov / codecov/patch

src/trace.jl#L210-L212

Added lines #L210 - L212 were not covered by tests
δ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, α)

Check warning on line 235 in src/trace.jl

View check run for this annotation

Codecov / codecov/patch

src/trace.jl#L231-L235

Added lines #L231 - L235 were not covered by tests
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
5 changes: 5 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 359 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L358-L359

Added lines #L358 - L359 were not covered by tests
LazyArrays.applied_eltype(::typeof(__zero), x) = eltype(x)
LazyArrays.applied_ndims(::typeof(__zero), x) = ndims(x)

0 comments on commit 4d100c9

Please sign in to comment.