Filtering a central scheme
Problem. A central scheme leaves the odd-even mode
The kernels
| Kernel | What | Window |
|---|---|---|
gideon_fd_filter_c<2k>, gideon_fv_filter_c<2k> | explicit centred filter of order | q(-k:k) |
gideon_fd_filter_p<2k> | the right side of the compact filter of order | q(-k:k) |
The explicit filter is one kernel call per point. The compact one is a right side per point and a solve:
do i=1, n
call gideon_fd_filter_p8(u(i-4:i+4), alpha, uf(i))
enddo
call gideon_tri_periodic_const(n, alpha, 1._R_P, alpha, uf, w1, w2) ! |alpha| < 1/2Code
Point values of
program cookbook_filter
!< Cookbook: filtering a central scheme. Point values of a smooth wave plus an odd-even (Nyquist) mode on a periodic
!< line, filtered by the explicit filters gideon_fd_filter_c<2k> and by the compact ones gideon_fd_filter_p<2k> at
!< alpha_f = 0.45: the odd-even mode is removed, the smooth wave kept to the filter's order.
use gideon
implicit none
integer(I4P), parameter :: G = 5
real(R_P), parameter :: PI = acos(-1._R_P), ALPHA = 0.45_R_P
real(R_P) :: e(2,5,2), noise(2,5)
integer(I4P) :: k, ig, n, kind
character(20) :: label
do ig=1, 2
n = 16*ig
do kind=1, 2
do k=1, 5
call filtered(kind, k, n, e(kind,k,ig), noise(kind,k))
enddo
enddo
enddo
print '(A)', 'u = sin x + cos(2x)/2 + 0.1 (-1)^i on a periodic line; the filter error on the smooth wave'
print '(A)', 'filter n = 16 n = 32 observed odd-even mode removed'
do kind=1, 2
do k=1, 5
write(label, '(A,I0,A,I0)') merge('gideon_fd_filter_c', 'gideon_fd_filter_p', kind == 1), 2*k
print '(A20,2ES12.3,F11.2,L14)', label, e(kind,k,1), e(kind,k,2), log(e(kind,k,1)/e(kind,k,2))/log(2._R_P), &
noise(kind,k) < 1.e-12_R_P
enddo
enddo
contains
subroutine filtered(kind, k, n, err, nyq)
!< Filter of order 2k (explicit: kind 1; compact: kind 2) on n points: its error on the smooth part, and what is left
!< of the odd-even mode (filtering is linear: each part filtered alone).
integer(I4P), intent(in) :: kind, k, n
real(R_P), intent(out) :: err, nyq
real(R_P) :: x(n), s(1-G:n+G), o(1-G:n+G), fs(n), fo(n), w(n,2)
integer(I4P) :: i
do i=1, n
x(i) = 2*PI*(i - 1)/n
s(i) = sin(x(i)) + 0.5_R_P*cos(2*x(i))
o(i) = 0.1_R_P*(1 - 2*modulo(i, 2))
enddo
s(1-G:0) = s(n-G+1:n) ; s(n+1:n+G) = s(1:G)
o(1-G:0) = o(n-G+1:n) ; o(n+1:n+G) = o(1:G)
do i=1, n
call apply(kind, k, s(i-k:i+k), fs(i))
call apply(kind, k, o(i-k:i+k), fo(i))
enddo
if (kind == 2) then ! the compact filter's band [alpha, 1, alpha]
call gideon_tri_periodic_const(n, ALPHA, 1._R_P, ALPHA, fs, w(:,1), w(:,2))
call gideon_tri_periodic_const(n, ALPHA, 1._R_P, ALPHA, fo, w(:,1), w(:,2))
endif
err = maxval(abs(fs - s(1:n)))
nyq = maxval(abs(fo))
endsubroutine filtered
subroutine apply(kind, k, q, v)
!< The filter of order 2k at one point (the compact one: its right side).
integer(I4P), intent(in) :: kind, k
real(R_P), intent(in) :: q(-k:k)
real(R_P), intent(out) :: v
select case(10*kind + k)
case(11) ; call gideon_fd_filter_c2(q, v)
case(12) ; call gideon_fd_filter_c4(q, v)
case(13) ; call gideon_fd_filter_c6(q, v)
case(14) ; call gideon_fd_filter_c8(q, v)
case(15) ; call gideon_fd_filter_c10(q, v)
case(21) ; call gideon_fd_filter_p2(q, ALPHA, v)
case(22) ; call gideon_fd_filter_p4(q, ALPHA, v)
case(23) ; call gideon_fd_filter_p6(q, ALPHA, v)
case(24) ; call gideon_fd_filter_p8(q, ALPHA, v)
case(25) ; call gideon_fd_filter_p10(q, ALPHA, v)
endselect
endsubroutine apply
endprogram cookbook_filteru = sin x + cos(2x)/2 + 0.1 (-1)^i on a periodic line; the filter error on the smooth wave
filter n = 16 n = 32 observed odd-even mode removed
gideon_fd_filter_c2 1.113E-01 2.864E-02 1.96 T
gideon_fd_filter_c4 1.217E-02 8.166E-04 3.90 T
gideon_fd_filter_c6 1.626E-03 2.845E-05 5.84 T
gideon_fd_filter_c8 2.321E-04 1.058E-06 7.78 T
gideon_fd_filter_c10 3.376E-05 4.001E-08 9.72 T
gideon_fd_filter_p2 6.553E-03 1.549E-03 2.08 T
gideon_fd_filter_p4 7.344E-04 4.445E-05 4.05 T
gideon_fd_filter_p6 9.898E-05 1.552E-06 5.99 T
gideon_fd_filter_p8 1.417E-05 5.774E-08 7.94 T
gideon_fd_filter_p10 2.063E-06 2.185E-09 9.88 T- Every filter removes the odd-even mode to round-off: that is its definition (the right symbol is zero at
). - Every filter keeps the smooth wave to its order; the compact one, at equal order, is 16 to 18 times more accurate on this wave, at every order alike: its transfer function stays near 1 over more of the band.
- Filter after every step of the central scheme, on each line of each direction. The compact filter costs a solve per line; the explicit one is a single pass.
Reference. Roadmap issue #2, phase 2 (2.1, 2.2). The rows: Operators, Compact operators; the transfer function in