This project implements a 1D Particle-in-Cell (PIC) simulation to model the two-stream instability in a plasma.
The code is written in C++17, modular, and supports:
- Initialization of two counter-propagating electron streams.
- Solving the 1D Poisson equation with a tridiagonal matrix solver (Thomas algorithm).
- Particle push using Leap Frog method (position and velocity update).
- Calculation of momentum, kinetic energy, potential energy, and total energy of the system.
- Exporting data to CSV for analysis and visualization in Python.
- Phase space animation to observe the temporal evolution of the instability.
The main goal is to study the time evolution of energy and momentum and observe the growth of the instability.
- PIC2si/ – Root directory of the project
- include/ – Header files
particles.hh
– Particle class declarationsfields.hh
– Field class declarationsdiagnostics.hh
– Functions for momentum and energy calculations
- src/ – Source files
main.cpp
– Main programparticles.cpp
– Particle class implementationfields.cpp
– Field class implementationdiagnostics.cpp
– Diagnostics function implementations
analysis.py
– Python script for plotting simulation resultsphaseSpace.py
– Python script for generating phase space animationCMakeLists.txt
– CMake build configurationREADME.md
– Project documentation
- include/ – Header files
CMake is used to generate and build the project:
mkdir build cd build cmake .. cmake --build . --config Release
The executable will be located in build/Release/pic1d.exe
(Windows/MSVC) or build/pic1d
(MinGW/Linux).
Run the simulation to generate the CSV:
./pic1d.exe
This will create diagnostics.csv
in the same directory as the executable, with columns:
time, momentum, Ekin, Epot, Etotal
A Python script analysis.py
is included to plot:
- Plasma momentum
- Kinetic energy
- Potential energy
- Total energy
Run the script in the same folder as the CSV:
python analysis.py
The script phaseSpace.py
generates an animated GIF showing the phase space dynamics:
It will generate a plot showing the temporal evolution of the physical quantities.
python phaseSpace.py
Parameters that can be modified in main.cpp
:
N
: number of super-particlesNG
: number of grid cellsL
: domain lengthV0
: initial stream velocityDT
: time stepNT
: number of time steps
MIT License © 2025 Juan Pablo Solís Ruiz
- Email: [email protected]
- GitHub: h4xter1612