2D fluid simulation

This is a Lab about a particle based fluid simulation. The goal was to implement the physics using the SPH method.

I implemented a grid-based neighbor search. The forces implemented are the pressure and the viscosity.

The most difficult part was to have a stable simulation. I was having segmentation faults with infinite accelerations. I spent a lot of time trying to find where my error was, and it was because I miscalculated my density, I didn’t count the current particle…

Afterwards, I added the wall with particles. To handle this, I store wall particles in the same vectors as the fluid particles and I don’t update their position and velocity. With this method, I can easily add static objects.

I also added a way to save videos. The program will save frames in the frames folder to have 25 fps. It can also save the positions of the particles in txt files in the positions folder. To do this, we can run the program with optional arguments. There are two which can be either 0 or 1. 1 will activate the saving. The first argument will save frames and the second one will save positions. To assemble the frames, I made a python script with opencv called groupframes.py.

With the positions, we can try different rendering techniques, like computing the density or pressure in each pixel. I chose instead to use metaballs, described here. The idea is to calculate in each pixel a distance to all particles and then apply a threshold. It is very close to the density calculation.