Visualizing Particle Trajectories in Electric Fields
Simulating the trajectory of a charged particle in an Electric field via Euler's method
Overview
This project simulates the motion of a test charge through an electric field generated by a discrete set of stationary charges. The primary goal is to visualize the resulting particle trajectories using Euler integration, drawing inspiration from classical electrodynamics. The project is loosely motivated by Poisson Flow Generative Models (PFGM), which use ideas from electric field dynamics to guide image generation. In this simulation, the stationary charges are confined to the x-axis, while the test charge is free to move in the x-y plane. This dimensional separation results in richer, more interesting trajectories and avoids mode collapse.
Computational Method
The simulation uses a modified Euler algorithm to evolve the position of the test charge over time. At each time step, the electric field vector at the test charge’s position is computed by summing the contributions from all the stationary charges. The test charge is then moved in the direction of the net electric field.
To ensure numerical stability and prevent unrealistic motion, a few key modifications are made:
- All physical constants and charge magnitudes are normalized to one. This simplification retains correct directional behavior while keeping the scale consistent with the OpenGL display.
- To prevent the test charge from taking infinitely large steps near stationary charges (where the electric field diverges), the magnitude of the electric field is capped. If the computed field exceeds a certain threshold, it is scaled to a fixed maximum while preserving its direction. This ensures that the particle moves smoothly through the field without jumping off the screen.
Implementation Details
The entire simulation is implemented in C using OpenGL for visualization. When the program starts, users are prompted to enter:
- The number of stationary charges
- The x-axis location of each charge
- The initial x and y position of the test charge
Multiple charges can be placed at the same location to simulate larger magnitudes, effectively allowing the construction of complex charge distributions. After the user inputs are complete, the OpenGL window opens. A left-click starts the simulation, and the motion continues until the test charge reaches the x-axis.
Results & Insights
The simulation reveals several interesting behaviors depending on the configuration of the charge distribution:
- In one scenario with two charges placed at x = -4 and x = 4, and a test charge starting at position (0, 4), the test charge converges toward x = 0 — a location not occupied by any charge. This shows that the resulting field can guide the test charge to novel positions.
- By stacking charges at positions approximating a Gaussian shape (e.g., more charges near x = 0 and fewer as we move away), the resulting field mimics the properties of a Gaussian distribution. Test charges placed at random positions tend to drift toward the mean, demonstrating how electric fields can be used to model continuous probability distributions.
- These behaviors illustrate how the simulation could be used to gain intuition for generative processes, where sampled points evolve toward structured distributions via field-like dynamics.
More Details
You can view the source code here and the full write-up here.