Skip to content

15. The leapfrog ​

Goal: run leapfrog and leapfrog-raw through the stepper: the start-up, the computational mode and the filter that damps it, a checkpoint with history() and resume_history, a new step with restart_history, and the stability bound.

The leapfrog is a two-step method, Un+1=Un−1+2ΔtR(Un): one residual per step, second order, and a second, spurious solution, the computational mode, which flips sign every step. leapfrog-raw adds the Robert–Asselin–Williams filter (ν=0.2, α=0.53), which damps that mode (Schemes: leapfrog). In the stepper both are FORM_LEAPFROG. The form keeps one more register than the others need between steps: the history, the filtered Un−1, which survives finish_step and is part of the solution's state (Stepper: leapfrog).

The program runs the tutorial's advection, u=sin⁡2πx with the central residual, at a Courant number of 0.4.

The step and the start-up ​

fortran
call st%begin_step(bx, q)
do k=1, st%stages()                                         ! the stage state is q itself in this form
   call st%begin_stage(k, bx, dt, q)
   r => st%rhs_register()
   call fill_ghosts(q)
   call residual(q, r)
   call st%end_stage(k, bx, dt, q)
enddo
call st%finish_step(bx, dt, q)                               ! the filter, and the history for the next step

The loop is chapter 4's, with q as the stage state: in this form the stage state is always q. It asks stages() at every step, because the first step is different:

fortran
do s=1, 2
   call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
   print '(A,A12,A,F7.4,A,F7.4)', 'scheme ', SCHEMES(s), ' imaginary-axis bound', st%imag_axis_bound(), &
         ', real-axis bound', st%real_axis_bound()
   call initial(q, smooth=.true.)
   print '(A,I0,A,L1)', '   before step 1: stages() = ', st%stages(), ', history_is_ready() = ', st%history_is_ready()
   call advance(st, q, dt, 1_I4P)                                  ! the start-up: one ssp-22 step
   print '(A,I0,A,L1)', '   after step 1:  stages() = ', st%stages(), ', history_is_ready() = ', st%history_is_ready()
   call st%destroy
enddo
  • The first step after initialize has no Un−1. It is a start-up: one ssp-22 step in the two-register form, so stages() is 2 for that step. After it the history holds U0 and stages() is 1.
  • history_is_ready() tells which kind of step comes next.
  • The start-up excites the computational mode only at about Δt4/16 on an oscillation (P6).

The computational mode ​

To see the mode, the program starts a second run without the start-up: it writes q into the history, U−1=U0, and declares the history valid with resume_history(dt). The first leapfrog step is then a step of 2Δt from U0, and the two-step oscillation it creates is the computational mode.

fortran
call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
call initial(q, smooth=.true.)
h => st%history()
h = q                                                           ! U^{-1} = U^0: a step of 2 dt from U^0 next
call st%resume_history(dt)                                      ! no start-up
call advance(st, q, dt, NMODE, cold(:,:,s))
call st%destroy

The program measures the mode as the two-step oscillation of the difference Dn between this run and the started one, max|Dn+1−2Dn+Dn−1|/4.

A checkpoint and a new step ​

