11. Limiters
Goal: limit a stage for positivity at the forward-Euler step that stage takes, stage_euler_fraction(k) times
A strong-stability-preserving (SSP) scheme is a convex combination of forward-Euler steps. In its Shu–Osher form, stage
so the residual of stage stage_euler_fraction(k). A limiter that makes
This chapter's program advects a density on the tutorial's four blocks: 1 on block 2, 0.1 elsewhere, with block 4 solid. The residual is the central difference, the scheme ssp-54 in the Butcher form, at a Courant number of 1.
The stepper and its fractions
mask = -1._R_P
mask(:,:,:,:,4) = 1._R_P ! block 4 is solid
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(q), ub=ubound(q), masked_q=.true.) ! solid cells of a stage: q^nprint '(A,*(F7.4))', SCHEME//' f_k =', [(st%stage_euler_fraction(k), k=1, st%stages())]
print '(A,F7.4,A,F9.6)', repeat(' ', len(SCHEME))//' C =', st%ssp_coefficient(), ', C max f_k =', &
st%ssp_coefficient() * maxval([(st%stage_euler_fraction(k), k=1, st%stages())])stage_euler_fraction(k)isfor stage : 0.3918, 0.6631, 0.6631, 0.6631, 0.5844 for ssp-54. It is the same in every storage form of a scheme, since the stages are the same.ssp_coefficient()is the SSP coefficient, 1.5082 for ssp-54, and. When forward Euler on the residual keeps the density admissible up to a step , the scheme keeps it up to ; at that step the longest forward-Euler step of any stage, , is itself. - A central difference has no such
: a cell at the floor next to a denser cell drops below the floor at any step. Here the limiter does the work, at any . masked_q=.true.is for the solid block. The Butcher form builds each stage state in a register, and with the mask policy the solid cells of every stage state hold(chapter 7). The program reads the whole stage state, solid cells included, and so does the ghost fill of the fluid cells next to block 4.
The limited stage
call st%begin_step(bx, q, mask=mask)
do k=1, st%stages()
call st%begin_stage(k, bx, dt, q, mask=mask)
if (st%stage_state_is_q(k)) then
u => q
else
u => st%stage_state()
endif
r => st%rhs_register()
lo(step) = min(lo(step), minval(u(1,1:NI,1,1,:))) ! every cell: the solid ones hold q^n (masked_q)
call fill_ghosts(u)
call residual(u, r, dx)
tau = st%stage_euler_fraction(k) * dt / share ! the forward-Euler step of stage k
call limit(u, r, tau, limited) ! u + tau r >= RHO_MIN in every fluid cell
call st%end_stage(k, bx, dt, q, mask=mask)
enddo
call st%finish_step(bx, dt, q, mask=mask)- The limiter acts in the window of stage
, after the residual and before end_stage(k): it changes the residual the stepper will combine, nothing else. - Its step is
stage_euler_fraction(k) * dt. The program runs the step twice: withshare = 1, the step the stage takes, and withshare = 2, half of it, so that the stage takes a forward-Euler step twice as long as the one the limiter made admissible. - The stage loop asks
stage_state_is_q(k), as in chapter 5, so it runs in every form; in this Butcher form the answer is always false.
The limiter
subroutine limit(u, r, tau, limited)
!< The residual of each fluid cell cut so that the forward-Euler state u + tau r stays >= RHO_MIN.
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: tau
integer(I4P), intent(inout) :: limited
integer(I4P) :: b, i
do b=1, NB
do i=1, NI
if (mask(1,i,1,1,b) >= 0._R_P) cycle ! a solid cell: the stepper ignores its residual
if (u(1,i,1,1,b) + tau * r(1,i,1,1,b) < RHO_MIN) then
r(1,i,1,1,b) = (RHO_MIN - u(1,i,1,1,b)) / tau ! the largest decrease the floor allows
limited = limited + 1
endif
enddo
enddo
endsubroutine limitThe simplest limiter that keeps
Running it
print '(A,ES9.3)', 'floor RHO_MIN = ', RHO_MIN
print '(A)', 'step lowest density of the step''s stage states and result'
print '(A)', ' tau_k = f_k dt tau_k = f_k dt / 2'
do step=1, NSTEPS
print '(I4,2ES17.3)', step, lo(step,:)
enddo
print '(A,2I17)', 'cells limited', limited
print '(A,L1)', 'tau_k = f_k dt: every stage state >= RHO_MIN: ', minval(lo(:,1)) >= RHO_MIN * (1._R_P - 1.e-12_R_P)
print '(A,L1)', 'tau_k = f_k dt / 2: a stage state < 0: ', minval(lo(:,2)) < 0._R_P
if (minval(lo(:,1)) < RHO_MIN * (1._R_P - 1.e-12_R_P)) error stop 'limiters: below the floor at tau_k = f_k dt'
if (minval(lo(:,2)) >= 0._R_P) error stop 'limiters: no negative density at tau_k = f_k dt / 2'$ limiters
ssp-54 f_k = 0.3918 0.6631 0.6631 0.6631 0.5844
C = 1.5082, C max f_k = 1.000000
floor RHO_MIN = 1.000E-02
step lowest density of the step's stage states and result
tau_k = f_k dt tau_k = f_k dt / 2
1 1.000E-02 -7.629E-02
2 1.000E-02 -2.169E-02
3 1.161E-02 2.003E-03
4 1.000E-02 -5.821E-04
5 1.289E-02 -1.176E-02
6 2.300E-02 -1.225E-03
7 1.659E-02 8.434E-03
8 1.461E-02 8.555E-03
9 1.000E-02 -2.680E-04
10 1.856E-02 -9.289E-03
11 1.000E-02 -1.465E-02
12 1.000E-02 -2.366E-03
13 1.000E-02 1.146E-02
14 2.122E-02 3.613E-03
15 1.000E-02 -1.807E-02
16 1.115E-02 1.161E-02
cells limited 79 51
tau_k = f_k dt: every stage state >= RHO_MIN: T
tau_k = f_k dt / 2: a stage state < 0: T
[exit status 0]With

