Skip to content

T3 Upwind faces and their mirror ​

Goal. The third-order face value from the cells on either side of a face; the mirror identity that makes every left-side kernel the reflection of its right-side twin, checked bit for bit; the upwind face values of the advection ut+aux=0 for both signs of a.

Concepts. Names: the bias names where the stencil lies, ur on the right of the face, ul on the left. Windows: an upwind face from S cells reads q(1:S) (right) or q(1-S:0) (left); an upwind cell operator reads q(0:S) or q(-S:0). Mirrors: Arithmetic. T2 for the face values as fluxes.

The flow carries information from upwind: for a>0 the face i+12 takes its value from the cells on its left, gideon_fv_face_ul3(u(i-2:i), f); for a<0 from the cells on its right, gideon_fv_face_ur3(u(i+1:i+3), f). The order-3 row is 116,−76,13, the nearest cell first: the stencil lies wholly on one side of the face (i−2,i−1,i for ul3, the leftmost candidate stencil of WENO5), not the upwind-biased i−1,i,i+1.

The mirror ​

Reflect the data about the face, rm=q1−m: the left-side kernel on q is the right-side kernel on r. For a cell operator the reflection is about the cell centre, rm=q−m, and the n-th derivative changes sign as (−1)n. GIDEON stores no left-side row: each ul kernel indexes the ur row backwards, so the identity holds by construction, and it holds bitwise because the reflected kernel performs the same operations on the same numbers.

The program ​

fortran
program upwind
!< Tutorial T3: upwind stencils. The mirror identity between left- and right-side kernels, checked bitwise, and the
!< upwind face values of the advection u_t + a u_x = 0 for both signs of a.
use gideon
implicit none
integer(I4P), parameter :: M = 1000              ! windows for the mirror check
integer(I4P), parameter :: G = 3                 ! ghost cells on each side: the reach of the order-3 faces
integer(I4P), parameter :: NGRID = 4             ! number of grids: 16, 32, 64, 128 cells
real(R_P),    parameter :: PI = acos(-1._R_P)
real(R_P)               :: q(-10:M+10)           ! arbitrary data for the mirror check
real(R_P)               :: fl, fr, dl, dr        ! left- and right-side results
real(R_P), allocatable  :: xf(:), u(:), f(:), res(:) ! faces, cell averages (with ghosts), face values, residuals
real(R_P)               :: h, a, err(2,NGRID)
integer(I4P)            :: i, k, n, s, nface, nd1, nd2

! the mirror: a left-side kernel on q equals its right-side twin on q reversed, times (-1)^n for the n-th derivative
q = [(sin(0.7_R_P*i + 0.3_R_P) + 0.01_R_P*i, i=-10, M+10)]
nface = 0 ; nd1 = 0 ; nd2 = 0
do i=1, M
   call gideon_fv_face_ul3(q(i-2:i), fl)          ! the face i+1/2 from cells i-2, i-1, i
   call gideon_fv_face_ur3(q(i:i-2:-1), fr)       ! the same cells, reversed: r(m) = q(i+1-m), m = 1..3
   if (fl == fr) nface = nface + 1
   call gideon_fv_d1_ul3(q(i-3:i), 1._R_P, dl)   ! d1 averaged over cell i, cells i-3..i
   call gideon_fv_d1_ur3(q(i:i-3:-1), 1._R_P, dr) ! reversed: r(m) = q(i-m), m = 0..3
   if (dl == -dr) nd1 = nd1 + 1
   call gideon_fv_d2_ul3(q(i-4:i), 1._R_P, dl)
   call gideon_fv_d2_ur3(q(i:i-4:-1), 1._R_P, dr)
   if (dl == dr) nd2 = nd2 + 1
enddo
print '(A)', 'mirror identity, bitwise, over 1000 windows'
print '(A,I0)', '  face value:  ul3(q) ==  ur3(reversed q)  ', nface
print '(A,I0)', '  d1 average:  ul3(q) == -ur3(reversed q)  ', nd1
print '(A,I0)', '  d2 average:  ul3(q) ==  ur3(reversed q)  ', nd2

! advection: the residual -a (f(i+1/2) - f(i-1/2))/h, f the face value from the upwind side
print '(A)', 'residual of u_t + a u_x = 0, u = exp(sin x), order-3 upwind faces, max error'
print '(A)', '     n     a = +1      a = -1'
do k=1, NGRID
   n = 8 * 2**k
   h = 2*PI / n
   allocate(xf(-1:n-1), u(-G:n-1+G), f(-1:n-1), res(0:n-1))
   xf = [(i*h + h, i=-1, n-1)]                    ! xf(i): the face i+1/2, right of cell i
   u(0:n-1) = cell_average(xf(-1:n-2), xf(0:n-1))
   u(-G:-1) = u(n-G:n-1)                          ! periodic ghost cells
   u(n:n-1+G) = u(0:G-1)
   do s=1, 2
      a = merge(1._R_P, -1._R_P, s == 1)
      do i=0, n-1
         if (a > 0._R_P) then
            call gideon_fv_face_ul3(u(i-2:i), f(i)) ! the flow comes from the left: cells i-2..i
         else
            call gideon_fv_face_ur3(u(i+1:i+3), f(i)) ! the flow comes from the right: cells i+1..i+3
         endif
      enddo
      f(-1) = f(n-1)
      res = -a * (f(0:n-1) - f(-1:n-2)) / h
      err(s,k) = maxval(abs(res + a * (exp(sin(xf(0:n-1))) - exp(sin(xf(-1:n-2)))) / h))
   enddo
   print '(I6,2ES12.3)', n, err(:,k)
   deallocate(xf, u, f, res)
