Fluid Dynamics Simulation

Click here to download program. The resolutions is much lower in the movie above than in the actual system.


This application implements a fluid dynamics solver to model 2-dimensional fluid behavior. The system was written using C++ and the OpenGL libraries. The user interface was designed using FLTK and fluid.

To model the fluids, the screen is broken into a 2-dimensional array of fluid cells, each following its own path in the fluid system. The granularity of the grid can be changed interactively. When the system is started, each fluid cell is initialized with a zero density, as well as a random velocity. At each time step, the velocity and density is recalculated using the Navier-Stokes Equation. A current was created which starts in the lower left corner and goes to the upper right corner of the display window. In addition, a density source is located in the lower left corner, continuously adding "smoke" to the system. Since the system is mass conserving, the fluid flows mainly in this upward, right direction, but begins to swirl back down as it hits the boundaries of the window.

The main data structure for the system is the FluidCell. The FluidCell is made up of a position, velocity, and density. There is a 2-dimensional grid of fluid cells that makes up the fluid system. Most of the calculations are done by the FluidSolver. This class is based on Jos Stam's implementation (2003). It uses the Navier-Stokes Equation to calculate changes in fluid density and fluid velocity at each time step. At each time step, the velocities for each FluidCell are updated by applying any forces, calculating the diffusion, and calculating the advection. In addition, there is a step to ensure the mass is conserved in the system. After the velocities are updated, the densities for each FluidCell are updated by adding any density from the source, calculating the diffusion, and calculating the advection. The FluidSolver contains the grid of fluid cells, as well as a 2-dimensional grid of force values and source values, corresponding with the FluidCell grid.

The application represents the density in each FluidCell as a shade of blue. The closer the color is to white, the denser the FluidCell is. Dark blue cells have a low density. To examine the velocity for each cell, there is a button "Show/Hide Velocity." The velocities are represented as black lines starting in the center of the cell, and extending in the direction and magnitude of the velocity. Similarly, the force acting upon a FluidCell can be displayed using the "Show/Hide Forces" button. The force is represented as a white line starting in the center of the cell and extending in the direction and magnitude of the force. The application also includes the ability to reset the system to random velocities and densities using the "Random Velocity and Density" button. In addition, new random force values are set along the x=y line. To clear all density values to zero, and reset the velocities to random amounts, there is a "Clear Density" button. To add density to the system, left click anywhere in the display window. To create a density source, right click anywhere in the display window. A density source will continuously add density to the system. There are instructions that appear in a separate window and are accessed using the "Instructions" button. Finally, there is a quit button to exit the application.

John Hugg's vector class was used to store and manipulate vectors.

My goal was to create an easy to use, interactive application to model fluid behavior. If I had more time, I would allow users to add forces while using the application. I'd also like to support 3-dimensional fluid dynamics.

REFERENCES
Chan, Eric. FLTK Tutorial - Comp 275. Tufts University Spring 2006.
Frisken, Sarah. Class Notes - Comp 275. Tufts University Spring 2006.
Hugg, John. Vec3 - a C++ Vector class. Tufts University Spring 2006.
Stam, Jos. Real-Time Fluid Dynamics for Games. 2003