Skip to content

T2 Finite volumes, conservatively ​

Goal. From the cell averages of u=sin⁡x+12cos⁡3x on a periodic grid: the sixth-order values at the faces, the cell average of u′ as their difference, and the telescoping sum that makes a finite-volume update conservative.

Concepts. Windows: an fv face operator reads the cell [o−1,o] at offset o and returns the value at the face x=0, between cells 0 and 1; an fv cell operator reads the cell [o−12,o+12] and returns the average over cell 0. Arithmetic, the flux difference: an fv cell derivative is (g+1/2−g−1/2)/hn, g the face row. Averages against point values: Averages and point values.

Integrate ut+f(u)x=0 over cell i:

du¯idt=−fi+1/2−fi−1/2h,∑ihdu¯idt=−∑i(fi+1/2−fi−1/2)=0 (periodic).

The cell average of ux is exactly (ui+1/2−ui−1/2)/h: a face value of order p gives a cell derivative of order p, and every face value enters the sum twice, with opposite signs.

The program ​

fortran
program fv_flux_difference
!< Tutorial T2: cell averages of u = sin x + cos(3x)/2 on a periodic grid; the sixth-order face values, the cell-average
!< first derivative as their difference, and the telescoping sum that makes a finite-volume update conservative.
use gideon
implicit none
integer(I4P), parameter :: G = 3                 ! ghost cells on each side: the half-width of the c6 cell window
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), allocatable  :: xf(:), q(:), f(:), d(:) ! faces, cell averages (with ghosts), face values, cell derivatives
real(R_P)               :: h, ef(NGRID), ed(NGRID), telescoped
integer(I4P)            :: n, i, k, nbitwise

print '(A)', 'cell averages of sin x + cos(3x)/2 on [0, 2 pi), order 6'
print '(A)', '     n  face error    d1 error   d1 == flux difference   telescoped to round-off'
do k=1, NGRID
   n = 8 * 2**k
   h = 2*PI / n
   allocate(xf(-1:n-1), q(-G:n-1+G), f(-1:n-1), d(0:n-1))
   xf = [(i*h + h, i=-1, n-1)]                    ! xf(i): the face i+1/2, right of cell i = [i h, (i+1) h]
   q(0:n-1) = (prim(xf(0:n-1)) - prim(xf(-1:n-2))) / h ! exact cell averages
   q(-G:-1) = q(n-G:n-1)                          ! periodic ghost cells
   q(n:n-1+G) = q(0:G-1)
   do i=0, n-1
      call gideon_fv_face_c6(q(i-2:i+3), f(i))   ! window q(-2:3): cells i-2..i+3, the face between i and i+1
      call gideon_fv_d1_c6(q(i-3:i+3), h, d(i))  ! window q(-3:3): the derivative averaged over cell i
   enddo
   f(-1) = f(n-1)                                 ! the face 0 - 1/2 is the face n - 1/2
   ef(k) = maxval(abs(f(0:n-1) - fun(xf(0:n-1))))
   ed(k) = maxval(abs(d - (fun(xf(0:n-1)) - fun(xf(-1:n-2))) / h))
   nbitwise = count(d == (f(0:n-1) - f(-1:n-2)) / h)
   telescoped = sum(f(0:n-1) - f(-1:n-2))        ! every face value enters twice, with opposite signs
   print '(I6,2ES12.3,6X,I4,A,I4,A,15X,L1)', n, ef(k), ed(k), nbitwise, ' of ', n, ' cells', &
         abs(telescoped) <= n * epsilon(1._R_P) * maxval(abs(f))
   deallocate(xf, q, f, d)
enddo
print '(A)', 'observed orders, log2(e(n/2) / e(n))'
do k=2, NGRID
   print '(I6,2F12.2)', 8 * 2**k, log(ef(k-1) / ef(k)) / log(2._R_P), log(ed(k-1) / ed(k)) / log(2._R_P)
