MAE 152 -- Fall 2003

Project 1: C programming review

Due Oct. 3 2003


This was last updated sept 14, 03

Learning objectives:


Problem Discussion:

Consider the problem of time varying heat conduction in a 2-Dimensional flat plate. There is an initial temperature differential across the plate which is used for controlled chemical reactions, and your job is to find the effects of initial differences in temperature as they develop over time.

The heat equation in two dimensions is given by :

We can discretize this equation in order to compute the temperature distribution by breaking the plate up into a specific number of blocks. Each block is represented as a "node." Every material has an associated thermal diffusivity, represented in our case by the greek letter . High alpha values correspond to high heat flux through a material, and low alpha values correspond to a low heat flux through a material. The units of are m^2/s.

 

Discretized grid over the flat plate

 

 

Diagrammatical view of the connection between each node group

 

 

 

 

Helpful Hints:

There are many ways to write this program, however one suggested way is to break it out into functions which encapsulate each main portion of the program. In order to get up and running with a basic console program, before attempting this project, first read the FAQ for creating a basic Visual C++ console program here.

1) Start by using the example below as a starting point for your code format. Use a console window, include stdio.h, and math.h at the beginning of your source. You may begin with an empty starter project generated by visual C++. Create a new text file, then save it in the same folder as the "your_project_name.dsw" file. Next you must add it to the project by going to the menus: project-add to project-files, then pick your new text file. The beginning of your program should look something like:

/*Beginning of program...

Author : Joe Bob

Project 1

Date: Sept. 20, 2003

Description: What this program will do

Assumptions: Heat assumptions

Inputs: Initial conditions, user input, etc.

Outputs: Graphics to screen

*/

#include <stdio.h>

/*===============Global Variables==========*/

int i;

float Temp[ npts ][ npts ][ npts ];

//.....

/*====================================*/

/*___________________function prototypes____________*/

void initascii ( void );

char asciiconvert ( float );

void compute( void );

/*_______________________________________________*/

//main program below:

int main( void )

{

 

return (0);

}

 

//initialization function

void initascii ( void )

{

 

}

char asciiconvert( float T)

{

 

}

It will, of course include other functions and variables. YOU MUST COMMENT YOUR CODE CLEARLY. If it is not clearly commented, and there is a problem, you will lose points. Please, no spaghetti code.

2) You will need some global variables to hold the computed temperature values. One will need to be a three-dimensional array of temperature values to hold for t, x, and y. Another will need to be at least two-dimensional to hold the ascii character versions of the temperature values. The other global variables you will need will be the parameter values for thermal diffusivity (alpha), timestep (dt), and the nodal step size (ds). The arrays should be dimensioned to allow a 25 x 25 grid and 25 timesteps.

3)Now create the function prototypes, You will need at least the following:

/*+++++++++++++++++++++++function prototypes+++++++++++++++++++*/
void initialize( void );
char asciiconvert( double );
void calculate( void );
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

4) Let's move into the main program. If you encapsulated everything into separate functions, as one does in good programming practice, you would have a main that is simply structured and clear.

5)Your initialization function will initialize the entire array of temperature variables to zero by looping through each space in the array (ie t, x, and y) and setting it equal to zero. It will also set the coefficient values for dt, ds and alpha. Finally it should set the two initial temperature source points. There will be three 'for' loops to zero out the temperature values, Use the following table for the coefficients:

Timestep

= 1.0sec

Thermal Diffusivity
= 0.4 m^2/sec
Node size ()
= 0.1m
Initial Conditions:

T(0,x,y)=0 deg C

T(0,10,5)=500.0 deg C

T(0,5,10)=100.0 deg C

 

6)Write a function to read in temperature at a point, then translate the numbers calculated into graphical characters. Use the symbols as follows to represent temperatures:

"."
0<=T<0.2
":"
0.2<=T<=1.0
"o"
1.0<T<=3.0
"O"
3.0<T<=5.0
"*"
T>5.0

You may want to create a switch statement, or use if - else statements. Be sure the function returns the character value

 

7) Write a function, finally, which computes the temperature at each node point using the finite difference equation given above. Recall it is:

First you must compute the 1/M value from the given equation in the table above. You are computing the future temperature distribution by computing the current one, starting at t=0 with temperature zero everywhere except the given points. You will need three for loops again (there are other ways to accomplish this, but we are emphasizing simplicity here). After each calculation (inside the loop) you will want to call the ascii graphic conversion (something like s[x][y] = asciiconvert(T[t][x][y]) ). Then display this with a printf() statement. At the end of each row you will need to perform another printf(), using only \n as the argument inside the parentheses, this will move you to the next line.

8) One other detail here. The goal in this assignment is to create a sense of the temperature on the plate distributing over time. But if we execute the program as designed so far, the 'graphics' will redraw too quickly, and we will see a mere blur.This is because the human perceptual system likes to put together a whole. So, we need to alter the balance of timing. If the image we want to display exists for a longer period of time than the scrolling during redraw, the perceptual system in the human brain can then create a 'whole,' or 'gestalt' where we percieve the plate.

Here are a couple of Gestalt examples. The shape on the left is most readily perceived as a circle, rather than a curved line. The image on the right is a classic one where a ghostly triangle is created by the 'pac-man' shapes. Our perceptual system tends to see a triangle on top of circles rather than only the pac-man shapes. The color of the 'triangle' area is actually the same as the background.

 

How do we perform this magic? Simply add two or more loops which will perform a calculation such as a sine or cosine calculation repeatedly inside the loop for each time step after the graphic is displayed. We will then see the graphic on the screen statically for a longer period of time than we see the graphic move up as we see the scrolling. If you find the correct number of iterations for the calculation, you will create a good looking animation.

9) Final output should look like this (depending on system and compiler):

You may want to add a scanf() at the end of your calculation that waits for the user to type any key before quitting.

10)You are finished unless you would like to attempt the bonus points!!!

 

Good luck!

Alex

 

Point break down:

Console window creation

30 pts

Basic code and functions are written
20
One grid created, with no compilation errors
20
Correct Heat Equation
10

Temperature "Animates" smoothly

(ie the image is visible on screen longer than the scrolling period)

20
BONUS
20
TOTAL POINTS POSSIBLE
120