Stiff terms
The implicit multistep schemes (am-k, bdf-k) advance iterations=2, stage 2 is the scheme's only implicit stage: you solve
Newton, cell by cell
A relaxation, a chemical source or a collision term couples the components of one cell only: the Jacobian of newton_solver makes the Newton step 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
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$ 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 writejac(cell, v, w), 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, , you evaluate there too, and updatetakesfrom the difference quotient: two residuals per iteration, no Jacobian to write. - The convergence test.
updatereturns its box's largest. With several boxes or ranks you reduce it (a maximum) before converged(dmax), which isdmax <= 1and stops the run aftermax_iterations. - The first guess is yours:
above. The stepper's own first iterate, , is far from the solution of a stiff cell. - The stage's residual. After the last update, evaluate
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-2on a 3-component relaxation withfrom 1 to gives the closed-form BDF step within in at most 2 iterations (4 with a cubic coupling), both layouts; the diagonal form on Prothero–Robinson at the trapezoid's closed form, 2 iterations. What is left is the update's own rounding, : cancels two terms of size . - Backward Euler is first order.
above against 0.7158 (Hairer & Wanner's reference): the steps near are 13 long. bdf-2and up keep a history: write its levels and callresume_history(dt)before the first step (The history), asselah_test_newtondoes.
The diagonal form
When each component's term depends on itself only (a radiative cooling, a pointwise decay), NEWTON_DIAGONAL takes perturb writes, and you write no Jacobian. Radiative cooling,
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$ 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 3The first step starts Newton from
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
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$ 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
and in the stage's register, residualmakes, the residual 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, andconverged(sums)isatol+rtol. Otherwise mixwrites the next iterate into the stage state, from the least-squares solve on the host (thenormal equations). - The stage's residual is already there when it converges:
of the last iterate, and the stepper's update is , 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_DEPTHis 16, atregisters of the field's size. - Its accuracy.
selah_test_fixed_point:am-1at= -2, -5, -20 (the plain iteration diverges) and a spectrum from -0.5 to -20 give the implicit step within 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 NK_MAX_RESTART = 16 iterations) is one Newton step, and a cycle stops early when its residual estimate falls below forcing (default
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$ 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:at state_view()intoresidual_view()(the stage state and its register for, or the perturbed state and the solver's own register). NK_REDUCE: sumsums(1:nsums)over your boxes and ranks.NK_PRECONDITION:precondition_out()precondition_in()for a right preconditioner of yours (initialize(preconditioned=.true.); its vector's norm costs one more reduction per iteration).NK_DONE:atol+rtol; the stage state is and its register . - One reduction per Krylov iteration. Classical Gram–Schmidt fuses the
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.
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, registers for the inner products and for the orthogonalisation. - Its accuracy.
selah_test_newton_krylov: the 2-D heat equation,cells, bdf-2at 100 times the explicit bound, 5 steps: withinof a dense direct solve (numpy's LU of the 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, stage_gamma(2) is explicit_part() is 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
Which solver
| The stiff term | Solver | Per iteration |
|---|---|---|
| couples the components of a cell only, Jacobian at hand | newton_solver, NEWTON_BLOCK | one residual, your blocks, an LU per cell |
| each component on its own | newton_solver, NEWTON_DIAGONAL | two residuals |
| couples the cells, moderately stiff ( | anderson_mixing | one residual, one reduction |
| couples the cells, far beyond the explicit bound | newton_krylov | one residual, one reduction |