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
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%destroyEvery 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
| Procedure | What it does | Preconditions and notes |
|---|---|---|
initialize(scheme, form, lb, ub [, partitions] [, registers] [, stages_on_q] [, components] [, masked_q]) | 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's | stops 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. 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 | |
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 bounds | Butcher 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 bounds | write 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 5 | null 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 | |
finish_step(bx, dt, q [, dq] [, mask, mask_comp] [, decay]) | closes the step: the Butcher form computes dq | dq 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 q | true for every stage of the forms other than Butcher; for the zero rows of A with stages_on_q |
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 | for a splitting, the fractions of the other partition's flows before flow k: see Time-dependent residuals |
stage_weight(k) | the effective Butcher weight k | in 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: | the step a limiter must make admissible: see Limiters. 0 when the scheme is not SSP; a splitting's flow: |
imag_axis_bound(), real_axis_bound() | the largest imag_axis_bound(), a decay of rate 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 | 0 when the scheme is not SSP, and for a splitting; |
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: 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() | jr, the caller's registers) is h; whether every | Butcher 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 field | see Companion states |
destroy() | frees what the stepper owns | no 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:
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
qreaches 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, andqhas the bounds given toinitialize. - The views.
stage_state()andrhs_register()keep their rank-5 result. A function cannot be generic on the rank of its result, so the rank-k pointers are the subroutinesstage_state_view(u)andrhs_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:ubon 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:
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)
enddoDuring 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).
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:
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 migrationThe 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 decay (an array of the state's shape) at every begin_stage and at finish_step, and write the source
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 dq.
The source is stiff too (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
call st%initialize(scheme, FORM_BUTCHER, lb, ub, masked_q=.true.)and the masked cells of every stage state then hold 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:
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 + dtIn a Runge–Kutta form stage_time(k) is the abscissa stage_time(k) is therefore the sum of the fractions of the other partition's flows before flow stage_time is 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
so the residual of stage stage_euler_fraction(k)
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| Scheme | ||
|---|---|---|
euler, ssp-22, ssp-33 | 1 every stage | 1 |
ssp-54 | 0.3918, 0.6631, 0.6631, 0.6631, 0.5844 | 1.508 |
ssp-104 | 1/6 every stage | 6 |
rk4, lsrk-33, lsrk-54 | 0 (not SSP: no positivity guarantee) | 0 |
Taking selah_test_stage_euler: the decay ssp_coefficient() times the forward-Euler limit of the residual.
Forms
| Form | Registers | Schemes |
|---|---|---|
FORM_BUTCHER | s + 1 | every Runge–Kutta scheme |
FORM_SHU_OSHER_2R | 2 | euler, ssp-22, ssp-33 |
FORM_WILLIAMSON_2N | 2 | lsrk-33, lsrk-54 |
FORM_KETCHESON_2R | 2 | ssp-104 |
FORM_SPLITTING | 1 | every splitting |
What each register holds, and where each form does its work: Concepts.
Verification
| What | Case |
|---|---|
| every scheme reaches its order in every form, through the stepper | A3 |
| the stage contract, stages on q, masks, caller registers, the error stops | C3 |
| the forms agree with the Butcher form to round-off | C2 |
stage_weight telescopes the stage fluxes to the mass change | P4 |
stage_euler_fraction and ssp_coefficient against the literature; a limiter with them keeps positivity | P2 |
| the stepper on an A30 | C5 |
| the stepper's own cost: none (0.999 of the same kernels called directly) | Performance |