Skip to content

Stiff terms ​

The implicit multistep schemes (am-k, bdf-k) advance Un+1=E+γR(Un+1), γ=Δtb0. Their own fixed-point iterations converge only where |γλ|<1 for the eigenvalues λ of the residual's Jacobian (Fixed-point stages). A stiff term is past that bound by construction. With iterations=2, stage 2 is the scheme's only implicit stage: you solve U=E+γR(U) in it, write the solution into the stage state, and evaluate the residual there; the stepper's update E+γR(U) returns U to your solver's tolerance. SELAH gives that stage solvers: the stage contract is unchanged, and each solver is an object you drive inside the stage (decision D1 of #11).

Newton, cell by cell ​

A relaxation, a chemical source or a collision term couples the components of one cell only: the Jacobian of R is block-diagonal, one nv×nv block per cell. newton_solver makes the Newton step U←U−(I−γJ)−1(U−E−γR(U)) as a small dense solve per cell (Gaussian elimination with partial pivoting, up to NEWTON_MAX_BLOCK = 16 components), on the device in a device build, with no linear solver across cells and no reduction but the convergence test. Robertson's kinetics, the classic stiff system, by bdf-1 (backward Euler), the step growing from 10−5 to t=40:

fortran
integer(I4P), parameter :: NV = 3, NCELL = 4
type(stepper)           :: st
type(newton_solver)     :: nw
type(box)               :: bx
real(R_P), allocatable, target :: q(:,:,:,:,:), jac(:,:,:,:,:,:)
real(R_P), pointer      :: y(:,:,:,:,:), r(:,:,:,:,:), e(:,:,:,:,:)
real(R_P)               :: t, dt, dmax
integer(I4P)            :: k, n, itmax

if (NV > NEWTON_MAX_BLOCK) error stop 'newton: a block of at most NEWTON_MAX_BLOCK components'
bx = box_interior(LAYOUT_V_FIRST, NV, NCELL, 1_I4P, 1_I4P, 0_I4P, 1_I4P) ! (v, i, j, k, b), no ghost cells
allocate(q(NV,NCELL,1,1,1), jac(NV,NCELL,1,1,1,NV))                        ! jac(v, cell, w) = dR_v/dy_w
q = 0._R_P ; q(1,:,:,:,:) = 1._R_P
call st%initialize('bdf-1', FORM_MULTISTEP, lb=[1_I4P, 1_I4P, 1_I4P, 1_I4P, 1_I4P], ub=bx%hi, iterations=2_I4P)
call nw%initialize(NEWTON_BLOCK, atol=1.e-14_R_P, rtol=1.e-10_R_P)
t = 0._R_P ; dt = 1.e-5_R_P ; itmax = 0
print '(A)', '     t          y1           y2           y3      |y1+y2+y3-1|  Newton iterations'
do n=1, 40
   call st%begin_step(bx, q)
   do k=1, st%stages()
      call st%begin_stage(k, bx, dt, q)
      call st%rhs_register_view(r)
      if (st%stage_state_is_q(k)) then
         y => q
      else                                              ! U = E + gamma R(U), Newton from U^n
         call st%stage_state_view(y)
         call st%explicit_part_view(e)
         y = q
         call nw%start
         do
            call robertson(y, r)
            call robertson_jacobian(y, jac)
            dmax = nw%update(bx, dt * st%stage_gamma(k), e, r, y, jac=jac)
            if (nw%converged(dmax)) exit                 ! with MPI: dmax reduced (max) over the ranks first
         enddo
         itmax = max(itmax, nw%iterations)
      endif
      call robertson(y, r)                              ! the stage's residual, at the solution
      call st%end_stage(k, bx, dt, q)
   enddo
   call st%finish_step(bx, dt, q)
   t = t + dt
   if (mod(n, 6) == 0 .or. t >= 40._R_P) print '(ES9.2,3ES13.5,A12,I8)', t, q(:,1,1,1,1), &
      merge('below 1e-14', 'ABOVE 1e-14', abs(sum(q(:,1,1,1,1)) - 1._R_P) < 1.e-14_R_P), itmax
   dt = min(1.5_R_P * dt, 40._R_P - t)
   if (dt <= 0._R_P) exit
enddo
call st%destroy
text
$ cookbook_stiff newton
     t          y1           y2           y3      |y1+y2+y3-1|  Newton iterations
 2.08E-04  9.99992E-01  8.10778E-06  2.04672E-07 below 1e-14       4
 2.57E-03  9.99897E-01  3.53276E-05  6.76334E-05 below 1e-14       4
 2.95E-02  9.98827E-01  3.63077E-05  1.13718E-03 below 1e-14       4
 3.37E-01  9.87521E-01  3.42743E-05  1.24444E-02 below 1e-14       4
 3.84E+00  9.12397E-01  2.32142E-05  8.75802E-02 below 1e-14       4
 4.00E+01  7.29539E-01  9.73811E-06  2.70451E-01 below 1e-14       5
  • The Jacobian. NEWTON_BLOCK: you write jac(cell, v, w) =∂Rv/∂yw, an array shaped like the field with one more dimension of the components, as the stepper's registers are. NEWTON_DIAGONAL: when each component depends on itself only, perturb(bx, y, yp) writes y+h, h=εmax(|y|,scale), you evaluate R there too, and update takes ∂Rv/∂yv from the difference quotient: two residuals per iteration, no Jacobian to write.
  • The convergence test. update returns its box's largest |δ|/(atol+rtol|y|). With several boxes or ranks you reduce it (a maximum) before converged(dmax), which is dmax <= 1 and stops the run after max_iterations.
  • The first guess is yours: Un above. The stepper's own first iterate, E+γR(Un), is far from the solution of a stiff cell.
  • The stage's residual. After the last update, evaluate R once more at the solution: the stepper's update reads it. Newton's iterations cost one residual each (two in the diagonal form), plus that one.
  • Its accuracy. selah_test_newton: bdf-2 on a 3-component relaxation with λΔt from 1 to 104 gives the closed-form BDF step within 10−12 in at most 2 iterations (4 with a cubic coupling), both layouts; the diagonal form on Prothero–Robinson at λΔt=100 the trapezoid's closed form, 2 iterations. What is left is the update's own rounding, γλε: E+γR(U) cancels two terms of size γλ|U|.
  • Backward Euler is first order. y1(40)=0.7295 above against 0.7158 (Hairer & Wanner's reference): the steps near t=40 are 13 long. bdf-2 and up keep a history: write its levels and call resume_history(dt) before the first step (The history), as selah_test_newton does.

The diagonal form ​

When each component's term depends on itself only (a radiative cooling, a pointwise decay), NEWTON_DIAGONAL takes ∂Rv/∂yv from one more residual per iteration, at the state perturb writes, and you write no Jacobian. Radiative cooling, u′=−s(u4−1), from u = 2 to 16, sΔt4u3 up to 1.6⋅105:

fortran
integer(I4P), parameter :: NCELL = 4
real(R_P),    parameter :: S = 100._R_P, DT = 0.1_R_P
type(stepper)           :: st
type(newton_solver)     :: nw
type(box)               :: bx
real(R_P), allocatable, target :: q(:,:,:,:,:), yp(:,:,:,:,:), rp(:,:,:,:,:)
real(R_P), pointer      :: y(:,:,:,:,:), r(:,:,:,:,:), e(:,:,:,:,:)
integer(I4P)            :: k, n, it(NCELL), i

bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NCELL, 1_I4P, 1_I4P, 0_I4P, 1_I4P)
allocate(q(1,NCELL,1,1,1), yp(1,NCELL,1,1,1), rp(1,NCELL,1,1,1))
q(1,:,1,1,1) = [(2._R_P**i, i=1, NCELL)]                ! 2, 4, 8, 16
call st%initialize('bdf-1', FORM_MULTISTEP, lb=[1_I4P, 1_I4P, 1_I4P, 1_I4P, 1_I4P], ub=bx%hi, iterations=2_I4P)
call nw%initialize(NEWTON_DIAGONAL, atol=1.e-14_R_P, rtol=1.e-12_R_P, max_iterations=30_I4P)
print '(A)', ' step       u (four cells)                              Newton iterations'
do n=1, 5
   call st%begin_step(bx, q)
   do k=1, st%stages()
      call st%begin_stage(k, bx, DT, q)
      call st%rhs_register_view(r)
      if (st%stage_state_is_q(k)) then
         y => q
      else
         call st%stage_state_view(y)
         call st%explicit_part_view(e)
         y = q
         call nw%start
         do
            r = -S * (y**4 - 1._R_P)                    ! R(y)
            call nw%perturb(bx, y, yp)
            rp = -S * (yp**4 - 1._R_P)                  ! R(y + h): the difference quotient's
            if (nw%converged(nw%update(bx, dt * st%stage_gamma(k), e, r, y, rp=rp, yp=yp))) exit
         enddo
         it(1) = nw%iterations
      endif
      r = -S * (y**4 - 1._R_P)
      call st%end_stage(k, bx, DT, q)
   enddo
   call st%finish_step(bx, DT, q)
   print '(I5,4F12.7,I10)', n, q(1,:,1,1,1), it(1)
enddo
call st%destroy
text
$ cookbook_stiff diagonal
 step       u (four cells)                              Newton iterations
    1   1.0235648   1.0664234   1.1395085   1.2542245        14
    2   1.0005743   1.0016163   1.0033858   1.0061451         6
    3   1.0000140   1.0000394   1.0000826   1.0001498         4
    4   1.0000003   1.0000010   1.0000020   1.0000037         3
    5   1.0000000   1.0000000   1.0000000   1.0000001         3

The first step starts Newton from Un = 16 for a solution near 1.25: far from the root each Newton step on u4 shrinks the iterate by about a quarter (its tangent's zero), and only near it does the convergence turn quadratic, so 14 iterations. The later steps start close and take 3 to 6.

Anderson's acceleration, no Jacobian ​

When the stiff part couples the cells and you have no Jacobian, anderson_mixing accelerates the stage's own iteration U←G(U)=E+γR(U) (Anderson 1965; Walker & Ni 2011): each iterate is G of the last one minus a combination of the last m differences of G, with the coefficients that make the same combination of the last m differences of the residual G(U)−U closest to the residual itself. On a linear residual it is GMRES in disguise, so it converges where the plain iteration diverges. The 1-D heat equation at 10 times forward Euler's bound (γλmax=20):

fortran
integer(I4P), parameter :: NX = 32
real(R_P),    parameter :: H = 1._R_P / (NX + 1), DT = 10._R_P * 0.5_R_P * H**2
type(stepper)           :: st
type(anderson_mixing)   :: aa
type(box)               :: bx
real(R_P), allocatable, target :: q(:)
real(R_P), pointer      :: y(:), r(:)
real(R_P), allocatable  :: sums(:)
integer(I4P)            :: k, istep, m, i, its

bx = box_interior(lb=[0_I4P], ub=[NX+1], ngc=1_I4P, components=.false.)
print '(A)', ' depth  residuals per step (5 steps)   u(1/2) at t = 5 dt'
do m=8, ANDERSON_MAX_DEPTH, 4                            ! depth 8, 12, 16 (4 diverges here)
   allocate(q(0:NX+1))
   q = [(sin(acos(-1._R_P) * i * H) + 0.5_R_P * sin(7._R_P * acos(-1._R_P) * i * H), i=0, NX+1)]
   q(0) = 0._R_P ; q(NX+1) = 0._R_P
   call st%initialize('bdf-1', FORM_MULTISTEP, lb=[0_I4P], ub=[NX+1], components=.false., iterations=2_I4P)
   call aa%initialize(m, rtol=1.e-12_R_P, max_iterations=200_I4P)
   allocate(sums(aa%sums_size()))
   write(*, '(I6,A)', advance='no') m, '  '
   do istep=1, 5
      call st%begin_step(bx, q)
      do k=1, st%stages()
         call st%begin_stage(k, bx, DT, q)
         call st%rhs_register_view(r)
         if (st%stage_state_is_q(k)) then
            call heat(q, r, H)
         else                                           ! Anderson from E + gamma R(U^n), the stepper's iterate
            call st%stage_state_view(y)
            call aa%start
            do
               call heat(y, r, H)
               call aa%residual(bx, DT * st%stage_gamma(k), st%explicit_part(), st%rhs_register(), &
                                st%stage_state(), sums)
               if (aa%converged(sums)) exit             ! with MPI: sums reduced (summed) over the ranks first
               call aa%mix(bx, st%stage_state(), sums)
            enddo
            its = aa%iterations
         endif
         call st%end_stage(k, bx, DT, q)
      enddo
      call st%finish_step(bx, DT, q)
      write(*, '(I5)', advance='no') its
   enddo
   print '(F17.10)', q(NX/2 + 1) / 2._R_P + q(NX/2) / 2._R_P
   call st%destroy ; call aa%destroy
   deallocate(q, sums)
enddo
text
$ cookbook_stiff anderson
 depth  residuals per step (5 steps)   u(1/2) at t = 5 dt
     8      9   18   19   18   20     0.7989169545
    12      9   15   15   16   14     0.7989169545
    16      9   15   15   16   14     0.7989169545
  • The loop. With the stage state x and R(x) in the stage's register, residual makes G(x), the residual f=G(x)−x and the newest differences, and returns sums_size() partial sums (the norms and the inner products of the differences); you sum them over your boxes and ranks, one reduction per iteration, and converged(sums) is ‖f‖≤ atol + rtol ‖G(x)‖. Otherwise mix writes the next iterate into the stage state, from the least-squares solve on the host (the m×m normal equations).
  • The stage's residual is already there when it converges: R(x) of the last iterate, and the stepper's update E+γR(x) is G(x), one more plain iteration.
  • The depth must cover the spectrum. Depth 4 diverges on this problem (32 modes, γλ up to 20): Anderson converges for a contractive map, and beyond it only as a Krylov method with memory enough. 8 to 16 here; ANDERSON_MAX_DEPTH is 16, at 2m+2 registers of the field's size.
  • Its accuracy. selah_test_fixed_point: am-1 at γλ = -2, -5, -20 (the plain iteration diverges) and a spectrum from -0.5 to -20 give the implicit step within 10−12 in 3 to 11 residuals; at γλ = -0.5 and -0.9 3 residuals against the plain iteration's 45 and 318.

Newton–Krylov, matrix-free ​

Far beyond the explicit bound a coupled stiff term needs Newton's method on the whole field. newton_krylov solves Newton's step Jδ=−F, F(U)=U−E−γR(U), by restarted GMRES (Saad & Schultz 1986), each product Jz=z−γ(R(U+εz)−R(U))/ε one residual of yours (Knoll & Keyes 2004): no Jacobian, no matrix. Each GMRES cycle (at most m = NK_MAX_RESTART = 16 iterations) is one Newton step, and a cycle stops early when its residual estimate falls below forcing (default 10−4) times ‖F‖. The same heat equation at 100 times forward Euler's bound:

fortran
integer(I4P), parameter :: NX = 32
real(R_P),    parameter :: H = 1._R_P / (NX + 1), DT = 100._R_P * 0.5_R_P * H**2
type(stepper)           :: st
type(newton_krylov)     :: nk
type(box)               :: bx
real(R_P), allocatable, target :: q(:)
real(R_P), pointer      :: y(:,:,:,:,:), r(:,:,:,:,:)
integer(I4P)            :: k, istep, i

bx = box_interior(lb=[0_I4P], ub=[NX+1], ngc=1_I4P, components=.false.)
allocate(q(0:NX+1))
q = [(sin(acos(-1._R_P) * i * H) + 0.5_R_P * sin(7._R_P * acos(-1._R_P) * i * H), i=0, NX+1)]
call st%initialize('bdf-1', FORM_MULTISTEP, lb=[0_I4P], ub=[NX+1], components=.false., iterations=2_I4P)
call nk%initialize(m=NK_MAX_RESTART, rtol=1.e-12_R_P)    ! GMRES(16), no preconditioner
print '(A)', ' step  Newton steps  Krylov iterations  reductions  residuals      u(1/2)'
do istep=1, 5
   call st%begin_step(bx, q)
   do k=1, st%stages()
      call st%begin_stage(k, bx, DT, q)
      if (st%stage_state_is_q(k)) then
         r => st%rhs_register()
         call heat(q, r(1,:,1,1,1), H)
      else                                              ! Newton-GMRES from E + gamma R(U^n)
         call nk%start(DT * st%stage_gamma(k), st%explicit_part(), st%stage_state(), st%rhs_register())
         do while (nk%request() /= NK_DONE)
            select case(nk%request())
            case(NK_RESIDUAL)                           ! R at the state it names, into the register it names
               y => nk%state_view() ; r => nk%residual_view()
               call heat(y(1,:,1,1,1), r(1,:,1,1,1), H)
            case(NK_REDUCE)                             ! with MPI: nk%sums(1:nk%nsums) summed over the ranks
            case(NK_PRECONDITION)                       ! not asked: initialize(preconditioned=.false.)
            endselect
            call nk%advance(bx)
         enddo
      endif
      call st%end_stage(k, bx, DT, q)
   enddo
   call st%finish_step(bx, DT, q)
   print '(I5,I10,I17,I15,I11,F17.10)', istep, nk%newton, nk%krylov, nk%reductions, nk%residuals, &
         q(NX/2 + 1) / 2._R_P + q(NX/2) / 2._R_P
enddo
call st%destroy ; call nk%destroy
text
$ cookbook_stiff krylov
 step  Newton steps  Krylov iterations  reductions  residuals      u(1/2)
    1         7               98            100        106     0.6664439177
    2         6               82             84         89     0.4723088467
    3         6               82             83         89     0.3257071439
    4         5               66             68         72     0.2242185766
    5         5               66             68         72     0.1543358606
  • The requests. The solver asks; you answer, then advance(bx). NK_RESIDUAL: R at state_view() into residual_view() (the stage state and its register for F, or the perturbed state U+εz and the solver's own register). NK_REDUCE: sum sums(1:nsums) over your boxes and ranks. NK_PRECONDITION: precondition_out() =M−1 precondition_in() for a right preconditioner of yours (initialize(preconditioned=.true.); its vector's norm costs one more reduction per iteration). NK_DONE: ‖F‖≤ atol + rtol ‖U‖; the stage state is U and its register R(U).
  • One reduction per Krylov iteration. Classical Gram–Schmidt fuses the j+1 inner products of the new vector with the basis and its own norm into one reduction; the new vector's norm then follows from Pythagoras. Where the new vector keeps less than a tenth of its norm, Pythagoras loses two digits, and a second pass (one more reduction) reorthogonalises it: 2 iterations in 816 in selah_test_newton_krylov; above, the reductions less the Krylov iterations.
  • The memory. m+4 registers of the field's size: the basis, the preconditioned vector, the perturbed state and its residual. A Krylov iteration streams the residual's inputs, 2+j registers for the inner products and j+1 for the orthogonalisation.
  • Its accuracy. selah_test_newton_krylov: the 2-D heat equation, 642 cells, bdf-2 at 100 times the explicit bound, 5 steps: within 6⋅10−13 of a dense direct solve (numpy's LU of the 4096×4096 system, notes/heat-bdf2-reference.py), unpreconditioned and with a Jacobi preconditioner of the caller's.

In the Nordsieck form ​

The Nordsieck form's BDF (nordsieck_stepper, NORDSIECK_BDF: Schemes) has the same implicit stage, y=E+γR(y), from its stage 2: stage_gamma(2) is l0, explicit_part() is E, and any of the solvers above takes it, with the step and order free to change between steps. selah_test_newton_krylov runs the heat equation above by its BDF of order 2 and newton_krylov: the same discrete solution as the stepper's bdf-2, within 8⋅10−14 of the dense direct solve.

Which solver ​

The stiff termSolverPer iteration
couples the components of a cell only, Jacobian at handnewton_solver, NEWTON_BLOCKone residual, your blocks, an LU per cell
each component on its ownnewton_solver, NEWTON_DIAGONALtwo residuals
couples the cells, moderately stiff (γλ up to tens)anderson_mixingone residual, one reduction
couples the cells, far beyond the explicit boundnewton_krylovone residual, one reduction