Computing Library › Worked Examples
Worked Examples

One Particle-in-Cell Time Step

Walk through the four-phase PIC cycle - deposit, solve, gather, push - that plasma codes repeat millions of times.

The PIC idea

Particle-in-cell simulates a plasma as many macroparticles moving in continuous space, coupled to fields defined on a fixed grid. Each step cycles between particle and grid representations.

The four phases

Kronos motion — countdown first plasma

A 1D electrostatic step

python
import numpy as np
ng=64; L=2*np.pi; dx=L/ng
x=np.random.rand(1000)*L; v=np.random.randn(1000)*0.1; q=-1.0
# 1. deposit charge (nearest grid point)
rho=np.zeros(ng)
idx=(x/dx).astype(int)%ng
np.add.at(rho,idx,q); rho/=dx
# 2. solve Poisson in Fourier space
k=2*np.pi*np.fft.fftfreq(ng,dx); k[0]=1
phi=np.real(np.fft.ifft(np.fft.fft(rho)/k**2)); phi-=phi.mean()
E=-np.gradient(phi,dx)
# 3. gather field to particles ; 4. push
Ep=E[idx]; dt=0.05
v+=q*Ep*dt; x=(x+v*dt)%L
print('step done; mean speed',round(abs(v).mean(),4))

Accuracy knobs

Better weighting (linear or higher-order splines instead of nearest-grid-point) reduces numerical noise; the grid spacing must resolve the Debye length and the time step must resolve the plasma frequency, or numerical instabilities appear. Real codes add magnetic fields, the Boris rotation, and collisions, but every one repeats this deposit-solve-gather-push loop.