The Electromagnetic Particle Simulator is a Python-based interactive application that simulates the motion of charged particles in electric and magnetic fields. This tool provides a practical way to visualize the effects of electromagnetic fields on particle trajectories, using multiple simulation methods. It's designed for educational purposes, helping users explore fundamental concepts in electromagnetism and numerical methods.
- Simulate the motion of charged particles in:
- Electric Field (with or without a magnetic field)
- Magnetic Field (affecting the particle's trajectory due to the Lorentz force)
- Runge-Kutta Method (for more accurate trajectory calculations)
- Adjustable particle properties:
- Charge (q)
- Mass (m)
- Initial Position (r₀)
- Initial Velocity (v₀)
- Interactive input through Streamlit interface for real-time simulation customization
This simulator is based on classical electromagnetism, governed by the following principles:
The motion of a charged particle under the influence of electric and magnetic fields is described by the Lorentz force equation:
Where:
- F⃗ is the force exerted on the particle
- q is the charge of the particle
- E⃗ is the electric field vector
- v⃗ is the velocity vector of the particle
- B⃗ is the magnetic field vector
The equations of motion for the particle are derived from Newton's second law:
Where:
- a⃗ is the acceleration of the particle
- m is the mass of the particle
When only an electric field is present, the particle's motion follows:
With both electric and magnetic fields, the motion becomes more complex and requires numerical integration of:
The 4th-order Runge-Kutta method solves these equations using:
Where:
- h is the time step
- k₁, k₂, k₃, k₄ are the intermediate slopes
- y⃗ represents both position and velocity vectors
class Particle:
def __init__(self, q, m, E, B, r0, v0):
self.q = q # Charge
self.m = m # Mass
self.E = E # Electric field
self.B = B # Magnetic field
self.r = r0 # Initial position
self.v = v0 # Initial velocitywithout_magnetic_field(particle): Electric field only simulationwith_magnetic_field(particle): Full electromagnetic simulationrunge_kutta(particle): Advanced numerical integration
- 2D and 3D trajectory plotting using Matplotlib
- Interactive controls through Streamlit interface
- Clone the repository:
git clone https://github.com/your-username/Electromagnetic-Particle-Simulator.git
cd Electromagnetic-Particle-Simulator- Install dependencies:
pip install -r requirements.txt- Run the application:
streamlit run main.py- Python 3.x
- NumPy
- Matplotlib
- Streamlit
Contributions are welcome! Please feel free to submit pull requests or open issues for improvements and bug fixes.
This project is licensed under the MIT License - see the LICENSE file for details.