Ghost cells for a window
Problem. Size the halo of an array (the ghost cells or nodes beyond the domain, filled by a boundary condition or by a neighbour's data) so that every call of a kernel reads data that exists.
The rule
Every kernel declares its window as an explicit-shape dummy q(LO:HI), by offset from the point of the result (Conventions). The caller passes q(i+LO:i+HI), so the reach of a loop follows from the window:
- A node or cell operator (
fd_dN,fv_dN,fv_point,fd_average) at the pointsreads : ghosts on the left, on the right. - A face operator (
face,face_dN) at the face betweenand reads the same offsets. The faces of cells are , so the reach is : ghosts on the left, on the right. - An upwind pair (
_urand_ulof the same order, the side chosen per point by the flow) needs the larger of the two on each side: the same number on both. - A 3-D operator reads one stencil per axis through the point,
(-S:S):ghosts per axis, and none on the edges or corners of a block.
Two different mistakes, two different outcomes. A halo too thin makes the last sections of a loop leave the array: a checked build (-fcheck=bounds, -C, -check all) stops there with a bounds error. A section of the wrong length is not caught: the explicit-shape dummy takes the section's elements in order, so u(i-3:i+3) passed to a kernel whose window is q(-4:4) is read as if it started at offset −4, and the result is that of node i+1, silently, even with -fcheck=all (gfortran 16.2); only a constant-length section of the wrong size fails, at compile time. The window bounds of each kernel are the caller's responsibility: write the section from the kernel's own LO and HI, as the code below does with named constants.
The table
Ghosts per side for every operator and order, computed by docs/scripts/make_cookbook_tables.py from the q(LO:HI) each compiled kernel declares (src/lib/*.F90) with the rule above; left / right where the sides differ:
Every operator, by order (left / right when they differ; blank: no kernel of that order)
| Operator | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|
fd_face_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_d1_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_d2_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_d3_c | 2 | 3 | 4 | 5 | |||||||
fd_d4_c | 2 | 3 | 4 | 5 | |||||||
fd_d5_c | 3 | 4 | 5 | ||||||||
fd_d6_c | 3 | 4 | 5 | ||||||||
fd_face_d1_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_face_d2_c | 2 | 3 | 4 | 5 | |||||||
fd_face_d3_c | 2 | 3 | 4 | 5 | |||||||
fd_face_d4_c | 3 | 4 | 5 | ||||||||
fd_face_d5_c | 3 | 4 | 5 | ||||||||
fd_face_ur | 0 / 1 | 0 / 2 | 0 / 3 | 0 / 4 | 0 / 5 | 0 / 6 | 0 / 7 | 0 / 8 | 0 / 9 | ||
fd_face_ul | 1 / 0 | 2 / 0 | 3 / 0 | 4 / 0 | 5 / 0 | 6 / 0 | 7 / 0 | 8 / 0 | 9 / 0 | ||
fd_face_br | 1 / 2 | 2 / 3 | 3 / 4 | 4 / 5 | |||||||
fd_face_bl | 2 / 1 | 3 / 2 | 4 / 3 | 5 / 4 | |||||||
fv_face_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_face_d1_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_face_d2_c | 2 | 3 | 4 | 5 | |||||||
fv_face_d3_c | 2 | 3 | 4 | 5 | |||||||
fv_face_d4_c | 3 | 4 | 5 | ||||||||
fv_face_d5_c | 3 | 4 | 5 | ||||||||
fv_d1_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_d2_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_d3_c | 2 | 3 | 4 | 5 | |||||||
fv_d4_c | 2 | 3 | 4 | 5 | |||||||
fv_d5_c | 3 | 4 | 5 | ||||||||
fv_d6_c | 3 | 4 | 5 | ||||||||
fv_face_ur | 0 / 1 | 0 / 2 | 0 / 3 | 0 / 4 | 0 / 5 | 0 / 6 | 0 / 7 | 0 / 8 | 0 / 9 | ||
fv_face_ul | 1 / 0 | 2 / 0 | 3 / 0 | 4 / 0 | 5 / 0 | 6 / 0 | 7 / 0 | 8 / 0 | 9 / 0 | ||
fv_face_d1_ur | 0 / 2 | 0 / 3 | 0 / 4 | 0 / 5 | 0 / 6 | 0 / 7 | 0 / 8 | 0 / 9 | |||
fv_face_d1_ul | 2 / 0 | 3 / 0 | 4 / 0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 8 | 0 / 9 | |||||
fv_d4_ul | 4 / 0 | 5 / 0 | 6 / 0 | 7 / 0 | 8 / 0 | 9 / 0 | |||||
fv_d5_ur | 0 / 5 | 0 / 6 | 0 / 7 | 0 / 8 | 0 / 9 | ||||||
fv_d5_ul | 5 / 0 | 6 / 0 | 7 / 0 | 8 / 0 | 9 / 0 | ||||||
fv_d6_ur | 0 / 6 | 0 / 7 | 0 / 8 | 0 / 9 | |||||||
fv_d6_ul | 6 / 0 | 7 / 0 | 8 / 0 | 9 / 0 | |||||||
fv_face_br | 1 / 2 | 2 / 3 | 3 / 4 | 4 / 5 | |||||||
fv_face_bl | 2 / 1 | 3 / 2 | 4 / 3 | 5 / 4 | |||||||
fv_point_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_average_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_face_diss_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_face_diss_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_face_diss_br | 2 | 3 | 4 | 5 | |||||||
fd_face_diss_bl | 2 | 3 | 4 | 5 | |||||||
fv_face_diss_br | 2 | 3 | 4 | 5 | |||||||
fv_face_diss_bl | 2 | 3 | 4 | 5 | |||||||
fd_filter_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_filter_c | 1 | 2 | 3 | 4 | 5 |
An upwind pair (both sides of a face or cell, the side chosen per point by the flow)
| Operator | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|
fd_face_ur + _ul | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ||
fv_face_ur + _ul | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ||
fv_face_d1_ur + _ul | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |||
fv_face_d2_ur + _ul | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ||||
fv_face_d3_ur + _ul | 4 | 5 | 6 | 7 | 8 | 9 | |||||
fv_face_d4_ur + _ul | 5 | 6 | 7 | 8 | 9 | ||||||
fv_face_d5_ur + _ul | 6 | 7 | 8 | 9 | |||||||
fv_d1_ur + _ul | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ||
fv_d2_ur + _ul | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |||
fv_d3_ur + _ul | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ||||
fv_d4_ur + _ul | 4 | 5 | 6 | 7 | 8 | 9 | |||||
fv_d5_ur + _ul | 5 | 6 | 7 | 8 | 9 | ||||||
fv_d6_ur + _ul | 6 | 7 | 8 | 9 |
The 3-D operators (per axis, along the axis only: no edge or corner ghosts)
| Operator | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|
fd_gradient_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_divergence_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_curl_c | 1 | 2 | 3 | 4 | 5 | ||||||
fd_laplacian_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_gradient_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_divergence_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_curl_c | 1 | 2 | 3 | 4 | 5 | ||||||
fv_laplacian_c | 1 | 2 | 3 | 4 | 5 |
Two consequences. A centred node or cell kernel of order
Code
A node loop with gideon_fd_d1_c6 (window q(-3:3)), and a face loop of the upwind pair gideon_fv_face_ul3 (q(-2:0)) and gideon_fv_face_ur3 (q(1:3)) over the faces
program cookbook_ghosts
!< Cookbook, ghost cells: the halo of an array derived from the windows of the kernels it feeds, filled periodically.
!< A node loop (gideon_fd_d1_c6, window q(-3:3)) and a face loop of upwind values (gideon_fv_face_ul3, q(-2:0), and
!< gideon_fv_face_ur3, q(1:3)), whose flux difference is the cell kernel gideon_fv_d1_ul3, bitwise.
use gideon
implicit none
integer(I4P), parameter :: N = 32 ! nodes or cells 1..N
real(R_P), parameter :: PI = acos(-1._R_P)
! node loop: the window of gideon_fd_d1_c6 is q(-3:3); node i reads i-3..i+3, so nodes 1..N read -2..N+3
integer(I4P), parameter :: D1_LO = -3, D1_HI = 3
real(R_P) :: u(1+D1_LO:N+D1_HI), du(N)
! face loop: face i+1/2 (i = 0..N) reads cells i-2..i (left side) or i+1..i+3 (right side): cells -2..N+3
integer(I4P), parameter :: UL_LO = -2, UR_HI = 3
real(R_P) :: a(UL_LO:N+UR_HI), fl(0:N), fr(0:N), dl
real(R_P) :: h, x
integer(I4P) :: i, same
h = 1._R_P / N
! point values at the nodes x = (i - 1) h, then the ghosts by periodicity
do i=1, N
u(i) = sin(2*PI*(i - 1)*h)
enddo
u(1+D1_LO:0) = u(N+1+D1_LO:N) ! left ghosts: the last -D1_LO nodes
u(N+1:N+D1_HI) = u(1:D1_HI) ! right ghosts: the first D1_HI nodes
do i=1, N
call gideon_fd_d1_c6(u(i+D1_LO:i+D1_HI), h, du(i))
enddo
print '(A,I0,A,I0,A,ES10.3)', 'nodes: halo ', -D1_LO, ' + ', D1_HI, ', max error of d1 ', &
maxval(abs(du - [(2*PI*cos(2*PI*(i - 1)*h), i=1, N)]))
! cell averages over [(i - 1) h, i h], then the ghosts by periodicity
do i=1, N
a(i) = (cos(2*PI*(i - 1)*h) - cos(2*PI*i*h))/(2*PI*h)
enddo
a(UL_LO:0) = a(N+UL_LO:N)
a(N+1:N+UR_HI) = a(1:UR_HI)
do i=0, N
call gideon_fv_face_ul3(a(i-2:i), fl(i)) ! the upwind value for a flow to the right
call gideon_fv_face_ur3(a(i+1:i+3), fr(i)) ! the upwind value for a flow to the left
enddo
x = 0._R_P
same = 0
do i=1, N
x = max(x, abs(fl(i) - sin(2*PI*i*h)), abs(fr(i) - sin(2*PI*i*h)))
call gideon_fv_d1_ul3(a(i-3:i), h, dl) ! the cell kernel reads cells i-3..i: the same halo on the left
if (dl == (fl(i) - fl(i-1))/h) same = same + 1
enddo
print '(A,I0,A,I0,A,ES10.3)', 'faces: halo ', -UL_LO + 1, ' + ', UR_HI, ', max error of the face values ', x
print '(A,I0,A,I0)', 'flux difference of the left-side faces = gideon_fv_d1_ul3, bitwise, in cells: ', same, ' of ', N
endprogram cookbook_ghostsnodes: halo 3 + 3, max error of d1 2.553E-06
faces: halo 3 + 3, max error of the face values 1.883E-03
flux difference of the left-side faces = gideon_fv_d1_ul3, bitwise, in cells: 32 of 32Both loops need 3 ghosts on each side, as the table says (fd_d1_c at order 6, fv_face_ur + _ul at order 3). The flux difference of the left-side faces, (f(i+1/2) − f(i−1/2))/h, equals the cell kernel gideon_fv_d1_ul3 in every cell, bitwise: the cell kernel computes the two face sums the same way and in the same order.
Reference. The windows: Conventions, every kernel's in Operators; a wrong window (ADAM's audit item B): Replace ADAM's FDV calls.