13. Particles
Goal: advance charged particles as a companion of the field stepper, keep Gauss's law at every stage with lookahead and stage_current, and push the same particles with selah_push (Boris) instead.
A particle-in-cell code has two states that advance together: the field, here the electric field
- with the field's Runge–Kutta scheme: the particle array is a companion of the field stepper, a second stepper with the same scheme and form, driven through the same calls in the same stage window;
- with a two-level pusher, staggered in time with the field:
selah_push, one call per step.
This chapter's program is a 1-D electrostatic plasma on the tutorial's four blocks of 16 cells: 64 electrons (
The field and its companion
type(stepper) :: st, sp ! the field's stepper and its companion, the particles'
type(box) :: bx, bp
real(R_P), allocatable :: e(:,:,:,:,:) ! E at the right face of each cell: e(1,i,1,1,b)
real(R_P), allocatable :: p(:,:), pt(:,:) ! p(8,NP): x 1:3, v 4:6, q 7, m 8; pt: a look-ahead
real(R_P), allocatable :: h(:,:,:,:,:), jk(:,:,:,:,:), jall(:,:,:,:,:,:) ! H, J_k and the stage currentsbx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 0_I4P, NB)
bp = box_range(lb=[1_I4P, 1_I4P], lo=[1_I4P, 1_I4P], hi=[6_I4P, NP], components=.true.) ! q, m do not evolve
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(e), ub=ubound(e))
call sp%initialize_companion(st, lb=[1_I4P, 1_I4P], ub=[8_I4P, NP], components=.true.)
ns = st%stages()
allocate(h, mold=e) ; allocate(jk, mold=e) ; allocate(jall(1:1, 1:NI, 1:1, 1:1, 1:NB, 1:ns))lives at the right face of each cell, e(1,i,1,1,b): a rank-5 field with no ghost cells, sincehas no stencil. initialize_companion(st, lb, ub, components=.true.)makesspa stepper with the scheme, the form and the stages on q ofst. Its bounds are a capacity:p(8, NP), withat 1:3, at 4:6, at 7 and at 8. - The box
bpcovers components 1:6 of particles1..NP: what evolves. The kernels never touch components 7 and 8 ofpor of the companion's registers. jallholds the stage currents, one slot per stage: the caller's registers.
The coupled step
call st%begin_step(bx, e) ; call sp%begin_step(bp, p)
do k=1, ns
call st%begin_stage(k, bx, DT, e) ; call sp%begin_stage(k, bp, DT, p)
ue => st%stage_state() ; call sp%stage_state_view(up) ! Butcher form: registers, not e and p
g(k,step,merge(1, 2, conserving)) = gauss(ue, up(1,:), p(7,:)) ! the stage state: field and particles
call sp%rhs_register_view(rp)
call lorentz(ue, up, p(7,:) / p(8,:), rp) ! the particles' residual; q, m from p
re => st%rhs_register()
if (conserving) then
call sp%lookahead(k, bp, DT, p, pt) ! the particles at stage k+1
call deposit(p(1,:), pt(1,:), p(7,:), h) ! H: the current that moves p to pt in DT
call st%stage_current(k, bx, h, jall, jk) ! J_k: sum_{r<=k} a_{k+1,r} J_r = H
jall(:,:,:,:,:,k) = jk
re = -jk ! Ampere: E' = -J
else
call deposit_direct(up, p(7,:), re)
re = -re
endif
call st%end_stage(k, bx, DT, e) ; call sp%end_stage(k, bp, DT, p)
enddo
call st%finish_step(bx, DT, e) ; call sp%finish_step(bp, DT, p)- Every call goes to both steppers, the field's first, in the same window of stage
. - The particles' residual is
, with gathered at the stage positions from the field's stage state. The charge-to-mass ratio comes from p, not from the companion's stage state (Pitfalls). - After that residual,
sp%lookahead(k, bp, DT, p, pt)gives the particles' state at stage(the step's result at the last stage), from the residuals written so far. depositgives, the current that moves the charge from ptoptinDT.st%stage_current(k, bx, h, jall, jk)gives, so that : the field at stage is the field moved by exactly the charge that moved. The caller stores in jalland writesas the field's residual. - The individual
are not physical currents (the inversion amplifies them, up to 25 times for ssp-104): only their weighted sums are (C9).
The deposit is Esirkepov's charge-conserving current in 1-D: through face
subroutine deposit(x0, x1, qc, h)
!< H: the charge-conserving current that moves the particles from x0 to x1 in DT (Esirkepov's in 1-D): through
!< face f, q (right(x1, f) - right(x0, f)) / DT. For any displacement below a cell.
real(R_P), intent(in) :: x0(:), x1(:), qc(:)
real(R_P), intent(out) :: h(1:,1:,1:,1:,1:)
real(R_P) :: jf(NC)
integer(I4P) :: j, f
jf = 0._R_P
do j=1, NP
if (abs(x1(j) - x0(j)) >= DX) error stop 'particles: a displacement of a cell or more'
do f=floor(min(x0(j), x1(j)) / DX) - 1, floor(max(x0(j), x1(j)) / DX) + 1
jf(cell(f)) = jf(cell(f)) + qc(j) * (right(x1(j), f) - right(x0(j), f)) / DT
enddo
enddo
h(1,:,1,1,:) = reshape(jf, [NI, NB])
endsubroutine depositThe check, at every stage state and after every step:
function gauss(e, x, qc) result(gmax)
!< max over the cells of |(E_c - E_{c-1})/DX - rho_c - 1|: Gauss's law, the background of charge 1 included.
real(R_P), intent(in) :: e(1:,1:,1:,1:,1:), x(:), qc(:)
real(R_P) :: gmax, ef(NC), rho(NC)
ef = reshape(e(1,:,1,1,:), [NC])
rho = density(x, qc)
gmax = maxval(abs((ef - cshift(ef, -1)) / DX - rho - 1._R_P))
endfunction gaussThe Boris pusher
The same plasma, the same deposit, no Runge–Kutta stages: the positions at
call gather(e, p(1,:), eg) ! E at the particles, x^n
bg = 0._R_P ; bg(3,:) = B0 ! B = B0 z
pn = p
call selah_push(PUSHER_BORIS, LAYOUT_V_FIRST, NP, DT, 1_I4P, 4_I4P, 7_I4P, 8_I4P, p, eg, bg)
call deposit(pn(1,:), p(1,:), p(7,:), h) ! the current that moves x^n to x^{n+1}
e = e - DT * hselah_push(pusher, layout, n, dt, ix, iv, iq, im, p, e, b) pushes the first n particles of p, with ix:ix+2, iv:iv+2, iq, im, in the fields e and b gathered at
Running it
call companion(.true., g) ! stage currents: lookahead + stage_current
call companion(.false., g) ! the stage particles' own current, deposited
ns = size(g, 1) - 1
print '(A)', SCHEME//', 64 particles, Gauss residual max |dE/dx - rho| at each stage state and after each step'
print '(A,I0,A)', 'step stage currents: < 1e-12 at stages 1-', ns, ', step direct deposit: largest'
do step=1, NSTEPS
write(flags, '(*(L2))') g(:,step,1) < TOL
print '(I4,2X,A24,ES26.3)', step, flags, maxval(g(:,step,2))
enddo
ok = all(g(:,:,1) < TOL)
print '(A,L1)', 'stage currents: Gauss''s law at every stage and step: ', ok
call boris(gb)
print '(A,L1)', 'Boris pusher (selah_push): Gauss''s law after every step: ', all(gb < TOL)
if (.not. ok) error stop 'particles: Gauss''s law broken at a stage'
if (.not. all(gb < TOL)) error stop 'particles: Gauss''s law broken by the Boris step'$ particles
ssp-54, 64 particles, Gauss residual max |dE/dx - rho| at each stage state and after each step
step stage currents: < 1e-12 at stages 1-5, step direct deposit: largest
1 T T T T T T 8.616E-04
2 T T T T T T 3.404E-03
3 T T T T T T 7.477E-03
4 T T T T T T 1.276E-02
5 T T T T T T 1.871E-02
6 T T T T T T 2.751E-02
7 T T T T T T 2.784E-02
8 T T T T T T 2.522E-02
stage currents: Gauss's law at every stage and step: T
Boris pusher (selah_push): Gauss's law after every step: T
[exit status 0]With the stage currents the Gauss residual is below

The direct deposit's largest Gauss residual of each step, and the bound the stage currents meet at every stage (docs/scripts/make_tutorial_figures.py).
What you learned
- A companion is a second stepper,
initialize_companion(field, lb, ub), driven through the same four calls in the same stage window; its box is the part that evolves. - Within the window of stage
, after the particles' residual: lookahead(k)gives the next stage's particles, the deposit gives, stage_current(k)gives, and is the field's residual. - With a charge-conserving deposit, Gauss's law then holds at every stage and step, in every scheme of the catalogue.
selah_pushis the other way: one staggered push per step, with the deposit of.
Reference: Particles, Stepper: companion states; the checks behind them: C8, C9, A8.
Try this
- Another scheme. Set
SCHEME=SCHEME_RK4: four stages, five flags per step, allT.SCHEME_SSP_104: ten stages, eleven flags, allT. The direct deposit's residual is 2.522E-02 at step 8 in all three schemes. - A longer step. Set
DT=0.5_R_P: every flag staysT; the direct deposit's residual is 3.402E-03 after the first step, four times that atDT=0.25. - Another pusher. Replace
PUSHER_BORISbyPUSHER_HIGUERA_CARY(relativistic,by default): Gauss's law still holds after every step. The deposit, not the pusher, keeps it.
Pitfalls
- Charges and masses from the companion's stage state. The box covers components 1:6, so components 7:8 of the companion's stage state hold the register's initial value, 0. With
up(7,:) / up(8,:)as the charge-to-mass ratio,IEEE_INVALID_FLAGis signalling, the Gauss check fails from stage 3 of step 1, the direct deposit's residual isNaNfrom step 2, and the program stops:ERROR STOP particles: Gauss's law broken at a stage. Fix: read what does not evolve fromp. - The look-ahead before the particles' residual.
lookahead(k)builds the next stage from the residuals written so far; called beforelorentz, it misses stage's. Gauss's law fails from stage 3 of step 1. Fix: the particles' residual first, then lookahead(k). in place of the stage current, or not kept. Writing as the field's residual breaks Gauss's law from stage 3 of step 1; not storing jkinjall(..., k)breaks it from stage 4. Fix:stage_current(k)every stage, andjall(..., k) = jkbefore the next.- Another storage form.
lookaheadandstage_currentare the Butcher form's. Withssp-33inFORM_SHU_OSHER_2Rthe program stops:ERROR STOP selah stepper: the look-ahead state is the Butcher form's. Fix:FORM_BUTCHERfor the field; the companion takes the field's form.
The whole program
program particles
!< Tutorial, chapter 13: a 1-D electrostatic particle-in-cell plasma. The particles are a companion of the field
!< stepper, their stage currents keep Gauss's law at every stage; then the same plasma with the Boris pusher.
use selah
implicit none
character(*), parameter :: SCHEME=SCHEME_SSP_54
integer(I4P), parameter :: NI=16, NB=4, NC=NI*NB, NP=64, NSTEPS=8 ! 4 blocks of 16 cells; 64 particles
real(R_P), parameter :: PI=acos(-1._R_P), DX=1._R_P/NC, DT=0.25_R_P, B0=1._R_P, TOL=1.e-12_R_P
real(R_P), allocatable :: g(:,:,:) ! |Gauss residual|: (stage or step, step, run)
real(R_P) :: gb(NSTEPS) ! |Gauss residual| after each Boris step
integer(I4P) :: step, ns
logical :: ok
character(24) :: flags
call companion(.true., g) ! stage currents: lookahead + stage_current
call companion(.false., g) ! the stage particles' own current, deposited
ns = size(g, 1) - 1
print '(A)', SCHEME//', 64 particles, Gauss residual max |dE/dx - rho| at each stage state and after each step'
print '(A,I0,A)', 'step stage currents: < 1e-12 at stages 1-', ns, ', step direct deposit: largest'
do step=1, NSTEPS
write(flags, '(*(L2))') g(:,step,1) < TOL
print '(I4,2X,A24,ES26.3)', step, flags, maxval(g(:,step,2))
enddo
ok = all(g(:,:,1) < TOL)
print '(A,L1)', 'stage currents: Gauss''s law at every stage and step: ', ok
call boris(gb)
print '(A,L1)', 'Boris pusher (selah_push): Gauss''s law after every step: ', all(gb < TOL)
if (.not. ok) error stop 'particles: Gauss''s law broken at a stage'
if (.not. all(gb < TOL)) error stop 'particles: Gauss''s law broken by the Boris step'
contains
subroutine companion(conserving, g)
!< NSTEPS steps of SCHEME on the field E and, as its companion, the particles; the Gauss residual at every stage.
logical, intent(in) :: conserving !< Stage currents (else the direct deposit of q v).
real(R_P), allocatable, intent(inout) :: g(:,:,:) !< |Gauss residual|.
type(stepper) :: st, sp ! the field's stepper and its companion, the particles'
type(box) :: bx, bp
real(R_P), allocatable :: e(:,:,:,:,:) ! E at the right face of each cell: e(1,i,1,1,b)
real(R_P), allocatable :: p(:,:), pt(:,:) ! p(8,NP): x 1:3, v 4:6, q 7, m 8; pt: a look-ahead
real(R_P), allocatable :: h(:,:,:,:,:), jk(:,:,:,:,:), jall(:,:,:,:,:,:) ! H, J_k and the stage currents
real(R_P), pointer :: ue(:,:,:,:,:), re(:,:,:,:,:), up(:,:), rp(:,:)
integer(I4P) :: step, k, ns
allocate(e(1:1, 1:NI, 1:1, 1:1, 1:NB), p(8,NP), pt(8,NP))
call initial(e, p)
bx = box_interior(LAYOUT_V_FIRST, 1_I4P, NI, 1_I4P, 1_I4P, 0_I4P, NB)
bp = box_range(lb=[1_I4P, 1_I4P], lo=[1_I4P, 1_I4P], hi=[6_I4P, NP], components=.true.) ! q, m do not evolve
call st%initialize(SCHEME, FORM_BUTCHER, lb=lbound(e), ub=ubound(e))
call sp%initialize_companion(st, lb=[1_I4P, 1_I4P], ub=[8_I4P, NP], components=.true.)
ns = st%stages()
allocate(h, mold=e) ; allocate(jk, mold=e) ; allocate(jall(1:1, 1:NI, 1:1, 1:1, 1:NB, 1:ns))
pt = p ! lookahead writes the box only: pt keeps q and m
if (.not. allocated(g)) allocate(g(ns+1, NSTEPS, 2))
do step=1, NSTEPS
call st%begin_step(bx, e) ; call sp%begin_step(bp, p)
do k=1, ns
call st%begin_stage(k, bx, DT, e) ; call sp%begin_stage(k, bp, DT, p)
ue => st%stage_state() ; call sp%stage_state_view(up) ! Butcher form: registers, not e and p
g(k,step,merge(1, 2, conserving)) = gauss(ue, up(1,:), p(7,:)) ! the stage state: field and particles
call sp%rhs_register_view(rp)
call lorentz(ue, up, p(7,:) / p(8,:), rp) ! the particles' residual; q, m from p
re => st%rhs_register()
if (conserving) then
call sp%lookahead(k, bp, DT, p, pt) ! the particles at stage k+1
call deposit(p(1,:), pt(1,:), p(7,:), h) ! H: the current that moves p to pt in DT
call st%stage_current(k, bx, h, jall, jk) ! J_k: sum_{r<=k} a_{k+1,r} J_r = H
jall(:,:,:,:,:,k) = jk
re = -jk ! Ampere: E' = -J
else
call deposit_direct(up, p(7,:), re)
re = -re
endif
call st%end_stage(k, bx, DT, e) ; call sp%end_stage(k, bp, DT, p)
enddo
call st%finish_step(bx, DT, e) ; call sp%finish_step(bp, DT, p)
g(ns+1,step,merge(1, 2, conserving)) = gauss(e, p(1,:), p(7,:))
enddo
call st%destroy ; call sp%destroy
endsubroutine companion
subroutine boris(g)
!< NSTEPS steps of the same plasma with the Boris pusher: x^n -> x^{n+1}, then E^{n+1} = E^n - DT H.
real(R_P), intent(out) :: g(:) !< |Gauss residual| after each step.
real(R_P), allocatable :: e(:,:,:,:,:), h(:,:,:,:,:), p(:,:), pn(:,:), eg(:,:), bg(:,:)
integer(I4P) :: step
allocate(e(1:1, 1:NI, 1:1, 1:1, 1:NB), p(8,NP), eg(3,NP), bg(3,NP))
allocate(h, mold=e)
call initial(e, p)
do step=1, NSTEPS
call gather(e, p(1,:), eg) ! E at the particles, x^n
bg = 0._R_P ; bg(3,:) = B0 ! B = B0 z
pn = p
call selah_push(PUSHER_BORIS, LAYOUT_V_FIRST, NP, DT, 1_I4P, 4_I4P, 7_I4P, 8_I4P, p, eg, bg)
call deposit(pn(1,:), p(1,:), p(7,:), h) ! the current that moves x^n to x^{n+1}
e = e - DT * h
g(step) = gauss(e, p(1,:), p(7,:))
enddo
endsubroutine boris
subroutine initial(e, p)
!< Electrons (q/m = -1, total charge -1) displaced by 0.01 sin(2 pi x) over a uniform background of charge 1, at
!< rest; E from Gauss's law (zero mean).
real(R_P), intent(out) :: e(1:,1:,1:,1:,1:), p(1:,1:)
real(R_P) :: ef(NC), rho(NC)
integer(I4P) :: j, c
do j=1, NP
p(1:3,j) = [(j - 0.5_R_P) / NP, 0._R_P, 0._R_P]
p(1,j) = p(1,j) + 0.01_R_P * sin(2._R_P*PI*p(1,j))
p(4:6,j) = 0._R_P
p(7,j) = -1._R_P / NP ; p(8,j) = 1._R_P / NP
enddo
rho = density(p(1,:), p(7,:))
ef(1) = DX * (rho(1) + 1._R_P)
do c=2, NC
ef(c) = ef(c-1) + DX * (rho(c) + 1._R_P)
enddo
ef = ef - sum(ef) / NC
e(1,:,1,1,:) = reshape(ef, [NI, NB])
endsubroutine initial
function gauss(e, x, qc) result(gmax)
!< max over the cells of |(E_c - E_{c-1})/DX - rho_c - 1|: Gauss's law, the background of charge 1 included.
real(R_P), intent(in) :: e(1:,1:,1:,1:,1:), x(:), qc(:)
real(R_P) :: gmax, ef(NC), rho(NC)
ef = reshape(e(1,:,1,1,:), [NC])
rho = density(x, qc)
gmax = maxval(abs((ef - cshift(ef, -1)) / DX - rho - 1._R_P))
endfunction gauss
pure function right(x, f) result(s)
!< The fraction of a particle's cloud (width DX, centred at x) right of face f (at f DX; positions not wrapped).
real(R_P), intent(in) :: x
integer(I4P), intent(in) :: f
real(R_P) :: s
s = min(1._R_P, max(0._R_P, (x + 0.5_R_P*DX - f*DX) / DX))
endfunction right
function density(x, qc) result(rho)
!< The charge density of the particles: cell c (between faces c-1 and c) holds q (right(c-1) - right(c)) / DX.
real(R_P), intent(in) :: x(:), qc(:)
real(R_P) :: rho(NC)
integer(I4P) :: j, f
rho = 0._R_P
do j=1, NP
do f=floor(x(j) / DX), floor(x(j) / DX) + 2 ! the particle's cell and its two neighbours
rho(cell(f)) = rho(cell(f)) + qc(j) * (right(x(j), f-1) - right(x(j), f)) / DX
enddo
enddo
endfunction density
subroutine deposit(x0, x1, qc, h)
!< H: the charge-conserving current that moves the particles from x0 to x1 in DT (Esirkepov's in 1-D): through
!< face f, q (right(x1, f) - right(x0, f)) / DT. For any displacement below a cell.
real(R_P), intent(in) :: x0(:), x1(:), qc(:)
real(R_P), intent(out) :: h(1:,1:,1:,1:,1:)
real(R_P) :: jf(NC)
integer(I4P) :: j, f
jf = 0._R_P
do j=1, NP
if (abs(x1(j) - x0(j)) >= DX) error stop 'particles: a displacement of a cell or more'
do f=floor(min(x0(j), x1(j)) / DX) - 1, floor(max(x0(j), x1(j)) / DX) + 1
jf(cell(f)) = jf(cell(f)) + qc(j) * (right(x1(j), f) - right(x0(j), f)) / DT
enddo
enddo
h(1,:,1,1,:) = reshape(jf, [NI, NB])
endsubroutine deposit
subroutine deposit_direct(p, qc, h)
!< The current q v of the particles where they are, linear weights to the two nearest faces: not charge-conserving.
real(R_P), intent(in) :: p(1:,1:), qc(:)
real(R_P), intent(out) :: h(1:,1:,1:,1:,1:)
real(R_P) :: jf(NC), w
integer(I4P) :: j, f
jf = 0._R_P
do j=1, NP
f = floor(p(1,j) / DX) ; w = p(1,j) / DX - f
jf(cell(f)) = jf(cell(f)) + qc(j) * p(4,j) * (1._R_P - w) / DX
jf(cell(f+1)) = jf(cell(f+1)) + qc(j) * p(4,j) * w / DX
enddo
h(1,:,1,1,:) = reshape(jf, [NI, NB])
endsubroutine deposit_direct
subroutine gather(e, x, ep)
!< E at the particles, linear between the two nearest faces; Ey = Ez = 0.
real(R_P), intent(in) :: e(1:,1:,1:,1:,1:), x(:)
real(R_P), intent(out) :: ep(1:,1:)
real(R_P) :: ef(NC), w
integer(I4P) :: j, f
ef = reshape(e(1,:,1,1,:), [NC])
ep = 0._R_P
do j=1, NP
f = floor(x(j) / DX) ; w = x(j) / DX - f
ep(1,j) = (1._R_P - w) * ef(cell(f)) + w * ef(cell(f+1))
enddo
endsubroutine gather
subroutine lorentz(e, u, qm, r)
!< The particles' residual: x' = v, v' = (q/m)(E + v x B), E gathered at the stage positions, B = B0 z.
real(R_P), intent(in) :: e(1:,1:,1:,1:,1:), u(1:,1:), qm(:)
real(R_P), intent(inout) :: r(1:,1:)
real(R_P) :: ep(3,NP)
integer(I4P) :: j
call gather(e, u(1,:), ep)
do j=1, NP
r(1:3,j) = u(4:6,j)
r(4,j) = qm(j) * (ep(1,j) + u(5,j) * B0)
r(5,j) = qm(j) * (ep(2,j) - u(4,j) * B0)
r(6,j) = qm(j) * ep(3,j)
enddo
endsubroutine lorentz
pure function cell(f) result(c)
!< Periodic index 1..NC of a cell or a face (face c is the right face of cell c; face 0 is face NC).
integer(I4P), intent(in) :: f
integer(I4P) :: c
c = 1 + modulo(f - 1, NC)
endfunction cell
endprogram particlesNext: 14. An absorbing layer.