4. The stepper
Chapter 1 wrote the step by hand. On a block-structured field, the step also has to leave room for what a real solver does between stages: fill the ghost cells of the stage state (exchange between blocks, AMR seams, physical boundaries) and evaluate the residual. The stepper (type(stepper)) owns the registers and the coefficients; you call it around your own work, one stage at a time. It never calls back.
call st%begin_step(bx, q)
do k=1, st%stages()
call st%begin_stage(k, bx, dt, q) ! the stepper builds the state of stage k
u => st%stage_state() ! Butcher form: its own register
r => st%rhs_register() ! where the residual of stage k goes
call fill_ghosts(u) ! the caller: ghost cells of the stage state
call residual(u, r, dx) ! the caller: r = -du/dx
call st%end_stage(k, bx, dt, q)
enddo
call st%finish_step(bx, dt, q) ! q^{n+1}begin_step(bx, q)opens the step. Forms that need a copy oftake it here. begin_stage(k, bx, dt, q)builds the state of stagek. In the Butcher form it goes into the stepper's own register, whichstage_state()returns.- Between the two stage calls, the stage is yours:
- fill the ghost cells of the stage state;
- write its residual into
rhs_register().
end_stage(k, bx, dt, q)closes the stage; the in-place forms (chapter 5) advanceqhere.finish_step(bx, dt, q)closes the step; the Butcher form computeshere.
The setup and the caller's two routines:
allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB)) ! (v,i,j,k,b): 1 component, NI x 1 x 1 cells, 1 ghost
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
call st%initialize(scheme, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))subroutine fill_ghosts(u)
!< Periodic ghost cells along i: each block's ghosts from its neighbours.
real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
integer(I4P) :: b
do b=1, NB
u(1,0,1,1,b) = u(1,NI,1,1,1 + modulo(b-2, NB))
u(1,NI+1,1,1,b) = u(1,1,1,1,1 + modulo(b, NB))
enddo
endsubroutine fill_ghosts
subroutine residual(u, r, dx)
!< Central flux form: r_i = -((u_i + u_{i+1})/2 - (u_{i-1} + u_i)/2)/dx.
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: dx
integer(I4P) :: b, i
real(R_P) :: d
do b=1, NB
do i=1, NI
call gideon_fd_d1_c2([u(1,i-1,1,1,b), u(1,i,1,1,b), u(1,i+1,1,1,b)], dx, d) ; r(1,i,1,1,b) = -d
enddo
enddo
endsubroutine residualinitialize(scheme, form, lb, ub)takes the bounds of the field, ghost cells included, and allocates the registers with them through FUNDAL: host memory here, device memory in a device build (chapter 9).- The box describes the field: one component, 16 × 1 × 1 cells per block, one ghost cell, 4 blocks.
Running it
$ stepper
steps 8: error 8.933E-06
steps 16: error 5.581E-07
steps 32: error 3.488E-08
observed orders: 4.00 4.00Fourth order through the stepper, as by hand.
The pointers are valid for one stage
stage_state() and rhs_register() are pointers into the stepper's registers. Take them after begin_stage(k) and use them before end_stage(k): the next stage uses other registers.
What you learned
The four calls of a step, and who does what: the stepper builds stage states and updates; the caller fills ghosts and writes residuals. Reference: Stepper, the stage contract.
A field of any rank
The field so far is rank 5: components, three cell indices and blocks. A 1-D, 2-D or 3-D solver holds fields of rank 1 to 4, and the stepper takes them at their own rank. SELAH views such a field as rank 5 with singleton dimensions, so the kernels are the same and nothing is copied. The box and the stepper are built from the field's own bounds, and components says whether its first dimension holds components:
! a scalar 1-D field q(i), one ghost cell each side: the box of its interior, viewed as rank 5 (1, i, 1, 1, 1)
allocate(q(1-NGC:N+NGC))
bx = box_interior(lb=[1-NGC], ub=[N+NGC], ngc=NGC, components=.false.)
print '(A,5I3)', 'rank 1, lo as rank 5:', bx%lo
print '(A,5I3)', 'rank 1, hi as rank 5:', bx%hi
call st%initialize(SCHEME_SSP_33, FORM_SHU_OSHER_2R, lb=[1-NGC], ub=[N+NGC], components=.false.)! two components on a 2-D grid, w(v, i, j): viewed as rank 5 (v, i, j, 1, 1)
allocate(w(1:NV, 1-NGC:N+NGC, 1-NGC:N+NGC))
bx3 = box_interior(lb=[1, 1-NGC, 1-NGC], ub=[NV, N+NGC, N+NGC], ngc=NGC, components=.true.)
print '(A,5I3)', 'rank 3, hi as rank 5:', bx3%hi
call st3%initialize(SCHEME_RK4, FORM_BUTCHER, lb=lbound(w), ub=ubound(w), components=.true.)The step is the one of this chapter, at the field's rank. stage_state() and rhs_register() keep their rank-5 result. At rank 1 to 4 the pointers come from stage_state_view(u) and rhs_register_view(r), with the field's bounds:
do step=1, nint(T_END/DT)
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 ; u => q ; else ; call st%stage_state_view(u) ; endif
call st%rhs_register_view(r) ! rank-1 pointers, with the field's bounds
r(1:N) = -u(1:N)**2 ! yours: y' = -y^2, cell by cell
call st%end_stage(k, bx, DT, q)
enddo
call st%finish_step(bx, DT, q)
enddoThe same field declared rank 5 gives the same values, bit for bit:
! the same 1-D field declared rank 5 (1, i, 1, 1, 1): the same values, bit for bit
allocate(q5(1:1, 1-NGC:N+NGC, 1:1, 1:1, 1:1))
q5(1,:,1,1,1) = [(1._R_P + 0.1_R_P * i, i=1-NGC, N+NGC)]
bx5 = box_range(lb=[1, 1-NGC, 1, 1, 1], lo=[1, 1, 1, 1, 1], hi=[1, N, 1, 1, 1], components=.true.)
call st5%initialize(SCHEME_SSP_33, FORM_SHU_OSHER_2R, lb=lbound(q5), ub=ubound(q5))
do step=1, nint(T_END/DT)
call st5%begin_step(bx5, q5)
do k=1, st5%stages()
call st5%begin_stage(k, bx5, DT, q5)
u5 => q5 ; r5 => st5%rhs_register()
r5(1,1:N,1,1,1) = -u5(1,1:N,1,1,1)**2
call st5%end_stage(k, bx5, DT, q5)
enddo
call st5%finish_step(bx5, DT, q5)
enddo
print '(A,L1)', 'rank 1 bitwise rank 5: ', all(q == q5(1,:,1,1,1))$ ranks
rank 1, lo as rank 5: 1 1 1 1 1
rank 1, hi as rank 5: 1 6 1 1 1
rank 3, hi as rank 5: 2 6 6 1 1
rank 1, q(3) at t = 1: 0.565207589
exact 1/(1/q0 + t): 0.565217391
rank 3, w(2,1,1) at t = 1: 0.367879461 exp(-1): 0.367879441
rank 1 bitwise rank 5: TThe rank-1 field is viewed as (1, i, 1, 1, 1), the rank-3 field w(v, i, j) as (v, i, j, 1, 1). The whole mapping is in Kernels; the preconditions (a contiguous field, caller registers for rank 5 only) are in Stepper.
Next: 5. Low-storage forms.