3D Multiphase Smoothed-Particle Hydrodynamics

I have wanted to implement a 3D version of a fluid simulation in Visions of Chaos for years (3D was a planned feature since I first got 2D SPH working back in 2013). It took a lot longer than expected.

For the 3D code I extended the 2D SPH code I had into the third dimension. In the end it wasn’t too difficult (everything seems simple once you work out how to do it). After a week or so of hacking away I had multiphase fluids flowing in 3D space.

The code is based on Tom Madam’s SPH Code in this blog post. For 3D you just need to add in a Z dimension for particle positions etc.

3D is slower than 2D with the extra dimension and even with a decent multi-core CPU the simulation starts to crawl as the particle count increases. Using the awesome Mitsuba to render some nicely lit spheres got me the following movie.


Multi-threading support in Visions of Chaos

A long overdue and much requested feature in Visions of Chaos has been better support for multi core CPUs. Watching Visions of Chaos churn away calculating a long series of frames for a movie and only seeing one of your CPU cores in use can be frustrating.

Converting single threaded code into multi-threaded capable code is not exactly easy (depending on the algorithm some code is easier than others) but I have started converting some of the easier modes into multi-thread capable code.

Smoothed Particle Hydrodynamics (SPH) was one of the modes in Visions of Chaos that really needed a speed up. See here and here for my previous experiments with SPH.

This next screenshot shows all the 12 cores of an i7 being used for calculating the SPH formulas. Note that the actual displaying of the particles cannot be parallel so there is not 100% CPU utilization. As the particle count goes up (and the time it takes to calculate the million particles moving takes longer than the display time) the CPU usage jumps closer to 100% on all cores.

Parallel Multiphase Smoother Particle Hydrodynamics

After conversion to multi-CPU capable code it was time to render some new 4K resolution SPH simulations. Once the particles count and resolution goes up to fill a 4K screen the times start to plummet again, but seeing these in full 4K resolution is really nice. The parts in the following movie used 1,000,000 SPH particles each which is double my previous particle counts.

Other than SPH, the other modes that take advantage of the rewritten parallel processing code are 4D Cellular Automata, Coupled Cellular Automata 2, Ying-Yang Fire Cellular Automata, Large Neighborhood Totalistic Cellular Automata and Liquid Crystal Cellular Automata. The CA modes are the easiest to convert to parallel processing as by nature they update each cell independent of the others.

There is still a lot of code and modes that would benefit from parallel processing. Yet another entry on my ever expanding to do list of features for Visions of Chaos.


Using Multiphase Smoothed-Particle Hydrodynamics to show the emergence of Rayleigh-Taylor instability patterns

Rayleigh-Taylor Instability

Rayleigh-Taylor instability (RT) occurs when a less dense fluid is forced into a heavier fluid. If a heavier fluid is resting on a lighter fluid then gravity pulls the heavier down through the lighter fluid resulting in fingering, mushrooming and swirling patterns.

Nicole Sharp from FYFD has this into video to RT.

Simulating Rayleigh-Taylor Instability

Here is an exmaple image courtesy of Wikipedia showing some steps from simulating RT.

Rayleigh-Taylor Instability

This is a much more complex example from a supercomputer run at the Laboratory for Computational Science and Engineering, University of Minnesota. Also check out their movie gallery for more incredible fluid simulation examples.

Rayleigh-Taylor Instability

RT patterns also emerge in supernova simulations like the following two images.

Rayleigh-Taylor Instability

Rayleigh-Taylor Instability

Mark J Stock uses his own fluid simulation code to create incredibly detailed RT examples like this

thunabrain has this example of using the GPU to simulate fluids showing RT

Real Life Rayleigh-Taylor Instabilities

Pouring milk into coffee leads to RT patterns. I took these with my phone so they are not as crisp as I would have liked.

Milk and coffee

Milk and coffee

Dropping ink into water also leads to RT patterns as in these photos by Alberto Seveso

Rayleigh-Taylor Instability

Rayleigh-Taylor Instability

Using SPH to simulate RT

I had some previous success with implementing Multiphase Smoothed-Particle Hydrodynamics so I was curious to see what sorts of RT like results the SPH code could create. I have now added the options to generate RT setups in the SPH mode of Visions of Chaos.

The following SPH RT simulations use approximately 500,000 discreet individual particles to make up the fluids. They are all full HD 1080p 1920×1080 60fps videos. It was very tedious to try various settings and wait for them to render. I spent the last few weeks tweaking code and (99.99% of that time) rendering test movies to see the changes before I was happy with the following three example movies.

The code is single threaded CPU only at this stage, so much patience was required for these movies.

For this first example the top half of the screen was filled with heavier purple particles and the lower half with lighter yellow particles. A very small random movement was added to each of the particles (just enough to stop a perfect grid of particles) and then the simulation was started. 73 hours (!!) later the calculations were completed for the 3000 frames making up the movie.

The next example took around 105 hours for the 4000 frames. This time three fluids are used. Heaviest on top, medium in the middle and lightest on the bottom.

And a final three fluid example that took 74 hours for the 3000 frames.

If you click the title text of the movies they will open in a new tab allowing them to be viewed in full screen HD resolution.


Multiphase Smoothed-Particle Hydrodynamics

Smoothed-Particle Hydrodynamics (SPH) is a method of simulating fluid flow. It is based on representing a fluid by a large number of discreet (individual) particles. Each particle has properties like position, pressure, density, mass, etc. The particles are fed through a bunch of equations that make them move in a fluid like flow.

If you are a maths nerd then one of the better and recommended papers for SPH fluids is Particle-based Viscoelastic Fluid Simulation.

Multiphase SPH is when you have 2 or more fluids with different densities etc (think oil and water) that flow around each other and clump together.

I have always had a fascination with simulating fluids. Over the years I have tried to understand SPH and have had many failed attempts at writing programs to do it (the basic formulas seem relatively simple, but getting the code to run stable without explosions and crashes is far from simple). Then I found this blog post by Tom Madams that had sample source code. With that I was finally able to get SPH working. It can still be fiddly to find a nice set of parameters to make a nice looking movie, but Tom’s code seems to be a great start. Thanks Tom!!

Multiphase SPH is now available in Visions Of Chaos.

A future release will include 3D support.