Ions Simulation



Source Code

Idea:

This project was designed to explore optimization of single threaded looped code. My goal to explore different types of computing optimizations. Modeling N-body interactions between particles' gave me ample opportunity for optimization. This project was quite a milestone in my programming ability, as it had my first ever serious memory leak (as in the app could only run for ~5 mins before eating up all 16 GB of RAM). This was also the first time I was seriously able to look at optimizing loops and see measurable improvements in performance, getting up to ~60 million calculations of gravity (G(m1*m2)/d^2) per second.

Now, I get around 55-60 million force calculations per second and am able to simulate things like this:

This video is sped up by ~20x to show the interactions better. Since there are 2500 particles, each simulation update requires 2500^2 (6,250,000) individual G(m1*m2)/d^2 calculations, and so it only gives 8-9 updates per second. The calculations are spread across 8 threads on the 4 cores of my machine. After doing each of the ~50 million calculations of gravity per second, the program then breaks down the force F into it's x-axis (F*dx/dist) and y-axis components (F*dx/dist), and renders the image in a separate thread.

Previous Work:

This idea of this project originated many years ago from the IOS Application, 'Ions' by Douglas Applewhite. This application allows the user to play with having a max of 20,000 particles, with up to 20 'Fields', each being either repulsive or attractive. This idea fascinated me for quite a long time. As I had been recently learning to program when I discovered the app, this was one of the first programs that I ever wrote. The performance was initially mediocre, getting around 900,000 individual force calculations per second:




Current State:

This project has fulfilled a long-time goal of mine. I have always wanted a tool to just test high quantities of particles under different forces. While this page only demonstrates the gravitational force, I have also implemented twirling, stretching, directional, and other random types of fun forces to play with - see below. This is such a fun tool that I find myself starting it up once every few months to test random force ideas and see how they would play out in a complex system.




All content on this page belongs to Zachary Porter. You may use, reproduce, or modify anything from this website, provided that you give credit to zackporter.com in your usage.