T2 Finite volumes, conservatively
Goal. From the cell averages of
Concepts. Windows: an fv face operator reads the cell fv cell operator reads the cell fv cell derivative is
Integrate
The cell average of
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
is , and its average is a difference of the primitive prim:. xf(i)is the face, the right face of cell . - Face values.
gideon_fv_face_c6(q(i-2:i+3), f(i)): windowq(-2:3), cells, the face between cells and . 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)): windowq(-3:3), the union of the windows of the two faces of cell. 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,.
Build and run
bash
fobis build --mode examples-gnu && ./exe-examples/fv_flux_differenceor 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_differenceOutput
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.98What to notice
- The cell kernel is the flux difference, bitwise, in every cell of every grid.
gideon_fv_d1_c6evaluates the two face rows exactly asgideon_fv_face_c6does, 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 (face evaluations), or call the cell kernel per cell ( ): 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
uses on its right is the one cell uses on its left, so the sum telescopes; but each difference 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
mode (its sixth derivative is against 1 for ), and differencing it across a cell differentiates it, which multiplies by 3.
Exercises
- Fourth order:
gideon_fv_face_c4(q(i-1:i+2), f(i))andgideon_fv_d1_c4(q(i-2:i+2), h, d(i)), withG = 2. - Make it a scheme: one forward-Euler step
of , and compare before and after. - Feed point values at the cell centres instead of averages: the face error falls to order 2, the size of
. gideon_fd_average_c<order>turns point values into averages (Averages and point values). - The second derivative:
gideon_fv_face_d1_c6(q(i-2:i+3), h, g(i))is the face value of, and gideon_fv_d2_c6(q(i-3:i+3), h, d2(i))the average ofover the cell. Compare d2(i)with(g(i) - g(i-1))/h: equal to round-off, not bitwise, because the cell kernel dividesby h*honce and the face kernel divides eachby h. Withhreplaced by1._R_Pin both calls the equality is bitwise again.