Saturday, March 1, 2014

Hobby Quest - Espresso Fluid Solver in Processing



During Christmas break this past December, when the family had all gone to bed, I spent some time walking through one of the best progressive tutorials I've seen for building a simple grid based fluid solver written by Chris Horvathe.

Not only does the tutorial have fun examples, it does a great job of isolating and teaching the building blocks like differential equations and solving a sparse matrix using a Jacobian. I wouldn't recommend this tutorial for a computer graphics novice and there are a few missing bits of understanding to get to the final smoke solver. In fact, it feels like one more article would put a nice little bow on the series. Luckily, the paper Real-Time Fluid Dynamics for Games written by Jos Stam on which Chris bases his tutorials is pretty approachable on its own and with the foundation that Chris builds over the course of the tutorials and the example code he provides, the leap to the final result isn't too bad.

Once I had my own solver, I made some modifications to prove to myself I understood what was going on.   I modified my implementation so the grid would wrap along the horizontal direction and added an external force that roughly approximates the effects of contrasting materials and gravity. Also, as I played with the code in Processing, it felt like mixing creme into an espresso, so I tweaked the colors to match.



Here's my final code running on jsfiddle using Processing.  You can copy the code into a Processing sketch on your own machine and increase the GRID_DIMENSION to get more details in the swirls.

I'll update this article when I finally get this code on github. For now, I slapped an MIT license on the code and give all the credit to Chris for the great tutorial.