Exploring the Intersection of AI and Fluid Dynamics
My project/dissertation for my MSc in AI involved designed and training a neural network to learn the movements of fluid in a 2D fluid dynamics simulation. This particular blog will be an exploration of how that went.
AI RESEARCH
Stephen Moreton-Howell
5/8/20244 min read
Fluid Flow Can Be Complicated


Think of what is happening when a fluid (air or liquid) flows through a container and/or past obstacles. Millions of particles bouncing off the container walls, the obstacles and each other. Below is a simulation I created of one million particles flowing from left to right around a circular obstacle. The enlarged section blows up the tiny rectangular area shown, illustrating that the grey mist you see there is actually lots of particles. A small sample of particles are drawn larger than the rest so as to better illustrate the flow. In the main view they are shown as large coloured dots. In the enlarged view they are the coloured balls.
Aim
The aim of this project was to investigate the application of neural networks (NN) to computational fluid dynamics (CFD). CFD is the computer modelling of the movements of fluids in 2 or 3 dimensions. It generally takes a lot of computing time. So the idea is to train a NN to be able to predict the movements of the computationally simulated fluid in the hope that the action of the trained NN can be made to be faster than the conventional CFD model, with an acceptably low loss of accuracy.

Play the video and you will see (towards the end) when I add velocity vectors to those large particles that there are just enough particles in this simulation to illustrate vortices in the flow around the circular obstacle. But this is "just" a million particles. That's tiny compared to the number of particles in a real life fluid flow system on a human scale or larger. And each frame of this simulation took far longer to calculate than it did to play back in this video (running on a PC with an Intel Core i5 CPU). Clearly for a simulation of anything close to real life we can't model every particle.


Here is a screenshot of the CFD model that I used for this project. I based it on a pre-existing simulation of the Navier-Stokes equations written in C++. I re-wrote it as a Windows Forms .NET application in C# and added everything I needed to use its output to train the NN.
The software here contains a 2D grid, 660 wide by 120 high, and at each time step it calculates the thermodynamic properties of each cell in that grid, showing the results as you can see. Those three green circles are obstacles getting in the way of the fluid, which is flowing from left to right through a rectangular 2D space.
You can see from the GUI ("Current Frame") that by this screenshot the simulation has been running for 1326 frames, and that it takes just over 1 second to generate each frame ("FPS"). By this time, the vorticity map shows that flow lines have built up behind the three obstacles into something that looks like a Kármán vortex street.
The software is designed to periodically output the data from a set of frames in a format that will be fed into my NN as training data. The way in which it does that is governed by those controls on the right of the GUI.
Instead of trying to model all those particles individually, we can divide up the volume of space in which they are moving into a grid of cells and model just those macroscopic properties of each cell. The temperature, pressure, velocity, etc assigned to each cell represent the quantity, movements and positions of all the particles in that cell.
This simplifies things, but for an accurate model we still need a lot of those cells and a lot of calculations. So running on a typical PC's CPU the simulation can still take a long time.
That's the microscopic view. We can simplify this picture with the macroscopic thermodynamic concepts of pressure, volume, density and temperature. These are collective properties of large numbers of particles. They simplify things by taking all of those millions of positions and velocities and representing them as a single number. Temperature, for example, is a single number representing the average speed of a whole load of particles. Density is a single number representing how many particles are packed into a given volume.
Thermodynamic Properties - The Grid
Creating and Training the Neural Network
As we've said, the fluid dynamics simulation is 2 dimensional. So each frame of data that we'll use to train the NN is a 2D grid of 2D velocity vectors. That is, for each point on that grid there will be an X and Y value representing the velocity of the fluid at that point. We can view this as being a bit like a still from a movie. And, as with a movie, there is a relationship between adjacent frames and between adjacent points within each frame. The action literally flows as time progresses, just as it metaphorically flows in a movie. So the best approach is to use the kind of NN that is good at finding these kinds of flow patterns, learning from them and reproducing that. I wanted to create a NN which, when trained, could be given a set of sequential frames and could then predict the frame that comes next. For this, the best kind of NN architecture is the U-Net.


