GPU - Project 3 - Fire Simulation in CUDA

The main purpose of this project was simulation of fire and its behavior using CUDA.  A clean Simulation of fire requires the use of mathematical functions such as Navier Stokes Equations.

Problem Solving

The plan of the project was to

1) Simulate enough number of particles.

2)Apply Effects using available methods.

 Chapter 38 "Fast Fluid Dynamics Simulation on the GPU" in GPU Gems gives a good introduction on how to use Navier Stokes Equations.

The following equations need to be used.

Equation 1

Equation 2

where r is the (constant) fluid density, n is the kinematic viscosity, and F = (fx , fy ) represents any external forces that act on the fluid. Notice that Equation 1 is actually two equations, because u is a vector quantity:

Thus, we have three unknowns (u, v, and p) and three equations

Advection

The velocity of a fluid causes the fluid to transport objects, densities, and other quantities along with the flow. Imagine squirting dye into a moving fluid. The dye is transported, or advected, along the fluid's velocity field. In fact, the velocity of a fluid carries itself along just as it carries the dye. The first term on the right-hand side of Equation 1 represents this self-advection of the velocity field and is called the advection term.

Pressure

Because the molecules of a fluid can move around each other, they tend to "squish" and "slosh." When force is applied to a fluid, it does not instantly propagate through the entire volume. Instead, the molecules close to the force push on those farther away, and pressure builds up. Because pressure is force per unit area, any pressure in the fluid naturally leads to acceleration. (Think of Newton's second law, F = m a.) The second term, called the pressure term, represents this acceleration.

Diffusion

From experience with real fluids, you know that some fluids are "thicker" than others. For example, molasses and maple syrup flow slowly, but rubbing alcohol flows quickly. We say that thick fluids have a high viscosity. Viscosity is a measure of how resistive a fluid is to flow. This resistance results in diffusion of the momentum (and therefore velocity), so we call the third term the diffusion term.

External Forces

The fourth term encapsulates acceleration due to external forces applied to the fluid. These forces may be either local forces or body forces. Local forces are applied to a specific region of the fluid—for example, the force of a fan blowing air. Body forces, such as the force of gravity, apply evenly to the entire fluid.

However the book suggests a few books and papers for simulating fire in a constrained manner and to have it simulated in 3d.

https://docs.google.com/document/edit?id=1EbavWuek6FE52dVSNYCUnnaZkTO9WOmXdixGxbUzutU&hl=en#

Problems Encountered

I started by playing with the parameters in a sample Fluid simulation present in the CUDA SDK. But after referring to GPU gems I realized to convert this 2d Simulation to 3d and to have the fire in a constrained manner is quite a difficult task to be finished in a short period of time. The result I got by tweaking with the parameters are as follows.

I later moved my attention to a non-CUDA fire simulation that was freely available on the web. I managed to get a decent simulation of fire by tweaking with the parameters. But had to do away with a lot of work when I realised that there was no scope for CUDA.

Simulation

The current simulation consists of an inflammable material. This material is generated entirely by points. Presently this simulation consists of 300000 points. We have a parameter 'ignition' which allows us to modulate the inflammability of the material. We also have a parameter age which deals with the burning of the material and changes its color accordingly. The simulation has the following features.

1. Inflammable material - Paper/Cloth

2. Ability to light the material

3. Ability to pause the spread of fire.

4. Ability to control the fuelsize.

Controls

1. Mouse left click for burning the material.

2. Key ' ' for pause/ play the spread of fire

3. Key 7 to increase the fuelsize

4. Key 8 to decrease the fuelsize

5. Mouse Roller for zoom in/out

6. Right click for Pan

Pseudocode

//Position Attributes:

h_particleData[pCounter].pos.x = pCounter%500; //X

  h_particleData[pCounter].pos.y = pCounter/500; //Y

This piece of code helps in getting a cloth/paper effect with 300000 points.

 

        kernel-

        //Flammability

        if( (my+1)->age >= 20 && my->ignition > 8||

//(my-1)->age >= 20 && my->ignition > 10||

(my+500)->age >= 20 && my->ignition > 8||

//(my-500)->age >= 20 && my->ignition > 8||

(my+500+1)->age >= 20 && my->ignition > 8||

(my+500-1)->age >= 20 && my->ignition > 8||

//(my-500+1)->age >= 20 && my->ignition > 10||

(my-500-1)->age >= 20 && my->ignition > 8)

         {

my->state = 1;

my->age +=dt ;

if(my->age > 1000)

my->age = 1000;

          }

Source Download:

http://www2.cs.uic.edu/~anaik/FireSim7.zip

Further on I will implement Volumetric Shaders with my simualtion to get a better effect of fire.

References:

http://http.developer.nvidia.com/GPUGems/gpugems_ch38.html

http://www.paulsprojects.net/opengl/gpufire/gpufire.html