Magnus schemes
A linear system whose operator changes in time,
magnus_stepper (selah_magnus) is driven by requests: after begin_step(bx, h, q), while request() is not MAGNUS_DONE, you answer it and call advance(bx). With the dense action (MAGNUS_DENSE: MAGNUS_MAX_BLOCK = 16), the requests are MAGNUS_NODE: write node_time(node()), into block_register(), blk(cell, v, w) advance makes and applies every exponential of every cell in one kernel.
A driven two-level system
character(*), intent(in) :: scheme
integer(I4P), intent(in) :: nsteps
real(R_P), intent(in) :: tend
real(R_P) :: y(4)
type(magnus_stepper) :: mg
type(box) :: bx
real(R_P), pointer :: q(:,:,:,:,:), blk(:,:,:,:,:,:)
real(R_P) :: h, t, f
integer(I4P) :: n
bx = box_interior(LAYOUT_V_FIRST, 4_I4P, 1_I4P, 1_I4P, 1_I4P, 0_I4P, 1_I4P) ! 4 components, one cell
allocate(q(4,1,1,1,1)) ; q(:,1,1,1,1) = [1._R_P, 0._R_P, 0._R_P, 0._R_P]
call mg%initialize(scheme, bx%lb, bx%hi, MAGNUS_DENSE, ncomp=4_I4P)
h = tend / nsteps
do n=0, nsteps - 1
t = n * h
call mg%begin_step(bx, h, q)
do while (mg%request() /= MAGNUS_DONE)
if (mg%request() == MAGNUS_NODE) then ! A(t + c_k h) = [[0, H], [-H, 0]] in the cell's block
f = F0 * cos(W0 * (t + mg%node_time(mg%node()) * h))
blk => mg%block_register() ; blk = 0._R_P
blk(1,1,1,1,1,3) = 0.5_R_P * W0 ; blk(1,1,1,1,1,4) = f
blk(2,1,1,1,1,3) = f ; blk(2,1,1,1,1,4) = -0.5_R_P * W0
blk(3:4,1,1,1,1,1:2) = -blk(1:2,1,1,1,1,3:4)
endif
call mg%advance(bx) ! after the last node: every exponential, per cell
enddo
enddo
y = q(:,1,1,1,1)
call mg%destroy ; deallocate(q)Each scheme at 40 and 80 steps over cf4-3opt at 5120:
type(magnus_scheme) :: ms
real(R_P) :: yref(4), y(4)
integer(I4P) :: s, n
yref = run('cf4-3opt', 5120_I4P, 10._R_P)
print '(A)', ' scheme order exponentials nodes steps error at t = 10'
do s=1, size(MAGNUS_SCHEMES)
ms = magnus(trim(MAGNUS_SCHEMES(s)))
do n=40, 80, 40
y = run(trim(MAGNUS_SCHEMES(s)), n, 10._R_P)
print '(1X,A9,I5,I10,I10,I9,ES16.2)', trim(MAGNUS_SCHEMES(s)), ms%order, ms%j, ms%k, n, norm2(y - yref)
enddo
enddo
print '(A,F10.6)', ' upper-level population at t = 10: ', yref(2)**2 + yref(4)**2$ cookbook_magnus two-level
scheme order exponentials nodes steps error at t = 10
cf4-2 4 2 2 40 5.09E-06
cf4-2 4 2 2 80 3.18E-07
cf4-3opt 4 3 3 40 1.52E-07
cf4-3opt 4 3 3 80 9.54E-09
cf4-4 4 4 3 40 1.22E-07
cf4-4 4 4 3 80 7.74E-09
cf6-5 6 5 3 40 2.90E-09
cf6-5 6 5 3 80 4.54E-11
cf6-5b 6 5 3 40 1.87E-09
cf6-5b 6 5 3 80 2.93E-11
cf6-5opt 6 5 4 40 2.90E-10
cf6-5opt 6 5 4 80 4.54E-12
cf8-11 8 11 4 40 6.30E-13
cf8-11 8 11 4 80 4.82E-14
upper-level population at t = 10: 0.275872The error falls 16 times per halving at order 4, 64 at order 6 and 13 at order 8 from 40 to 80 steps (it reaches the reference's round-off there). cf4-3opt and cf4-4 are 30 to 40 times more accurate than cf4-2 for one or two more exponentials; cf8-11 reaches cf4-4's exponentials all advance forward in time (

Every scheme at its order on two systems whose operators do not commute (A10).
The norm against a Runge–Kutta scheme
cf4-2 and rk4 through the stepper, the residual
real(R_P), parameter :: H = 0.05_R_P
integer(I4P), parameter :: NSTEPS = 10000_I4P
type(stepper) :: st
type(box) :: bx
real(R_P), allocatable, target :: u(:,:,:,:,:)
real(R_P), pointer :: y(:,:,:,:,:), r(:,:,:,:,:)
real(R_P) :: t, a(4,4), f
integer(I4P) :: n, k
print '(A,ES10.2)', ' cf4-2: | |psi|^2 - 1 | after 10^4 steps:', abs(sum(run('cf4-2', NSTEPS, NSTEPS * H)**2) - 1._R_P)
bx = box_interior(LAYOUT_V_FIRST, 4_I4P, 1_I4P, 1_I4P, 1_I4P, 0_I4P, 1_I4P)
allocate(u(4,1,1,1,1)) ; u(:,1,1,1,1) = [1._R_P, 0._R_P, 0._R_P, 0._R_P]
call st%initialize(SCHEME_RK4, FORM_BUTCHER, lb=bx%lb, ub=bx%hi)
do n=0, NSTEPS - 1
t = n * H
call st%begin_step(bx, u)
do k=1, st%stages()
call st%begin_stage(k, bx, H, u)
y => st%stage_state() ; if (st%stage_state_is_q(k)) y => u
r => st%rhs_register()
f = F0 * cos(W0 * (t + st%stage_time(k) * H))
a = 0._R_P ; a(1,3) = 0.5_R_P * W0 ; a(1,4) = f ; a(2,3) = f ; a(2,4) = -0.5_R_P * W0
a(3:4,1:2) = -a(1:2,3:4)
r(:,1,1,1,1) = matmul(a, y(:,1,1,1,1))
call st%end_stage(k, bx, H, u)
enddo
call st%finish_step(bx, H, u)
enddo
print '(A,ES10.2)', ' rk4: | |psi|^2 - 1 | after 10^4 steps:', abs(sum(u(:,1,1,1,1)**2) - 1._R_P)
call st%destroy$ cookbook_magnus norm
cf4-2: | |psi|^2 - 1 | after 10^4 steps: 1.44E-14
rk4: | |psi|^2 - 1 | after 10^4 steps: 1.51E-07The dense action computes each exponential by the diagonal Padé approximant of degree 8 with scaling and squaring, which is orthogonal for a skew-symmetric block: the probability is kept to rounding (rk4's drifts by
A coupled operator: the Krylov action
When MAGNUS_KRYLOV builds each exponential's action in a Krylov space: MAGNUS_APPLY asks you to apply weight(exponential(), k), to apply_in() into apply_out() (your halo exchange first), and MAGNUS_REDUCE to sum sums(1:nsums) over your boxes and ranks: one reduction per Krylov iteration, the inner products fused, one more per step for skew=.true. keeps the norm exactly), 20 steps to
integer(I4P), parameter :: NX = 64_I4P, NSTEPS = 20_I4P
real(R_P), parameter :: DX = 1._R_P / NX, PI2 = 2._R_P * acos(-1._R_P)
character(8), parameter :: SCHEMES(3) = [character(8) :: 'cf4-2', 'cf6-5opt', 'cf8-11']
type(magnus_stepper) :: mg
type(box) :: bx
real(R_P), pointer :: q(:,:,:,:,:), x(:,:,:,:,:), y(:,:,:,:,:)
real(R_P) :: h, t, w, d, th, err, ex
integer(I4P) :: s, n, i, k, iters, reductions
allocate(q(1,0:NX+1,1,1,1)) ! one ghost cell each side of the 64 cells
bx%lb = lbound(q) ; bx%lo = [1_I4P, 1_I4P, 1_I4P, 1_I4P, 1_I4P] ; bx%hi = [1_I4P, NX, 1_I4P, 1_I4P, 1_I4P]
h = 1._R_P / NSTEPS
print '(A)', ' scheme error at t = 1 | |u| - |u0| | Krylov iterations per exponential reductions per step'
do s=1, size(SCHEMES)
q(1,1:NX,1,1,1) = [(sin(PI2 * (i - 0.5_R_P) * DX), i=1, NX)]
call mg%initialize(trim(SCHEMES(s)), lbound(q), ubound(q), MAGNUS_KRYLOV, skew=.true.)
iters = 0_I4P ; reductions = 0_I4P
do n=0, NSTEPS - 1
t = n * h
call mg%begin_step(bx, h, q)
do while (mg%request() /= MAGNUS_DONE)
select case(mg%request())
case(MAGNUS_APPLY) ! apply_out = -(sum_k a_jk c(t_k)) D apply_in
w = 0._R_P
do k=1, mg%nodes()
w = w + mg%weight(mg%exponential(), k) * (1._R_P + 0.5_R_P * cos(PI2 * (t + mg%node_time(k) * h)))
enddo
x => mg%apply_in() ; y => mg%apply_out()
x(1,0,1,1,1) = x(1,NX,1,1,1) ; x(1,NX+1,1,1,1) = x(1,1,1,1,1) ! the halo: periodic here
do i=1, NX
call gideon_fd_d1_c2(x(1,i-1:i+1,1,1,1), DX, d)
y(1,i,1,1,1) = -w * d
enddo
case(MAGNUS_REDUCE) ! mpi_allreduce(mg%sums(1:mg%nsums)) over the boxes and ranks: one here
endselect
call mg%advance(bx)
enddo
iters = iters + mg%krylov ; reductions = reductions + mg%reductions
enddo
th = sin(PI2 * DX) / (PI2 * DX) ! the semi-discrete speed factor; int_0^1 c = 1
err = 0._R_P
do i=1, NX
ex = sin(PI2 * ((i - 0.5_R_P) * DX - th)) ; err = max(err, abs(q(1,i,1,1,1) - ex))
enddo
print '(1X,A9,ES14.2,ES18.2,F22.1,F26.1)', trim(SCHEMES(s)), err, &
abs(norm2(q(1,1:NX,1,1,1)) - sqrt(0.5_R_P * NX)), real(iters, R_P) / (NSTEPS * mg%exponentials()), &
real(reductions, R_P) / NSTEPS
call mg%destroy
enddo
print '(A,I3)', ' the Krylov space holds at most MAGNUS_MAX_KRYLOV =', MAGNUS_MAX_KRYLOV$ cookbook_magnus krylov
scheme error at t = 1 | |u| - |u0| | Krylov iterations per exponential reductions per step
cf4-2 7.85E-13 1.15E-14 3.3 10.5
cf6-5opt 2.49E-12 6.22E-15 2.7 20.9
cf8-11 5.22E-12 3.20E-14 3.8 59.5
the Krylov space holds at most MAGNUS_MAX_KRYLOV = 16The operators at different times commute here (cf8-11's 11 exponentials make 60 a step where cf4-2 makes 10 (Stepper: exponential stages).
An exponential of your own
With MAGNUS_CALLER SELAH gives the nodes, the weights and the order; you apply each exponential: MAGNUS_ACTION, q q with weight(exponential(), k). Here a rotation at the rate
type(magnus_stepper) :: mg
type(box) :: bx
real(R_P), pointer :: q(:,:,:,:,:)
real(R_P) :: h, t, th, y(2)
integer(I4P) :: s, n, k
bx = box_interior(LAYOUT_V_FIRST, 2_I4P, 1_I4P, 1_I4P, 1_I4P, 0_I4P, 1_I4P)
allocate(q(2,1,1,1,1))
h = 0.1_R_P
print '(A,I3)', ' the dense action takes blocks of up to MAGNUS_MAX_BLOCK =', MAGNUS_MAX_BLOCK
do s=1, size(MAGNUS_SCHEMES)
q(:,1,1,1,1) = [1._R_P, 0._R_P]
call mg%initialize(trim(MAGNUS_SCHEMES(s)), bx%lb, bx%hi, MAGNUS_CALLER)
do n=0, 9
t = n * h
call mg%begin_step(bx, h, q)
do while (mg%request() /= MAGNUS_DONE)
if (mg%request() == MAGNUS_ACTION) then ! q <- exp(h sum_k a_jk A(t_k)) q: a rotation by th
th = 0._R_P
do k=1, mg%nodes()
th = th + h * mg%weight(mg%exponential(), k) * (1._R_P + (t + mg%node_time(k) * h)**2)
enddo
y = q(:,1,1,1,1)
q(:,1,1,1,1) = [cos(th) * y(1) - sin(th) * y(2), sin(th) * y(1) + cos(th) * y(2)]
endif ! MAGNUS_NODE: nothing to prepare
call mg%advance(bx)
enddo
enddo
th = 1._R_P + 1._R_P / 3._R_P
print '(1X,A9,A,ES10.2)', trim(MAGNUS_SCHEMES(s)), ': error at t = 1', norm2(q(:,1,1,1,1) - [cos(th), sin(th)])
call mg%destroy
enddo$ cookbook_magnus caller
the dense action takes blocks of up to MAGNUS_MAX_BLOCK = 16
cf4-2: error at t = 1 4.16E-16
cf4-3opt: error at t = 1 6.89E-16
cf4-4: error at t = 1 3.78E-16
cf6-5: error at t = 1 6.21E-16
cf6-5b: error at t = 1 4.24E-16
cf6-5opt: error at t = 1 4.48E-16
cf8-11: error at t = 1 1.52E-15The rotation's matrices commute, so the step is the rotation by the Gauss quadrature of the angle, exact for the quadratic rate: the error is the rounding's. ADAM's CFM-4 was this contract with each exponential replaced by a forward-Euler step; that made it a Runge–Kutta method of order 2 (A10).