Skip to content

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 points i=1…N reads 1+LO…N+HI: max(0,−LO) ghosts on the left, max(0,HI) on the right.
  • A face operator (face, face_dN) at the face between i and i+1 reads the same offsets. The N+1 faces of cells 1…N are i=0…N, so the reach is LO…N+HI: max(0,1−LO) ghosts on the left, max(0,HI) on the right.
  • An upwind pair (_ur and _ul of 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): 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)

Operator1234567891012
fd_face_c12345
fd_d1_c12345
fd_d2_c12345
fd_d3_c2345
fd_d4_c2345
fd_d5_c345
fd_d6_c345
fd_face_d1_c12345
fd_face_d2_c2345
fd_face_d3_c2345
fd_face_d4_c345
fd_face_d5_c345
fd_face_ur0 / 10 / 20 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fd_face_ul1 / 02 / 03 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fd_face_br1 / 22 / 33 / 44 / 5
fd_face_bl2 / 13 / 24 / 35 / 4
fv_face_c12345
fv_face_d1_c12345
fv_face_d2_c2345
fv_face_d3_c2345
fv_face_d4_c345
fv_face_d5_c345
fv_d1_c12345
fv_d2_c12345
fv_d3_c2345
fv_d4_c2345
fv_d5_c345
fv_d6_c345
fv_face_ur0 / 10 / 20 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_face_ul1 / 02 / 03 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_face_d1_ur0 / 20 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_face_d1_ul2 / 03 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_face_d2_ur0 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_face_d2_ul3 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_face_d3_ur0 / 40 / 50 / 60 / 70 / 80 / 9
fv_face_d3_ul4 / 05 / 06 / 07 / 08 / 09 / 0
fv_face_d4_ur0 / 50 / 60 / 70 / 80 / 9
fv_face_d4_ul5 / 06 / 07 / 08 / 09 / 0
fv_face_d5_ur0 / 60 / 70 / 80 / 9
fv_face_d5_ul6 / 07 / 08 / 09 / 0
fv_d1_ur0 / 10 / 20 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_d1_ul1 / 02 / 03 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_d2_ur0 / 20 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_d2_ul2 / 03 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_d3_ur0 / 30 / 40 / 50 / 60 / 70 / 80 / 9
fv_d3_ul3 / 04 / 05 / 06 / 07 / 08 / 09 / 0
fv_d4_ur0 / 40 / 50 / 60 / 70 / 80 / 9
fv_d4_ul4 / 05 / 06 / 07 / 08 / 09 / 0
fv_d5_ur0 / 50 / 60 / 70 / 80 / 9
fv_d5_ul5 / 06 / 07 / 08 / 09 / 0
fv_d6_ur0 / 60 / 70 / 80 / 9
fv_d6_ul6 / 07 / 08 / 09 / 0
fv_face_br1 / 22 / 33 / 44 / 5
fv_face_bl2 / 13 / 24 / 35 / 4
fv_point_c12345
fd_average_c12345
fd_face_diss_c12345
fv_face_diss_c12345
fd_face_diss_br2345
fd_face_diss_bl2345
fv_face_diss_br2345
fv_face_diss_bl2345
fd_filter_c12345
fv_filter_c12345

An upwind pair (both sides of a face or cell, the side chosen per point by the flow)

Operator1234567891012
fd_face_ur + _ul123456789
fv_face_ur + _ul123456789
fv_face_d1_ur + _ul23456789
fv_face_d2_ur + _ul3456789
fv_face_d3_ur + _ul456789
fv_face_d4_ur + _ul56789
fv_face_d5_ur + _ul6789
fv_d1_ur + _ul123456789
fv_d2_ur + _ul23456789
fv_d3_ur + _ul3456789
fv_d4_ur + _ul456789
fv_d5_ur + _ul56789
fv_d6_ur + _ul6789

The 3-D operators (per axis, along the axis only: no edge or corner ghosts)

Operator1234567891012
fd_gradient_c12345
fd_divergence_c12345
fd_curl_c12345
fd_laplacian_c12345
fv_gradient_c12345
fv_divergence_c12345
fv_curl_c12345
fv_laplacian_c12345

Two consequences. A centred node or cell kernel of order p needs p/2 ghosts for d1 and d2, one more for d3 and d4, two more for d5 and d6. A one-sided kernel needs none on the side away from its stencil, but the pair a solver uses needs the full width on both.

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 0…N, on a periodic grid; the halo is declared from the window bounds, and the ghosts are copies from the other end of the domain:

fortran
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_ghosts
text
nodes: 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 32

Both 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.

Released under your choice of GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT.