NVIDIA PhysX FleX and other fluid solvers for high-quality fluid simulation

Posted in Software on 23 November 2016

FleX and Redshift Milkshake Dragon FiestaBeware the strawberry milkshake monster!

Simulations are hard.

When it comes to doing simulations on meshes with a finite number of vertices it's relatively easy to achieve desired results. But as soon as you try taming hundreds of thousands or even millions of particles, you're in trouble. Especially when it comes to doing fluid simulations. You need a special kind of solver, a powerful rig or a network of rigs and a lot of patience. It took me by surprise how difficult seemingly trivial simulations can be.

In the animated film I'm working on I will have bodies of water large and small and certain gaseous liquids in the background for increased production value.

If you're a freelancer or a hobbyist on a budget in need to simulate some fluids, off-the-shelf tools available on the market can be a good choice... But there are so many of them that finding out their differences as well as pros and cons is a quest in itself. In this post I'll explore some of the ways an amateur like me can do various fluid-like simulations and what technologies there are to help get the job done.

The big guns

I'll briefly cover two of perhaps the most well known and renowned fluid sims on the market - Naiad and Realflow.

NAiad logo

There was the time when you could only purchase a single Naiad license for 5500$ or rent it quarterly for about 1400$. Luckily those times are over since in 2012 Naiad was sold over to Autodesk and turned into Maya Bifrost. So now you can get your hands on Naiad tech within maya for just $185 a month. You can find out more about Bifrost in this blog post at Digitaltutors. It's a powerful FLIP solver (more on this method below) and well integrated into Maya too with GPU caching and an ability to playback tens of thousands or even millions of particles in real-time directly within the DCC as well as a variety of tools for artistic direction of your simulations.

Realflow logo

Then there's Realflow, which comes with several solvers for you to choose (SPH, PBF, HYBRIDO) and with its Dyverso particle solver (the one which uses PBF) gives you the ability to simulate on CPU or GPU, the latter using OpenCL for computations. You can read more about Realflow's solvers here. Overall, Realflow isn't terribly slow and well scalable when you give it lots of cores to work with, but as soon as you realize your hardware limitations and the fact that the cheapest single-seat license with the C4D integration costs over 750 bucks you start looking for other solutions.

Other freeware and commercial tools for fluid simulation

I won't spend too much time on different types of solvers available on the market, only mention some of them for the sake of argument. There's an excellent (albeit slightly dated) article on the subject at fxguide explaining them in detail if you're interested in finding out more.

Smoothed Particle Hydrodynamics and NVIDIA PhysX SPH solver

Smoothed Particle Hydrodynamics (SPH) method was developed a long time ago - in 1977 - and since then used in all areas of engineering and computer graphics. Nowadays it's gradually being replaced by more efficient newer Fluid-Implicit Particle (FLIP) or Position-Based Fluids (PBF) methods.

There exists a well known open-source PhysX dynamics library you can use to simulate lots of meshes and particles. Also PhysX fluid particle solver is based upon the SPH method and can therefore be used for various kinds of fluid simulation.

PhysX simulate SPH Softimage ICE node

PhysX is good for what it was designed for - real-time physics for games. I don't really know if you can simulate fluids with PhysX realistically though (that is with sim quality good enough for CG-production, not just real-time applications). The reason is simple: I find SPH simulations produced by PhysX (as well as many other SPH solvers out there) to be very bouncy. Liquid which should be basically incompressible is extremely difficult and computationally complex to achieve. Also surface contact particle collisions aren't as accurate as one would like them to be due to noticeable loss of energy upon contact.

Specifically, when you pour liquid into an already existing body of liquid, you should in most cases get splashes on collision and not just after particles penetrate the body of liquid and start affecting the rest of the particle mass "from below". The problem lies in SPH method's inability to do surface tension effects very well as well as maintain fluid incompressibility and energy conservation when there aren't too many substeps used. In general you need about 5-10x more substeps to achieve the same level of incompressibility using SPH than with the Position Based Fluids (PBF) method. Miles Macklin explains it very well in his paper on PBF (which is used in the NVIDIA FleX unified physics library). In fact, the PBF method implemented in FleX is very close to the one described in the paper by Matthias Müller-Fischer called Position-based Methods for the Simulation of Solid Objects in Computer Graphics.

There are other shortcomings of the SPH method like particle clumping, but it's time to move on to other tools.

SPH method particle clumping vs PBF added surface tension and pressure

Voxel or grid-based fluid solvers

Another type of a fluid solver relies on splitting the simulation domain into a 3D-array of cells which can then take part in the simulation. One of the most well-known commercial sims of this type is the Phoenix FD by Chaos Group. It's a complete fluid simulation solution which can also simulate realistic gaseous fluids like smoke and even explosions as well as quickly preview them using GPU.

Phoenix can produce some very impressive results, like this glass filled with beer with splashes and foam generated during simulation:

Phoenix FD beer with foam

A tutorial is available if you're interested.

Phoenix FD is relatively fast (for a CPU solver) and scalable, that is you can preview your simulation in lower res and bump it up to, say, 20-50 million cells when ready without the need to change simulation settings much.

This sure is a state-of-the art fluid solver aimed to provide the best quality, and the trade-off is, as usual, the solve speed. Also the plugin isn't free and a single-seat license costs about eight hundred dollars. You better have clients ready to pay for quality fluid simulation otherwise you might probably want to keep looking.

Blender's LBM solver

Then there's Blender which has been, is and always will be free. Blender’s fluid simulation uses Lattice Boltzmann Method (LBM). Now, LBM sits somewhere between the Navier-Stokes (NS) solver and Smoothed Particle Hydrodynamics (SPH) method and requires you to manually specify the simulation domain and unfortunately gets slower and slower the longer the duration of the simulation or when liquid viscosity values are too low or high. No offense to Blender team and users, but when trying out Blender's fluid dynamics tool-set I kept getting the feeling that the fluid sim solver was in a state similar to that of the Cycles renderer. That is - it kind of works, but takes a lot of time and effort to go from "look, it's doing something" to "I need to do this specifically". So when you get to sim with the production-ready settings it gets ugly very quickly.

Fluid-Implicit Particle (FLIP) fluid solvers

A FLIP solver is a kind of a hybrid between a particle based and volume based one. All fluid data is stored in the particles and only particles need to persist frame to frame, so no unwanted mixing or loss of fluid occurs unlike the SPH solver. However, the pressure projection step is done on a volume that is created during simulation. Particles can be placed on top of each other without destabilizing the system. SPH on the other hand tends to become very unstable when you place particles too close. The advantage of the FLIP solver is that you run with only one time step per frame while SPH requires at least 7 to 20 time steps or more per frame to produce stable results. This is because FLIP utilizes a few grids to stabilize the sudden impulses that can arise in a fluid simulation.

For example Naiad utilized PIC-FLIP and deFLIP solvers and was successfully used in production of hundreds of ads and feature films including Avatar. SideFX Houdini comes with a FLIP solver as well as the recent 3-rd version of Phoenix FD.

Avatar Naiad waterfall

FLIP solvers are great, allow for impressively stable large-scale simulations and are actively developed. Take a look at the Narrow-Band FLIP method that massively decreases amount of computations while producing nearly the same result as a full-fledged FLIP:

narrow-band flip simulation

If you're looking to try out FLIP in your workflow take a look at the mantaflow, an open-source framework which can do FLIP simulations for liquids. Or you can check out GridFluidSim3d, an implementation of a PIC/FLIP liquid fluid simulation written in C++11 with support for GPU accelerated fourth-order Runge-Kutta integration using and GPU accelerated velocity advection using OpenCL.

If you're a studio, you're pretty much set – download and implement one of those open-source solutions. But if you're a freelancer or a hobbyist looking for a more-or less ready to use and free or inexpensive one – you're out of luck.

Well, not really. There is a new fluid solver called Cataclysm also developed by NVIDIA which available for Unreal Endine 4. It uses a custom FLIP based GPU solver can simulate up to two million liquid particles within the UE4 engine in real time.

nvidia Cataclysm ue4 flip solver

Here's the demo.

All of this is implemented using DirectX compute shaders. The custom solver is combined with Unreal Engine 4's GPU Particles with Distance Field Collisions. So you can say that it's a very tightly integrated solution which only works in UE4, so in its current state is of no use for "offline" CGI unless you're really thinking of doing your simulations in UE4 and somehow exporting them back to your DCC. Even if you are, results are still far from what you'd expect to use in a CG-film which is something to be expected of a real-time solution.

FleX as a fast Particle-Based Fluid (PBF) solver

As a PBF solver FleX can't directly compete with a specialized FLIP one like Cataclysm when it comes to simulating hundreds of thousands of fluid particles in real-time, but it was never meant to do such large-scale simulations by design. FleX is a different type of solver, it can model not only liquids but solids, cloth and soft bodies (hence the term: "unified"). It also supports phase changes between different material types, which means you can take a mesh, turn it into liquid, freeze the liquid and vaporize it, for example.

NVIDIA FleX fluid simulation

In the role of a fluid solver, FleX is free to use, runs on any consumer-grade GPU newer than GTX 650, requires no simulation domes to be set up or voxel resolutions to be calculated, can be easily integrated into pretty much any game engine or DCC, produces very high-quality results and is FAST (again: for a PBF solver). To me it looks like sort of a middle-ground solution for small or medium-scale simulations.

Having said that, you actually can simulate lots and lots of fluid particles with FleX, just not in real-time, which I will now demonstrate.

A side-note: my own shaky and buggy integration of FleX for Softimage just didn't do it for anything involving complex simulations where particle phases or simulation active particle counts change, the latter being something you actually need for fluid simulation. So I asked a good friend of mine, a talented programmer, to create his own version of FleX node for Softimage, which he then let me use for various experiments. It took a while, and is still a work in progress, but it worked fine in this case.

So without further ado, here's a test with 500 000 particles:

At 10 sub-steps (we're going for high quality here) and 3 iterations the simulation speed was about 2.5 fps on my GTX 1070.

If you bump the count up to a whooping million particles you'll slow it down to about 1.2 fps, which is still very impressive for PBF, where each and every particle always takes part in the simulation. Here's what a million particles looks like after meshing into a rather high density mesh (decimated to about 1.6 mil polys for sanity):

1 mil flex particles redshift render

And here's the viewport screen-grab:

flex 1million particles softimage viewport

And an up-and-personal beauty shot:

1 million flex particles up close render with redshift

Above and beyond?

So it looks like with FleX and fluid simulation the sky is the limit, right? Well...

Not quite.

Wanna see what 2 million or more particles look like when simulated with FleX? You won't. Here's why.

Know your limits

At its heart FleX is still a real-time physics solver built for real-time applications, so it comes with certain caveats. The most apparent one being that when you go over about 1.3 million particles in a single FleX simulation environment, things tend to get unstable.

Come to think of it... It makes sense when you're talking about a specialized live simulation solution: there's no way you can simulate a million particles with a Point-Based solver in real-time even with the most powerful GPU on the market.

Still, the biggest issue is not the choice of the solver or a framework, or the tool-set... It's:

Know what you're going for

There's a threshold beyond which fun and games turn into serious business with very different technical and time requirements.

Sea storm photo

For example if you simulate 500k particles for the duration as in the video above you will need 1.7 GB of storage space to cache the results out to Alembic. Because of this I didn't even bother to cache out the 1 mil simulation, just meshed it in place and rendered right away.

Ah, yes... Meshing....

At some point meshing all these particles becomes a nightmare considering that popular DCCs just aren't fit to mesh such enormous amounts of particles (and some of meshers aren't even multi-threaded!). Also remember that you will probably need to cache out meshed result for faster rendering iterations and motion blur, which calls for even longer production times and more storage space required.

So in my opinion simulations where you need a lot particles should be done with specialized tools, not general DCCs and only when you really need them.

tl;dr

In case you're looking to simulate small to medium-scale bodies of liquids NVIDIA FleX is a great choice: it provides fast iteration, stable results and comes with additional perks like phase changes and no need for setting up simulation domains.

Did I forget anything?..

Oh, yeah... It's also free.