gardn banner

particle simulation program

you can find the code for this project on gitlab.

smarticles

what started this project is me discovering a tiny program called smarticles which was written in rust by chevyray.

this program is a particle life simulation program; i found it on github after watching this youtube video which i found absolutely mesmerizing.

i wanted to play with a software similar to the one presented in the video (because it was not available on linux) and smarticles was exactly what i was looking for.

particle life ?

particle life refers to systems of particles that feature emergent properties/patterns and look kind of similar to the game of life, hence the name particle life.

the idea behind particle life is rather simple and built upon a few key elements:

in the original software, there were 4 classes, and the force was a non-zero constant (function of the interaction coefficient) if the distance between the two particles was below a certain threshold and zero otherwise.

first tweaks

increasing the number of particles

after playing with the software a bit, i wanted to increase the number of particles as well as the number of classes.

it was pretty straightforward to change in the code, yet i quickly reached a dead end: when the number of particles was too high, the simulation ran very very slowly...

i tried to make the code run a little bit faster without getting any major improvement.

i also increased the number of classes to 6 because i found it more interesting to make the system a bit more complex.

changing the particle interaction

i also changed the force of interaction: i used a function very similar to the one in the youtube video. this made the patterns in the simulation a bit more interesting and i was happy with that.

the force of interaction is a piecewise affine function of the distance between the particle applying the force and the particle on which it is applied. the function is split into 3 domains:

  1. the first domain where the force is independent of the interaction coefficient of the pair of particles and where it is supposed to mimic a universal repulsive force when particles are too close to each other.
  2. the second domain where the force is driven by the interaction coefficient of the pair of particles; it can be either repulsive or attractive depending on the value of that coefficient, and it is weaker at the ends of the domain and stronger at its center.
  3. the third domain where the force is zero everywhere: when particles are far away from each other, they don't interact.
force of interaction with respect to the distance between the two particles.

panning and zooming

one the biggest changes i made to the software was adding the ability to pan and zoom from the main view. this was very satisfying because it meant i could make the "world" much larger.

a screenshot of the gui

tipe

some context

when i discovered the original software, i was in first year of prépa. in france, prépa is a public educational course designed to train for competitive exams leading to the "grandes écoles" (most of them are engineering schools).

one of the oral exams is the tipe, it is an oral presentation on a subject chosen by the student but which has to be somehow linked to a certain yearly theme given by the exam organizers (the ministry of national education).

the theme of my year was transition, transformation, conversion. at first, i didn't really know what i wanted to do for my presentation but at the end of my first year, i chose to use my fork of smarticles for my presentation. however, i wanted it not to be only about smarticles, so i tried to model an observable of the particle system using a small dense neural network.

why writing about this ?

while working on the software for my presentation, i came up with an idea to drastically reduce computation time and thus, to increase the number of particles.

the idea was to use spatial partitioning. i indeed noticed that two particles only interact when the distance between them is smaller than a certain threshold. by splitting the plane into square-shaped cells with a side length equal to that threshold, it was now possible to update the particles' positions in a cell only based on the cell itself and its neighborhood i.e. the 8 neighboring cells.

this made parallel computation much more efficient and it led to a massive reduction in update time. this made it possible to simulate up to 100,000 particles in real time (with a maximum update time of 50ms) on a 6-core/12-thread cpu clocked at 4.4ghz. for reference, the original software could simulate up to 5,000 to 8,000 particles in the same conditions.

computing the forces applied on the red particle in the red cell only requires knowing the positions of particles in the blue neighboring

the end of the day

i loved working on this programming project, the fact i could present my work in my tipe made me really happy because it was a way to share the work i've done in a more academic way.

but what i like the most is being able to simulate those particle systems and discover all the weird emergent properties they hide.