SPH Fluid Simulation in Metal

I recently watched a video by Sebastian Lague, who produces many excellent videos on computer graphics, simulation, and game development. It motivated me to replicate this simulation myself. In this write-up, I will present the scientific choices I made, as well as an explanation of SPH simulation and the underlying graphics pipeline.



I’ve always loved physics simulations. Lately, I’ve watched many videos on YouTube about them, and it motivated me to finally experiment and build something of my own. Sebastian Lague made a this simulation using Unity. I gave it a try, but I didn’t like the interface, and I had less low-level access than I wanted. Since I often use deep learning frameworks that handle all heavy computation on the GPU, I thought it would be a good idea to understand more deeply how they work under the hood. So I decided to work directly with a GPU framework, and since I only have Mac hardware, I chose Metal, Apple’s native GPU framework. I never touch a GPU framework before so before diving into this simulation I went to a tutorial: “Metal by Tutorials Beginning Game Engine Development With Metal” by Caroline Begbie and Marius Horga. After this spinoff I felt ready to build the simulation. Let’s first talk about the science behind it.

// add picture

This simulation is using SPH (Smooth Particule Hydrodynamics). In mecanisch there is two view point: eulerien and lagrangien.

// Visualisation side by side

In Lagrangien point of view we directly follow particules. Lagrangien is fit for solid modelisation as there is a few of them so we can keep track of them. In fluid dynamics there is far too many particules that all follow chaotic behavior.

But we don’t need to keep track of every particules to know predict how the fluid move, the information lie in statistical values. Eulerienne point of view we look at statistical values for physical fields.

Most numerical methods: Finite Difference Method (FDM), Finite Element Method (FEM), Finite Volume Method (FVM), Lattice Boltzmann Method (LBM), Spectral Method, use eulerien point of view. They used grid to hold the physical information.

SPH take a rather singular path. In SPH the physical information is hold by particules. The particules doesn’t represent physical atom, molecule, droplet or grain but are rather virtual quantity of a region in space.