Skip to content

T5 Gradient, divergence, curl ​

Goal. The gradient, Laplacian, divergence and curl at every node of a periodic n3 grid of point values: gather one contiguous stencil per direction (and per component) out of the 3-D arrays, call the operators, compare with the exact values, and observe orders 2, 4 and 6.

Concepts. 3-D operators and their place among the families: ADAM's FNL signature, one stencil per direction and component, gathered by the caller. T4 for the run-time order; A2 3-D operators for their verification.

The fields:

φ=sin⁡xsin⁡ysin⁡z,∇φ=(cos⁡xsin⁡ysin⁡z, sin⁡xcos⁡ysin⁡z, sin⁡xsin⁡ycos⁡z),∇2φ=−3φ,v=(sin⁡xcos⁡y, sin⁡ycos⁡z, sin⁡zcos⁡x),∇⋅v=cos⁡xcos⁡y+cos⁡ycos⁡z+cos⁡zcos⁡x,∇×v=(sin⁡ysin⁡z, sin⁡zsin⁡x, sin⁡xsin⁡y).

The gather ​

OperatorStencilsFrom the 3-D arrays at node (i,j,k)
gradient, Laplacianqx, qy, qz: the field along each axisphi(i-s:i+s,j,k), phi(i,j-s:j+s,k), phi(i,j,k-s:k+s)
divergencevx_x, vy_y, vz_z: each component along its own axisvx(i-s:i+s,j,k), vy(i,j-s:j+s,k), vz(i,j,k-s:k+s)
curlvy_x, vz_x, vx_y, vz_y, vx_z, vy_z: the off-axis componentsvy(i-s:i+s,j,k), vz(i-s:i+s,j,k), ...

vy_x is the y component along x. A stencil along x is contiguous in memory; along y and z it is strided. The operators take explicit-shape dummies (qx(-S:S)), so a strided section passed directly would be copied into a temporary by the compiler at every call, and in device code such a copy is what ADAM's FNL signature exists to avoid. The caller copies into small local arrays instead: the data movement is explicit, and one gather of φ serves both the gradient and the Laplacian.

The program ​

fortran
program operators_3d
!< Tutorial T5: gradient, Laplacian, divergence and curl of analytic fields on a periodic 3-D grid of point values,
!< from directional stencils gathered out of the 3-D arrays; orders 2, 4, 6 and their observed accuracy.
use gideon
implicit none
integer(I4P), parameter :: G = 3                 ! ghost nodes: the half-width of the order-6 stencils
integer(I4P), parameter :: ORDERS(3) = [2, 4, 6]
integer(I4P), parameter :: NS(3) = [16, 32, 64]  ! nodes per direction
real(R_P),    parameter :: PI = acos(-1._R_P)
real(R_P), allocatable  :: x(:)                  ! node coordinates, the same along x, y, z
real(R_P), allocatable  :: phi(:,:,:)            ! the scalar sin x sin y sin z
real(R_P), allocatable  :: vx(:,:,:), vy(:,:,:), vz(:,:,:) ! the vector (sin x cos y, sin y cos z, sin z cos x)
real(R_P)               :: qx(-G:G), qy(-G:G), qz(-G:G)     ! phi along x, y, z through the node
real(R_P)               :: vx_x(-G:G), vy_y(-G:G), vz_z(-G:G) ! each component along its own axis: the divergence
real(R_P)               :: vy_x(-G:G), vz_x(-G:G), vx_y(-G:G), vz_y(-G:G), vx_z(-G:G), vy_z(-G:G) ! the curl
real(R_P)               :: dxyz(3), grad(3), curl(3), div, lap, h, sx, sy, sz, cx, cy, cz
real(R_P)               :: err(4,3,3)            ! gradient, divergence, curl, Laplacian; order; grid
integer(I4P)            :: l, o, order, n, s, i, j, k

do l=1, 3
   n = NS(l)
   h = 2*PI / n
   dxyz = h
   allocate(x(-G:n-1+G))
   allocate(phi(-G:n-1+G,-G:n-1+G,-G:n-1+G))
   allocate(vx, vy, vz, mold=phi)
   x = [(i*h, i=-G, n-1+G)]
   do k=-G, n-1+G ; do j=-G, n-1+G ; do i=-G, n-1+G  ! the ghosts hold the fields too, as a halo exchange would
      phi(i,j,k) = sin(x(i)) * sin(x(j)) * sin(x(k))
      vx(i,j,k) = sin(x(i)) * cos(x(j))
      vy(i,j,k) = sin(x(j)) * cos(x(k))
      vz(i,j,k) = sin(x(k)) * cos(x(i))
   enddo ; enddo ; enddo
   do o=1, 3
      order = ORDERS(o)
      s = order / 2
      err(:,o,l) = 0._R_P
      do k=0, n-1 ; do j=0, n-1 ; do i=0, n-1
         ! gather: one contiguous stencil per direction (and component), the node (i,j,k) at offset 0
         qx(-s:s) = phi(i-s:i+s,j,k) ; qy(-s:s) = phi(i,j-s:j+s,k) ; qz(-s:s) = phi(i,j,k-s:k+s)
         vx_x(-s:s) = vx(i-s:i+s,j,k) ; vy_y(-s:s) = vy(i,j-s:j+s,k) ; vz_z(-s:s) = vz(i,j,k-s:k+s)
         vy_x(-s:s) = vy(i-s:i+s,j,k) ; vz_x(-s:s) = vz(i-s:i+s,j,k)
         vx_y(-s:s) = vx(i,j-s:j+s,k) ; vz_y(-s:s) = vz(i,j-s:j+s,k)
         vx_z(-s:s) = vx(i,j,k-s:k+s) ; vy_z(-s:s) = vy(i,j,k-s:k+s)
         call gideon_fd_gradient_c(order, qx(-s:s), qy(-s:s), qz(-s:s), dxyz, grad)
         call gideon_fd_laplacian_c(order, qx(-s:s), qy(-s:s), qz(-s:s), dxyz, lap)
         call gideon_fd_divergence_c(order, vx_x(-s:s), vy_y(-s:s), vz_z(-s:s), dxyz, div)
         call gideon_fd_curl_c(order, vy_x(-s:s), vz_x(-s:s), vx_y(-s:s), vz_y(-s:s), vx_z(-s:s), vy_z(-s:s), &
                               dxyz, curl)
         ! the exact values
         sx = sin(x(i)) ; sy = sin(x(j)) ; sz = sin(x(k)) ; cx = cos(x(i)) ; cy = cos(x(j)) ; cz = cos(x(k))
         err(1,o,l) = max(err(1,o,l), maxval(abs(grad - [cx*sy*sz, sx*cy*sz, sx*sy*cz])))
         err(2,o,l) = max(err(2,o,l), abs(div - (cx*cy + cy*cz + cz*cx)))
         err(3,o,l) = max(err(3,o,l), maxval(abs(curl - [sy*sz, sz*sx, sx*sy])))
         err(4,o,l) = max(err(4,o,l), abs(lap + 3*sx*sy*sz))
      enddo ; enddo ; enddo
   enddo
   deallocate(x, phi, vx, vy, vz)
