Skip to content

Stepper ​

SELAH's L2 layer is the stepper (type(stepper)): one time step that the caller drives one stage at a time. The stepper owns the registers and the coefficients. The caller owns the solution, the time loop, the ghost cells and the residual. The stepper never calls back.

A step ​

fortran
use selah
type(stepper) :: st

call st%initialize(SCHEME_SSP_54, FORM_BUTCHER, lb=lbound(q), ub=ubound(q))
! ... every step:
call st%begin_step(bx, q)
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   ! the stage state: q itself when st%stage_state_is_q(k), else st%stage_state()
   ! fill its ghost cells (exchange, seams, boundaries), then write its residual into st%rhs_register()
   call st%end_stage(k, bx, dt, q)
enddo
call st%finish_step(bx, dt, q)        ! the Butcher form can also return the step residual: dq=...
! ... at the end:
call st%destroy
The stage contract

One step, four calls, two owners

Whatever the scheme or the storage form, a step is the same sequence. You own the solution, its ghosts and the residual; SELAH owns the registers and every update rule.

The stage contract: the calls of one step and who acts at eachOne step of any scheme: begin_step(bx, q); then for k from 1 to stages(): begin_stage(k, bx, dt, q), the caller fills the ghost cells of the stage state and writes its residual into rhs_register(), end_stage(k, bx, dt, q); then finish_step(bx, dt, q). Inside the stage window of k, stage_state() and rhs_register() are live; outside it they are null.YOUR SOLVERSELAH STEPPERtime loop, q,ghosts, residualreflux withstage_weight(k)1 evaluate on q or stage_state()2 fill the ghosts of the stage state3 write the residual into rhs_register()begin_step(bx, q)begin_stage(k, bx, dt, q)end_stage(k, bx, dt, q)finish_step(bx, dt, q)next k, up to stages()copies qⁿ where theform needs itbuilds the stage stateButcher form; the otherskeep it in qadvances q in place2R, 2N, Ketcheson,splitting, leapfrog, linear SSPthe Butcher update(and dq); the leapfrogkeeps its historythe stage window of k: stage_state(), rhs_register() live · outside it: null
  1. begin_step(bx, q): SELAH copies qⁿ where the form needs it.
  2. For k from 1 to stages(): begin_stage(k, bx, dt, q), where SELAH builds the stage state (Butcher form; the others keep it in q).
  3. Inside the stage window, you evaluate on q or stage_state(), fill the ghost cells of the stage state, and write the residual into rhs_register().
  4. end_stage(k, bx, dt, q): SELAH advances q in place in the 2R, 2N, Ketcheson, splitting, leapfrog, linear SSP forms; then the next k.
  5. finish_step(bx, dt, q): the Butcher update (and dq); the leapfrog keeps its history. You reflux with stage_weight(k).
  6. Outside the stage window, stage_state() and rhs_register() are null.

Every call takes the box (see Concepts), and all four step calls take an optional mask and mask_comp. Pass the same mask to all four. A splitting also needs its two partitions (partitions=), and for each flow stage_partition(k) says which partition to compute the residual of. The rules the two sides keep are the stage contract.

Procedures ​

