T7 A compact derivative on a periodic line
Goal. The first derivative of
Concepts. A compact operator is a band solved along the line, its right side an ordinary kernel (Compact operators): gideon_fd_d1_p6 returns the right side at one node, from the window q(-2:2); the band FD_D1_P6_LEFT (gideon_tri_periodic_const. Line operators.
The program
fortran
program compact_derivative
!< Tutorial T7: the first derivative of u = exp(sin x) on a periodic line, compact (Pade) operators of order 4 to 10
!< against the explicit centred one of order 6: the right side point by point, then one solve of the band.
use gideon
implicit none
integer(I4P), parameter :: G = 3 ! ghost nodes on each side: the half-width of the widest right side
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(:,:), w(:,:) ! nodes, point values (with ghosts), derivatives, scratch
real(R_P) :: h, err(5,NGRID)
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,5), w(0:n-1,4))
x = [(i*h, i=-G, n-1+G)]
u(0:n-1) = exp(sin(x(0:n-1)))
u(-G:-1) = u(n-G:n-1) ; u(n:n-1+G) = u(0:G-1) ! periodic ghosts
do i=0, n-1 ! the right sides, one kernel call per node
call gideon_fd_d1_p4(u(i-1:i+1), h, du(i,1))
call gideon_fd_d1_p6(u(i-2:i+2), h, du(i,2))
call gideon_fd_d1_p8(u(i-2:i+2), h, du(i,3))
call gideon_fd_d1_p10(u(i-3:i+3), h, du(i,4))
call gideon_fd_d1_c6(u(i-3:i+3), h, du(i,5)) ! the explicit operator of order 6, for comparison
enddo
! the bands: tridiagonal for orders 4 and 6, pentadiagonal for 8 and 10 (their pairs: index j at offsets +-j)
call gideon_tri_periodic_const(n, FD_D1_P4_LEFT(1), FD_D1_P4_LEFT(0), FD_D1_P4_LEFT(1), du(:,1), w(:,1), w(:,2))
call gideon_tri_periodic_const(n, FD_D1_P6_LEFT(1), FD_D1_P6_LEFT(0), FD_D1_P6_LEFT(1), du(:,2), w(:,1), w(:,2))
call gideon_penta_periodic_const(n, FD_D1_P8_LEFT(2), FD_D1_P8_LEFT(1), FD_D1_P8_LEFT(0), FD_D1_P8_LEFT(1), &
FD_D1_P8_LEFT(2), du(:,3), w(:,1), w(:,2), w(:,3), w(:,4))
call gideon_penta_periodic_const(n, FD_D1_P10_LEFT(2), FD_D1_P10_LEFT(1), FD_D1_P10_LEFT(0), FD_D1_P10_LEFT(1), &
FD_D1_P10_LEFT(2), du(:,4), w(:,1), w(:,2), w(:,3), w(:,4))
do o=1, 5
err(o,k) = maxval(abs(du(:,o) - cos(x(0:n-1))*u(0:n-1)))
enddo
deallocate(x, u, du, w)
enddo
print '(A)', 'd/dx exp(sin x) on [0, 2 pi), max error over the nodes'
print '(A)', ' n p4 p6 p8 p10 c6'
do k=1, NGRID
print '(I6,5ES11.3)', 8 * 2**k, err(:,k)
enddo
print '(A)', 'observed orders, log2(e(n/2) / e(n))'
do k=2, NGRID
print '(I6,5F11.2)', 8 * 2**k, log(err(:,k-1) / err(:,k)) / log(2._R_P)
enddo
endprogram compact_derivative- The right side. One kernel call per node, exactly as for an explicit derivative (T1): the window by offset, the step an argument, the ghost nodes the periodic image. The widest right side,
p10, readsq(-3:3): three ghosts on each side. - The solve. The array of right sides becomes the derivative in place:
gideon_tri_periodic_const(n, a, b, c, d, w, z)solveswith indices modulo , the band passed as scalars (the _constvariant), two scratch arrays the caller's. Orders 8 and 10 have pentadiagonal bands:gideon_penta_periodic_const, four scratch arrays. - Nothing else changes. The kernels and the solvers are
pureand device-callable, so the same two calls run one line per thread on a GPU (T6, C8).
The output
text
d/dx exp(sin x) on [0, 2 pi), max error over the nodes
n p4 p6 p8 p10 c6
16 3.850E-03 5.902E-04 8.855E-05 2.452E-05 4.392E-03
32 2.078E-04 7.395E-06 2.134E-07 1.230E-08 9.312E-05
64 1.293E-05 1.098E-07 7.556E-10 1.160E-11 1.577E-06
128 8.015E-07 1.695E-09 2.913E-12 2.554E-14 2.515E-08
observed orders, log2(e(n/2) / e(n))
32 4.21 6.32 8.70 10.96 5.56
64 4.01 6.07 8.14 10.05 5.88
128 4.01 6.02 8.02 8.83 5.97What it shows
- The orders. 4.01, 6.02, 8.02 at 128 nodes;
p10measures 10.05 at 64 and then meets round-off (its error at 128 nodes is). The orders of every compact operator are measured in quadruple precision by A4. - The accuracy at equal order.
p6reads five points where the explicitc6reads seven, and its error is about 1/15 ofc6's on the same grid (against at 128 nodes): the band buys the resolution of the short waves. p4, with three points, is more accurate thanc6on the coarsest grid (16 nodes, where the short waves dominate), less on the finer ones, where order wins. - The cost. A solve per line. Here each grid has one line, so
_constis the natural call; when many lines share the band and the length (every line of a grid direction), factor the band once withgideon_tri_periodic_factorand solve each line withgideon_tri_periodic_solve, multiplications only:p6then costs about 3 timesc6per point on a CPU core, 5 times on a GPU (Compact operators, Performance).
Next. A compact filter after each step of a central scheme: Filtering a central scheme; compact rows coupled with a nonlinear scheme at a step: Compact-WENO rows.