14. An absorbing layer
Goal: advance a stiff auxiliary field decay=lambda, so that its decay is integrated exactly and the step stays stable at
Absorbing layers carry auxiliary fields that relax fast: the convolutional perfectly matched layer (PML) of a wave solver is the common case, with real_axis_bound(), and explicit stages of decay=lambda the stepper integrates the decay exactly and the residual is the source
This chapter's program is not a PML. It has the PML's auxiliary equation without its geometry: a Gaussian pulse advected with the tutorial's central residual,
The layer and its auxiliary field
lam = 0._R_P ; sig = 0._R_P ! outside the layer psi stays 0
do i=1, NI ! the layer: block 4, x in (3/4, 1)
lam(1,i,1,1,4) = LAMBDA_DT / dt ! the decay rate of psi: lambda dt = 50
sig(1,i,1,1,4) = KAPPA * ((i - 0.5_R_P) / NI)**2 * lam(1,i,1,1,4) ! psi -> kappa(x) u: a graded damping
enddo
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))
call sp%initialize_companion(st, lb=lbound(psi), ub=ubound(psi))psiis a companion of the field stepper (chapter 13): the same scheme and form, the same calls in the same stage window.lamhas the shape ofpsi. It is 0 outside the layer, where the step withdecay=is bitwise the explicit one, andinside.
print '(A,F7.4,A,F5.1,A,F9.7)', SCHEME//': real-axis bound', st%real_axis_bound(), ', lambda dt in the layer', &
LAMBDA_DT, ', dt ', dtreal_axis_bound() is 5.3315 for ssp-54: the largest
The step
call st%begin_step(bx, q) ; call sp%begin_step(bx, psi)
do k=1, st%stages()
call st%begin_stage(k, bx, dt, q)
if (exact) then
call sp%begin_stage(k, bx, dt, psi, decay=lam) ! the stage state of psi, its decay exact
else
call sp%begin_stage(k, bx, dt, psi)
endif
u => st%stage_state() ; p => sp%stage_state()
ru => st%rhs_register() ; rp => sp%rhs_register()
call fill_ghosts(u)
call residual(u, p, ru, rp, merge(0._R_P, 1._R_P, exact)) ! with decay=, the source of psi alone
call st%end_stage(k, bx, dt, q) ; call sp%end_stage(k, bx, dt, psi)
enddo
call st%finish_step(bx, dt, q)
if (exact) then
call sp%finish_step(bx, dt, psi, decay=lam)
else
call sp%finish_step(bx, dt, psi)
endifsubroutine residual(u, p, ru, rp, explicit)
!< u' = -u_x - psi; psi' = sigma u - explicit * lambda psi (explicit = 0 when the stepper integrates the decay).
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:), p(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: ru(1:,0:,0:,0:,1:), rp(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: explicit
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)
ru(1,i,1,1,b) = -d - p(1,i,1,1,b)
rp(1,i,1,1,b) = sig(1,i,1,1,b) * u(1,i,1,1,b) - explicit * lam(1,i,1,1,b) * p(1,i,1,1,b)
enddo
enddo
endsubroutine residual- With
decay=lamat everybegin_stageofpsiand at itsfinish_step, the residual ofpsiis the sourcealone. With the stage state is and the step , . - The explicit run is the same program with
in the residual and no decay=. - The field
uis stepped as always; onlypsitakesdecay=.
The exact decay is exponential Euler on the Runge–Kutta-averaged source: exact for a source frozen over the step and for static equilibria, stable at any
Running it
print '(A)', ' t energy / energy(0): decay= explicit'
do step=0, NSTEPS, NSTEPS / 16
do run=1, 2
col(run) = ' stopped'
if (energy(step,run) >= 0._R_P) write(col(run), '(ES13.3)') energy(step,run) / energy(0,run)
enddo
print '(F6.3,A28,A13)', step * dt, col
enddo
print '(A)', 'explicit, step by step until it stops:'
print '(4ES11.3)', pack(energy(1:,2), energy(1:,2) >= 0._R_P) / energy(0,2)
do run=1, 2
if (blowup(run) > 0) then
print '(A,I0,A,F6.4)', trim(merge('decay=: ', 'explicit:', run == 1))// &
' the energy passed 1e6 times its start at step ', blowup(run), ', t = ', blowup(run) * dt
else
print '(A,F6.4,A,ES9.2)', trim(merge('decay=: ', 'explicit:', run == 1))// &
' bounded: largest energy / energy(0) ', maxval(energy(:,run)) / energy(0,run), &
', at t = 1 ', energy(NSTEPS,run) / energy(0,run)
endif
enddo
if (blowup(2) == 0) error stop 'decay_layer: the explicit run did not blow up'
if (blowup(1) > 0 .or. maxval(energy(:,1)) > energy(0,1)) error stop 'decay_layer: the energy grew with decay='
if (energy(NSTEPS,1) > 1.e-2_R_P * energy(0,1)) error stop 'decay_layer: the layer did not absorb the pulse'$ decay_layer
ssp-54: real-axis bound 5.3315, lambda dt in the layer 50.0, dt 0.0078125
t energy / energy(0): decay= explicit
0.000 1.000E+00 1.000E+00
0.062 1.000E+00 stopped
0.125 1.000E+00 stopped
0.188 1.000E+00 stopped
0.250 1.000E+00 stopped
0.312 9.999E-01 stopped
0.375 9.953E-01 stopped
0.438 9.181E-01 stopped
0.500 6.036E-01 stopped
0.562 2.145E-01 stopped
0.625 5.114E-02 stopped
0.688 1.774E-02 stopped
0.750 8.168E-03 stopped
0.812 4.636E-03 stopped
0.875 3.114E-03 stopped
0.938 2.382E-03 stopped
1.000 1.997E-03 stopped
explicit, step by step until it stops:
1.000E+00 1.000E+00 1.000E+00 1.000E+00
2.918E+04 3.683E+16
decay=: bounded: largest energy / energy(0) 1.0000, at t = 1 2.00E-03
explicit: the energy passed 1e6 times its start at step 6, t = 0.0469
[exit status 0]The explicit run blows up at step 6, long before the pulse arrives: the Gaussian's tail in the layer, below decay= the energy never exceeds its start, and the layer takes it down to 0.2% by

The energy of both runs, from the table and the step-by-step line above (docs/scripts/make_tutorial_figures.py).
What you learned
decay=lambdaat everybegin_stageand atfinish_stepintegratesexactly; the residual is the source alone. - It is stable at any
; the explicit stages are stable only up to real_axis_bound(). - Where
the step is bitwise the explicit one, so one lamarray covers the layer and the rest. decay=is the Butcher form's, takes no mask and nodq; the field that feeds the source still sets the step.
Reference: Stepper: a decay integrated exactly, Stepper: companion states; the checks behind them: P5 Exact decay.
Try this
- Below the bound. Set
LAMBDA_DT=5._R_P, under 5.3315: both runs stay bounded and absorb alike (2.01E-03 and 1.99E-03 of the energy left at), and the program stops: ERROR STOP decay_layer: the explicit run did not blow up. AtLAMBDA_DT=6._R_Pthe explicit run blows up at step 59 (), once the pulse is in the layer. - A stronger layer. Set
KAPPA=200._R_P: withdecay=4.03E-04 of the energy is left at, five times less; the explicit run still blows up at step 6. - Another scheme. Set
SCHEME=SCHEME_RK4: the real-axis bound is 2.7853,decay=leaves 1.99E-03 of the energy at, and the explicit run blows up at step 6. SCHEME_SSP_33: 2.5127, 1.71E-03, step 8.
Pitfalls
left in the residual. With decay=the stepper integrates the decay; keeping it in the residual counts it twice. The run stays bounded but absorbs less: 3.37E-02 of the energy left at, and the program stops: ERROR STOP decay_layer: the layer did not absorb the pulse. Fix: the residual is the source alone.decay=atbegin_stageonly. Without it atfinish_stepthe step's update is explicit while its stages are not: 6.85E-01 of the energy is left at, the same stop. Fix: pass decay=lamto everybegin_stageand tofinish_stepof the companion.- A source too stiff for the step. The source
is stiff too: the damping it gives the field is integrated explicitly. With KAPPA=1000._R_P,reaches 7.3, above the bound 5.3315, and the run with decay=blows up at step 76 (), when the pulse is in the layer: ERROR STOP decay_layer: the energy grew with decay=. Fix: takefrom the coupled field– limit, not from the CFL condition alone (Stepper). - Another form.
decay=is the Butcher form's. Withssp-33inFORM_SHU_OSHER_2Rthe program stops:ERROR STOP selah stepper: decay= is the Butcher form's; the leapfrog form stops the same way (selah_test_xfail_leapfrog_decay), and a mask with it stops withdecay= takes no mask. Fix:FORM_BUTCHER, no mask.
The whole program
program decay_layer
!< Tutorial, chapter 14: a pulse advected into an absorbing layer whose damping is an auxiliary field psi, a companion
!< of the field stepper; the decay of psi integrated exactly (decay=) or explicitly, at the same step.
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=128
real(R_P), parameter :: PI=acos(-1._R_P), T_END=1._R_P, LAMBDA_DT=50._R_P, KAPPA=40._R_P
real(R_P), allocatable, target :: q(:,:,:,:,:), psi(:,:,:,:,:)
real(R_P), allocatable :: lam(:,:,:,:,:), sig(:,:,:,:,:)
type(stepper) :: st, sp
type(box) :: bx
real(R_P) :: dx, dt, energy(0:NSTEPS,2)
integer(I4P) :: run, step, blowup(2), i
character(13) :: col(2)
dx = 1._R_P / (NI * NB) ; dt = T_END / NSTEPS ! a Courant number of 1/2
allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB))
allocate(psi, lam, sig, mold=q)
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
lam = 0._R_P ; sig = 0._R_P ! outside the layer psi stays 0
do i=1, NI ! the layer: block 4, x in (3/4, 1)
lam(1,i,1,1,4) = LAMBDA_DT / dt ! the decay rate of psi: lambda dt = 50
sig(1,i,1,1,4) = KAPPA * ((i - 0.5_R_P) / NI)**2 * lam(1,i,1,1,4) ! psi -> kappa(x) u: a graded damping
enddo
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))
call sp%initialize_companion(st, lb=lbound(psi), ub=ubound(psi))
print '(A,F7.4,A,F5.1,A,F9.7)', SCHEME//': real-axis bound', st%real_axis_bound(), ', lambda dt in the layer', &
LAMBDA_DT, ', dt ', dt
do run=1, 2
call advance(run == 1, energy(:,run), blowup(run))
enddo
print '(A)', ' t energy / energy(0): decay= explicit'
do step=0, NSTEPS, NSTEPS / 16
do run=1, 2
col(run) = ' stopped'
if (energy(step,run) >= 0._R_P) write(col(run), '(ES13.3)') energy(step,run) / energy(0,run)
enddo
print '(F6.3,A28,A13)', step * dt, col
enddo
print '(A)', 'explicit, step by step until it stops:'
print '(4ES11.3)', pack(energy(1:,2), energy(1:,2) >= 0._R_P) / energy(0,2)
do run=1, 2
if (blowup(run) > 0) then
print '(A,I0,A,F6.4)', trim(merge('decay=: ', 'explicit:', run == 1))// &
' the energy passed 1e6 times its start at step ', blowup(run), ', t = ', blowup(run) * dt
else
print '(A,F6.4,A,ES9.2)', trim(merge('decay=: ', 'explicit:', run == 1))// &
' bounded: largest energy / energy(0) ', maxval(energy(:,run)) / energy(0,run), &
', at t = 1 ', energy(NSTEPS,run) / energy(0,run)
endif
enddo
if (blowup(2) == 0) error stop 'decay_layer: the explicit run did not blow up'
if (blowup(1) > 0 .or. maxval(energy(:,1)) > energy(0,1)) error stop 'decay_layer: the energy grew with decay='
if (energy(NSTEPS,1) > 1.e-2_R_P * energy(0,1)) error stop 'decay_layer: the layer did not absorb the pulse'
call st%destroy ; call sp%destroy
contains
subroutine advance(exact, energy, blowup)
!< NSTEPS steps of the field and its auxiliary psi; the energy sum(u^2) dx after each, -1 once the run stops.
logical, intent(in) :: exact !< decay= (else -lambda psi in the residual of psi).
real(R_P), intent(out) :: energy(0:)
integer(I4P), intent(out) :: blowup !< The step at which the energy passed 1e6 times its start (0: never).
real(R_P), pointer :: u(:,:,:,:,:), p(:,:,:,:,:), ru(:,:,:,:,:), rp(:,:,:,:,:)
integer(I4P) :: step, k, b, i
psi = 0._R_P
do b=1, NB ! a Gaussian pulse at x = 3/8
do i=1, NI
q(1,i,1,1,b) = exp(-((((b-1)*NI + i - 0.5_R_P)*dx - 0.375_R_P) / 0.05_R_P)**2)
enddo
enddo
energy = -1._R_P ; energy(0) = sum(q(1,1:NI,1,1,:)**2) * dx ; blowup = 0
do step=1, NSTEPS
call st%begin_step(bx, q) ; call sp%begin_step(bx, psi)
do k=1, st%stages()
call st%begin_stage(k, bx, dt, q)
if (exact) then
call sp%begin_stage(k, bx, dt, psi, decay=lam) ! the stage state of psi, its decay exact
else
call sp%begin_stage(k, bx, dt, psi)
endif
u => st%stage_state() ; p => sp%stage_state()
ru => st%rhs_register() ; rp => sp%rhs_register()
call fill_ghosts(u)
call residual(u, p, ru, rp, merge(0._R_P, 1._R_P, exact)) ! with decay=, the source of psi alone
call st%end_stage(k, bx, dt, q) ; call sp%end_stage(k, bx, dt, psi)
enddo
call st%finish_step(bx, dt, q)
if (exact) then
call sp%finish_step(bx, dt, psi, decay=lam)
else
call sp%finish_step(bx, dt, psi)
endif
energy(step) = sum(q(1,1:NI,1,1,:)**2) * dx
if (energy(step) > 1.e6_R_P * energy(0)) then ! blown up: stop before an overflow
blowup = step
exit
endif
enddo
endsubroutine advance
subroutine residual(u, p, ru, rp, explicit)
!< u' = -u_x - psi; psi' = sigma u - explicit * lambda psi (explicit = 0 when the stepper integrates the decay).
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:), p(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: ru(1:,0:,0:,0:,1:), rp(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: explicit
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)
ru(1,i,1,1,b) = -d - p(1,i,1,1,b)
rp(1,i,1,1,b) = sig(1,i,1,1,b) * u(1,i,1,1,b) - explicit * lam(1,i,1,1,b) * p(1,i,1,1,b)
enddo
enddo
endsubroutine residual
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
endprogram decay_layerNext: 15. The leapfrog.