Skip to content

tatuskarjaiwanth/ParSim-A-Multithreaded-Particle-Collision-Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

🧵 ParSim: A Multithreaded Particle Collision Simulator

A high-performance 2D particle simulation using multithreading in Python. Simulates particle motion and collisions in a bounded space with real-time grid-based spatial partitioning for efficient collision detection.


🚀 Features

  • Simulates 10,000 particles (customizable)
  • Multithreaded for better CPU utilization (default: 8 threads)
  • Realistic collision handling with elastic response
  • Spatial grid for optimized neighbor checks
  • Per-step logs of:
    • Duration
    • Collisions resolved
    • CPU utilization
    • Grid rebuild time
    • Active threads
  • Built-in performance metrics: FPS, total simulation time, CPU average

🧠 How It Works

  1. Initialization:

    • Each particle is randomly assigned position, velocity, radius, and mass.
    • The 2D space is divided into grid cells based on particle size.
  2. Each Simulation Step Includes:

    • Particle movement update
    • Boundary collision management
    • Grid repopulation
    • Particle-particle collision resolution across cells (with lock-based safety)
    • Performance logging (timing, CPU, etc.)
  3. Multithreading:

    • Uses ThreadPoolExecutor for parallel updates and collision checks.
    • Dynamically distributes work across threads.

📦 Files

File Description
physics_simulator.py Main simulation script
output.txt (optional) Console logs of each simulation step

▶️ How to Run

✅ Requirements

  • Python 3.7+
  • psutil: for measuring CPU usage

Install required package:

pip install psutil

🔁 Run the simulation

python physics_simulator.py

You’ll see logs printed step-by-step in the console, e.g.:

Step 1/50 | Duration: 0.048s | Collisions: 2400 | CPU util: 99.7% | Active threads: 9 | Grid Rebuild: 0.005s
...

At the end, summary metrics like total time, FPS, and total collisions will be shown.


⚙️ Configuration

You can tweak simulation parameters at the top of physics_simulator.py:

no_of_particles = 10000        # total particles
no_of_sim_steps = 50           # how many steps to simulate
no_of_threads = 8              # parallel threads to use
width = 1000                   # width of the space
height = 800                   # height of the space
particle_max_initial_velocity = 100

📈 Output Metrics

  • Total simulation time
  • Average, max, min step duration
  • Estimated FPS
  • Total collisions handled
  • Average CPU usage per step

🧪 Benchmarking Tips

  • Try changing no_of_particles or no_of_threads to see how it scales.
  • You can redirect output to a file:
python physics_simulator.py > output.txt

📌 Notes

  • Particle locking ensures safe concurrent updates.
  • CPU utilization may show >100% due to multithreading on multi-core CPUs (normal in Python’s psutil).
  • Grid-based collision detection significantly boosts performance compared to brute-force.

🧊 Future Improvements (Suggestions)

  • Add visualization (e.g., using pygame or matplotlib)
  • Profile memory usage
  • Try multiprocessing for CPU-bound steps
  • Experiment with alternative data structures (QuadTrees, etc.)

🧑‍💻 Author

T Jaiwanth


Happy Simulating! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages