Skip to content

T1 A derivative on a periodic grid ​

Goal. The first derivative of u=esin⁡x at the nodes of a periodic grid with the centred kernels of order 2, 4, 6 and 8, and the orders measured by the program itself.

Concepts. Names: gideon_fd_d1_c6 is the first derivative (d1) at a node, from point values (fd), centred (c), order 6. Windows: the kernel's dummy is q(-3:3), the node at offset 0; the caller passes the section u(i-3:i+3). Ghost nodes: Ghost cells for a window.

The program ​

fortran
program fd_derivative
!< Tutorial T1: the first derivative of u = exp(sin x) on a periodic grid, centred kernels of order 2 to 8, and the
!< orders observed by halving the step.
use gideon
implicit none
integer(I4P), parameter :: G = 4                 ! ghost nodes on each side: the half-width of the widest window
integer(I4P), parameter :: NGRID = 4             ! number of grids: 16, 32, 64, 128 nodes
real(R_P),    parameter :: PI = acos(-1._R_P)
real(R_P), allocatable  :: x(:), u(:), du(:,:)   ! nodes, point values (with ghosts), derivatives (one column per order)
real(R_P)               :: h, err(4,NGRID)       ! step, max error per order and grid
integer(I4P)            :: n, i, k, o

do k=1, NGRID
   n = 8 * 2**k
   h = 2*PI / n
   allocate(x(-G:n-1+G), u(-G:n-1+G), du(0:n-1,4))
   x = [(i*h, i=-G, n-1+G)]
   u(0:n-1) = exp(sin(x(0:n-1)))                 ! the n distinct nodes of the period [0, 2 pi)
   u(-G:-1) = u(n-G:n-1)                          ! ghosts on the left: the last G nodes
   u(n:n-1+G) = u(0:G-1)                          ! ghosts on the right: the first G nodes
   do i=0, n-1
      call gideon_fd_d1_c2(u(i-1:i+1), h, du(i,1)) ! window q(-1:1): node i at offset 0
      call gideon_fd_d1_c4(u(i-2:i+2), h, du(i,2)) ! window q(-2:2)
      call gideon_fd_d1_c6(u(i-3:i+3), h, du(i,3)) ! window q(-3:3)
      call gideon_fd_d1_c8(u(i-4:i+4), h, du(i,4)) ! window q(-4:4)
   enddo
   do o=1, 4
      err(o,k) = maxval(abs(du(:,o) - cos(x(0:n-1))*u(0:n-1)))
   enddo
   deallocate(x, u, du)
enddo

print '(A)', 'd/dx exp(sin x) on [0, 2 pi), max error over the nodes'
print '(A)', '     n       c2         c4         c6         c8'
do k=1, NGRID
   print '(I6,4ES11.3)', 8 * 2**k, err(:,k)
enddo
print '(A)', 'observed orders, log2(e(n/2) / e(n))'
do k=2, NGRID
   print '(I6,4F11.2)', 8 * 2**k, log(err(:,k-1) / err(:,k)) / log(2._R_P)
enddo
endprogram fd_derivative
  • The grid. n nodes xi=ih, i=0,…,n−1, h=2π/n. The period holds n distinct nodes: x=2π is node 0 again.
  • Ghost nodes. The widest window is q(-4:4) (gideon_fd_d1_c8): node 0 reads u(-4:4), node n−1 reads u(n-5:n+3). So u carries G = 4 nodes beyond each end, filled with the periodic image before the loop. The kernels never see a boundary, only a window.
  • The call. call gideon_fd_d1_c6(u(i-3:i+3), h, du(i,3)): the section is the window, and node i lands at offset 0 because the dummy is declared q(-3:3). The step is an argument; the kernel divides by it (decision D4, Arithmetic).
  • The measurement. The error is the maximum over the nodes against u′=cos⁡xesin⁡x; the observed order is log2⁡(en/2/en), the step halved.

Build and run ​

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

or against the library:

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

Output ​

text
d/dx exp(sin x) on [0, 2 pi), max error over the nodes
     n       c2         c4         c6         c8
    16  9.359E-02  1.575E-02  4.392E-03  1.598E-03
    32  2.583E-02  1.132E-03  9.312E-05  1.066E-05
    64  6.512E-03  7.590E-05  1.577E-06  4.818E-08
   128  1.631E-03  4.783E-06  2.515E-08  2.002E-10
observed orders, log2(e(n/2) / e(n))
    32       1.86       3.80       5.56       7.23
    64       1.99       3.90       5.88       7.79
   128       2.00       3.99       5.97       7.91

What to notice ​

  • Every column reaches its design order: 2.00, 3.99, 5.97 and 7.91 between 64 and 128 nodes. Between 16 and 32 nodes the orders are lower (7.23 for c8): esin⁡x carries every harmonic, and 16 nodes per period do not yet resolve it. An order is a statement about h→0; it shows once the grid resolves the function.
  • At 128 nodes the eighth-order error is 2.0×10−10, eight million times below the second-order one, for a window of 9 nodes instead of 3. It is still a truncation error: the round-off of this kernel is about εmax|u|∑|cm|/h≈3×10−14 here, and it grows as 1/h (exercise 3).
  • The ghost count is the half-width of the widest window used, not a constant of the library. Each kernel's window is in the table of Operators.

The window's length is the caller's

gideon_fd_d1_c8 declares q(-4:4): it reads 9 values from the first element of the section it receives. gfortran rejects a section of constant wrong length at compile time ("Actual argument contains too few elements"), but a section with run-time bounds is not checked, even with -fcheck=all (gfortran 16.2): u(i-3:i+3) passed to gideon_fd_d1_c8 returns the derivative at node i+1. Write the section from the kernel's window, u(i-S:i+S) for q(-S:S), as the program does.

Exercises ​

  1. Replace gideon_fd_d1_c* by gideon_fd_d2_c* (the same windows for orders 2 to 8) and measure against u″=(cos2⁡x−sin⁡x)esin⁡x.
  2. Add the tenth-order kernel, gideon_fd_d1_c10(u(i-5:i+5), h, d). What else must change in the program?
  3. Set NGRID = 8 (up to 2048 nodes). The c8 error stops near 10−13 at 512 nodes, then grows: the observed order becomes −1, round-off amplified by the division by the step.
  4. The value at the midpoint xi+1/2: gideon_fd_face_c6(u(i-2:i+3), f), window q(-2:3). Measure its order.

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