enddo

print '(A)', 'periodic grid of n^3 nodes, point values, max error over the nodes'
print '(A)', 'order     n    gradient  divergence        curl   Laplacian'
do o=1, 3
   do l=1, 3
      print '(I5,I6,4ES12.3)', ORDERS(o), NS(l), err(:,o,l)
   enddo
enddo
print '(A)', 'observed orders, log2(e(n/2) / e(n))'
do o=1, 3
   do l=2, 3
      print '(I5,I6,4F12.2)', ORDERS(o), NS(l), log(err(:,o,l-1) / err(:,o,l)) / log(2._R_P)
   enddo
enddo
endprogram operators_3d
  • Ghosts. The fields are evaluated on the ghost nodes too, as a halo exchange would deliver them; G = 3 is the half-width of the order-6 stencils.
  • Run-time order. The local arrays are declared (-G:G) once; for order order the program fills and passes the sections qx(-s:s), s = order/2, to the dispatchers gideon_fd_<op>_c(order, ...): the windows of T4.
  • Errors. The maximum over the nodes, and over the components for the vector results.

Build and run ​

bash
fobis build --mode examples-gnu && ./exe-examples/operators_3d

or against the library:

bash
fobis build --mode static-gnu
gfortran -I lib/gnu/static/mod docs/examples/src/operators_3d.F90 lib/gnu/static/libgideon.a -o operators_3d
./operators_3d

Output ​

text
periodic grid of n^3 nodes, point values, max error over the nodes
order     n    gradient  divergence        curl   Laplacian
    2    16   2.550E-02   7.651E-02   2.550E-02   3.836E-02
    2    32   6.413E-03   1.924E-02   6.413E-03   9.626E-03
    2    64   1.606E-03   4.817E-03   1.606E-03   2.409E-03
    4    16   7.783E-04   2.335E-03   7.783E-04   7.819E-04
    4    32   4.932E-05   1.480E-04   4.932E-05   4.937E-05
    4    64   3.093E-06   9.279E-06   3.093E-06   3.094E-06
    6    16   2.542E-05   7.627E-05   2.542E-05   1.918E-05
    6    32   4.063E-07   1.219E-06   4.063E-07   3.051E-07
    6    64   6.383E-09   1.915E-08   6.383E-09   4.789E-09
observed orders, log2(e(n/2) / e(n))
    2    32        1.99        1.99        1.99        1.99
    2    64        2.00        2.00        2.00        2.00
    4    32        3.98        3.98        3.98        3.99
    4    64        4.00        4.00        4.00        4.00
    6    32        5.97        5.97        5.97        5.97
    6    64        5.99        5.99        5.99        5.99

What to notice ​

  • Every operator reaches its order: 2.00, 4.00 and 5.99 between 32 and 64 nodes per direction, the orders of the 1-D kernels they sum (each 3-D operator is the 1-D kernels in ADAM's order, x then y then z).
  • The divergence error is three times the gradient's: each of its three terms carries the error of one first derivative of a sine, and the three peak together where cos⁡x=cos⁡y=cos⁡z=1. The gradient and the curl have equal errors: each of their components is one first derivative of a sine times a product of two others.
  • The Laplacian at order 6 is more accurate than the gradient (4.8E-09 against 6.4E-09 at n=64), at order 2 less (2.4E-03 against 1.6E-03): the leading constants of the d2 and d1 rows differ, the orders do not.

Exercises ​

  1. Finite volumes: replace the fields by their cell averages (a product of 1-D averages; the average of sin over [x−h/2,x+h/2] is sin⁡x⋅sin⁡(h/2)/(h/2)) and call gideon_fv_gradient_c, ...; the exact answer is the cell average of the derivative.
  2. Order known at compile time: replace the dispatchers by gideon_fd_gradient_c6 and friends, and the sections by the whole local arrays declared (-3:3).
  3. Discrete identities: compute ∇φ at every node (ghosts included, by periodicity), then its curl. The result is round-off, not O(hp): centred first derivatives along different axes commute on a uniform periodic grid. Does ∇⋅(∇×v) vanish the same way?

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