The lowest density of each step's stage states and result, from the table above (docs/scripts/make_tutorial_figures.py). At
What you learned
stage_euler_fraction(k)is the forward-Euler step, as a fraction of, that the residual of stage enters; a limiter makes the state of stage plus that step of its residual admissible. - The limiter acts between the residual and
end_stage(k), on the residual register. - Less than
loses the guarantee; for every stage keeps it and limits more than needed. ssp_coefficient()is: the bound for a residual that has a forward-Euler limit. - With
masked_q=.true.the solid cells of a Butcher stage state hold, and a limiter or a stencil that reads them reads the solution.
Reference: Stepper: limiters and forward-Euler steps, Stepper: masks; the checks behind them: P2 SSP coefficients.
Try this
- No limiter. Comment out
call limit(u, r, tau, limited). Both columns are the same, negative at every step (at step 1, at the lowest, step 11), and the program stops at its first check: ERROR STOP limiters: below the floor at tau_k = f_k dt. - Another SSP scheme. Set
SCHEME=SCHEME_SSP_33:at every stage and ; the first column is at the floor at all 16 steps (67 cells limited), the second negative at 13 of them. SCHEME_SSP_104givesand : 123 cells limited, and at half the step the density is negative at steps 1 and 10 only ( and ). - The whole step for every stage (ADAM's FLUME did so). Write
tau = dt / share. The first column stays above the floor (lowest 0.0131, at step 13), and the limiter changes 113 cells instead of 79: it enforces a forward-Euler step longer than any stage ofssp-54takes. In this run the second column stays positive as well, so the program stops at its second check:ERROR STOP limiters: no negative density at tau_k = f_k dt / 2.
Pitfalls
- Solid cells without
masked_q. Initialise withoutmasked_q=.true.and the solid cells of the Butcher stage state keep the register's initial value, 0 here; the ghost fill hands those zeros to the fluid cells next to block 4. The lowest density is 0.000E+00 at every step, the limiter changes 442 cells instead of 79, and the program stops:ERROR STOP limiters: below the floor at tau_k = f_k dt. Fix:initialize(..., masked_q=.true.)whenever the limiter, the residual or a diagnostic reads the solid cells of a stage state (Stepper: masks). - The wrong query for the step.
stage_weight(k)is the weightof the stage in the step (the flux register's query, chapter 8), not its forward-Euler step. With tau = st%stage_weight(k) * dt / sharethe first column isat step 1 and negative at 9 of the 16 steps. Fix: stage_euler_fraction(k). - A scheme that is not SSP.
rk4,lsrk-33andlsrk-54have no forward-Euler decomposition:stage_euler_fraction(k)andssp_coefficient()are 0. WithSCHEME=SCHEME_RK4the limiter's step is 0, it divides by it, and the density is-Infinityfrom step 1 (IEEE_DIVIDE_BY_ZEROsignalling). Fix: limit with an SSP scheme, and checkst%ssp_coefficient() > 0afterinitialize.
The whole program
program limiters
!< Tutorial, chapter 11: a positivity limiter inside every stage of ssp-54, at the forward-Euler step of each stage.
use selah
use gideon, only : gideon_fd_d1_c2 ! the space operator: GIDEON's centred first derivative
implicit none
character(*), parameter :: SCHEME=SCHEME_SSP_54
integer(I4P), parameter :: NI=16, NB=4, NSTEPS=16
real(R_P), parameter :: RHO_MIN=1.e-2_R_P ! the floor: a density is admissible when >= RHO_MIN
real(R_P), allocatable, target :: q(:,:,:,:,:)
real(R_P), allocatable :: mask(:,:,:,:,:)
type(stepper) :: st
type(box) :: bx
real(R_P) :: dt, dx, lo(NSTEPS,2)
integer(I4P) :: limited(2), step, k, share
dx = 1._R_P / (NI * NB) ; dt = 0.25_R_P / NSTEPS ! dt = dx: a Courant number of 1
allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB), mask(1:1, 0:NI+1, 0:2, 0:2, 1:NB))
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
mask = -1._R_P
mask(:,:,:,:,4) = 1._R_P ! block 4 is solid
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(q), ub=ubound(q), masked_q=.true.) ! solid cells of a stage: q^n
print '(A,*(F7.4))', SCHEME//' f_k =', [(st%stage_euler_fraction(k), k=1, st%stages())]
print '(A,F7.4,A,F9.6)', repeat(' ', len(SCHEME))//' C =', st%ssp_coefficient(), ', C max f_k =', &
st%ssp_coefficient() * maxval([(st%stage_euler_fraction(k), k=1, st%stages())])
do share=1, 2
call advect(share, lo(:,share), limited(share))
enddo
print '(A,ES9.3)', 'floor RHO_MIN = ', RHO_MIN
print '(A)', 'step lowest density of the step''s stage states and result'
print '(A)', ' tau_k = f_k dt tau_k = f_k dt / 2'
do step=1, NSTEPS
print '(I4,2ES17.3)', step, lo(step,:)
enddo
print '(A,2I17)', 'cells limited', limited
print '(A,L1)', 'tau_k = f_k dt: every stage state >= RHO_MIN: ', minval(lo(:,1)) >= RHO_MIN * (1._R_P - 1.e-12_R_P)
print '(A,L1)', 'tau_k = f_k dt / 2: a stage state < 0: ', minval(lo(:,2)) < 0._R_P
if (minval(lo(:,1)) < RHO_MIN * (1._R_P - 1.e-12_R_P)) error stop 'limiters: below the floor at tau_k = f_k dt'
if (minval(lo(:,2)) >= 0._R_P) error stop 'limiters: no negative density at tau_k = f_k dt / 2'
call st%destroy
contains
subroutine advect(share, lo, limited)
!< NSTEPS steps of the limited advection, the limiter's step tau_k = f_k dt / share; the lowest density per step.
integer(I4P), intent(in) :: share !< 1: the step the stage takes; 2: half of it.
real(R_P), intent(out) :: lo(:) !< The lowest density of each step's stage states and result.
integer(I4P), intent(out) :: limited !< Cells the limiter changed, over every stage.
real(R_P), pointer :: u(:,:,:,:,:), r(:,:,:,:,:)
real(R_P) :: tau
integer(I4P) :: step, k, b, i
do b=1, NB ! a slab of density 1 on block 2, 0.1 elsewhere
do i=0, NI+1
q(1,i,:,:,b) = merge(1._R_P, 0.1_R_P, b == 2 .and. i >= 1 .and. i <= NI)
enddo
enddo
limited = 0
do step=1, NSTEPS
lo(step) = huge(1._R_P)
call st%begin_step(bx, q, mask=mask)
do k=1, st%stages()
call st%begin_stage(k, bx, dt, q, mask=mask)
if (st%stage_state_is_q(k)) then
u => q
else
u => st%stage_state()
endif
r => st%rhs_register()
lo(step) = min(lo(step), minval(u(1,1:NI,1,1,:))) ! every cell: the solid ones hold q^n (masked_q)
call fill_ghosts(u)
call residual(u, r, dx)
tau = st%stage_euler_fraction(k) * dt / share ! the forward-Euler step of stage k
call limit(u, r, tau, limited) ! u + tau r >= RHO_MIN in every fluid cell
call st%end_stage(k, bx, dt, q, mask=mask)
enddo
call st%finish_step(bx, dt, q, mask=mask)
lo(step) = min(lo(step), minval(q(1,1:NI,1,1,:)))
enddo
endsubroutine advect
subroutine limit(u, r, tau, limited)
!< The residual of each fluid cell cut so that the forward-Euler state u + tau r stays >= RHO_MIN.
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: tau
integer(I4P), intent(inout) :: limited
integer(I4P) :: b, i
do b=1, NB
do i=1, NI
if (mask(1,i,1,1,b) >= 0._R_P) cycle ! a solid cell: the stepper ignores its residual
if (u(1,i,1,1,b) + tau * r(1,i,1,1,b) < RHO_MIN) then
r(1,i,1,1,b) = (RHO_MIN - u(1,i,1,1,b)) / tau ! the largest decrease the floor allows
limited = limited + 1
endif
enddo
enddo
endsubroutine limit
subroutine fill_ghosts(u)
real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
integer(I4P) :: b
do b=1, NB
u(1,0,1,1,b) = u(1,NI,1,1,1 + modulo(b-2, NB))
u(1,NI+1,1,1,b) = u(1,1,1,1,1 + modulo(b, NB))
enddo
endsubroutine fill_ghosts
subroutine residual(u, r, dx)
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: dx
integer(I4P) :: b, i
real(R_P) :: d
do b=1, NB
do i=1, NI
call gideon_fd_d1_c2([u(1,i-1,1,1,b), u(1,i,1,1,b), u(1,i+1,1,1,b)], dx, d) ; r(1,i,1,1,b) = -d
enddo
enddo
endsubroutine residual
endprogram limitersNext: 12. Additive splitting.