This is adapted from an assignment from NUS CS3210 (Parallel Computing). The objective is to simulate the motion of particles in a 2D space, taking into account collisions between particles and the boundaries of the space with OpenMP.
Particle Sim is a simple particle simulation program that demonstrates basic physics principles such as gravity, collision detection, and motion. It is designed for educational purposes and can be used to visualize how particles interact in a 2D space.
long_random70.mp4
10k_density_0.7.mp4
100k_density_0.8.mp4
The program accepts a text file as input, which specifies the initial conditions of the particles. The input file should be formatted as follows:
- N – Number of particles in the simulation
- L – Size of the square (in units)
- r – Radius of the particle (in units)
- S – Number of timesteps to run the simulation for
- For each particle, the following space-delimited information will be present on one line:
- i – the index of the particle from 0 to N − 1
- x – initial position of particle index i on x axis (in units)
- y – initial position of particle index i on y axis (in units)
- vx – initial velocity on the x axis of particle i (in units per timestep)
- vy – initial velocity on the y axis of particle i (in units per timestep)
4
13
1
25
0 3 5 -1 0
1 5.05 5 0 0
2 7.1 5 0 0
3 9.15 5 0 0
To run the simulation, compile the code using a C++ compiler with OpenMP support and execute the resulting binary with the input file as an argument:
- First run
maketo compile the program. - Then run the simulation with the command:
./sim <input_file> <num_threads>Where <input_file> is the path to your input file and <num_threads> is the number of threads to use for parallel processing.
For example:
./sim tests/correctness/patterns.in 1This will create a positions.out file containing the positions of the particles at each timestep.
To visualize the simulation, you can create a video from the output file using the provided Python script
./scripts/viz.py <input_file> positions.outWhere <input_file> is the same input file used for the simulation, positions.out is the output file generated by the simulation. This creates a simulation.mp4 video file showing the particle movements.