An adaptive step
An embedded pair (heun-euler-21, cash-karp-54, dopri-54, calvo-65, feagin-108, ssp-43 and the pairs of issue #11: Schemes) estimates the error of each step from the difference of its two sets of weights. SELAH computes the estimate; the decision stays yours, as the time loop does (the stepper): after the last end_stage, step_error gives the largest scaled error of the box and the sum of their squares, and you accept the step (finish_step) or do it again with a smaller dt. A step controller proposes both the decision and the next step from your norm of the error. A rejected step costs nothing to undo: q is untouched until finish_step.
do while (t < 1._R_P)
dt_try = min(dt, 1._R_P - t)
call st%begin_step(bx, u)
do k=1, st%stages()
call st%begin_stage(k, bx, dt_try, u)
if (st%stage_needs_residual(k)) then ! not stage 1 of a step redone, or after an accepted one
if (st%stage_state_is_q(k)) then ; us => u ; else ; call st%stage_state_view(us) ; endif
call st%rhs_register_view(r)
r = (t + st%stage_time(k) * dt_try) * us**2 ! the caller's residual
nres = nres + 1
endif
call st%end_stage(k, bx, dt_try, u)
enddo
call st%step_error(bx, dt_try, u, TOL, 0._R_P, emax, esum, ncells) ! atol = TOL, rtol = 0
! with several boxes or ranks: emax = the max over all of them (MPI_Allreduce, MPI_MAX) here
call ctl%propose(dt_try, emax, dt, accept) ! accept or reject, and the next step
if (accept) then
call st%finish_step(bx, dt_try, u)
t = t + dt_try ; dt_min = min(dt_min, dt_try) ; dt_max = max(dt_max, dt_try)
if (any(u < 0._R_P)) then ! a limiter changing u: the carried residual is stale
u = max(u, 0._R_P) ; call st%invalidate_residuals
endif
endif ! rejected: u is still u^n, redone with dt
enddo$ cookbook_adaptive_step
steps accepted 21, rejected 1
steps from 1.0E-03 to 8.1E-02
residuals evaluated 133 of 154
largest error at t = 1: 9.2E-09- The scaled error. At each cell
, divided by , computed but not written. emaxis its largest magnitude over the box,esumthe sum of its squares over thencellscells visited (the active ones with a mask). - A tolerance per component. Components of different scales (a density near 1, an energy near
) need their own: pass atolandrtolas arrays, one value per component (step_error(bx, dt, q, atol_v, rtol_v, ...)). The stepper copies them to the device only when they change. - Your norm. The max norm is
emax; the root mean square issqrt(esum / ncells)once each is summed over your boxes and ranks. A step is accepted when the norm is at most 1. - Several boxes and ranks. SELAH has no MPI: reduce
emaxwith a max,esumandncellswith a sum, over every box of every rank, before you decide. Every rank then takes the same decision. - The controller.
ctl%propose(dt_try, err, dt, accept)accepts the step when the norm is at most 1 and gives the next step, larger or smaller: here the PI controller, fromup to with one rejection. The elementary, H211b, PI.4.2 and your own filters, their limits and when to choose which: Step control. The step cut to end at is one the controller did not propose: it takes the ratio of the steps you took. - No residual twice. With
fsal=.true.,dopri-54's last residual is the next step's first, and a step done again after a rejection keeps its first:stage_needs_residual(k)says which to skip (Stepper: residuals already in their register): here 133 residuals of 154. - A pair in two registers plus one.
ssp-43, Kraaijevanger's SSP(4,3) with the embedded second-order weights of Conde, Fekete and Shadid, runs in the two-register form with a third register that accumulatesstage by stage: 3 registers instead of the Butcher form's 5, and the estimate of the Butcher form to its rounding. That form advances qin place: afterstep_error,qis, and a rejected step calls reject_step(bx, q)to putback before it starts again (in the Butcher form reject_stepdoes nothing, so one loop serves both):
do while (t < 1._R_P)
dt_try = min(dt, 1._R_P - t)
call st%begin_step(bx, u)
do k=1, st%stages()
call st%begin_stage(k, bx, dt_try, u)
call st%rhs_register_view(r)
r = (t + st%stage_time(k) * dt_try) * u**2 ! every stage state is u itself in this form
call st%end_stage(k, bx, dt_try, u)
enddo
call st%step_error(bx, dt_try, u, TOL, 0._R_P, emax, esum, ncells) ! u is already u^{n+1} here
call ctl%propose(dt_try, emax, dt, accept)
if (accept) then
call st%finish_step(bx, dt_try, u)
t = t + dt_try
else
call st%reject_step(bx, u) ! u^n back, then the step again with dt
endif
enddo$ cookbook_adaptive_low_storage
registers: two-register form 3, Butcher form 5
steps accepted 51, rejected 1
largest error at t = 1: 1.2E-05- Two estimates.
dopri-853andbs-54carry a second embedded set,b_hat2:step_error(..., second=.true.)gives its sums, from the same registers. Combine the two once reduced over your boxes and ranks:dop853_error(esum, esum2, ncells)is DOP853's norm,, where the third-order estimate guards the fifth-order one; bs-54passes a step when both of its fourth-order estimates do:
do while (t < 1._R_P)
dt_try = min(dt, 1._R_P - t)
call st%begin_step(bx, u)
do k=1, st%stages()
call st%begin_stage(k, bx, dt_try, u)
if (st%stage_state_is_q(k)) then ; us => u ; else ; call st%stage_state_view(us) ; endif
call st%rhs_register_view(r)
r = (t + st%stage_time(k) * dt_try) * us**2
call st%end_stage(k, bx, dt_try, u)
enddo
call st%step_error(bx, dt_try, u, TOL, TOL, emax, esum, ncells) ! the first estimate
call st%step_error(bx, dt_try, u, TOL, TOL, emax2, esum2, ncells, second=.true.) ! the second
! with several boxes or ranks: reduce emax, emax2 (max), esum, esum2, ncells (sums) here
if (PAIRS(p) == SCHEME_DOPRI_853) then
err = dop853_error(esum, esum2, ncells) ! DOP853's combination of the two
else
err = sqrt(max(esum, esum2) / ncells) ! bs-54: both estimates must pass
endif
call ctl%propose(dt_try, err, dt, accept)
if (accept) then
call st%finish_step(bx, dt_try, u)
t = t + dt_try
endif
enddo$ cookbook_adaptive_two_estimates
pair estimates steps accepted rejected largest error at t = 1
dopri-853 2 10 1 9.70E-11
bs-54 2 20 0 9.61E-11- Variable order. The Nordsieck form chooses its order too:
error_sumsandselectreplacestep_errorand the controller, andset_spectral_radiuscaps the step at the stability bound of each order (Step control: order and step in the Nordsieck form). - Inequality constraints. A concentration, a density, a temperature must not cross 0, and a step the error estimate accepts can still take it there: once a decay is below the tolerance the controller grows the step to the scheme's stability bound, where the stability function is negative. After the stages,
step_constraint(bx, dt, q, constraints, ratio)gives the smallest ratio of the step that keeps every constrained component admissible along the straight path from: constraints(v)isCONSTRAINT_NONE,CONSTRAINT_NONNEGATIVE(), CONSTRAINT_POSITIVE(), CONSTRAINT_NONPOSITIVE() or CONSTRAINT_NEGATIVE(). Reduce it (a minimum over your boxes and ranks); below 1, reject the step and redo it with times the ratio times a safety factor below 1, before asking the error estimate:
allocate(u(2,M)) ; u(1,:) = 1._R_P ; u(2,:) = 1._R_P ! (component, cell): c, and a free quantity
bx = box_interior(lb=[1_I4P, 1_I4P], ub=[2_I4P, M], ngc=0_I4P, components=.true.)
call st%initialize('bs-32', FORM_BUTCHER, lb=[1_I4P, 1_I4P], ub=[2_I4P, M], components=.true.)
call ctl%initialize(CONTROL_PI, 3_I4P)
t = 0._R_P ; dt = 1.e-3_R_P ; nstep = 0 ; nredo = 0 ; cmin = 1._R_P
do while (t < 1._R_P)
dt_try = min(dt, 1._R_P - t)
call st%begin_step(bx, u)
do k=1, st%stages()
call st%begin_stage(k, bx, dt_try, u)
if (st%stage_state_is_q(k)) then
us => u
else
call st%stage_state_view(us)
endif
call st%rhs_register_view(r)
if (st%stage_needs_residual(k)) then ! a redone step reuses stage 1's residual
r(1,:) = -K_DECAY * us(1,:) ; r(2,:) = -1._R_P
endif
call st%end_stage(k, bx, dt_try, u)
enddo
if (how == 2) then ! the constraint before the error estimate
call st%step_constraint(bx, dt_try, u, [CONSTRAINT_NONNEGATIVE, CONSTRAINT_NONE], ratio)
! with MPI: ratio reduced (min) over the ranks first
if (ratio < 1._R_P) then ! redo it shorter: the step's path is not straight
call st%reject_step(bx, u)
dt = 0.9_R_P * ratio * dt_try ; nredo = nredo + 1
cycle
endif
endif
call st%step_error(bx, dt_try, u, 1.e-3_R_P, 1.e-3_R_P, emax, esum, ncells)
call ctl%propose(dt_try, emax, dt, accept)
if (accept) then
call st%finish_step(bx, dt_try, u)
t = t + dt_try ; nstep = nstep + 1 ; cmin = min(cmin, minval(u(1,:)))
else
call st%reject_step(bx, u)
endif
enddo$ cookbook_adaptive_constraints
the codes of none, >= 0, > 0, <= 0, < 0: 0 1 2 -1 -2
steps redone smallest c over the run
no constraint 54 0 -1.280E-003
c >= 0 143 93 2.902E-052The constraint costs steps where it binds: 93 steps redone here, each reusing its first stage's residual. It reads the step's result without writing anything, so a run whose constraints never bind takes the same steps, bit for bit, as one that never asks (selah_test_constraints). A ratio of 0 means
- FOODIE's rule.
embedded_step_size(dt, err, tol, embedded_order)is FOODIE's:when the error exceeds the tolerance, otherwise. It only shrinks the step. - Reproducibility.
emaxis the same in every build.esumis summed in the loop order on the host without OpenMP, and in the order of the OpenMP or OpenACC reduction otherwise: equal to rounding, not bit for bit. A loop that must take the same decisions on every build usesemax.
selah_vv_embedded runs this loop with FOODIE's rule on the Riccati equation for the four pairs whose error R8P can resolve: the global error at selah_test_step_control runs it with each controller (Step control: verification).