enddo
print '(A)', 'observed orders, log2(e(n/2) / e(n))'
do k=2, NGRID
   print '(I6,2F12.2)', 8 * 2**k, log(err(:,k-1) / err(:,k)) / log(2._R_P)
enddo
contains
   elemental function cell_average(xa, xb) result(avg)
   !< The average of exp(sin x) over [xa, xb]: 8-point Gauss-Legendre per cell, exact far below the errors above.
   real(R_P), intent(in) :: xa, xb
   real(R_P)             :: avg
   real(R_P), parameter  :: GX(4) = [0.1834346424956498_R_P, 0.5255324099163290_R_P, &
                                     0.7966664774136267_R_P, 0.9602898564975363_R_P]
   real(R_P), parameter  :: GW(4) = [0.3626837833783620_R_P, 0.3137066458778873_R_P, &
                                     0.2223810344533745_R_P, 0.1012285362903763_R_P]
   real(R_P)             :: c, r
   c = (xa + xb) / 2 ; r = (xb - xa) / 2
   avg = sum(GW * (exp(sin(c - r*GX)) + exp(sin(c + r*GX)))) / 2
   endfunction cell_average
endprogram upwind
  • The mirror check. q(i:i-2:-1) is the reflected window, passed as it is: a section with a negative stride is copied into a contiguous temporary for the explicit-shape dummy. For d1 the comparison is with -dr, for d2 with +dr. The data are arbitrary: the identity is algebraic, not a property of smooth fields.
  • The residual. Ri=−a(fi+1/2−fi−1/2)/h with f the upwind face value, against the exact −a(u(xi+1/2)−u(xi−1/2))/h. Cell averages of esin⁡x come from an 8-point Gauss-Legendre rule per cell, exact far below the errors measured.
  • Ghosts. For a>0 the faces reach 2 cells to the left of cell 0; for a<0 3 cells to the right of cell n−1. G = 3 covers both.

Build and run ​

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

or against the library:

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

Output ​

text
mirror identity, bitwise, over 1000 windows
  face value:  ul3(q) ==  ur3(reversed q)  1000
  d1 average:  ul3(q) == -ur3(reversed q)  1000
  d2 average:  ul3(q) ==  ur3(reversed q)  1000
residual of u_t + a u_x = 0, u = exp(sin x), order-3 upwind faces, max error
     n     a = +1      a = -1
    16   1.308E-01   1.308E-01
    32   1.939E-02   1.939E-02
    64   2.534E-03   2.534E-03
   128   3.203E-04   3.203E-04
observed orders, log2(e(n/2) / e(n))
    32        2.75        2.75
    64        2.94        2.94
   128        2.98        2.98

What to notice ​

  • The mirror is exact: 1000 of 1000 windows for the face value, the first and the second derivative, with the sign (−1)n. This is what C3 Mirrors and symmetries verifies for every left-side kernel.
  • Order 3 from three cells, 2.98 at 128 cells, for either sign of a.
  • The two columns are equal. esin⁡x is symmetric about x=π/2, and the reflection x→π−x maps faces onto faces (π is a multiple of h for even n). Reflected, the flow to the right becomes the flow to the left, and the mirror identity carries the errors across: the same numbers to the digits printed.
  • Accuracy says nothing about stability. The downwind face value (ur3 for a>0) has an error of the same order, and the upwind one is not stable by being upwind. The flux difference of ul3 is (11u¯i−18u¯i−1+9u¯i−2−2u¯i−3)/(6h); a mode eiθx/h of ut+aux=0 grows at the rate ah(1−cos⁡θ)2(4cos⁡θ−1)/3, positive for θ<arccos⁡14≈1.318. Of the left-side first derivatives only those of 1 and 2 cells damp every wavenumber; the wider one-sided rows amplify a band, and they are building blocks (the candidate stencils a WENO reconstruction weighs), not schemes on their own (A3 Spectral properties).

Exercises ​

  1. Fifth order: gideon_fv_face_ul5(u(i-4:i), f(i)) for a>0, gideon_fv_face_ur5(u(i+1:i+5), f(i)) for a<0, and G = 5.
  2. The cell kernel is the flux difference (T2), upwind too: compare gideon_fv_d1_ul3(u(i-3:i), h, d) with (f(i) - f(i-1))/h for a>0, bit for bit.
  3. The symbol, from the compiled kernels: with h = 1, call gideon_fv_d1_ul3 on the window qo=cos⁡(oθ), o=−3,…,0; the result is −1 times the growth rate of the mode θ (for a=1). Scan θ∈(0,π], find the band where it is negative and compare its edge with arccos⁡14. Repeat for gideon_fv_d1_ul2: no such band.
  4. The third derivative: gideon_fv_d3_ul3 against gideon_fv_d3_ur3 on the reflected window. Which sign?

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