Skip to content

Write your own step from the flat kernels ​

The stepper runs the catalogue's schemes on a field with ghost cells and a box. For anything else (a scheme of your own, a state with no ghost cells, a loop the stepper does not drive) the flat kernels do one stage operation over a whole contiguous rank-1 array, with the stepper's summation order and the same device placement: no box, no mask. The operations: Kernels.

The recipe on the oscillator x′=v, v′=−x as a flat array y=(x,v), to t=1, with four hand-written steps:

fortran
do step=1, nsteps                                           ! ssp-22: u = y + dt F(y); y = (y + u + dt F(u)) / 2
   call selah_copy(y, u)
   call selah_axpy(dt, f(y), u)
   call selah_axpy(dt, f(u), u)
   call selah_lincomb(0.5_R_P, u, 0.5_R_P, y)               ! y = 0.5 u + 0.5 y
enddo
fortran
r2 = shu_osher_2r_form(stages=4, a=[0._R_P, 0._R_P, 2._R_P/3, 0._R_P], b=[1._R_P, 1._R_P, 1._R_P/3, 1._R_P], &
                       c=[0.5_R_P, 0.5_R_P, 1._R_P/6, 0.5_R_P])  ! the SSP(4,3) of the forms recipe
do step=1, nsteps
   call selah_copy(y, y0)                                   ! u^n: the second register
   do k=1, r2%stages
      r = f(y)                                              ! the residual of the current stage, y itself
      call selah_shu_osher_2r_stage(dt, r2%a(k), r2%b(k), r2%c(k), y0, r, y)
   enddo
enddo
fortran
w = williamson_2n(SCHEME_LSRK_33)
do step=1, nsteps
   do k=1, w%stages                                         ! dqr = A_k dqr + dt F(y); y = y + B_k dqr
      r = f(y)
      call selah_williamson_2n_stage(dt, w%a(k), w%b(k), r, dqr, y)  ! A_1 = 0: dqr not read at stage 1
   enddo
enddo
fortran
! the start-up, one step of euler or ssp-22 in the two-register form; then h holds U^0 and y U^1
if (method == 4) then ; r2 = shu_osher_2r(SCHEME_EULER) ; else ; r2 = shu_osher_2r(SCHEME_SSP_22) ; endif
call selah_copy(y, h)
do k=1, r2%stages
   r = f(y)
   call selah_shu_osher_2r_stage(dt, r2%a(k), r2%b(k), r2%c(k), h, r, y)
enddo
lf = leapfrog(SCHEME_LEAPFROG)                              ! nu = 0: no filter (leapfrog-raw: nu, alpha)
do step=2, nsteps                                           ! U^{n+1} = U^{n-1} + 2 dt F(U^n), filtered
   r = f(y)
   call selah_leapfrog_step(dt, lf%nu, lf%alpha, h, r, y)   ! h: filtered U^{n-1} in, U^n out
enddo
text
$ cookbook_own_step
method                  error, 16 steps    32 steps    64 steps  observed orders
ssp-22 (axpy, lincomb)     5.308E-04   1.349E-04   3.398E-05   1.98   1.99
SSP(4,3) (two-register)    4.174E-06   5.285E-07   6.647E-08   2.98   2.99
lsrk-33 (Williamson 2N)    8.274E-06   1.052E-06   1.327E-07   2.97   2.99
leapfrog, euler start      2.001E-03   4.992E-04   1.247E-04   2.00   2.00
leapfrog, ssp-22 start     5.489E-04   1.370E-04   3.424E-05   2.00   2.00
[exit status 0]

A multistep scheme of the catalogue needs no hand-written start-up: the stepper's multistep form starts it with a Runge–Kutta scheme of order p−1 and keeps the history (Stepper: the history). The leapfrog above is written by hand to show the kernels.

