Fluid.c File Reference

CPU Fluid Simulation Program. More...

#include <stdio.h>
#include <stdlib.h>
#include "glut.h"

Go to the source code of this file.

Defines

#define IX(i, j)   ((i)+(N+2)*(j))
 Convert 2 dimension array location to a 1 dimensional array index.
#define SWAP(x0, x)   {float *tmp = x0; x0 = x; x = tmp;}
 Swap two arrays.
#define DIM   128
 Dimension of the fluid matrix.
#define SIZE   DIM*DIM
 Size of the fluid matrix.
#define max(x, y)   x>y?x:y
 Return the maximum value.

Functions

void printArray (int N, float *x)
 Prints out the values of the passed in array.
void init (void)
 Creates the OpenGL display list.
void display (void)
 Main program loop.
void idle (void)
 Called when window sytem events aren't being received.
void reshape (int x, int y)
 Called when the window is resized.
void keyboard (unsigned char key, int x, int y)
 Processes keyboard input.
void click (int button, int state, int x, int y)
 Processes the mouse click event.
void motion (int x, int y)
 Processes the mouse moved event.
void timer (int value)
 Timer to control the frame rate.
void updateFluid (int button, int mouseMove, int state)
 Adds density or velocity to the current fluid.
void add_source (int N, float *x, float *s, float dt)
 Adds external sources to the fluid.
void diffuse (int N, int b, float *x, float *x0, float diff, float dt)
 The diffusion step.
void advect (int N, int b, float *d, float *d0, float *u, float *v, float dt)
 The advection step.
void project (int N, float *u, float *v, float *p, float *div)
 The projection step.
void dens_step (int N, float *x, float *x0, float *u, float *v, float diff, float dt)
 The density step.
void vel_step (int N, float *u, float *v, float *u0, float *v0, float visc, float dt)
 The velocity step.
void set_bnd (int N, int b, float *x)
 Set the values of the invisible boundary.
int main (int argc, char *argv[])

Variables

float u [SIZE]
 Horizontal Velocity Field.
float v [SIZE]
 Vertical Velocity Field.
float u_prev [SIZE]
 Horizontal Velocity Field for previos step.
float v_prev [SIZE]
 Vertical Velocity Field for previous step.
float dens [SIZE]
 Density Field.
float dens_prev [SIZE]
 Density Field for previous step.
int boundary [SIZE]
 Contains which cells are boundarys.
int wWidth = max(512,DIM)
 GUI window width.
int wHeight = max(512,DIM)
 GUI window height.
float cellSize
 Size of each cell that is drawn.
float diff = 0.5f
 Rate of diffusion of the fluid.
float dt = .2f
 Time step.
float visc = 0.0f
 Viscosity of the fluid.
int stepToggle = 0
 Steps the program by 1 time step.
int runToggle = 1
 Starts or stops the program from running in a loop.
int consoleToggle = 0
 Shows or hide console debug output.
int fluidToggle = 0
 Injects fluid into simulation.
int densityToggle = 1
 Show or hide the denisity field drawing.
int velocityToggle = 0
 Show or hide the velocity field drawing.
int gridToggle = 0
 Show or hide the grid drawing.
int boundaryToggle = 1
 Show or hide the boundarys.
int drawBoundaryToggle = 0
 Turn drawing of boundarys on or off.
int id
 OpenGL display list.
int lastX = 0
 The last x position the mouse was in.
int lastY = 0
 The last y position the mouse was in.
int currentX = 0
 The current x position the mouse is in.
int currentY = 0
 The current y position the mouse is in.
int clicked = 0
 A mouse button was clicked.
int leftClick = 0
 The left mouse button was clicked.
int N = DIM-2
 The dimension of the fluid array without the boundary walls.
int emitterLoc = DIM/2
 Location of the fluid injection.
int frameRate = 60
 Frame Rate.
int updown = 0
 State of the mouse button.
int lastCellX
 The x coordinate of the last touched boundary cell.
int lastCellY
 The y coordinate of the last touched boundary cell.
float totalDensity = 0
 The total density in the simulation.


Detailed Description

CPU Fluid Simulation Program.

Author:
Dirk Hekhuis
Version:
4-9-09

Definition in file Fluid.c.


Define Documentation

#define DIM   128

Dimension of the fluid matrix.

Definition at line 74 of file Fluid.c.

#define IX ( i,
 )     ((i)+(N+2)*(j))

Convert 2 dimension array location to a 1 dimensional array index.

Definition at line 72 of file Fluid.c.

#define max ( x,
 )     x>y?x:y

Return the maximum value.

Definition at line 76 of file Fluid.c.

#define SIZE   DIM*DIM

Size of the fluid matrix.

Definition at line 75 of file Fluid.c.

#define SWAP ( x0,
 )     {float *tmp = x0; x0 = x; x = tmp;}

Swap two arrays.

Definition at line 73 of file Fluid.c.


Function Documentation

void add_source ( int  N,
float *  x,
float *  s,
float  dt 
)

Adds external sources to the fluid.

It adds the external forces that where provided by user input to the current fluid array.

Parameters:
N Size of the array
x The array that holds the current fluid information
s The array that holds the external forces that where supplied by user input
dt The time step

Definition at line 841 of file Fluid.c.

void advect ( int  N,
int  b,
float *  d,
float *  d0,
float *  u,
float *  v,
float  dt 
)

The advection step.

The basic idea of the advection step is: instead of moving the cell centers to forward in time through the velocity field we look for the particles which end up exactly at the cell centers by tracing backwards in time from the cell centers.

Parameters:
N The size of the array
b Value for set_bnd()
d The current field
d0 The field from the previous step
u Horizontal velocity field
v Vertical velocity field
dt The time step

Definition at line 889 of file Fluid.c.

void click ( int  button,
int  state,
int  x,
int  y 
)

Processes the mouse click event.

It processes the mouse click event and calls updateFluid() with the button that was pushed.

Parameters:
button The mouse button that was clicked
state The state that the clicked mouse button is in, up or down
x The x location of the mouse when the mouse button was clicked
y The y location of the mouse when the mouse button was clicked

Definition at line 713 of file Fluid.c.

void dens_step ( int  N,
float *  x,
float *  x0,
float *  u,
float *  v,
float  diff,
float  dt 
)

The density step.

It computes the density step of the fluid simulation.

Parameters:
N The size of the array
x The current density field
x0 The source densities
u The horizontal velocity field
v The vertical velocity field
diff The rate of diffusion
dt The time step

Definition at line 1122 of file Fluid.c.

void diffuse ( int  N,
int  b,
float *  x,
float *  x0,
float  diff,
float  dt 
)

The diffusion step.

Bug:
The density of the fluid next to the boundary is less than it should be.
Todo:
To fix the density next to the boundary I will need to make the density equal to the density of its direct neighbors.
The diffusion step exchanges density with its direct neighbors.

Parameters:
N The size of the array
b Value for set_bnd()
x The current field
x0 The field from the previous step
diff The rate of diffusion
dt The time step

Definition at line 853 of file Fluid.c.

void display ( void   ) 

Main program loop.

Visualizes the information the the various arrays and and runs the fluid calculations.

Definition at line 443 of file Fluid.c.

void idle ( void   ) 

Called when window sytem events aren't being received.

It calls glutPostRedisplay to call display().

Definition at line 605 of file Fluid.c.

void init ( void   ) 

Creates the OpenGL display list.

Initializes cellSize, creates the display list that is used to draw the cells of the various arrays, and sets the glClearColor to black.

Definition at line 417 of file Fluid.c.

void keyboard ( unsigned char  key,
int  x,
int  y 
)

Processes keyboard input.

It processes the keyboard input and sets the various toggle variables based what key was pressed.

Parameters:
key The ASCII value of the key pressed
x The x location of the mouse when the key was pressed
y The y location of the mouse when the key was pressed

Definition at line 638 of file Fluid.c.

int main ( int  argc,
char *  argv[] 
)

Definition at line 364 of file Fluid.c.

void motion ( int  x,
int  y 
)

Processes the mouse moved event.

It processes the mouse moved event and sets the currentX and currentY variables to the current location of the mouse if the left click button is held down.

Parameters:
x The x position of the mouse
y The y position of the mouse

Definition at line 735 of file Fluid.c.

void printArray ( int  N,
float *  x 
)

Prints out the values of the passed in array.

Prints out the values of the passed in array printing each row on a seperate line to the console.

Parameters:
N The dimension of the array
x Pointer to the array to be printed

Definition at line 399 of file Fluid.c.

void project ( int  N,
float *  u,
float *  v,
float *  p,
float *  div 
)

The projection step.

The projection step is used to make sure the velocity field conserves mass. Every velocity field is the sum of a mass conserving field and a gradient field.

Parameters:
N The size of the array
u The current horizontal velocity field
v The current vertical velocity field
p The horizontal velocity field from the previous step
div The vertical velocity field from the previous step

Definition at line 972 of file Fluid.c.

void reshape ( int  x,
int  y 
)

Called when the window is resized.

Bug:
The grid doesn't seem to scale correctly.
Todo:
The cause of the bug is because the cellSize is based on the width of the window so I assume that the height of the cell is the same as the width. So if the window isn't a perfect square the bug shows up. So I have to get a seperate cellSize for the width and a seperate cellSize for the height. Also width and height of the window needs to be a float instead of an int.
It sets wWidth to x and wHeight to y, resets cellSize based on the new window size, deletes the display list and calls init() to create a new display list based on the new cellSize, and sets up the camera.

Parameters:
x The new windows width
y The new windows height

Definition at line 612 of file Fluid.c.

void set_bnd ( int  N,
int  b,
float *  x 
)

Set the values of the invisible boundary.

Bug:
When dragging the mouse into the corners or along the sides, a lot of velocity can shoot out from the corner or the sides.
Sets the value of the invisble boundry to the value of the cell next to it if b = 0 otherwhise it sets the value of the invisible boundry to 0.

Parameters:
N The size of the array
b If 0 set the value to the value of the cell next to it otherwise set it to 0
x The array to work on

Definition at line 1179 of file Fluid.c.

void timer ( int  value  ) 

Timer to control the frame rate.

The timer calls glutPostRedisplay() every time the timer goes off and creates a new timer since the timer is destroyed everytime it goes off. The timer keeps the program running at a fixed frame rate.

Parameters:
value A value that is passed along to the timer function when the timer goes off

Definition at line 751 of file Fluid.c.

void updateFluid ( int  button,
int  mouseMove,
int  state 
)

Adds density or velocity to the current fluid.

It add density to the fluid at the current mouse position if the user right clicked, add velocity to the fluid at the current mouse position if the user is holding down left click and moves the mouse.

Parameters:
button The mouse button that was clicked
mouseMove If the mouse moving it equals 1 else it equals 0
state If the mouse button is pushed down it equals 0 else it equals 1

Definition at line 759 of file Fluid.c.

void vel_step ( int  N,
float *  u,
float *  v,
float *  u0,
float *  v0,
float  visc,
float  dt 
)

The velocity step.

It computes the velocity step of the fluid simulation.

Parameters:
N The size of the array
u The current horizontal velocity field
v The current vertical velocity field
u0 The horizontal source velocities
v0 The vertical source velocities
visc The amount of viscosity of the fluid
dt The time step

Definition at line 1140 of file Fluid.c.


Variable Documentation

int boundary[SIZE]

Contains which cells are boundarys.

Definition at line 332 of file Fluid.c.

int boundaryToggle = 1

Show or hide the boundarys.

Definition at line 346 of file Fluid.c.

float cellSize

Size of each cell that is drawn.

Definition at line 335 of file Fluid.c.

int clicked = 0

A mouse button was clicked.

Definition at line 353 of file Fluid.c.

int consoleToggle = 0

Shows or hide console debug output.

Definition at line 341 of file Fluid.c.

int currentX = 0

The current x position the mouse is in.

Definition at line 351 of file Fluid.c.

int currentY = 0

The current y position the mouse is in.

Definition at line 352 of file Fluid.c.

float dens[SIZE]

Density Field.

Definition at line 330 of file Fluid.c.

float dens_prev[SIZE]

Density Field for previous step.

Definition at line 331 of file Fluid.c.

int densityToggle = 1

Show or hide the denisity field drawing.

Definition at line 343 of file Fluid.c.

float diff = 0.5f

Rate of diffusion of the fluid.

Definition at line 336 of file Fluid.c.

Turn drawing of boundarys on or off.

Definition at line 347 of file Fluid.c.

float dt = .2f

Time step.

Definition at line 337 of file Fluid.c.

int emitterLoc = DIM/2

Location of the fluid injection.

Definition at line 356 of file Fluid.c.

int fluidToggle = 0

Injects fluid into simulation.

Definition at line 342 of file Fluid.c.

int frameRate = 60

Frame Rate.

Definition at line 357 of file Fluid.c.

int gridToggle = 0

Show or hide the grid drawing.

Definition at line 345 of file Fluid.c.

int id

OpenGL display list.

Definition at line 348 of file Fluid.c.

int lastCellX

The x coordinate of the last touched boundary cell.

Definition at line 359 of file Fluid.c.

int lastCellY

The y coordinate of the last touched boundary cell.

Definition at line 360 of file Fluid.c.

int lastX = 0

The last x position the mouse was in.

Definition at line 349 of file Fluid.c.

int lastY = 0

The last y position the mouse was in.

Definition at line 350 of file Fluid.c.

int leftClick = 0

The left mouse button was clicked.

Definition at line 354 of file Fluid.c.

int N = DIM-2

The dimension of the fluid array without the boundary walls.

Definition at line 355 of file Fluid.c.

int runToggle = 1

Starts or stops the program from running in a loop.

Definition at line 340 of file Fluid.c.

int stepToggle = 0

Steps the program by 1 time step.

Definition at line 339 of file Fluid.c.

float totalDensity = 0

The total density in the simulation.

Definition at line 361 of file Fluid.c.

float u[SIZE]

Horizontal Velocity Field.

Definition at line 326 of file Fluid.c.

float u_prev[SIZE]

Horizontal Velocity Field for previos step.

Definition at line 328 of file Fluid.c.

int updown = 0

State of the mouse button.

Definition at line 358 of file Fluid.c.

float v[SIZE]

Vertical Velocity Field.

Definition at line 327 of file Fluid.c.

float v_prev[SIZE]

Vertical Velocity Field for previous step.

Definition at line 329 of file Fluid.c.

int velocityToggle = 0

Show or hide the velocity field drawing.

Definition at line 344 of file Fluid.c.

float visc = 0.0f

Viscosity of the fluid.

Definition at line 338 of file Fluid.c.

int wHeight = max(512,DIM)

GUI window height.

Definition at line 334 of file Fluid.c.

int wWidth = max(512,DIM)

GUI window width.

Definition at line 333 of file Fluid.c.


Generated on Thu Apr 9 17:33:43 2009 for CPU Fluid Simulation by  doxygen 1.5.8