12. Additive splitting
Goal: advance additive_splitting, the stepper integrating lie and 2 of strang.
Some terms are better integrated by the caller than by a Runge–Kutta stage: a cooling law with an exact solution, a stiff relaxation, a particle step. additive_splitting composes a step of the stepper (the field step,
This chapter's program advects ssp-54), and cools it with
The composition
call sp%initialize(scheme) ! lie: field, flow; strang: flow/2, field, flow/2
do n=1, nsteps
do m=1, sp%substeps()
h = sp%substep_fraction(m) * dt ! the substep's length
if (sp%substep_operator(m) == OPERATOR_FIELD) then
call field_step(h) ! one step of the stepper, of length h
else
call cool(q, h) ! the caller's flow over h
endif
enddo
enddoinitialize('lie')is field, then flow ; initialize('strang')is flow, field , flow . The first=argument swaps the operators:OPERATOR_FIELDfor strang is field, flow , field . substeps()is 2 for lie and 3 for strang. Substepmruns operatorsubstep_operator(m),OPERATOR_FIELDorOPERATOR_FLOW, forsubstep_fraction(m)of the step.- Both operators here are autonomous. With a term that depends on time, substep
mstarts att + substep_time(m)*dt: each operator keeps its own clock (Composition: time).
The two operators
The field step is one step of the stepper, of length h, on the advection alone:
subroutine field_step(h)
!< One step of the stepper on the advection alone, of length h.
real(R_P), intent(in) :: h
real(R_P), pointer :: u(:,:,:,:,:), r(:,:,:,:,:)
integer(I4P) :: k
call st%begin_step(bx, q)
do k=1, st%stages()
call st%begin_stage(k, bx, h, q)
u => st%stage_state()
r => st%rhs_register()
call fill_ghosts(u)
call residual(u, r, cooling=.false.)
call st%end_stage(k, bx, h, q)
enddo
call st%finish_step(bx, h, q)
endsubroutine field_stepThe flow is the exact solution of h:
subroutine cool(u, h)
!< The exact flow of u' = -k(x) u^2 over h: 1/u grows by k h (Townsend's exact cooling has this form).
real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: h
integer(I4P) :: b, i
do b=1, NB
do i=1, NI
u(1,i,1,1,b) = u(1,i,1,1,b) / (1._R_P + kc((b-1)*NI + i) * h * u(1,i,1,1,b))
enddo
enddo
endsubroutine cool- The flow is the caller's code: SELAH calls nothing. It runs on
q, between two steps of the stepper, when no stage window is open. - The order of the composition is that of
lieorstrangonly when each operator is at least that accurate. The exact flow is; the field step,ssp-54of order 4, adds anerror that is invisible next to the splitting's (A6 measures this with exact field flows: the same errors to three digits). - The reference solution is the whole system
through the stepper with rk4and 4096 steps.
Running it
print '(A)', 'steps dt error, lie error, strang'
do l=1, NLEVELS
print '(I5,ES10.2,2ES13.3)', 8 * 2**(l-1), T_END / (8 * 2**(l-1)), err(l,:)
enddo
print '(A,4F6.2)', 'observed orders, lie: ', p(:,1)
print '(A,4F6.2)', 'observed orders, strang:', p(:,2)
do s=1, 2
if (any(abs(p(:,s) - ORDERS(s)) > 0.15_R_P)) error stop 'additive: '//trim(SCHEMES(s))//' off its order'
enddo
print '(A)', 'lie order 1, strang order 2: ok'$ additive
steps dt error, lie error, strang
8 3.12E-02 3.584E-02 1.302E-03
16 1.56E-02 1.821E-02 3.348E-04
32 7.81E-03 9.179E-03 8.440E-05
64 3.91E-03 4.609E-03 2.115E-05
128 1.95E-03 2.309E-03 5.290E-06
observed orders, lie: 0.98 0.99 0.99 1.00
observed orders, strang: 1.96 1.99 2.00 2.00
lie order 1, strang order 2: ok
[exit status 0]Halving the step halves the error of lie and quarters that of strang: the observed orders are 0.98 to 1.00, and 1.96 to 2.00, and the program checks every one of them within 0.15 of 1 and 2. At 128 steps strang is 436 times more accurate than lie, for one more flow per step.