KernelOperationHere
selah_copy(x, y)y=xun into the second register
selah_axpy(alpha, x, y)y=y+αxa forward-Euler step
selah_lincomb(alpha, x, beta, y)y=αx+βythe convex average closing ssp-22
selah_shu_osher_2r_stage(dt, a, b, c, q0, r, q)q=aq0+bq+Δtcrany two-register scheme: SSP(4,3) of Coefficient forms, the leapfrog's start-up
selah_williamson_2n_stage(dt, a, b, r, dqr, q)ΔQ=aΔQ+Δtr; q=q+bΔQwilliamson_2n(SCHEME_LSRK_33)
selah_leapfrog_step(dt, nu, alpha, h, r, q)Un+1=Un−1+2Δtr, filteredthe leapfrog; h holds the history
  • Each scheme reaches its order: 2, 3, 3, and 2 for the leapfrog with either start-up.
  • The leapfrog's start-up is yours here: one step of shu_osher_2r(SCHEME_EULER) or shu_osher_2r(SCHEME_SSP_22), after which h holds U0. Both keep order 2; the ssp-22 start (the stepper's) has an error 3.6 times smaller.
  • leapfrog(SCHEME_LEAPFROG) has ν=0, the plain leapfrog; pass the nu and alpha of leapfrog_filter (leapfrog(SCHEME_LEAPFROG_RAW), or your own) for the Robert–Asselin–Williams filter. The filter's effect on the orders: Schemes.
  • In a device build every array argument must be device resident.

An implicit step with your own solver ​

The implicit multistep schemes run by fixed-point stages, which converge only where |γλ|<1. On a stiff problem, solve the implicit equation yourself: in a fixed-point stage, explicit_part_view(e) is E and dt * stage_gamma(k) is γ; write the solution of U=E+γR(U) into the stage state, then evaluate the residual there as usual. With iterations=2, stage 2 is the last one, and the update E+γR(U) returns your U. The recipe solves Prothero–Robinson, u′=−λ(u−sin⁡t)+cos⁡t with λΔt=100, by am-1 (the trapezoid rule), whose implicit equation is linear here (one Newton step), and runs the stepper's own two iterations beside it:

fortran
real(R_P), parameter :: LAMBDA = 1.e4_R_P, DT = 0.01_R_P
type(stepper)        :: st
type(box)            :: bx
real(R_P), target    :: q(0:2)                          ! one cell, one ghost cell each side
real(R_P), pointer   :: r(:), y(:), e(:)
real(R_P)            :: t, gam
integer(I4P)         :: n, k, how

do how=1, 2                                             ! 1: the caller's Newton; 2: the stepper's iterations
   call st%initialize('am-1', FORM_MULTISTEP, lb=[0_I4P], ub=[2_I4P], components=.false., iterations=2_I4P)
   bx = box_interior(lb=[0_I4P], ub=[2_I4P], ngc=1_I4P, components=.false.)
   if (.not. associated(st%explicit_part())) error stop 'newton: E is null' ! null for an explicit scheme
   q = 0._R_P ; t = 0._R_P
   do n=1, 100
      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)
            if (how == 1) then                          ! solve Y = E + gamma R(Y): linear here, one Newton step
               call st%explicit_part_view(e)
               gam = DT * st%stage_gamma(k)
               y(1) = (e(1) + gam * (LAMBDA * sin(t + DT) + cos(t + DT))) / (1._R_P + gam * LAMBDA)
            endif
         endif
         r(1) = -LAMBDA * (y(1) - sin(t + st%stage_time(k) * DT)) + cos(t + st%stage_time(k) * DT)
         call st%end_stage(k, bx, DT, q)
      enddo
      call st%finish_step(bx, DT, q)
      t = t + DT
      if (abs(q(1)) > 1.e10_R_P) exit
   enddo
   if (how == 1) then
      print '(A,ES10.2)', 'am-1, the caller''s Newton in stage 2: |u - sin t| at t = 1:', abs(q(1) - sin(t))
   else
      print '(A,I4,A)', 'am-1, two fixed-point iterations: |u| > 1e10 after', n, ' steps (|gamma lambda| = 50)'
   endif
   call st%destroy
enddo
text
$ cookbook_implicit newton
am-1, the caller's Newton in stage 2: |u - sin t| at t = 1:  4.35E-10
am-1, two fixed-point iterations: |u| > 1e10 after   6 steps (|gamma lambda| = 50)
  • The Newton step keeps the trapezoid rule's A-stability: the error at t=1 is that of the scheme. The fixed-point iterations, |γλ|=50, diverge within a few steps, as FOODIE's do.
  • A nonlinear residual needs Newton or Krylov iterations. SELAH gives E and γ and evaluates nothing itself (P2: the residual is yours); when the stiff part couples the components of a cell only, newton_solver makes the Newton iterations cell by cell, on your residual and Jacobian (Stiff terms).
  • Any implicit multistep scheme works the same way; bdf-<k> is the usual choice for stiff problems (the implicit multistep schemes).