T1 A derivative on a periodic grid
Goal. The first derivative of
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
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.
nodes , , . The period holds distinct nodes: is node 0 again. - Ghost nodes. The widest window is
q(-4:4)(gideon_fd_d1_c8): node 0 readsu(-4:4), nodereads u(n-5:n+3). SoucarriesG = 4nodes 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 nodeilands at offset 0 because the dummy is declaredq(-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
; the observed order is , the step halved.
Build and run
fobis build --mode examples-gnu && ./exe-examples/fd_derivativeor against the library:
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_derivativeOutput
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.91What 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):carries every harmonic, and 16 nodes per period do not yet resolve it. An order is a statement about ; it shows once the grid resolves the function. - At 128 nodes the eighth-order error is
, 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 here, and it grows as (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
- Replace
gideon_fd_d1_c*bygideon_fd_d2_c*(the same windows for orders 2 to 8) and measure against. - Add the tenth-order kernel,
gideon_fd_d1_c10(u(i-5:i+5), h, d). What else must change in the program? - Set
NGRID = 8(up to 2048 nodes). Thec8error stops nearat 512 nodes, then grows: the observed order becomes , round-off amplified by the division by the step. - The value at the midpoint
: gideon_fd_face_c6(u(i-2:i+3), f), windowq(-2:3). Measure its order.