The code numerically solves the non-relativistic Lorentz force equation for charged particles moving in prescribed electromagnetic fields using either a fourth-order Runge–Kutta method or the Boris particle pusher. It supports several benchmark configurations, including simple gyration, ExB drift, and a magnetic X-point.
Why the Boris pusher. Relative kinetic-energy error over 500 time
units of gyration. RK4 shows a secular drift that grows without bound
(reaching
Left: simulated E×B drift velocity against the analytical
prediction
Animations (3D, generated with Gnuplot): simple gyration · E×B drift · magnetic X-point
The equation of motion is
where:
-
$$\mathbf{x}$$ is the particle position, -
$$\mathbf{v}$$ is the particle velocity, -
$$q/m$$ is the charge-to-mass ratio, -
$$\mathbf{E}$$ is the electric field, -
$$\mathbf{B}$$ is the magnetic field, -
$$c$$ is the speed of light in normalized units.
The code uses normalized units by default.
- Non-relativistic charged particle dynamics
- Single-particle and multi-particle simulations
- Runge-Kutta 4th order integrator
- Boris particle pusher
- Uniform magnetic-field gyration test
- ExB drift test
- Magnetic X-point configuration
- Case-dependent diagnostics
- Gnuplot-compatible output files
- Modular C++ project structure
- Reproducible compilation through a Makefile
- Python-based 3D GIF animations
- Automated generation of animated particle trajectories
- Long-time diagnostics for energy and speed conservation
Particle_EM/
├── main.cpp
├── particle_em.h
├── fields.cpp
├── integrators.cpp
├── initialization.cpp
├── output.cpp
├── Makefile
├── README.md
├── LICENSE
├── .gitignore
├── Data/
├── Plots/
└── Gnuplot/
| File | Description |
|---|---|
main.cpp |
Main simulation workflow |
particle_em.h |
Global parameters, case selection, constants, and function declarations |
fields.cpp |
Electric and magnetic field definitions |
integrators.cpp |
Equations of motion and numerical integrators |
initialization.cpp |
Particle initialization and random utilities |
output.cpp |
Output files, diagnostics, and final summaries |
Makefile |
Compilation and cleaning commands |
Gnuplot/ |
Plotting scripts |
Data/ |
Simulation output data |
Plots/ |
Generated figures |
The physical case is selected in particle_em.h using:
#define CASE CASE_X_POINTAvailable cases are:
#define CASE_SIMPLE_GYRATION 1
#define CASE_EXB_DRIFT 2
#define CASE_X_POINT 3This case simulates the motion of a charged particle in a uniform magnetic field:
E = 0
B = B0 z_hat
The particle performs circular or helical motion depending on the initial parallel velocity.
This case is useful for validating:
- Larmor rotation,
- conservation of perpendicular speed,
- numerical stability of the integrator,
- long-time behavior in a purely magnetic field.
The gyration type is selected with:
#define TEST_CASE GYRATION_CIRCLEor
#define TEST_CASE GYRATION_HELIXThis case simulates a charged particle in crossed electric and magnetic fields:
E = E0 y_hat
B = B0 z_hat
The expected guiding-center drift is the ExB drift:
v_drift = E x B / B^2
In the normalized setup used here, the theoretical drift velocity is controlled by E0.
This case is useful for testing whether the numerical integrator correctly reproduces the expected drift motion.
This case simulates an ensemble of particles in a two-dimensional magnetic X-point configuration.
The magnetic field has the form:
Bx = y / L
By = -x / L
Bz = 0
An electric field can be imposed to accelerate particles along the out-of-plane direction.
This configuration is useful for studying:
- particle acceleration,
- magnetic null-point dynamics,
- multi-particle statistical behavior,
- velocity growth and spatial spreading.
For this case, the number of particles is controlled by:
const int NPART_XPOINT = 1000;The numerical method is selected in particle_em.h:
#define METHOD METHOD_BORISAvailable methods are:
#define METHOD_RK4 1
#define METHOD_BORIS 2The RK4 method is a classical high-order explicit integrator.
It is useful because:
- it is general-purpose,
- it has fourth-order accuracy in time,
- it is simple to compare against other methods.
However, for charged-particle motion in magnetic fields, RK4 is not structure-preserving. Over long integrations, it may introduce artificial energy errors, especially in purely magnetic problems.
The Boris method is widely used in plasma physics and particle-in-cell simulations.
It is particularly suitable for charged-particle dynamics because:
- it handles magnetic rotation very well,
- it is time-centered,
- it has excellent long-term stability,
- it avoids artificial energy growth in purely magnetic fields,
- it is computationally efficient.
For this reason, the Boris method is usually the preferred choice for long-time charged-particle simulations.
Compile and execute the simulation with:
make runSimulation data are written to the Data/ directory.
Example files:
Data/simple_gyration_boris.dat
Data/exb_drift_boris.dat
Data/xpoint_boris.dat
Diagnostics are written as:
Data/simple_gyration_boris_diagnostics.dat
Data/exb_drift_boris_diagnostics.dat
Data/xpoint_boris_diagnostics.dat
The output filename automatically depends on the selected physical case and numerical method.
Static diagnostic plots can be generated using Gnuplot scripts stored in the Gnuplot/ directory.
Example:
gnuplot Gnuplot/Plot_xpoint.gpAnimated three-dimensional GIFs can be generated with the Python visualization script:
python Scripts/plot_3d_gifs.pyGenerated figures and animations are saved in the Plots/ directory.
- Select the physical case in
particle_em.h. - Select the numerical method in
particle_em.h. - Compile the code and run the simulation:
make run- Generate plots with Gnuplot.
- Inspect data, diagnostics, and final terminal output.
Generated data and plots are not tracked by Git.
The folders Data/ and Plots/ are kept in the repository using .gitkeep files, while generated files are ignored through .gitignore.
Future improvements may include:
- relativistic particle dynamics,
- adaptive time stepping,
- OpenMP parallelization,
- unit tests,
- additional electromagnetic field configurations,
This project is released under the MIT License.
Lorenzo Monti, May 2026