ProcedureWhat it doesPreconditions and notes
initialize(scheme, form, lb, ub [, partitions] [, registers] [, stages_on_q] [, components] [, masked_q] [, nu, alpha] [, iterations] [, fsal])sets the scheme and form for a field of rank size(lb) (1 to 5); allocates registers_needed(scheme, form) registers with the bounds lb:ub through FUNDAL (device memory in a device build), or takes the caller'sstops if the scheme has no such form, or a splitting comes without partitions. partitions(:,p) = (v1, v2), the components of partition p. components is required below rank 5 (fields of rank 1 to 4). registers (see below), for rank 5 only. masked_q: the mask policy. nu, alpha: the leapfrog's filter, that form only. iterations: an implicit multistep scheme's fixed-point stages, that family only. fsal: residuals already in their register, a first-same-as-last scheme in the Butcher form only. Calls destroy first, so a stepper can be re-initialised
begin_step(bx, q [, mask, mask_comp])opens a step: the two-register and Ketcheson forms keep a copy of qn
begin_stage(k, bx, dt, q [, mask, mask_comp] [, decay])opens stage k: the Butcher form builds its state into its register (unless stage_state_is_q(k))k from 1 to stages(), in order; decay: a decay integrated exactly
stage_state()pointer to the stage state, with the field's boundsButcher form only; null on a stage evaluated on q, and always null in the other forms (their state is q). Null outside a stage window (before begin_stage, after end_stage)
rhs_register()pointer to the register the caller writes the residual of the active stage into, with the field's boundswrite the cells of the box; for a splitting, the components of stage_partition(k). Null outside a stage window (before begin_stage, after end_stage)
stage_state_view(u), rhs_register_view(r)the same two pointers at the field's rank: u and r are pointers of rank 1 to 5null exactly when stage_state() and rhs_register() are; the rank must be the field's
end_stage(k, bx, dt, q [, mask, mask_comp])closes stage k: the two-register, 2N, Ketcheson and splitting forms advance q here
step_error(bx, dt, q, atol, rtol, emax, esum, ncells [, mask, mask_comp] [, second])after the last end_stage and before finish_step, for an embedded pair (with second=.true., its second estimate, b−b^2, for dopri-853 and bs-54): the scaled error $\Delta t \sum_k (b_k - \hat b_k) R_k / (\mathrm{atol} + \mathrm{rtol}\max(q^n
dense_output(theta, bx, dt, q, u [, mask, mask_comp])after finish_step, the solution at tn+θΔt from the step's continuous extension: u=qn+1+Δt∑i(bi(θ)−bi)Ri, the residuals still in their registers until the next begin_step; θ within 64 ε past 0 or 1 (an output time's rounding) is that endButcher form, a scheme with b_dense (dopri-54), not after decay=; the multistep form of ab-k: its interpolant of order k from the residual history; Dense output
hermite_output(theta, bx, dt, q, u [, r_end] [, mask, mask_comp])after finish_step, Hermite's cubic through qn, qn+1 and the residuals at both ends: u=qn+1+Δt(−h00(θ)∑ibiRi+h10(θ)R1+h11(θ)Rend)Butcher form, any scheme; r_end = F(tn+1,qn+1) unless the scheme is first-same-as-last; Dense output
reject_step(bx, q [, mask, mask_comp])after step_error rejects a step, before it starts again: puts qn back into q in the two-register form, which advanced q in place (from its copy of qn); nothing to do in the Butcher form, which never wrote qthe forms step_error takes; a loop that calls it on every rejection runs either form unchanged
step_constraint(bx, dt, q, constraints, ratio)after the last end_stage, before finish_step: the smallest ratio of the step keeping each constrained component of qn+1 admissible along the straight path from qn (1 when it is; 0 when qn is not), nothing written; constraints(v) is CONSTRAINT_NONE, CONSTRAINT_NONNEGATIVE, CONSTRAINT_POSITIVE, CONSTRAINT_NONPOSITIVE or CONSTRAINT_NEGATIVEButcher form; reduce ratio (a minimum) over the boxes and ranks; An adaptive step
finish_step(bx, dt, q [, dq] [, mask, mask_comp] [, decay])closes the step: the Butcher form computes qn+1, and dq =∑kbkFk in the same pass if askeddq with any form other than Butcher stops the program; dq is 0 where the mask is inactive
stage_state_is_q(k)the state of stage k is q itself: evaluate its residual on qtrue for every stage of the forms other than Butcher; for the zero rows of A with stages_on_q
stage_needs_residual(k)the residual of stage k must be evaluated: false when it is already in its registerstage 1 of the Butcher form only: a step begun again, and with fsal the step after an accepted one (residuals already in their register); writing it anyway gives the same step
first_same_as_last()the scheme's last stage is the next step's first: explicit, as,:=b, cs=1dopri-54, calvo-65
invalidate_residuals()forget the residuals kept for the next stepafter changing q between steps, or abandoning a step
initial_step_norms(bx, q, atol, rtol, parts, ncells [, mask, mask_comp]), initial_step_probe(bx, h0, q [, mask, mask_comp]), initial_step_difference(bx, h0, q, atol, rtol, dmax, dsum, ncells [, mask, mask_comp])the starting step's norms of q0 and F0, its probe q0+h0F0 as the stage state (its residual into rhs_register()), the norm of (F1−F0)/h0Butcher form, two stages or more, after stage 1 of the first step: Step control: the starting step
stages()the stages of a step (the flows of a splitting)
stage_time(k)the fraction of the step at which the residual of stage k is evaluated: the abscissa ckfor a splitting, the fractions of the other partition's flows before flow k: see Time-dependent residuals
stage_weight(k)the effective Butcher weight bk; for a splitting, the fraction of the step of flow kin every Runge–Kutta form, the low-storage ones included
stage_partition(k)the partition flow k advances (1 or 2)0 for a Runge–Kutta form
stage_euler_fraction(k)the forward-Euler step stage k's residual takes, as a fraction of dt: fk=maxiβik/αik of the scheme's Shu–Osher formthe step a limiter must make admissible: see Limiters. 0 when the scheme is not SSP; a splitting's flow: ck (0 if negative)
imag_axis_bound(), real_axis_bound()the largest β with |R(iy)|≤1 on [0,β] and with |R(−x)|≤1 on [0,β]: a mode of frequency ω (a gyration, a wave) is stable for ωΔt≤ imag_axis_bound(), a decay of rate λ for λΔt≤ real_axis_bound()from the stability polynomial of the tableau; 0 for a splitting. euler and ssp-22 have no imaginary-axis interval (0)
ssp_coefficient()the SSP coefficient C of the scheme, from its Shu–Osher form0 when the scheme is not SSP, and for a splitting; maxkfk=1/C
state_aliases_q()the form keeps the stage state in q (every form but Butcher)the per-stage stage_state_is_q(k) generalises it
lookahead(k, bx, dt, q, u)in the window of stage k, after its residual: u=q+Δt∑r≤kak+1,rRr (br at the last stage), the state of stage k+1 (the step's result at the last stage)Butcher form, any rank; see Charge-conserving stage currents
stage_current(k, bx, h, jr, jk), stage_current_admissible()Jk=(H−∑r<kak+1,rJr)/ak+1,k (b at the last stage): the stage current whose weighted sum with the earlier ones (jr, the caller's registers) is h; whether every ak+1,k and bs is non-zeroButcher form, rank 5; stops on an inadmissible scheme
initialize_companion(field, lb, ub [, partitions] [, registers] [, components])a stepper of the scheme, form and stages on q of the stepper field, for another state that advances with the fieldsee Companion states
history([level]), history_view(h [, level])a level of the history, with the field's bounds (rank 5, rank k); level 1 by default (the leapfrog's filtered Un−1)null outside the history forms and past history_levels()
history_levels()the levels of the history: ms solution levels, then mr residual levels0 outside the history forms
history_is_ready(), restart_history(), resume_history(dt), resume_history(dts)whether the next step is the scheme's own (not a start-up step); forget the history (the next steps are start-up steps); declare the history written back valid for steps of dt (or made with the steps dts, the newest first)history forms (leapfrog, multistep)
history_step(j)Δtn−j, the step that made level j of a multistep scheme (a variable-step scheme's checkpoint saves them)0 outside the multistep form
stage_gamma(k)the coefficient of the implicit term of stage k, as a fraction of dt: a fixed-point stage's state approximates the solution of U=E+γR(U), γ=Δt stage_gamma(k)0 for every explicit stage (every stage of the explicit schemes, stage 1, a start-up step)
explicit_part(), explicit_part_view(e)the explicit part E of an implicit multistep step, with the field's bounds (rank 5, rank k)made at begin_stage(2) (at finish_step for a one-stage step), valid until finish_step; null for the explicit schemes
destroy()frees what the stepper ownsno finalizer: call it

Fields of rank 1 to 4 ​

initialize takes the bounds of a field of any rank from 1 to 5. Below rank 5 it also takes components: whether the field's first dimension holds its components. The field is then viewed as rank 5 with singleton dimensions (the box). The four step procedures are generic over the rank of q, and dq and mask have q's rank. The box must be built at the field's rank:

fortran
real(R_P), pointer :: q(:), u(:), r(:)        ! a scalar 1-D field q(0:n+1), one ghost cell each side
type(box)          :: bx

bx = box_interior(lb=[0], ub=[n+1], ngc=1, components=.false.)
call st%initialize(SCHEME_SSP_33, FORM_SHU_OSHER_2R, lb=[0], ub=[n+1], components=.false.)
call st%begin_step(bx, q)
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   if (st%stage_state_is_q(k)) then ; u => q ; else ; call st%stage_state_view(u) ; endif
   call st%rhs_register_view(r)
   call residual(u, r)                       ! yours
   call st%end_stage(k, bx, dt, q)
enddo
call st%finish_step(bx, dt, q)
  • No copy. A lower-rank q reaches the rank-5 procedures as the rank-5 array it is in memory, through an explicit-shape dummy, with no copy when it is contiguous. In a device build it must be contiguous.
  • Checked at every call: the box and the stepper have q's rank, and q has the bounds given to initialize.
  • The views. stage_state() and rhs_register() keep their rank-5 result. A function cannot be generic on the rank of its result, so the rank-k pointers are the subroutines stage_state_view(u) and rhs_register_view(r), with the field's bounds.
  • The stage contract is unchanged at every rank. Its clauses apply to the cells of the box, whatever the rank.
  • Caller registers (registers=) are for rank-5 fields only.

The caller's registers ​

initialize(..., registers=pool) takes the registers from the caller instead of allocating them. The stepper checks two things and stops on a mismatch:

  • the array is rank 6 and contiguous, with the field's bounds lb:ub on dimensions 1–5;
  • its slots are numbered from 1 on dimension 6, and there are at least registers_needed(scheme, form) of them.

It cannot check the rest:

  • the actual must be a pointer or a target;
  • in a device build it must be device memory;
  • two steppers in use at the same time need two pools.

The stepper never frees a caller's pool, and does not need it zeroed: no form reads a register before writing it (C3).

Stages on q ​

The state of a stage whose row of A is zero is q itself: the first stage of every explicit scheme (in SELAH's catalogue, the only one). By default the Butcher form still copies it into its stage-state register. With stages_on_q=.true. it does not:

fortran
call st%initialize(SCHEME_SSP_54, FORM_BUTCHER, lb=lbound(q), ub=ubound(q), stages_on_q=.true.)
! ...
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   if (st%stage_state_is_q(k)) then
      u => q                ! stage 1: fill the ghost cells of q, evaluate the residual on q
   else
      u => st%stage_state()
   endif
   ! ... ghost fill and residual of u into st%rhs_register()
   call st%end_stage(k, bx, dt, q)
enddo

During such a stage stage_state() is null, so a caller that does not ask stage_state_is_q(k) fails at once instead of reading a stale register. The caller then writes the ghost cells of q at stage 1 (contract clause 3), which the plain Butcher form leaves alone. The solution is bitwise that of the plain Butcher form. On the A30 the option takes 6% off the integrator's SSP(5,4) step (Performance).

Residuals already in their register ​

Two of the Butcher form's stage-1 residuals are known before their stage, and stage_needs_residual(k) says so:

  • A step begun again. After a rejected step (its stages and step_error done, no finish_step), begin_step opens the step again on the same q. Stage 1, whose row of A is zero, has the same state and time whatever the new dt: its residual is still in its register. Every scheme, no option.
  • First same as last (fsal=.true., first_same_as_last() schemes: dopri-54, calvo-65). The last stage's state is qn+1 and its time tn+1: its residual is the next step's first, and begin_step copies it into stage 1's register. Without the option the stepper keeps nothing across steps.
fortran
call st%initialize(SCHEME_DOPRI_54, FORM_BUTCHER, lb=lbound(q), ub=ubound(q), fsal=.true.)
! ...
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   if (st%stage_needs_residual(k)) then
      ! ... ghost fill and residual of the stage state into st%rhs_register()
   endif
   call st%end_stage(k, bx, dt, q)
enddo

The stepper cannot see q change between steps. A caller that changes it (a limiter after the step, a regrid, another flow of a splitting, a step abandoned) calls invalidate_residuals() first, and stage 1 is evaluated again. A caller that does not ask stage_needs_residual writes the residual anyway and gets the same step: bitwise for a step begun again, and to rounding for the first-same-as-last stage, whose state and qn+1 the kernels sum in the same order (bitwise with gfortran). Not with decay=. selah_test_fsal: 6 and 8 evaluations per step for dopri-54 and calvo-65 instead of 7 and 9, the solution after 100 steps bitwise that of the run evaluating every stage; s−1 evaluations for a step begun again, bitwise a fresh step.

Companion states ​

A state that advances with the field in the same step, its residual built inside the field's at each stage (particles pushed by the fields of each stage, the auxiliary fields of a perfectly matched layer), is the same scheme applied to another state. A companion is a second stepper with the field stepper's scheme, form and stages on q, driven by the same calls in the same stage window:

fortran
call st%initialize(SCHEME_SSP_54, FORM_BUTCHER, lb, ub)                      ! the field
call sp%initialize_companion(st, lb=[1, 1], ub=[8, np_max], components=.true.) ! the particles, (8, np_max)
! each step, n particles active, components 1:6 evolving
bp = box_range(lb=[1, 1], lo=[1, 1], hi=[6, n], components=.true.)
call st%begin_step(bx, q) ; call sp%begin_step(bp, p)
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q) ; call sp%begin_stage(k, bp, dt, p)
   ! the residuals of both: the particles' from the stage fields, the field's from the stage particles
   call st%end_stage(k, bx, dt, q) ; call sp%end_stage(k, bp, dt, p)
enddo
call st%finish_step(bx, dt, q) ; call sp%finish_step(bp, dt, p)
! then what the caller does on the final state: particle wrap and migration

The companion's bounds are a capacity; the box of each call is the active part: the particles 1..n of a pool whose n changes between steps, the components that evolve (charge and mass, components 7:8, stay as they are). The kernels never touch the rest. A layout with the particles first, (np, 8) (the device's), is a rank-2 field with components=.false. whose box ranges over 1:n and 1:6. Re-initialise the companion when its shape changes (a regrid): between steps its registers are scratch. Stepped together, field and companion are bitwise the two stepped as one state (selah_test_companion, every form). The stage window of the two must coincide: a companion's stage state and residual register are, like the field's, null outside it.

A decay integrated exactly ​

For a state ψ′=g−λψ with λ≥0 per element and constant in time, the auxiliary fields of a perfectly matched layer (PML) above all, the decay is often far stiffer than the rest: λΔt beyond the scheme's real-axis bound makes the explicit stages blow up. Pass the rate as decay (an array of the state's shape) at every begin_stage and at finish_step, and write the source g alone as the residual:

fortran
call sp%begin_stage(k, bp, dt, psi, decay=lambda)   ! psi: a companion of the field stepper
! ... the residual: g = (sigma/kappa^2) d_n F only, without -lambda psi
call sp%finish_step(bp, dt, psi, decay=lambda)

With z=−λΔt the state of stage i is ecizψn+φ1(ciz)Δt∑jaijgj and the step ezψn+φ1(z)Δt∑jbjgj, φ1(z)=(ez−1)/z: exponential Euler on the Runge–Kutta-averaged source, the Runge–Kutta analogue of the convolutional PML of Roden & Gedney (2000). It is exact for a source frozen over the step and for static equilibria at any λΔt, stable for any λΔt, second order with a varying source (whatever the scheme's order), and bitwise the explicit step where λ=0 (nvfortran's optimised build: within a few ulp for the 13- and 14-stage schemes, P5). The Butcher form only, no mask, no dq.

The source is stiff too (g∝σ^): inside a stiff layer the coupled field–ψ system has a smaller stable step than either part alone. Take the time step from the coupled limit, not from a CFL condition and a λΔt condition separately (notes/companions-pushers-pml.md §5 has the coupled limits of the catalogue).

Masks ​

With mask present the kernels update the active cells only: a cell is active where mask(..., mask_comp) < 0 (ADAM's immersed-boundary distance, negative in the fluid; mask_comp defaults to the last component). The masked cells of q keep their values through the step, in every form.

The stage state is another matter. The forms other than Butcher keep it in q, so its masked cells hold qn. The Butcher form builds it in a register, and a masked cell of that register is left as it was: the previous stage's, the previous step's, or the register's initial value. A caller whose residual reads masked cells (an immersed boundary extrapolating from the solid side, a stencil that reaches into a solid block) asks for the mask policy at initialisation:

fortran
call st%initialize(scheme, FORM_BUTCHER, lb, ub, masked_q=.true.)

and the masked cells of every stage state then hold qn, written by the stage-state kernel in the same pass that builds the active cells (no extra pass over the field). This is the Runge–Kutta solution of u′=0 in the masked cells. Bitwise, it is what a caller gets by copying qn into the masked cells of the stage state itself after begin_stage (ADAM's adapter did so before szaghi/adam#134; selah_test_stepper_options). Default .false.: the register's masked cells are left untouched. A caller whose masked cells must evolve within the step (ghost values of an immersed boundary rebuilt from the fluid at every stage) still writes them itself, between begin_stage and its residual.

Time-dependent residuals ​

A residual that depends on time (boundary conditions, sources, forcing) is evaluated at t + st%stage_time(k)*dt:

fortran
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   ! ... the residual of the stage state at time t + st%stage_time(k)*dt into st%rhs_register()
   call st%end_stage(k, bx, dt, q)
enddo
call st%finish_step(bx, dt, q)
t = t + dt

In a Runge–Kutta form stage_time(k) is the abscissa ck. A splitting needs a definition: its sub-flows are explicit Euler steps of one partition, exact only when that partition's residual does not depend on its own variables. Time is a variable with t′=1, so it advances with the flows of the partition whose residual does not depend on t (those flows stay exact), and it is frozen through the flows of the other one (which stay exact too, their residual taken at a fixed time). stage_time(k) is therefore the sum of the fractions of the other partition's flows before flow k. Strang, partition 1 first, flows (1,12),(2,1),(1,12): stage_time is 0,12,1. If partition 2 depends on time, its flow sees t+Δt/2; if partition 1 does, its flows see t and t+Δt. Either way the composition keeps its order (measured: selah_test_nonautonomous).

When both partitions' residuals depend on time, no sub-flow is exact and no stage time gives the composition's order: split the problem so that one partition's residual is autonomous.

Limiters and forward-Euler steps ​

A strong-stability-preserving (SSP) scheme is a convex combination of forward-Euler steps. In its Shu–Osher form, stage i is

Yi=∑k<iαik(Yk+ΔtβikαikF(Yk)),αik≥0, βik≥0, ∑kαik=1,

so the residual of stage k enters forward-Euler steps of Δtβik/αik from Yk. A limiter that makes Yk+τF(Yk) admissible (positive density and pressure, a bound) for τ=fkΔt, with fk = stage_euler_fraction(k) =maxiβik/αik, makes it admissible for every 0≤τ≤fkΔt (Yk+τF is then a convex combination of Yk and that state, and the set is convex). Every later stage and the step are then convex combinations of admissible states (Zhang & Shu 2010; Gottlieb, Ketcheson & Shu 2011). The limiter's step is fkΔt, not Δt:

fortran
do k=1, st%stages()
   call st%begin_stage(k, bx, dt, q)
   ! ... the residual of the stage state into st%rhs_register()
   tau = st%stage_euler_fraction(k) * dt
   ! ... the limiter: the state of stage k plus tau times its residual admissible (a source term time-centred within
   !     that forward-Euler step at tau/2)
   call st%end_stage(k, bx, dt, q)
enddo
SchemefkC
euler, ssp-22, ssp-331 every stage1
ssp-540.3918, 0.6631, 0.6631, 0.6631, 0.58441.508
ssp-1041/6 every stage6
rk4, lsrk-33, lsrk-540 (not SSP: no positivity guarantee)0

Taking Δt for every stage is exact for euler, ssp-22 and ssp-33, and over-limits ssp-54 and ssp-104: it enforces a forward-Euler step longer than the scheme takes. Taking less than fkΔt loses the guarantee (selah_test_stage_euler: the decay u′=−λu at λΔt=20, limited with fkΔt/2, goes negative; with fkΔt it does not). fk is the same in every storage form of a scheme: the stages are the same. The time step itself is bounded by Δt≤CΔtFE, ssp_coefficient() times the forward-Euler limit of the residual.

Leapfrog ​

FORM_LEAPFROG (schemes) is the one form whose registers are not scratch after finish_step: its history, the filtered Un−1, carries over to the next step. Item 5 of the stage contract holds for every other register.

  • Start-up. The first step after initialize or restart_history() has no Un−1. It is one ssp-22 step in the two-register form: stages() is 2 for that step (stage_time 0 and 1) and 1 afterwards, so ask stages() at every step, as the loop above does. The start-up keeps the global order 2 and excites the computational mode at O(Δt4) on an oscillation.
  • A constant step. A leapfrog step with a dt other than the start-up's stops the program. When dt changes (a CFL limit), call restart_history(): the next step is a start-up with the new dt.
  • What q holds. After finish_step, q is the singly filtered Un+1, offset from the solution by (α−1)d=O(ν(1−α)Δt2) (schemes); the history is the doubly filtered Un. Both are the state: a checkpoint saves both, as they are.
  • Checkpoints. Save q and history() (history_view(h) below rank 5) after finish_step; to restart, write both back and call resume_history(dt). The run continues bitwise.
  • Regrids. Prolong the history with q and call resume_history(dt), or call restart_history().
  • Queries. stage_time 0; stage_weight 0 (the filtered step is not a weighted sum of residuals, so a flux register cannot telescope it); stage_euler_fraction 0 (1 on the start-up's stages); ssp_coefficient 0; imag_axis_bound and real_axis_bound those of the filter.
  • Not with the leapfrog. decay=, dq, lookahead and stage_current are the Butcher form's. A mask works as in the other forms; a masked cell's history is left untouched. A companion (initialize_companion) takes the field's filter.

The history ​

FORM_MULTISTEP runs the linear multistep schemes (schemes): ab-<k>. A step of a k-step scheme reads earlier levels: ms solution levels Un−1,…,Un−ms and mr residual levels Rn−1,…,Rn−mr (lmm_history). The stepper keeps them in two rings of registers, which survive finish_step and are part of the solution's state, as the leapfrog's history is.

  • One stage, one residual. Stage 1 is evaluated on q (Un, at stage_time 0); its residual, written into rhs_register(), is Rn at the head of the residual ring. A multistep-multistage scheme (msrk-ssp-*) has more stages: stage k≥2 is built at begin_stage in the stage-state register (stage_state_is_q(k) false) from the levels and the stage residuals of this step, its residual written into its own register. finish_step forms Un+1=∑jajUn+1−j+Δt∑jbjRn+1−j in one pass over the box, writing Un into the solution ring as it goes, and the rings turn by one index. Nothing is copied, and no residual is evaluated twice (FOODIE's Adams–Bashforth evaluated all k of them at every step).
  • The order of the sums is the order of the levels, newest first, whatever slot the ring puts each level in: the slots of every phase of the rings are tabulated at initialize (on the device in a device build), and the kernel selah_history reads them. So a run restarted from a checkpoint, whose rings start at another phase, is bitwise the uninterrupted run.
  • The levels. history_levels() is ms+mr; level j≤ms is Un−j, level ms+j is Rn−j, with Un the state q holds (after finish_step, the history of the next step). history_view(h, level) gives each at the field's rank and bounds. ab-<k>: k−1 residual levels, no solution level.
  • Start-up. The first steps after initialize or restart_history() have no history. Until the history holds every level, each step is a step of the built-in start-up scheme, multistep_starter(scheme): the catalogue's explicit Runge–Kutta scheme of fewest stages with order ≥p−1 (starter_for_order: euler, ssp-22, ssp-33, rk4 or ssp-54 when SSP, cash-karp-54, calvo-65, feagin-108 up to order 10), which keeps the global order p. stages() is the starter's for those steps (ask it at every step), stage_time its abscissae, stage_euler_fraction its fractions; its stages past the first have their state in the stage-state register (stage_state_is_q(k) false, state_aliases_q() false for this form). The residual of each starting state, its stage 1, enters the residual ring as it is evaluated: no residual is evaluated twice. ab-<k> takes k−1 such steps, with the starter's stage registers on top of the rings in registers_needed (ab-5: rk4, 5 + 4 registers). Past order 11 (ab-12 to ab-16) no starter has the order: the caller writes the history, each level through history_view, then resume_history(dt) (Checkpoint shows two ssp-33 steps doing it by hand), and a step before that stops the program. Either way the start-up steps keep one dt.
  • A constant step. A step with a dt other than the history's stops the program (D7 of #10: the coefficients are those of a constant step). When dt changes, restart_history(), and a new history. The variable-step SSP schemes (lmm-ssp-vss-*) are the exception: they keep the steps that made their levels and recompute their coefficients from the step ratios at every step (one small device copy when they change, none at a constant step); ssp_coefficient() and stage_euler_fraction(1) follow.
  • Checkpoints. Save q and every level after finish_step; to restart, write them back into a new stepper and call resume_history(dt). The run continues bitwise. A variable-step scheme also saves its steps, history_step(j) for j=1,…,k−1 (Δtn−j, the newest first), and resumes with resume_history(dts).
  • Regrids. Prolong q and every level, then resume_history(dt) (AMR); or restart_history().
  • Queries. stage_weight 0 (the step is not a weighted sum of this step's residuals: no flux telescoping); stage_euler_fraction 0 and ssp_coefficient 0 for ab-<k>, b1/a1 and C for the SSP schemes; imag_axis_bound and real_axis_bound from the root condition (lmm_imag_axis_bound, lmm_real_axis_bound).
  • Not in this form. decay=, dq, lookahead and stage_current are the Butcher form's. A mask works as in the other forms: a masked cell's levels are left untouched. The leapfrog's own form, FORM_LEAPFROG, keeps its history with the same calls (one solution level, rings of length 1) and its own filtered step.

Fixed-point stages ​

The implicit multistep schemes (schemes: am-<k>, abm-<k>, bdf-<k>) advance

Un+1=E+γR(Un+1),E=∑jajUn+1−j+Δt∑j≥1bjRn+1−j,γ=Δtb0,

by m fixed-point iterations U←E+γR(U), each one a stage with the caller's residual, as FOODIE does (D9 of #10): no callback, the stage contract unchanged.

  • The stages. Stage 1 is on q (Un, at stage_time 0); its residual Rn is the residual ring's head and also the first iteration's residual, as the iterations start from Un. begin_stage(2) makes E once, one pass over the levels into its register, and the first iterate E+γRn. Every later stage's state is E+γR(Yk−1), made in place over that residual by selah_lincomb (two registers, by turns state and residual: 3 streams per iteration). finish_step writes q=E+γR(Ym), Un into the solution ring. So stages() =m and m residuals per step; no residual of the history is evaluated again (FOODIE's defect F4). Every iterate is at tn+1 (stage_time 1). A predictor-corrector (abm-<k>) has one more stage: stage 2's state is the predictor's step (multistep_predictor), the iterations start from it, stages() =m+1.
  • The iterations, initialize(..., iterations=m): by default multistep_iterations(scheme), the fewest that keep the scheme's order. From Un each iteration gains one order, so am-<k> (order k+1) takes k+1 and bdf-<k> (order k) k; abm-<k> starts from a prediction of its order and takes 1. FOODIE's default is 1, a first-order step for every am-<k>; iterations=1 reproduces it.
  • Where they converge. The iterations contract where |γλ|<1 for the eigenvalues λ of the residual's Jacobian; a stiff problem diverges, as FOODIE's does. imag_axis_bound() and real_axis_bound() are those of the step the m iterations make (fixed_point_imag_axis_bound and fixed_point_real_axis_bound of Schemes), not the converged scheme's: am-1 (the trapezoid rule, A-stable) iterated twice is Heun's method, real-axis bound 2, imaginary 0.
  • A solver of your own (D9 of #10). stage_gamma(k) is b0 for the fixed-point stages (γ=Δtstage_gamma(k); 0 for stage 1 and every explicit stage), explicit_part() (explicit_part_view(e)) is E from begin_stage(2) to finish_step. A caller who solves U=E+γR(U) by its own Newton iteration writes the solution into stage_state() of a fixed-point stage before evaluating the residual there; with iterations=2, stage 2 is the last one, and the update E+γR(U) returns U to its rounding. The solver stays the caller's (P2): A step of your own.
  • Solvers for that stage (#11, D1: objects the caller drives inside the stage, the stage contract unchanged): newton_solver (Newton cell by cell, for terms that couple the components of one cell), anderson_mixing (Anderson's acceleration of the iterations U←E+γR(U): no Jacobian, one residual and one reduction per iteration, converging where they diverge), newton_krylov (Newton–GMRES, matrix-free, for terms that couple the cells far beyond the explicit bound) (Stiff terms).
  • Registers. Two stage registers and E on top of the rings: am-1, 4 (registers_needed). The start-up, the history, checkpoints and a change of dt are those of the history.

Forms ​

FormRegistersSchemes
FORM_BUTCHERs + 1every Runge–Kutta scheme
FORM_SHU_OSHER_2R2 (3)euler, ssp-22, ssp-33; ssp-43 with its error accumulator
FORM_WILLIAMSON_2N2lsrk-33 to lsrk-144
FORM_KETCHESON_2R2ssp-104
FORM_SPLITTING1every splitting
FORM_LEAPFROG2leapfrog, leapfrog-raw
FORM_LINEAR_SSP_2R2lssp-<s>, lssp-<s>-1
FORM_MULTISTEPmr+1+ms (+ the stages, the start-up's or the scheme's; + 3 for the implicit ones)ab-<k>, lmm-ssp-*, lmm-ssp-vss-*, msrk-ssp-*, am-<k>, abm-<k>, bdf-<k>

What each register holds, and where each form does its work: Concepts.

The Nordsieck form, variable-step Adams and BDF, is an object of its own, nordsieck_stepper, with the same stage contract and qmax+4 registers (Schemes: the Nordsieck form). The Nyström form, Runge–Kutta–Nyström schemes for y″=f(t,y), is another: nystrom_stepper, s+1 registers, the position and the velocity advanced together (Second-order systems).

Exponential stages: the Magnus form ​

A linear non-autonomous system y′=A(t)y (a rotation at a varying rate, a driven quantum system in real form, a linear wave operator with time-dependent coefficients) is advanced by a commutator-free Magnus scheme, a product of exponentials (#6):

yn+1=eBJ⋯eB1yn,Bj=h∑k=1KajkA(tn+ckh),

with the ck the Gauss nodes of the step and B1 applied first (Schemes). A stage of this kind is an evaluation of the operator, not of a residual, and its update is the action of an exponential, not a linear combination: it is an object of its own, magnus_stepper (selah_magnus), driven by its caller through requests, as newton_krylov is (Stiff terms). The library never sums over boxes or ranks: what needs a global sum is asked of the caller.

ProcedureWhat it does
initialize(scheme, lb, ub, action [, ncomp] [, m] [, tol] [, skew])the scheme (MAGNUS_SCHEMES), the field's bounds (rank 5), the action of the exponentials: MAGNUS_DENSE, MAGNUS_KRYLOV or MAGNUS_CALLER; ncomp, the block size of the dense action (≤ MAGNUS_MAX_BLOCK = 16); m, the largest Krylov space (≤ MAGNUS_MAX_KRYLOV = 16, default 16); tol, the Krylov action's error per unit step, relative to ‖y‖ (default 1000 ε); skew, the operator is skew-symmetric (the Lanczos structure imposed)
nodes(), exponentials(), node_time(k), weight(j, k)K, J, ck, ajk
begin_step(bx, h, q)a step of h on the caller's state q (a pointer with the field's bounds, updated in place); the first request
request()the pending request (below), MAGNUS_DONE when q is yn+1
advance(bx)the request answered: on to the next
node(), block_register()MAGNUS_NODE: the node k; the dense action's register, blk(cell, v, w) = Avw(tn+ckh), the components v, w of the box (the component dimension first or last, as newton_solver's blocks)
exponential(), apply_in(), apply_out()MAGNUS_APPLY: apply_out() =∑kajkA(tn+ckh) apply_in(), j = exponential(); MAGNUS_ACTION: q ←eh∑kajkAk q by the caller
sums(1:nsums)MAGNUS_REDUCE: the partial sums to add over the boxes and ranks
krylov, reductions, reorthogonalisations, applications, substepsthe Krylov action's counts in the step: iterations, MAGNUS_REDUCE requests (iterations + reorthogonalisations + 1), second Gram–Schmidt passes, MAGNUS_APPLY requests, sub-steps (J at least)
destroy()frees the registers

The requests.

  • MAGNUS_NODE, K times at the step's start, k = node(): whatever A(tn+ckh) needs (the dense action: the blocks into block_register(); the other actions: the caller's own data at that time, or nothing).
  • MAGNUS_APPLY (Krylov): the operator of exponential j applied to a vector. One per Krylov iteration.
  • MAGNUS_REDUCE (Krylov): a sum over the boxes and ranks. One per Krylov iteration (the inner products with the basis and the new vector's own, fused), one more when the new vector is reorthogonalised, one per step for ‖yn‖.
  • MAGNUS_ACTION (the caller's action): exponential j = exponential() applied to q in place, J times in order.

The registers. Dense: ncomp ×K of the field's size (the blocks at the nodes); every exponential of a cell is made and applied in one kernel (the diagonal Padé approximant of degree 8 with scaling and squaring: orthogonal for a skew-symmetric block, so the norm is kept to rounding). Krylov: m+1 (the basis). The caller's action: none.

The Krylov action. Exponential j's operator Mj=∑kajkA(tn+ckh) builds the Arnoldi basis Vi and Hi from v1=q/‖q‖, one MAGNUS_APPLY and one MAGNUS_REDUCE per iteration (classical Gram–Schmidt: the i+1 inner products fused; a second pass, one more reduction, when the new vector keeps less than a tenth of its norm). After each iteration Saad's estimate hhi+1,i|[eτhHi]i,1| (relative to ‖q‖) is compared with tol τ, τ the part of the exponential still to do: below it, q←‖q‖VieτhHie1 and the exponential is done; at i=m the same space serves a shorter sub-step (τ halved until the estimate passes), and the next sub-step restarts from the new q. The small exponential is the dense action's Padé approximant, on the host. With skew=.true. the skew-tridiagonal (Lanczos) structure is imposed on Hi, so eτhHi is orthogonal and ‖q‖ is carried exactly, to the orthogonality of the basis. selah_test_magnus: a 1-D advection of 64 cells, cf4-2, 100 steps, within 4.8⋅10−13 of the dense 64 × 64 exponentials (notes/magnus-reference.py, 30 digits), the norm within 1.9⋅10−15, 8.8 iterations per exponential, no reorthogonalisation.

The three loops this contract serves ​

A rotation at a varying rate, per cell (MAGNUS_DENSE, ncomp = 2, the components first):

fortran
call mg%initialize('cf4-2', lb, ub, MAGNUS_DENSE, ncomp=2)
call mg%begin_step(bx, h, q)
do while (mg%request() /= MAGNUS_DONE)
   select case (mg%request())
   case (MAGNUS_NODE)                     ! A = omega(t) [[0, -1], [1, 0]] in every cell
      blk => mg%block_register() ; t = time + mg%node_time(mg%node()) * h
      blk(1,:,:,:,:,1) = 0._R_P ; blk(1,:,:,:,:,2) = -omega(t)
      blk(2,:,:,:,:,1) = omega(t) ; blk(2,:,:,:,:,2) = 0._R_P
   endselect
   call mg%advance(bx)
enddo

A driven two-level system in real form, per cell (MAGNUS_DENSE, ncomp = 4): the same loop, the block (0H−H0), H=ω02σz+f(t)σx, written at each node.

A coupled operator by Krylov (MAGNUS_KRYLOV, skew=.true.): a 1-D advection ut=−c(t)ux by a centred derivative, the boxes and ranks the caller's:

fortran
call mg%initialize('cf4-2', lb, ub, MAGNUS_KRYLOV, skew=.true.)
call mg%begin_step(bx, h, q)
do while (mg%request() /= MAGNUS_DONE)
   select case (mg%request())
   case (MAGNUS_APPLY)                    ! out = sum_k a_jk A(t_k) in = -(sum_k a_jk c(t_k)) D in
      w = 0._R_P
      do k=1, mg%nodes()
         w = w + mg%weight(mg%exponential(), k) * speed(time + mg%node_time(k) * h)
      enddo
      x => mg%apply_in() ; y => mg%apply_out()
      call exchange_ghosts(x)              ! the caller's halo exchange
      call centred_derivative(bx, -w, x, y)
   case (MAGNUS_REDUCE)
      call mpi_allreduce(MPI_IN_PLACE, mg%sums, mg%nsums, MPI_DOUBLE_PRECISION, MPI_SUM, comm, ierr)
   endselect                              ! MAGNUS_NODE: nothing to prepare
   call mg%advance(bx)
enddo

Each loop uses only the procedures above; the global sum is the caller's mpi_allreduce (nothing to do with one box on one rank), and scripts/check_separation.py keeps MPI out of the library.

Verification ​

WhatCase
every scheme reaches its order in every form, through the stepperA3
the stage contract, stages on q, masks, caller registers, the error stopsC3
the forms agree with the Butcher form to round-offC2
stage_weight telescopes the stage fluxes to the mass changeP4
stage_euler_fraction and ssp_coefficient against the literature; a limiter with them keeps positivityP2
a companion stepped with the field is the coupled Runge–Kutta step, bitwise, in every formC8
decay=: exact for a frozen source and for static equilibria, stable at any λΔt, second orderP5
lookahead and stage_current: Gauss's law at every stage and stepC9
FORM_LEAPFROG with its start-up, every filter: orders and stability bounds; the filter's damping and the data it leaves aloneA9, P6
FORM_MULTISTEP: checkpoint and restart bitwise for ab-1 to ab-6, a regrid by injection, the ghost cells; the built-in start-up (its table of orders; within each scheme's own error of an exact history)selah_test_history
the fixed-point stages: am-1 converged to the closed-form trapezoid step (1e-14), the contraction |γλ| per stage (1e-12), a caller's Newton step, the bounds of 1, 2, 3 iterations; bdf-1 to bdf-6 at λΔt=−100: a caller's Newton step = the closed form (1e-12), the fixed point divergingselah_test_fixed_point
the stepper on an A30C5
the stepper's own cost: none (0.999 of the same kernels called directly)Performance