|
| 1 | +@concrete struct NonlinearSolveTracing |
| 2 | + trace_mode <: Union{Val{:minimal}, Val{:condition_number}, Val{:all}} |
| 3 | + print_frequency::Int |
| 4 | + store_frequency::Int |
| 5 | +end |
| 6 | + |
| 7 | +""" |
| 8 | + TraceMinimal(freq) |
| 9 | + TraceMinimal(; print_frequency = 1, store_frequency::Int = 1) |
| 10 | +
|
| 11 | +Trace Minimal Information |
| 12 | +
|
| 13 | + 1. Iteration Number |
| 14 | + 2. f(u) inf-norm |
| 15 | + 3. Step 2-norm |
| 16 | +
|
| 17 | +See also [`TraceWithJacobianConditionNumber`](@ref) and [`TraceAll`](@ref). |
| 18 | +""" |
| 19 | +function TraceMinimal(; print_frequency = 1, store_frequency::Int = 1) |
| 20 | + return NonlinearSolveTracing(Val(:minimal), print_frequency, store_frequency) |
| 21 | +end |
| 22 | + |
| 23 | +""" |
| 24 | + TraceWithJacobianConditionNumber(freq) |
| 25 | + TraceWithJacobianConditionNumber(; print_frequency = 1, store_frequency::Int = 1) |
| 26 | +
|
| 27 | +[`TraceMinimal`](@ref) + Print the Condition Number of the Jacobian. |
| 28 | +
|
| 29 | +See also [`TraceMinimal`](@ref) and [`TraceAll`](@ref). |
| 30 | +""" |
| 31 | +function TraceWithJacobianConditionNumber(; print_frequency = 1, store_frequency::Int = 1) |
| 32 | + return NonlinearSolveTracing(Val(:condition_number), print_frequency, store_frequency) |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | + TraceAll(freq) |
| 37 | + TraceAll(; print_frequency = 1, store_frequency::Int = 1) |
| 38 | +
|
| 39 | +[`TraceWithJacobianConditionNumber`](@ref) + Store the Jacobian, u, f(u), and δu. |
| 40 | +
|
| 41 | +!!! warning |
| 42 | +
|
| 43 | + This is very expensive and makes copyies of the Jacobian, u, f(u), and δu. |
| 44 | +
|
| 45 | +See also [`TraceMinimal`](@ref) and [`TraceWithJacobianConditionNumber`](@ref). |
| 46 | +""" |
| 47 | +function TraceAll(; print_frequency = 1, store_frequency::Int = 1) |
| 48 | + return NonlinearSolveTracing(Val(:all), print_frequency, store_frequency) |
| 49 | +end |
| 50 | + |
| 51 | +for Tr in (:TraceMinimal, :TraceWithJacobianConditionNumber, :TraceAll) |
| 52 | + @eval $(Tr)(freq) = $(Tr)(; print_frequency = freq, store_frequency = freq) |
| 53 | +end |
| 54 | + |
| 55 | +# NonlinearSolve Tracing Utilities |
| 56 | +@concrete struct NonlinearSolveTraceEntry |
| 57 | + iteration::Int |
| 58 | + fnorm |
| 59 | + stepnorm |
| 60 | + condJ |
| 61 | + storage |
| 62 | + norm_type::Symbol |
| 63 | +end |
| 64 | + |
| 65 | +function Base.getproperty(entry::NonlinearSolveTraceEntry, sym::Symbol) |
| 66 | + hasfield(typeof(entry), sym) && return getfield(entry, sym) |
| 67 | + return getproperty(entry.storage, sym) |
| 68 | +end |
| 69 | + |
| 70 | +function print_top_level(io::IO, entry::NonlinearSolveTraceEntry) |
| 71 | + if entry.condJ === nothing |
| 72 | + @printf io "%-8s\t%-20s\t%-20s\n" "----" "-------------" "-----------" |
| 73 | + if entry.norm_type === :L2 |
| 74 | + @printf io "%-8s\t%-20s\t%-20s\n" "Iter" "f(u) 2-norm" "Step 2-norm" |
| 75 | + else |
| 76 | + @printf io "%-8s\t%-20s\t%-20s\n" "Iter" "f(u) inf-norm" "Step 2-norm" |
| 77 | + end |
| 78 | + @printf io "%-8s\t%-20s\t%-20s\n" "----" "-------------" "-----------" |
| 79 | + else |
| 80 | + @printf io "%-8s\t%-20s\t%-20s\t%-20s\n" "----" "-------------" "-----------" "-------" |
| 81 | + if entry.norm_type === :L2 |
| 82 | + @printf io "%-8s\t%-20s\t%-20s\t%-20s\n" "Iter" "f(u) 2-norm" "Step 2-norm" "cond(J)" |
| 83 | + else |
| 84 | + @printf io "%-8s\t%-20s\t%-20s\t%-20s\n" "Iter" "f(u) inf-norm" "Step 2-norm" "cond(J)" |
| 85 | + end |
| 86 | + @printf io "%-8s\t%-20s\t%-20s\t%-20s\n" "----" "-------------" "-----------" "-------" |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +function Base.show(io::IO, ::MIME"text/plain", entry::NonlinearSolveTraceEntry) |
| 91 | + entry.iteration == 0 && print_top_level(io, entry) |
| 92 | + if entry.iteration < 0 # Special case for final entry |
| 93 | + @printf io "%-8s\t%-20.8e\n" "Final" entry.fnorm |
| 94 | + @printf io "%-28s\n" "----------------------" |
| 95 | + elseif entry.condJ === nothing |
| 96 | + @printf io "%-8d\t%-20.8e\t%-20.8e\n" entry.iteration entry.fnorm entry.stepnorm |
| 97 | + else |
| 98 | + @printf io "%-8d\t%-20.8e\t%-20.8e\t%-20.8e\n" entry.iteration entry.fnorm entry.stepnorm entry.condJ |
| 99 | + end |
| 100 | +end |
| 101 | + |
| 102 | +function NonlinearSolveTraceEntry(prob::AbstractNonlinearProblem, iteration, fu, δu, J, u) |
| 103 | + norm_type = ifelse(prob isa NonlinearLeastSquaresProblem, :L2, :Inf) |
| 104 | + fnorm = prob isa NonlinearLeastSquaresProblem ? L2_NORM(fu) : Linf_NORM(fu) |
| 105 | + condJ = J !== missing ? Utils.condition_number(J) : nothing |
| 106 | + storage = u === missing ? nothing : |
| 107 | + (; u = copy(u), fu = copy(fu), δu = copy(δu), J = copy(J)) |
| 108 | + return NonlinearSolveTraceEntry( |
| 109 | + iteration, fnorm, L2_NORM(δu), condJ, storage, norm_type |
| 110 | + ) |
| 111 | +end |
| 112 | + |
| 113 | +@concrete struct NonlinearSolveTrace |
| 114 | + show_trace <: Union{Val{false}, Val{true}} |
| 115 | + store_trace <: Union{Val{false}, Val{true}} |
| 116 | + history |
| 117 | + trace_level <: NonlinearSolveTracing |
| 118 | + prob |
| 119 | +end |
| 120 | + |
| 121 | +reset!(trace::NonlinearSolveTrace) = reset!(trace.history) |
| 122 | +reset!(::Nothing) = nothing |
| 123 | +reset!(history::Vector) = empty!(history) |
| 124 | + |
| 125 | +function Base.show(io::IO, ::MIME"text/plain", trace::NonlinearSolveTrace) |
| 126 | + if trace.history !== nothing |
| 127 | + foreach(trace.history) do entry |
| 128 | + show(io, MIME"text/plain"(), entry) |
| 129 | + end |
| 130 | + else |
| 131 | + print(io, "Tracing Disabled") |
| 132 | + end |
| 133 | +end |
| 134 | + |
| 135 | +function init_nonlinearsolve_trace( |
| 136 | + prob, alg, u, fu, J, δu; show_trace::Val = Val(false), |
| 137 | + trace_level::NonlinearSolveTracing = TraceMinimal(), store_trace::Val = Val(false), |
| 138 | + uses_jac_inverse = Val(false), kwargs... |
| 139 | +) |
| 140 | + return init_nonlinearsolve_trace( |
| 141 | + prob, alg, show_trace, trace_level, store_trace, u, fu, J, δu, uses_jac_inverse |
| 142 | + ) |
| 143 | +end |
| 144 | + |
| 145 | +function init_nonlinearsolve_trace( |
| 146 | + prob::AbstractNonlinearProblem, alg, show_trace::Val, |
| 147 | + trace_level::NonlinearSolveTracing, store_trace::Val, u, fu, J, δu, |
| 148 | + uses_jac_inverse::Val |
| 149 | +) |
| 150 | + if show_trace isa Val{true} |
| 151 | + print("\nAlgorithm: ") |
| 152 | + Base.printstyled(alg, "\n\n"; color = :green, bold = true) |
| 153 | + end |
| 154 | + J = uses_jac_inverse isa Val{true} ? |
| 155 | + (trace_level.trace_mode isa Val{:minimal} ? J : LinearAlgebra.pinv(J)) : J |
| 156 | + history = init_trace_history(prob, show_trace, trace_level, store_trace, u, fu, J, δu) |
| 157 | + return NonlinearSolveTrace(show_trace, store_trace, history, trace_level, prob) |
| 158 | +end |
| 159 | + |
| 160 | +function init_trace_history( |
| 161 | + prob::AbstractNonlinearProblem, show_trace::Val, trace_level, |
| 162 | + store_trace::Val, u, fu, J, δu |
| 163 | +) |
| 164 | + store_trace isa Val{false} && show_trace isa Val{false} && return nothing |
| 165 | + entry = if trace_level.trace_mode isa Val{:minimal} |
| 166 | + NonlinearSolveTraceEntry(prob, 0, fu, δu, missing, missing) |
| 167 | + elseif trace_level.trace_mode isa Val{:condition_number} |
| 168 | + NonlinearSolveTraceEntry(prob, 0, fu, δu, J, missing) |
| 169 | + else |
| 170 | + NonlinearSolveTraceEntry(prob, 0, fu, δu, J, u) |
| 171 | + end |
| 172 | + show_trace isa Val{true} && show(stdout, MIME"text/plain"(), entry) |
| 173 | + store_trace isa Val{true} && return NonlinearSolveTraceEntry[entry] |
| 174 | + return nothing |
| 175 | +end |
| 176 | + |
| 177 | +function update_trace!( |
| 178 | + trace::NonlinearSolveTrace, iter, u, fu, J, δu, α = true; last::Val = Val(false) |
| 179 | +) |
| 180 | + trace.store_trace isa Val{false} && trace.show_trace isa Val{false} && return nothing |
| 181 | + |
| 182 | + if last isa Val{true} |
| 183 | + norm_type = ifelse(trace.prob isa NonlinearLeastSquaresProblem, :L2, :Inf) |
| 184 | + fnorm = trace.prob isa NonlinearLeastSquaresProblem ? L2_NORM(fu) : Linf_NORM(fu) |
| 185 | + entry = NonlinearSolveTraceEntry(-1, fnorm, NaN32, nothing, nothing, norm_type) |
| 186 | + trace.show_trace isa Val{true} && show(stdout, MIME"text/plain"(), entry) |
| 187 | + return trace |
| 188 | + end |
| 189 | + |
| 190 | + show_now = trace.show_trace isa Val{true} && |
| 191 | + (mod1(iter, trace.trace_level.print_frequency) == 1) |
| 192 | + store_now = trace.store_trace isa Val{true} && |
| 193 | + (mod1(iter, trace.trace_level.store_frequency) == 1) |
| 194 | + if show_now || store_now |
| 195 | + entry = if trace.trace_level.trace_mode isa Val{:minimal} |
| 196 | + NonlinearSolveTraceEntry(trace.prob, iter, fu, δu .* α, missing, missing) |
| 197 | + elseif trace.trace_level.trace_mode isa Val{:condition_number} |
| 198 | + NonlinearSolveTraceEntry(trace.prob, iter, fu, δu .* α, J, missing) |
| 199 | + else |
| 200 | + NonlinearSolveTraceEntry(trace.prob, iter, fu, δu .* α, J, u) |
| 201 | + end |
| 202 | + show_now && show(stdout, MIME"text/plain"(), entry) |
| 203 | + store_now && push!(trace.history, entry) |
| 204 | + end |
| 205 | + return trace |
| 206 | +end |
| 207 | + |
| 208 | +function update_trace!(cache, α = true) |
| 209 | + trace = Utils.safe_getproperty(cache, Val(:trace)) |
| 210 | + trace === missing && return nothing |
| 211 | + |
| 212 | + J = Utils.safe_getproperty(cache, Val(:J)) |
| 213 | + if J === missing |
| 214 | + update_trace!( |
| 215 | + trace, cache.nsteps + 1, get_u(cache), get_fu(cache), nothing, cache.du, α |
| 216 | + ) |
| 217 | + # XXX: Implement |
| 218 | + # elseif cache isa ApproximateJacobianSolveCache && store_inverse_jacobian(cache) |
| 219 | + # update_trace!(trace, cache.nsteps + 1, get_u(cache), get_fu(cache), |
| 220 | + # ApplyArray(__safe_inv, J), cache.du, α) |
| 221 | + else |
| 222 | + update_trace!(trace, cache.nsteps + 1, get_u(cache), get_fu(cache), J, cache.du, α) |
| 223 | + end |
| 224 | +end |
0 commit comments