fortran
call sa%initialize(SCHEME_LEAPFROG_RAW, FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
call initial(qa, smooth=.true.)
call advance(sa, qa, dt, 12_I4P)
h => sa%history()
qb = qa ; hc = h                                                   ! the checkpoint: q and the history
call advance(sa, qa, dt, 12_I4P)                                   ! the uninterrupted run, 24 steps
call sb%initialize(SCHEME_LEAPFROG_RAW, FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
h => sb%history()
h = hc                                                             ! the restart: the history written back
call sb%resume_history(dt)
call advance(sb, qb, dt, 12_I4P)                                   ! 12 + 12 steps
same = all(qa == qb)
print '(A,L1)', 'checkpoint at step 12, resumed to step 24: bitwise the uninterrupted run: ', same
  • A checkpoint saves q and history() after finish_step. A restart writes both back into a new stepper and calls resume_history(dt), with the step the history was made with.
  • Below rank 5 the history comes from history_view(h), as stage_state_view does for the stage state.
  • With a filter, q is the singly filtered Un+1, offset from the solution by O(ν(1−α)Δt2), and the history is the doubly filtered Un. Both are the state: save them as they are.
fortran
call sa%restart_history                                            ! a new step: the next step is a start-up
print '(A,I0)', 'restart_history, then dt / 2: stages() = ', sa%stages()
call advance(sa, qa, dt / 2, 4_I4P)
print '(A,I0,A,L1)', '   after 4 steps of dt / 2: stages() = ', sa%stages(), ', history_is_ready() = ', &
      sa%history_is_ready()

The leapfrog and its filter assume a constant step. restart_history() forgets the history: the next step is a start-up, with any dt, and after it the steps must keep that dt.

The stability bound ​

fortran
do s=1, 2                                                          ! a square wave, 400 steps
   do j=1, 2                                                       ! Courant numbers below and above the bound
      call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
      w(j) = merge(0.95_R_P, 2._R_P, j == 1) * st%imag_axis_bound()  ! omega dt of the fastest mode, 4 cells long
      call leapfrog_roots(leapfrog(trim(SCHEMES(s))), cmplx(0._R_P, w(j), R_P), a_phys, a_comp)
      amp(j,s) = max(abs(a_phys), abs(a_comp))                     ! the larger root of Williams's eq. (11)
      call initial(q, smooth=.false.)
      call advance(st, q, w(j) * DX, 400_I4P)
      grow(j,s) = maxval(abs(q(1,1:NI,1,1,:)))
      call st%destroy
   enddo
enddo
print '(A)', 'Courant number / bound:            0.95                     2'
print '(A)', '              largest |root|   max |q| at step 400   largest |root|   max |q| at step 400'
do s=1, 2
   print '(A12,2(F17.4,ES20.3E3))', SCHEMES(s), (amp(j,s), grow(j,s), j=1, 2)
enddo

imag_axis_bound() is the largest ωΔt of a stable oscillation: 1 for leapfrog, 0.4371 for leapfrog-raw. The central residual's fastest mode, 4 cells long, has ωΔt equal to the Courant number. leapfrog_roots(f, z, a_phys, a_comp) gives the two roots of one filtered step at z=iωΔt, the physical mode and the computational one; the larger of the two says how fast the fastest mode grows.

The amplification factors of the leapfrog and its filters against the step

The two roots of each filter's amplification factor on y′=iωy: the bound is where they leave the unit circle (A9).

Running it ​

text
$ leapfrog
scheme leapfrog     imaginary-axis bound 1.0000, real-axis bound 0.0000
   before step 1: stages() = 2, history_is_ready() = F
   after step 1:  stages() = 1, history_is_ready() = T
scheme leapfrog-raw imaginary-axis bound 0.4371, real-axis bound 0.1988
   before step 1: stages() = 2, history_is_ready() = F
   after step 1:  stages() = 1, history_is_ready() = T
step   computational mode, history = q at the start: leapfrog   leapfrog-raw
   1                                          1.962E-02      1.600E-02
   2                                          1.958E-02      1.278E-02
   3                                          1.962E-02      1.025E-02
   4                                          1.960E-02      8.175E-03
   5                                          1.961E-02      6.560E-03
   6                                          1.961E-02      5.231E-03
   7                                          1.960E-02      4.200E-03
   8                                          1.961E-02      3.347E-03
   9                                          1.961E-02      2.689E-03
  10                                          1.960E-02      2.142E-03
  11                                          1.962E-02      1.722E-03
  12                                          1.959E-02      1.370E-03
  13                                          1.962E-02      1.102E-03
  14                                          1.960E-02      8.767E-04
  15                                          1.961E-02      7.056E-04
  16                                          1.961E-02      5.613E-04
  17                                          1.959E-02      4.515E-04
  18                                          1.961E-02      3.599E-04
  19                                          1.961E-02      2.882E-04
  20                                          1.961E-02      2.315E-04
  21                                          1.961E-02      1.832E-04
  22                                          1.959E-02      1.498E-04
  23                                          1.961E-02      1.155E-04
  24                                          1.961E-02      9.789E-05
  25                                          1.960E-02      7.210E-05
  26                                          1.962E-02      6.501E-05
  27                                          1.959E-02      4.391E-05
  28                                          1.962E-02      4.430E-05
  29                                          1.960E-02      2.571E-05
  30                                          1.961E-02      3.121E-05
  31                                          1.961E-02      1.391E-05
checkpoint at step 12, resumed to step 24: bitwise the uninterrupted run: T
restart_history, then dt / 2: stages() = 2
   after 4 steps of dt / 2: stages() = 1, history_is_ready() = T
Courant number / bound:            0.95                     2
              largest |root|   max |q| at step 400   largest |root|   max |q| at step 400
leapfrog               1.0000          1.319E+000           3.7321          2.244E+226
leapfrog-raw           0.9999          1.268E+000           1.0421          3.328E+005
[exit status 0]
  • Without the filter the computational mode keeps its amplitude, 1.96E-02, at every step. With it, the mode shrinks by 0.8 per step, 1−ν, to 1.39E-05 by step 31, where the measure wobbles at a few 10−5.
  • The run resumed from the checkpoint at step 12 equals the uninterrupted run at step 24 bit for bit, and the program checks it.
  • At 0.95 times the bound both schemes stay bounded. At twice the bound the plain leapfrog's root is 3.7321 and q reaches 2.244E+226 in 400 steps; leapfrog-raw's is 1.0421, and q reaches 3.328E+05.

The computational mode per step: constant without the filter, falling by 1 - nu per step with it

The computational mode of the run without start-up, from the table above, with the filter's damping (1−ν)n (docs/scripts/make_tutorial_figures.py).

What you learned ​

  • FORM_LEAPFROG starts with a two-stage ssp-22 step; ask stages() at every step.
  • The computational mode keeps its amplitude without a filter; leapfrog-raw damps it by 1−ν per step.
  • The history is part of the state: a checkpoint saves q and history(), a restart writes both and calls resume_history(dt), and the run continues bit for bit.
  • A new dt needs restart_history() first; the next step is a start-up.
  • imag_axis_bound() bounds ωΔt: 1 unfiltered, 0.4371 with the filter.

Reference: Schemes: leapfrog, Stepper: leapfrog; the checks behind them: A9 Leapfrog, P6 Leapfrog filter.

Try this ​

  • Robert–Asselin. Add alpha=1._R_P to every initialize (it changes leapfrog-raw only: the plain leapfrog has ν=0). The imaginary-axis bound rises to 0.9045, and the computational mode still falls by 1−ν per step, 1.766E-02 to 1.461E-05 by step 31: the damping is the filter's ν, not its α. The price is a first-order amplitude drift (A9).
  • Just past the bound. Replace 2._R_P by 1.05_R_P in the bound loop. The plain leapfrog's root is 1.3702 and q reaches 4.294E+50; leapfrog-raw's is 1.0001 and q stays at 1.259E+00 after 400 steps: past its bound the filtered scheme grows, but slowly.
  • Restart instead of resume. After the checkpoint, call sb%restart_history in place of sb%resume_history(dt). The resumed run starts with a start-up instead of a leapfrog step, is no longer bit for bit the uninterrupted one, and the program stops: ERROR STOP leapfrog: the resumed run differs from the uninterrupted one.

Pitfalls ​

  • A new step without restart_history. Comment out call sa%restart_history: the step with dt / 2 stops the program, ERROR STOP selah stepper: the leapfrog step changed (dt): call restart_history() before such a step (also selah_test_xfail_leapfrog_dt). Fix: restart_history() before the first step with the new dt.
  • A checkpoint without the history. Skip h = hc: the new stepper resumes from its zero history, the run differs from the uninterrupted one, and the program stops: ERROR STOP leapfrog: the resumed run differs from the uninterrupted one. Fix: save and write back history() with q.
  • A fixed stage count. A loop do k=1, 1 runs the start-up with one of its two stages, and nothing stops: the results change silently (the bound table's 1.319E+000 becomes 1.320E+000). Fix: do k=1, st%stages(), asked at every step.
  • What the leapfrog does not take. decay=, dq, lookahead and stage_current are the Butcher form's; begin_stage(..., decay=) on a leapfrog stepper stops with selah stepper: decay= is the Butcher form's (selah_test_xfail_leapfrog_decay). Fix: a Runge–Kutta scheme in FORM_BUTCHER for those features.
The whole program
fortran
program leapfrog_tour
!< Tutorial, chapter 15: the leapfrog and its filter on the periodic advection: the start-up, the computational mode,
!< a checkpoint and a change of step, the stability bound.
use selah
use gideon, only : gideon_fd_d1_c2   ! the space operator: GIDEON's centred first derivative
implicit none
integer(I4P), parameter        :: NI=16, NB=4, NMODE=32
real(R_P),    parameter        :: PI=acos(-1._R_P), DX=1._R_P/(NI*NB), COURANT=0.4_R_P
character(*), parameter        :: SCHEMES(2)=[character(12) :: SCHEME_LEAPFROG, SCHEME_LEAPFROG_RAW]
real(R_P), allocatable, target :: q(:,:,:,:,:), qa(:,:,:,:,:), qb(:,:,:,:,:)
real(R_P), allocatable         :: hc(:,:,:,:,:), rec(:,:,:), cold(:,:,:), mode(:,:)
real(R_P), pointer             :: h(:,:,:,:,:)
type(stepper)                  :: st, sa, sb
type(box)                      :: bx
real(R_P)                      :: dt, grow(2,2), amp(2,2), w(2)
complex(R_P)                   :: a_phys, a_comp
integer(I4P)                   :: s, n, j
logical                        :: same

allocate(q(1:1, 0:NI+1, 0:2, 0:2, 1:NB))
allocate(qa, qb, hc, mold=q)
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 1_I4P, NB)
dt = COURANT * DX                                                       ! a Courant number of 0.4
do s=1, 2
   call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
   print '(A,A12,A,F7.4,A,F7.4)', 'scheme ', SCHEMES(s), ' imaginary-axis bound', st%imag_axis_bound(), &
         ', real-axis bound', st%real_axis_bound()
   call initial(q, smooth=.true.)
   print '(A,I0,A,L1)', '   before step 1: stages() = ', st%stages(), ', history_is_ready() = ', st%history_is_ready()
   call advance(st, q, dt, 1_I4P)                                  ! the start-up: one ssp-22 step
   print '(A,I0,A,L1)', '   after step 1:  stages() = ', st%stages(), ', history_is_ready() = ', st%history_is_ready()
   call st%destroy
enddo

! the computational mode: a run whose history is set to q (U^{-1} = U^0, no start-up) against the started run
allocate(rec(NI*NB, 0:NMODE, 2), cold(NI*NB, 0:NMODE, 2), mode(NMODE-1, 2))
do s=1, 2
   call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
   call initial(q, smooth=.true.)
   call advance(st, q, dt, NMODE, rec(:,:,s))
   call st%destroy
   call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
   call initial(q, smooth=.true.)
   h => st%history()
   h = q                                                           ! U^{-1} = U^0: a step of 2 dt from U^0 next
   call st%resume_history(dt)                                      ! no start-up
   call advance(st, q, dt, NMODE, cold(:,:,s))
   call st%destroy
   do n=1, NMODE-1                                                 ! the 2-step oscillation of the difference
      mode(n,s) = maxval(abs(cold(:,n+1,s) - rec(:,n+1,s) - 2*(cold(:,n,s) - rec(:,n,s)) &
                             + cold(:,n-1,s) - rec(:,n-1,s))) / 4
   enddo
enddo
print '(A)', 'step   computational mode, history = q at the start: leapfrog   leapfrog-raw'
do n=1, NMODE-1
   print '(I4,ES51.3,ES15.3)', n, mode(n,:)
enddo

call sa%initialize(SCHEME_LEAPFROG_RAW, FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
call initial(qa, smooth=.true.)
call advance(sa, qa, dt, 12_I4P)
h => sa%history()
qb = qa ; hc = h                                                   ! the checkpoint: q and the history
call advance(sa, qa, dt, 12_I4P)                                   ! the uninterrupted run, 24 steps
call sb%initialize(SCHEME_LEAPFROG_RAW, FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
h => sb%history()
h = hc                                                             ! the restart: the history written back
call sb%resume_history(dt)
call advance(sb, qb, dt, 12_I4P)                                   ! 12 + 12 steps
same = all(qa == qb)
print '(A,L1)', 'checkpoint at step 12, resumed to step 24: bitwise the uninterrupted run: ', same
call sa%restart_history                                            ! a new step: the next step is a start-up
print '(A,I0)', 'restart_history, then dt / 2: stages() = ', sa%stages()
call advance(sa, qa, dt / 2, 4_I4P)
print '(A,I0,A,L1)', '   after 4 steps of dt / 2: stages() = ', sa%stages(), ', history_is_ready() = ', &
      sa%history_is_ready()
call sa%destroy ; call sb%destroy

do s=1, 2                                                          ! a square wave, 400 steps
   do j=1, 2                                                       ! Courant numbers below and above the bound
      call st%initialize(trim(SCHEMES(s)), FORM_LEAPFROG, lb=lbound(q), ub=ubound(q))
      w(j) = merge(0.95_R_P, 2._R_P, j == 1) * st%imag_axis_bound()  ! omega dt of the fastest mode, 4 cells long
      call leapfrog_roots(leapfrog(trim(SCHEMES(s))), cmplx(0._R_P, w(j), R_P), a_phys, a_comp)
      amp(j,s) = max(abs(a_phys), abs(a_comp))                     ! the larger root of Williams's eq. (11)
      call initial(q, smooth=.false.)
      call advance(st, q, w(j) * DX, 400_I4P)
      grow(j,s) = maxval(abs(q(1,1:NI,1,1,:)))
      call st%destroy
   enddo
enddo
print '(A)', 'Courant number / bound:            0.95                     2'
print '(A)', '              largest |root|   max |q| at step 400   largest |root|   max |q| at step 400'
do s=1, 2
   print '(A12,2(F17.4,ES20.3E3))', SCHEMES(s), (amp(j,s), grow(j,s), j=1, 2)
enddo
if (.not. same) error stop 'leapfrog: the resumed run differs from the uninterrupted one'

contains
   subroutine advance(st, q, dt, nsteps, rec)
   !< nsteps steps; stages() asked at every step (2 for a start-up, 1 after); q after each step into rec.
   type(stepper), intent(inout)           :: st
   real(R_P),     intent(inout), target   :: q(1:,0:,0:,0:,1:)
   real(R_P),     intent(in)              :: dt
   integer(I4P),  intent(in)              :: nsteps
   real(R_P),     intent(inout), optional :: rec(:,0:)
   real(R_P), pointer                     :: r(:,:,:,:,:)
   integer(I4P)                           :: step, k

   if (present(rec)) rec(:,0) = reshape(q(1,1:NI,1,1,:), [NI*NB])
   do step=1, nsteps
      call st%begin_step(bx, q)
      do k=1, st%stages()                                         ! the stage state is q itself in this form
         call st%begin_stage(k, bx, dt, q)
         r => st%rhs_register()
         call fill_ghosts(q)
         call residual(q, r)
         call st%end_stage(k, bx, dt, q)
      enddo
      call st%finish_step(bx, dt, q)                               ! the filter, and the history for the next step
      if (present(rec)) rec(:,step) = reshape(q(1,1:NI,1,1,:), [NI*NB])
   enddo
   endsubroutine advance

   subroutine initial(q, smooth)
   !< sin(2 pi x), or a square wave (1 on block 2).
   real(R_P), intent(out) :: q(1:,0:,0:,0:,1:)
   logical,   intent(in)  :: smooth
   integer(I4P)           :: b, i

   q = 0._R_P
   do b=1, NB
      do i=1, NI
         q(1,i,1,1,b) = merge(sin(2._R_P*PI*((b-1)*NI + i - 0.5_R_P)*DX), merge(1._R_P, 0._R_P, b == 2), smooth)
      enddo
   enddo
   endsubroutine initial

   subroutine fill_ghosts(u)
   real(R_P), intent(inout) :: u(1:,0:,0:,0:,1:)
   integer(I4P)             :: b
   do b=1, NB
      u(1,0,1,1,b)    = u(1,NI,1,1,1 + modulo(b-2, NB))
      u(1,NI+1,1,1,b) = u(1,1,1,1,1 + modulo(b, NB))
   enddo
   endsubroutine fill_ghosts

   subroutine residual(u, r)
   real(R_P), intent(in)    :: u(1:,0:,0:,0:,1:)
   real(R_P), intent(inout) :: r(1:,0:,0:,0:,1:)
   integer(I4P)             :: b, i
   real(R_P)                :: d
   do b=1, NB
      do i=1, NI
         call gideon_fd_d1_c2([u(1,i-1,1,1,b), u(1,i,1,1,b), u(1,i+1,1,1,b)], DX, d) ; r(1,i,1,1,b) = -d
      enddo
   enddo
   endsubroutine residual
endprogram leapfrog_tour

Next: Cookbook.