Skip to content

Diagnostics ​

Monitor convergence with the step residual ​

The step residual dq=∑kbkFk=(qn+1−qn)/Δt measures how much a step changes the solution. A steady-state solver stops when it is small. finish_step(..., dq=dq) writes it in the same pass as the update.

fortran
do step=1, 10000
   call st%begin_step(bx, u)
   do k=1, st%stages()
      call st%begin_stage(k, bx, 0.5_R_P, u)
      s_ => st%stage_state() ; r => st%rhs_register()
      r(1,1:N,1,1,1) = -(s_(1,1:N,1,1,1) - g(1,1:N,1,1,1))
      call st%end_stage(k, bx, 0.5_R_P, u)
   enddo
   call st%finish_step(bx, 0.5_R_P, u, dq=dq)
   if (maxval(abs(dq(1,1:N,1,1,1))) < TOL) exit             ! converged: the step changes nothing any more
enddo
text
$ cookbook_diagnostics steady
converged in 47 steps: max |dq| =  6.79E-11, max |u - g| =  5.18E-11

dq belongs to the Butcher form, which holds every stage residual until the update. The in-place forms (two-register, 2N, Ketcheson) never do, and asking them for dq stops the program.

Check the stage contract while you drive the stepper ​

Two cheap checks catch most mistakes in a new solver loop:

  • the stage state is q exactly when stage_state_is_q(k), in which case stage_state() is null;
  • the residual register is never the stage state.
fortran
call st%begin_step(bx, q)
do k=1, st%stages()
   call st%begin_stage(k, bx, 0.1_R_P, q)
   u => st%stage_state() ; r => st%rhs_register()
   ! the stage state is q itself or a register, never both; never the residual register
   if (st%stage_state_is_q(k) .eqv. associated(u)) error stop 'stage state ambiguous'
   if (associated(u, r)) error stop 'residual aliases the stage state'
   print '(A,I0,A,L1,A,L1)', 'stage ', k, ': state is q ', st%stage_state_is_q(k), ', stage_state() associated ', &
         associated(u)
   r = 0._R_P
   call st%end_stage(k, bx, 0.1_R_P, q)
enddo
call st%finish_step(bx, 0.1_R_P, q)
text
$ cookbook_diagnostics contract
stage 1: state is q T, stage_state() associated F
stage 2: state is q F, stage_state() associated T
stage 3: state is q F, stage_state() associated T
stage 4: state is q F, stage_state() associated T
stage 5: state is q F, stage_state() associated T

gfortran 16.2: two pointer functions in one associated()

associated(st%stage_state(), st%rhs_register()) makes gfortran 16.2 stop with an internal compiler error (gfc_conv_procedure_call). Assign the two pointers to local variables first, as above.

A request the stepper cannot honour ​

The stepper stops with a message instead of computing something wrong. The errors it detects:

  • a scheme asked for in a form it does not have;
  • a splitting without its partitions;
  • registers that are too few, numbered from anything but 1, or not of the field's bounds;
  • dq asked of a form other than the Butcher one.
fortran
call st%initialize(SCHEME_RK4, FORM_SHU_OSHER_2R, lb=[1, 0, 0, 0, 1], ub=[1, 5, 2, 2, 1])
text
$ cookbook_diagnostics wrong-form
ERROR STOP selah stepper: scheme "rk4" has no form "shu-osher-2r"
[exit status 1]

Each of these stops is tested by an expected-failure program (src/tests/selah_test_xfail_*.F90), which passes only when it exits with an error.

GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT, at your choice