@@ -130,7 +130,7 @@ struct ForwardDiffStepSize3 <: ForwardDiffStepSize end
130130Computes the Jacobian-vector product `j(x[n]) * Δx[n]` for a Newton-Krylov
131131method without directly using the Jacobian `j(x[n])`, and instead only using
132132`x[n]`, `f(x[n])`, and other function evaluations `f(x′)`. This is done by
133- calling `jvp!(::JacobianFreeJVP, cache, jΔx, Δx, x, f!, f, post_implicit !)`.
133+ calling `jvp!(::JacobianFreeJVP, cache, jΔx, Δx, x, f!, f, prepare_for_f !)`.
134134The `jΔx` passed to a Jacobian-free JVP is modified in-place. The `cache` can
135135be obtained with `allocate_cache(::JacobianFreeJVP, x_prototype)`, where
136136`x_prototype` is `similar` to `x` (and also to `Δx` and `f`).
@@ -151,13 +151,13 @@ end
151151
152152allocate_cache (:: ForwardDiffJVP , x_prototype) = (; x2 = zero (x_prototype), f2 = zero (x_prototype))
153153
154- function jvp! (alg:: ForwardDiffJVP , cache, jΔx, Δx, x, f!, f, post_implicit !)
154+ function jvp! (alg:: ForwardDiffJVP , cache, jΔx, Δx, x, f!, f, prepare_for_f !)
155155 (; default_step, step_adjustment) = alg
156156 (; x2, f2) = cache
157157 FT = eltype (x)
158158 ε = FT (step_adjustment) * default_step (Δx, x)
159159 @. x2 = x + ε * Δx
160- isnothing (post_implicit !) || post_implicit ! (x2)
160+ isnothing (prepare_for_f !) || prepare_for_f ! (x2)
161161 f! (f2, x2)
162162 @. jΔx = (f2 - f) / ε
163163end
343343Finds an approximation `Δx[n] ≈ j(x[n]) \\ f(x[n])` for Newton's method such
344344that `‖f(x[n]) - j(x[n]) * Δx[n]‖ ≤ rtol[n] * ‖f(x[n])‖`, where `rtol[n]` is the
345345value of the forcing term on iteration `n`. This is done by calling
346- `solve_krylov!(::KrylovMethod, cache, Δx, x, f!, f, n, post_implicit !, j = nothing)`,
346+ `solve_krylov!(::KrylovMethod, cache, Δx, x, f!, f, n, prepare_for_f !, j = nothing)`,
347347where `f` is `f(x[n])` and, if it is specified, `j` is either `j(x[n])` or an
348348approximation of `j(x[n])`. The `Δx` passed to a Krylov method is modified in-place.
349349The `cache` can be obtained with `allocate_cache(::KrylovMethod, x_prototype)`,
@@ -428,14 +428,14 @@ function allocate_cache(alg::KrylovMethod, x_prototype)
428428 )
429429end
430430
431- NVTX. @annotate function solve_krylov! (alg:: KrylovMethod , cache, Δx, x, f!, f, n, post_implicit !, j = nothing )
431+ NVTX. @annotate function solve_krylov! (alg:: KrylovMethod , cache, Δx, x, f!, f, n, prepare_for_f !, j = nothing )
432432 (; jacobian_free_jvp, forcing_term, solve_kwargs) = alg
433433 (; disable_preconditioner, debugger) = alg
434434 type = solver_type (alg)
435435 (; jacobian_free_jvp_cache, forcing_term_cache, solver, debugger_cache) = cache
436436 jΔx! (jΔx, Δx) =
437437 isnothing (jacobian_free_jvp) ? mul! (jΔx, j, Δx) :
438- jvp! (jacobian_free_jvp, jacobian_free_jvp_cache, jΔx, Δx, x, f!, f, post_implicit !)
438+ jvp! (jacobian_free_jvp, jacobian_free_jvp_cache, jΔx, Δx, x, f!, f, prepare_for_f !)
439439 opj = LinearOperator (eltype (x), length (x), length (x), false , false , jΔx!)
440440 M = disable_preconditioner || isnothing (j) || isnothing (jacobian_free_jvp) ? I : j
441441 print_debug! (debugger, debugger_cache, opj, M)
@@ -567,25 +567,9 @@ function allocate_cache(alg::NewtonsMethod, x_prototype, j_prototype = nothing)
567567 )
568568end
569569
570- solve_newton! (
571- alg:: NewtonsMethod ,
572- cache:: Nothing ,
573- x,
574- f!,
575- j! = nothing ,
576- post_implicit! = nothing ,
577- post_implicit_last! = nothing ,
578- ) = nothing
579-
580- NVTX. @annotate function solve_newton! (
581- alg:: NewtonsMethod ,
582- cache,
583- x,
584- f!,
585- j! = nothing ,
586- post_implicit! = nothing ,
587- post_implicit_last! = nothing ,
588- )
570+ solve_newton! (alg:: NewtonsMethod , cache:: Nothing , x, f!, j! = nothing , prepare_for_f! = nothing ) = nothing
571+
572+ NVTX. @annotate function solve_newton! (alg:: NewtonsMethod , cache, x, f!, j! = nothing , prepare_for_f! = nothing )
589573 (; max_iters, update_j, krylov_method, convergence_checker, verbose) = alg
590574 (; krylov_method_cache, convergence_checker_cache) = cache
591575 (; Δx, f, j) = cache
@@ -605,22 +589,18 @@ NVTX.@annotate function solve_newton!(
605589 ldiv! (Δx, j, f)
606590 end
607591 else
608- solve_krylov! (krylov_method, krylov_method_cache, Δx, x, f!, f, n, post_implicit !, j)
592+ solve_krylov! (krylov_method, krylov_method_cache, Δx, x, f!, f, n, prepare_for_f !, j)
609593 end
610594 is_verbose (verbose) && @info " Newton iteration $n : ‖x‖ = $(norm (x)) , ‖Δx‖ = $(norm (Δx)) "
611595
612596 x .- = Δx
613597 # Update x[n] with Δx[n - 1], and exit the loop if Δx[n] is not needed.
614598 # Check for convergence if necessary.
615599 if is_converged! (convergence_checker, convergence_checker_cache, x, Δx, n)
616- isnothing (post_implicit_last!) || post_implicit_last! (x)
617600 break
618- elseif n == max_iters
619- isnothing (post_implicit_last!) || post_implicit_last! (x)
620- else
621- isnothing (post_implicit!) || post_implicit! (x)
622- end
623- if is_verbose (verbose) && n == max_iters
601+ elseif n < max_iters
602+ isnothing (prepare_for_f!) || prepare_for_f! (x)
603+ elseif is_verbose (verbose)
624604 @warn " Newton's method did not converge within $n iterations: ‖x‖ = $(norm (x)) , ‖Δx‖ = $(norm (Δx)) "
625605 end
626606 end
0 commit comments