The errors of the table against the step, with slope-1 and slope-2 guides (docs/scripts/make_tutorial_figures.py).
strang puts the flow outside by default: the field step, the costly one, runs once per step. An exact autonomous flow merges across steps, strang then costs what lie does (Composition). The program keeps them apart.
What you learned
additive_splittinggives the composition (substeps,substep_operator,substep_fraction,substep_time); the caller runs both operators.- The field step is an ordinary step of the stepper, with
hin place ofdt; the flow is any code that advancesqoverh. lieis first order andstrangsecond, when each operator is at least as accurate as the composition.- The default
firstputs the cheap flow outside instrang, so the field step runs once per step.
Reference: Composition; the checks behind it: A6 Additive splitting.
Try this
- Field first.
call sp%initialize(scheme, first=OPERATOR_FIELD):strangbecomes field, flow , field , twice the field steps and half the error here (2.618E-06 at 128 steps), still order 2. lieis unchanged, since field first is its default. - Flow first for lie.
first=OPERATOR_FLOWmakeslieflow, field : errors within 7% of the field-first ones (2.319E-03 at 128 steps), orders 1.02 to 1.00. - A stiffer cooling. Set
K0=1000._R_P, so thatreaches about 31 at 8 steps. liekeeps orders 1.01 to 1.11;strangmeasures 3.36, 2.28, 1.81, 1.95 and reaches its order only at the finest steps, so the program stops:ERROR STOP additive: strang off its order.
Pitfalls
- A splitting of higher order with cooling.
yoshida-4,blanes-moan-s6andblanes-moan-s10run each operator backward over part of the step, which is ill-posed for cooling or for a field step with an upwind or limited residual.initializerefuses them:ERROR STOP selah additive splitting: "yoshida-4" runs substeps backward in time: lie or strang for an irreversible field step or flow (reversible=.true. if both operators are reversible). Fix:lieorstrang; passreversible=.true.only when both operators are reversible (a particle Hamiltonian). - The flow over
dtinstead ofh.call cool(q, dt)changes nothing forlie, whose fraction is 1, butstrangthen cools twice per step: its error is 0.211 at every step count, order 0. Fix: every operator runs forsubstep_fraction(m) * dt. - A flow less accurate than the composition. Replace the exact flow by a forward-Euler step,
u = u - k h u**2:liekeeps order 1,strangdrops to order 1 (1.453E-03 at 128 steps, 275 times the exact flow's error), and the program stops. Fix: an exact flow, or one of order 2 at least forstrang.
The whole program
program additive
!< Tutorial, chapter 12: additive splitting of the stepper's advection and the caller's exact cooling, lie and strang.
use selah
use gideon, only : gideon_fd_d1_c2 ! the space operator: GIDEON's centred first derivative
implicit none
integer(I4P), parameter :: NI=16, NB=4, NLEVELS=5, NREF=4096
real(R_P), parameter :: PI=acos(-1._R_P), T_END=0.25_R_P, K0=8._R_P
character(*), parameter :: SCHEMES(2)=[character(6) :: SCHEME_LIE, SCHEME_STRANG]
real(R_P), parameter :: ORDERS(2)=[1._R_P, 2._R_P]
real(R_P), allocatable, target :: q(:,:,:,:,:)
real(R_P), allocatable :: kc(:), qref(:,:,:,:,:)
type(stepper) :: st
type(box) :: bx
real(R_P) :: dx, err(NLEVELS,2), p(NLEVELS-1,2)
integer(I4P) :: s, l, b, i
dx = 1._R_P / (NI * NB)
allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB), kc(NI*NB))
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
do b=1, NB
do i=1, NI
kc((b-1)*NI + i) = K0 * sin(PI*((b-1)*NI + i - 0.5_R_P)*dx)**2 ! the cooling rate: 0 at x = 0, K0 at x = 1/2
enddo
enddo
call reference ! rk4 on the whole system, NREF steps
call st%initialize(SCHEME_SSP_54, FORM_BUTCHER, lb=lbound(q), ub=ubound(q)) ! the field step
do s=1, 2
do l=1, NLEVELS
err(l,s) = split(trim(SCHEMES(s)), 8 * 2**(l-1))
enddo
p(:,s) = log(err(1:NLEVELS-1,s) / err(2:NLEVELS,s)) / log(2._R_P)
enddo
print '(A)', 'steps dt error, lie error, strang'
do l=1, NLEVELS
print '(I5,ES10.2,2ES13.3)', 8 * 2**(l-1), T_END / (8 * 2**(l-1)), err(l,:)
enddo
print '(A,4F6.2)', 'observed orders, lie: ', p(:,1)
print '(A,4F6.2)', 'observed orders, strang:', p(:,2)
do s=1, 2
if (any(abs(p(:,s) - ORDERS(s)) > 0.15_R_P)) error stop 'additive: '//trim(SCHEMES(s))//' off its order'
enddo
print '(A)', 'lie order 1, strang order 2: ok'
call st%destroy
contains
function split(scheme, nsteps) result(err)
!< nsteps steps of the composition to T_END; the largest difference from the reference.
character(*), intent(in) :: scheme
integer(I4P), intent(in) :: nsteps
real(R_P) :: err
type(additive_splitting) :: sp
real(R_P) :: dt, h
integer(I4P) :: n, m
call initial(q)
dt = T_END / nsteps
call sp%initialize(scheme) ! lie: field, flow; strang: flow/2, field, flow/2
do n=1, nsteps
do m=1, sp%substeps()
h = sp%substep_fraction(m) * dt ! the substep's length
if (sp%substep_operator(m) == OPERATOR_FIELD) then
call field_step(h) ! one step of the stepper, of length h
else
call cool(q, h) ! the caller's flow over h
endif
enddo
enddo
err = maxval(abs(q(1,1:NI,1,1,:) - qref(1,1:NI,1,1,:)))
endfunction split
subroutine field_step(h)
!< One step of the stepper on the advection alone, of length h.
real(R_P), intent(in) :: h
real(R_P), pointer :: u(:,:,:,:,:), r(:,:,:,:,:)
integer(I4P) :: k
call st%begin_step(bx, q)
do k=1, st%stages()
call st%begin_stage(k, bx, h, q)
u => st%stage_state()
r => st%rhs_register()
call fill_ghosts(u)
call residual(u, r, cooling=.false.)
call st%end_stage(k, bx, h, q)
enddo
call st%finish_step(bx, h, q)
endsubroutine field_step
subroutine cool(u, h)
!< The exact flow of u' = -k(x) u^2 over h: 1/u grows by k h (Townsend's exact cooling has this form).
real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(in) :: h
integer(I4P) :: b, i
do b=1, NB
do i=1, NI
u(1,i,1,1,b) = u(1,i,1,1,b) / (1._R_P + kc((b-1)*NI + i) * h * u(1,i,1,1,b))
enddo
enddo
endsubroutine cool
subroutine reference
!< The whole system u' = -u_x - k u^2 with rk4 through the stepper, NREF steps: the reference solution.
type(stepper) :: ref
real(R_P), pointer :: u(:,:,:,:,:), r(:,:,:,:,:)
real(R_P) :: dt
integer(I4P) :: n, k
call initial(q)
dt = T_END / NREF
call ref%initialize(SCHEME_RK4, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))
do n=1, NREF
call ref%begin_step(bx, q)
do k=1, ref%stages()
call ref%begin_stage(k, bx, dt, q)
u => ref%stage_state()
r => ref%rhs_register()
call fill_ghosts(u)
call residual(u, r, cooling=.true.)
call ref%end_stage(k, bx, dt, q)
enddo
call ref%finish_step(bx, dt, q)
enddo
call ref%destroy
qref = q
endsubroutine reference
subroutine initial(u)
!< u = 1 + sin(2 pi x)/2: positive, so that the cooling is defined.
real(R_P), intent(out) :: u(1:,0:,0:,0:,1:)
integer(I4P) :: b, i
u = 0._R_P
do b=1, NB
do i=1, NI
u(1,i,1,1,b) = 1._R_P + 0.5_R_P * sin(2._R_P*PI*((b-1)*NI + i - 0.5_R_P)*dx)
enddo
enddo
endsubroutine initial
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, cooling)
!< r = -u_x (GIDEON's central difference), minus k u^2 when cooling.
real(R_P), intent(in) :: u(1:,0:,0:,0:,1:)
real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
logical, intent(in) :: cooling
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
if (cooling) r(1,i,1,1,b) = r(1,i,1,1,b) - kc((b-1)*NI + i) * u(1,i,1,1,b)**2
enddo
enddo
endsubroutine residual
endprogram additiveNext: 13. Particles.