enddo
contains
   elemental function fun(x)
   !< The function.
   real(R_P), intent(in) :: x
   real(R_P)             :: fun
   fun = sin(x) + cos(3*x) / 2
   endfunction fun

   elemental function prim(x)
   !< A primitive of the function: its cell averages are differences of prim.
   real(R_P), intent(in) :: x
   real(R_P)             :: prim
   prim = -cos(x) + sin(3*x) / 6
   endfunction prim
endprogram fv_flux_difference
  • Exact averages. Cell i is [ih,(i+1)h], and its average is a difference of the primitive prim: u¯i=(P(xi+1/2)−P(xi−1/2))/h. xf(i) is the face i+12, the right face of cell i.
  • Face values. gideon_fv_face_c6(q(i-2:i+3), f(i)): window q(-2:3), cells i−2,…,i+3, the face between cells i and i+1. One value per face, f(-1) = f(n-1) closing the period.
  • Cell derivative. gideon_fv_d1_c6(q(i-3:i+3), h, d(i)): window q(-3:3), the union of the windows of the two faces of cell i. Three ghost cells on each side cover both calls.
  • Two checks. How many cells have d(i) equal, bit for bit, to (f(i) - f(i-1))/h; and whether the sum of the flux differences is zero to round-off, |∑i(fi+1/2−fi−1/2)|≤nεmax|f|.

Build and run ​

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

or against the library:

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

Output ​

text
cell averages of sin x + cos(3x)/2 on [0, 2 pi), order 6
     n  face error    d1 error   d1 == flux difference   telescoped to round-off
    16   7.286E-03   2.024E-02        16 of   16 cells               T
    32   1.395E-04   4.107E-04        32 of   32 cells               T
    64   2.292E-06   6.849E-06        64 of   64 cells               T
   128   3.631E-08   1.088E-07       128 of  128 cells               T
observed orders, log2(e(n/2) / e(n))
    32        5.71        5.62
    64        5.93        5.91
   128        5.98        5.98

What to notice ​

  • The cell kernel is the flux difference, bitwise, in every cell of every grid. gideon_fv_d1_c6 evaluates the two face rows exactly as gideon_fv_face_c6 does, the same coefficients in the same order, then divides their difference by the step. A scheme may compute one value per face and difference them (n face evaluations), or call the cell kernel per cell (2n): the numbers are the same, the cost is the caller's choice.
  • Conservation is to round-off, not exact zero. The face value that cell i uses on its right is the one cell i+1 uses on its left, so the sum telescopes; but each difference fi+1/2−fi−1/2 is rounded, and a sum of rounded differences is not zero. What a conservative scheme needs is the first property: one number per face, shared by the two cells.
  • Both errors are of order 6, 5.98 at 128 cells. The cell-derivative error is about three times the face error: the leading face error is carried by the cos⁡3x mode (its sixth derivative is 36/2 against 1 for sin⁡x), and differencing it across a cell differentiates it, which multiplies by 3.

Exercises ​

  1. Fourth order: gideon_fv_face_c4(q(i-1:i+2), f(i)) and gideon_fv_d1_c4(q(i-2:i+2), h, d(i)), with G = 2.
  2. Make it a scheme: one forward-Euler step u¯i←u¯i−Δt(fi+1/2−fi−1/2)/h of ut+ux=0, and compare ∑iu¯i before and after.
  3. Feed point values at the cell centres instead of averages: the face error falls to order 2, the size of u¯−u(xi)=h2u″/24. gideon_fd_average_c<order> turns point values into averages (Averages and point values).
  4. The second derivative: gideon_fv_face_d1_c6(q(i-2:i+3), h, g(i)) is the face value of u′, and gideon_fv_d2_c6(q(i-3:i+3), h, d2(i)) the average of u″ over the cell. Compare d2(i) with (g(i) - g(i-1))/h: equal to round-off, not bitwise, because the cell kernel divides g+1/2−g−1/2 by h*h once and the face kernel divides each g by h. With h replaced by 1._R_P in both calls the equality is bitwise again.

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