1. A step by hand
Before any field, any box or any stepper: what does SELAH compute? An explicit Runge–Kutta step of
SELAH provides the first and the third as kernels. The second, the residual, is always yours. This chapter writes a whole step of SSP(5,4) with them, on three decoupled equations
program by_hand
!< Tutorial, chapter 1: one step of SSP(5,4) by hand, with the flat kernels, on y' = -lambda y.
use selah
implicit none
integer(I4P), parameter :: N=3 ! three independent equations
real(R_P), parameter :: LAMBDA(N) = [0.5_R_P, 1._R_P, 2._R_P] ! their rates
type(butcher_tableau) :: bt ! the scheme's coefficients
real(R_P), allocatable :: regs(:,:) ! one residual register per stage
real(R_P) :: y(N), u(N), err(4), dt, a_row(5)
integer(I4P) :: nsteps, step, s, k
bt = butcher(SCHEME_SSP_54)
allocate(regs(N, bt%stages))
print '(A,I0,A,I0)', 'ssp-54: stages ', bt%stages, ', order ', bt%order
do k=1, 4
nsteps = 5 * 2**(k-1)
dt = 1._R_P / nsteps
y = 1._R_P
do step=1, nsteps
do s=1, bt%stages
a_row = bt%a(s,:) ! row s of A, contiguous
call selah_stage_state(s-1, dt, a_row, y, regs, u) ! u = y + dt sum_{j<s} a(s,j) F_j
regs(:,s) = -LAMBDA * u ! the caller's residual F_s = f(u)
enddo
call selah_step_update(bt%stages, dt, bt%b, regs, y) ! y = y + dt sum_s b(s) F_s
enddo
err(k) = maxval(abs(y - exp(-LAMBDA)))
print '(A,I3,A,ES10.3)', 'steps ', nsteps, ': error at t = 1 ', err(k)
enddo
print '(A,3F6.2)', 'observed orders:', log(err(1:3) / err(2:4)) / log(2._R_P)
endprogram by_handbutcher(SCHEME_SSP_54)returns the scheme's coefficients as abutcher_tableau:stages,order, the matrixa, the weightsband the abscissaec.selah_stage_state(n, dt, coef, q, regs, u)writesu = q + dt Σ_{k≤n} coef(k) regs(:,k). For stages,n = s-1andcoefis rowsof A. Withn = 0it is a copy.- The loop body
regs(:,s) = -LAMBDA * uis the caller's residual: SELAH never calls back into your code. selah_step_update(n, dt, coef, regs, y)writesy = y + dt Σ_k coef(k) regs(:,k).
These are the flat forms of the kernels: they work on rank-1 arrays of any length (particles, a reduced model, a whole field flattened). Chapter 3 introduces their box forms for block-structured fields.
Running it
$ by_hand
ssp-54: stages 5, order 4
steps 5: error at t = 1 3.443E-05
steps 10: error at t = 1 1.897E-06
steps 20: error at t = 1 1.112E-07
steps 40: error at t = 1 6.734E-09
observed orders: 4.18 4.09 4.05Halving the step divides the error by 16: fourth order, as SSP(5,4) promises.
Contiguous coefficients on a device
The program copies row s of A into a_row before the call. On the host a strided section bt%a(s,:) would work too, but a device kernel receives its arrays by address (deviceptr), so on a GPU the coefficients must be contiguous. The stepper keeps the transpose of A for this reason.
What you learned
A Runge–Kutta step is stage states, residuals and an update; SELAH computes the first and the last, you compute the residual. Reference: Kernels, Schemes.
Next: 2. The catalogue.