@@ -52,7 +52,7 @@ for Tr in (:TraceMinimal, :TraceWithJacobianConditionNumber, :TraceAll)
5252end
5353
5454# NonlinearSolve Tracing Utilities
55- @concrete struct NonlinearSolveTraceEntry
55+ @concrete struct NonlinearSolveTraceEntry{nType}
5656 iteration:: Int
5757 fnorm
5858 stepnorm
6363 δu
6464end
6565
66- function __show_top_level (io:: IO , entry:: NonlinearSolveTraceEntry )
66+ function __show_top_level (io:: IO , entry:: NonlinearSolveTraceEntry{nType} ) where {nType}
6767 if entry. condJ === nothing
6868 @printf io " %-8s %-20s %-20s\n " " ----" " -------------" " -----------"
69- @printf io " %-8s %-20s %-20s\n " " Iter" " f(u) inf-norm" " Step 2-norm"
69+ if nType === :L2
70+ @printf io " %-8s %-20s %-20s\n " " Iter" " f(u) 2-norm" " Step 2-norm"
71+ else
72+ @printf io " %-8s %-20s %-20s\n " " Iter" " f(u) inf-norm" " Step 2-norm"
73+ end
7074 @printf io " %-8s %-20s %-20s\n " " ----" " -------------" " -----------"
7175 else
7276 @printf io " %-8s %-20s %-20s %-20s\n " " ----" " -------------" " -----------" " -------"
73- @printf io " %-8s %-20s %-20s %-20s\n " " Iter" " f(u) inf-norm" " Step 2-norm" " cond(J)"
77+ if nType === :L2
78+ @printf io " %-8s %-20s %-20s %-20s\n " " Iter" " f(u) 2-norm" " Step 2-norm" " cond(J)"
79+ else
80+ @printf io " %-8s %-20s %-20s %-20s\n " " Iter" " f(u) inf-norm" " Step 2-norm" " cond(J)"
81+ end
7482 @printf io " %-8s %-20s %-20s %-20s\n " " ----" " -------------" " -----------" " -------"
7583 end
7684end
7785
78- function Base. show (io:: IO , entry:: NonlinearSolveTraceEntry )
86+ function Base. show (io:: IO , entry:: NonlinearSolveTraceEntry{nType} ) where {nType}
7987 entry. iteration == 0 && __show_top_level (io, entry)
8088 if entry. iteration < 0
8189 # Special case for final entry
@@ -89,25 +97,32 @@ function Base.show(io::IO, entry::NonlinearSolveTraceEntry)
8997 return nothing
9098end
9199
92- function NonlinearSolveTraceEntry (iteration, fu, δu)
93- return NonlinearSolveTraceEntry (
94- iteration, norm (fu, Inf ), norm (δu, 2 ), nothing , nothing , nothing , nothing , nothing )
100+ function NonlinearSolveTraceEntry (prob:: AbstractNonlinearProblem , iteration, fu, δu)
101+ nType = ifelse (prob isa NonlinearLeastSquaresProblem, :L2 , :Inf )
102+ fnorm = prob isa NonlinearLeastSquaresProblem ? norm (fu, 2 ) : norm (fu, Inf )
103+ return NonlinearSolveTraceEntry {nType} (
104+ iteration, fnorm, norm (δu, 2 ), nothing , nothing , nothing , nothing , nothing )
95105end
96106
97- function NonlinearSolveTraceEntry (iteration, fu, δu, J)
98- return NonlinearSolveTraceEntry (iteration, norm (fu, Inf ), norm (δu, 2 ),
99- __cond (J), nothing , nothing , nothing , nothing )
107+ function NonlinearSolveTraceEntry (prob:: AbstractNonlinearProblem , iteration, fu, δu, J)
108+ nType = ifelse (prob isa NonlinearLeastSquaresProblem, :L2 , :Inf )
109+ fnorm = prob isa NonlinearLeastSquaresProblem ? norm (fu, 2 ) : norm (fu, Inf )
110+ return NonlinearSolveTraceEntry {nType} (
111+ iteration, fnorm, norm (δu, 2 ), __cond (J), nothing , nothing , nothing , nothing )
100112end
101113
102- function NonlinearSolveTraceEntry (iteration, fu, δu, J, u)
103- return NonlinearSolveTraceEntry (iteration, norm (fu, Inf ), norm (δu, 2 ), __cond (J),
114+ function NonlinearSolveTraceEntry (prob:: AbstractNonlinearProblem , iteration, fu, δu, J, u)
115+ nType = ifelse (prob isa NonlinearLeastSquaresProblem, :L2 , :Inf )
116+ fnorm = prob isa NonlinearLeastSquaresProblem ? norm (fu, 2 ) : norm (fu, Inf )
117+ return NonlinearSolveTraceEntry {nType} (iteration, fnorm, norm (δu, 2 ), __cond (J),
104118 __copy (J), __copy (u), __copy (fu), __copy (δu))
105119end
106120
107121@concrete struct NonlinearSolveTrace{
108122 show_trace, store_trace, Tr <: AbstractNonlinearSolveTraceLevel }
109123 history
110124 trace_level:: Tr
125+ prob
111126end
112127
113128function reset! (trace:: NonlinearSolveTrace )
@@ -123,61 +138,63 @@ function Base.show(io::IO, trace::NonlinearSolveTrace)
123138 return nothing
124139end
125140
126- function init_nonlinearsolve_trace (alg, u, fu, J, δu; show_trace:: Val = Val (false ),
141+ function init_nonlinearsolve_trace (prob, alg, u, fu, J, δu; show_trace:: Val = Val (false ),
127142 trace_level:: AbstractNonlinearSolveTraceLevel = TraceMinimal (),
128143 store_trace:: Val = Val (false ), uses_jac_inverse = Val (false ), kwargs... )
129144 return init_nonlinearsolve_trace (
130- alg, show_trace, trace_level, store_trace, u, fu, J, δu, uses_jac_inverse)
145+ prob, alg, show_trace, trace_level, store_trace, u, fu, J, δu, uses_jac_inverse)
131146end
132147
133- function init_nonlinearsolve_trace (
134- alg, :: Val{show_trace} , trace_level:: AbstractNonlinearSolveTraceLevel ,
135- :: Val{store_trace} , u, fu, J, δu,
136- :: Val{uses_jac_inverse} ) where {show_trace, store_trace, uses_jac_inverse}
148+ function init_nonlinearsolve_trace (prob:: AbstractNonlinearProblem , alg, :: Val{show_trace} ,
149+ trace_level:: AbstractNonlinearSolveTraceLevel , :: Val{store_trace} , u, fu, J,
150+ δu, :: Val{uses_jac_inverse} ) where {show_trace, store_trace, uses_jac_inverse}
137151 if show_trace
138152 print (" \n Algorithm: " )
139153 Base. printstyled (alg, " \n\n " ; color = :green , bold = true )
140154 end
141155 J_ = uses_jac_inverse ? (trace_level isa TraceMinimal ? J : __safe_inv (J)) : J
142156 history = __init_trace_history (
143- Val {show_trace} (), trace_level, Val {store_trace} (), u, fu, J_, δu)
144- return NonlinearSolveTrace {show_trace, store_trace} (history, trace_level)
157+ prob, Val {show_trace} (), trace_level, Val {store_trace} (), u, fu, J_, δu)
158+ return NonlinearSolveTrace {show_trace, store_trace} (history, trace_level, prob )
145159end
146160
147- function __init_trace_history (:: Val{show_trace} , trace_level, :: Val{store_trace} ,
148- u, fu, J, δu) where {show_trace, store_trace}
161+ function __init_trace_history (
162+ prob:: AbstractNonlinearProblem , :: Val{show_trace} , trace_level,
163+ :: Val{store_trace} , u, fu, J, δu) where {show_trace, store_trace}
149164 ! store_trace && ! show_trace && return nothing
150- entry = __trace_entry (trace_level, 0 , u, fu, J, δu)
165+ entry = __trace_entry (prob, trace_level, 0 , u, fu, J, δu)
151166 show_trace && show (entry)
152167 store_trace && return NonlinearSolveTraceEntry[entry]
153168 return nothing
154169end
155170
156- function __trace_entry (:: TraceMinimal , iter, u, fu, J, δu, α = 1 )
157- return NonlinearSolveTraceEntry (iter, fu, δu .* α)
171+ function __trace_entry (prob, :: TraceMinimal , iter, u, fu, J, δu, α = 1 )
172+ return NonlinearSolveTraceEntry (prob, iter, fu, δu .* α)
158173end
159- function __trace_entry (:: TraceWithJacobianConditionNumber , iter, u, fu, J, δu, α = 1 )
160- return NonlinearSolveTraceEntry (iter, fu, δu .* α, J)
174+ function __trace_entry (prob, :: TraceWithJacobianConditionNumber , iter, u, fu, J, δu, α = 1 )
175+ return NonlinearSolveTraceEntry (prob, iter, fu, δu .* α, J)
161176end
162- function __trace_entry (:: TraceAll , iter, u, fu, J, δu, α = 1 )
163- return NonlinearSolveTraceEntry (iter, fu, δu .* α, J, u)
177+ function __trace_entry (prob, :: TraceAll , iter, u, fu, J, δu, α = 1 )
178+ return NonlinearSolveTraceEntry (prob, iter, fu, δu .* α, J, u)
164179end
165180
166181function update_trace! (trace:: NonlinearSolveTrace{ShT, StT} , iter, u, fu, J, δu,
167182 α = 1 ; last:: Val{L} = Val (false )) where {ShT, StT, L}
168183 ! StT && ! ShT && return nothing
169184
170185 if L
171- entry = NonlinearSolveTraceEntry (
172- - 1 , norm (fu, Inf ), NaN32 , nothing , nothing , nothing , nothing , nothing )
186+ nType = ifelse (trace. prob isa NonlinearLeastSquaresProblem, :L2 , :Inf )
187+ fnorm = trace. prob isa NonlinearLeastSquaresProblem ? norm (fu, 2 ) : norm (fu, Inf )
188+ entry = NonlinearSolveTraceEntry {nType} (
189+ - 1 , fnorm, NaN32 , nothing , nothing , nothing , nothing , nothing )
173190 ShT && show (entry)
174191 return trace
175192 end
176193
177194 show_now = ShT && (mod1 (iter, trace. trace_level. print_frequency) == 1 )
178195 store_now = StT && (mod1 (iter, trace. trace_level. store_frequency) == 1 )
179196 (show_now || store_now) &&
180- (entry = __trace_entry (trace. trace_level, iter, u, fu, J, δu, α))
197+ (entry = __trace_entry (trace. prob, trace . trace_level, iter, u, fu, J, δu, α))
181198 store_now && push! (trace. history, entry)
182199 show_now && show (entry)
183200 return trace
0 commit comments