Assignment 7 ============ Due: Last day of term 6th December 1994 Weight: 45 Assignment designed by: Kevin Theobald - Gee Thanks!! E-Mail your program source code & numeric answer to >> HCA7 << Assignment will be graded by Vince Delle Donne. ADDING HEAT TO LIFE This assignment will build on your knowledge of C, and will introduce three new concepts: SOFTWARE REUSE: This is the principle that the easiest way to get a job done is to take something that does a similar job, and modify it to do what you want. SIMULATION: This is the use of computers to model the behavior of a physical system. This is an important application of computers in most engineering fields. GRAPHICS: Graphics enable the computer to present its results in a way which is easier to comprehend than a raw table of numbers. Also, they're cool. For this assignment, you will take an existing program for the Life problem, and modify it to model a simple heat conduction problem. CONWAY'S LIFE PROBLEM John Conway devised a game called Life which models the behavior of complex systems. The game is introduced in the Mathematical Games section of Scientific American, October 1970 and February 1971. The basic rules are as follows: 1. Make a square grid, like a piece of graph paper. Each square is either empty or filled. Initially, the game starts by filling in some of the squares. 2. The game is played by going through an unlimited number of discrete "turns." During each turn, each square in the grid is examined. Each square is surrounded by 8 neighbors (including the squares it touches diagonally). 3. If the square is empty, and if EXACTLY 3 of its neighbors are filled, then there is a "birth" there -- that square is filled. 4. If the square is filled, and if it has 2 or 3 neighbors, it stays filled for this turn. If it has fewer than 2 neighbors, it "dies" from starvation -- the square becomes empty. If it has more than 3 neighbors, it "dies" from over-population. 5. Births and deaths don't affect other births and deaths in the same turn. For instance, consider this pattern: ............. ............. .....000..... ............. ............. The dots represent empty squares, the zeroes represent filled squares. In the first turn, the 7th square in the 2nd row has 3 neighbors, so there is a birth there. There is also a birth in the 7th square of the 4th row. No other empty squares have 3 neighbors. Meanwhile, the filled squares at the two ends only have 1 neighbor apiece (the new births don't count yet), so they die. The one in the middle is OK because it has 2 neighbors. After this turn, the board looks like: ............. ......0...... ......0...... ......0...... ............. The next turn goes back to the previous configuration, so this pattern will oscillate forever. (This pattern is called a "blinker.") THE LIFE PROGRAM To begin working on this assignment, GET A LIFE! This is easier than it sounds. Just go into DOS and type the commands: map g:=emf\home:\theobald copy g:life.c f: Go into the Turbo C compiler and Make the program. Now go back to DOS and run the program. The program will ask you to put in an initial pattern. Since the program has a large grid, it would be a pain to type in a bunch of blank lines for a small pattern like the blinker. Therefore, the program will first ask you for the number of rows and columns in the initial pattern. It will also ask you for the initial position of the upper-left corner of your input pattern (rows are numbered from 0 to 39, 0 on top, and columns are numbered from 0 to 63, 0 on the left). Usually, you will want to put the pattern in the middle to give it room to grow on all sizes (things disappear if they run off the edges). If you really want to look at it on MUSIC B it is in INF8:LIFE.C For example, run the program and put in the blinker. The number of rows is 1, the number of columns is 3, the first row is 20, and the first column is 29. When it asks you for each row, type spaces for empty squares and zeroes for full squares. Therefore, type 000 followed by ENTER. Now you should see the blinker in action. Play with this program to understand how it works. See what happens when you vary the starting position. Try this pattern 0 0 000 near the upper-left corner of the screen. What does it do? HEAT TRANSFER Enough fun and games! Now you're going to modify this program to simulate a heat sink, which is a passive device for dissipating heat from hot components so that they don't overheat. For example - an Intel Pentium 80586 is known to run hot, and has a heat sink to remove heat - (a tiny on-chip fan is an option!!) To make the problem simple, we will assume the heat sink is a rectangular plate of a uniform material of thickness d. A hot component is attached to this plate somewhere. The component injects energy at a constant rate into the plate. The plate dissipates this energy (hopefully). There are three mechanisms at work: CONDUCTION: Heat conducts from hot to cool parts of the plate. CONVECTION: Heat is transferred from the hot plate to the air. RADIATION: Heat is radiated away from the hot plate. Due to the simple shape of the plate, we can model it by dividing it into small squares of length L, and assuming that the temperature is uniform across an individual square. Also, we break time into a series of steps of length t, and assume that the temperature in a square is constant during each period of time. This simplifies things a lot. As any introductory thermodynamics textbook (e.g., Fundamentals of Heat Transfer, by L. C. Thomas) will show, conduction and convection calculations require the use of gradients. Our simple model allows us to use simple difference equations instead. Below is the list of physical behaviors we want to model. CONDUCTION: Thermal energy flows from hot parts to cool parts. In our model, heat flows from/to a square to/from each of its 4 neighbors (we don't count diagonal neighbors, since they don't have much contact). If squares 1 and 2 share an edge, and square 1 has a temperature of T1 and square 2 has a temperature of T2, then the energy flow FROM square 1 TO square 2 is k(A/L)(T1-T2)t. k is a positive constant which depends on the material (also on the temperature, but we'll ignore that and assume k is constant). A typical value of k is 200W/mK (watts per meter- Kelvin) for aluminum. (Remember that 1W, a watt, is 1 joule per second.) A is the cross-sectional area through which the heat flows. In this case, A = Ld, so the formula reduces to kd(T1-T2)t. t is the duration of one time step. Note that when you cancel all the units out, you end up with joules. CONVECTION: Heat is transferred from the hot plate to the air. This is also in proportion to the temperature difference. If a square has temperature T, then the net flow to the air is hA(T-Ta)t, where Ta is the ambient air temperature, and h depends on the air and how it flows. A typical value for h is 6.9 W/m**2 K. A is the area, which is just L**2. We will assume h is constant, and that Ta will stay the same no matter how much heat is dissipated. RADIATION: Heat is radiated away from the hot plate. The formula is similar to the convection formula, but is changed to ebA(T**4 - Ta**4)t. Again, A is the area, which is L**2. b is Boltzman's constant, 5.67E-08 W/m**2 K**4 (it should be a lower-case sigma, but these computers don't handle Greek letters). e is the emissivity of the material, a unitless constant between 0 and 1; 0.1 is typical for aluminum. (Again, e changes with temperature, but we'll ignore this.) HEATING: When energy flows into a square, the temperature rises. If E joules flow into a square, then the temperature rises by E/cM degrees, where c is the specific heat of the material (assume it is constant for all temperatures) and M is the mass. We can rewrite this as E/cpV, where p is the density and V is the volume (in this case, dL**2). (Normally, specific heat is expressed in terms of mass, and the unit is J/kg K, but we can multiply it by the density p to get it in terms of volume -- a "volumetric specific heat." Thus, the change in temperature becomes E/cV degrees. Aluminum has a specific heat of 896 J/kg K and a density of 2707 kg/m**3, so multiplying gives c = 2.43E6 J/m**3 K. Notice that conduction involves an interaction between a square and its neighbors, just like the Life game. This is why the Life program makes a good starting point for implementing the heat transfer simulation. The data and control structures used in Life can be modified to handle the heat simulation with only minor changes. Convection and radiation don't involve interactions between the squares, so they can be added without much trouble. MODIFICATIONS Therefore, you should study the Life program which you just copied into your directory. Understand how it works. Then modify it so it models the heat transfer problem. Here are the modifications needed: 1. Life asks you for an initial configuration. For the heat problem, you can assume the plate, including the part in contact with the hot component, starts out with every part uniformly at the ambient air temperature. (Assume that the device that generates heat is initially off, and is turned on at the beginning of the experiment.) Therefore, your program doesn't need to ask for the initial pattern. Instead, it should ask for the following: Physical constants: k (conduction constant - W/mK) h (convection constant - W/m**2 K) e (emissivity) c (volumetric specific heat - J/m**3 K) Simulation parameters: L (length of a square - m) t (duration of a time step - sec) Problem parameters: d (thickness of the plate - m) h (height of the plate; 1<=h<=40) w (width of the plate; 1<=w<=64) Ta (ambient air temperature - K) p (component power dissipation - W) x (horizontal position of component) y (vertical position of component) (HINT: make a copy of getInt and change to getReal.) (WARNING: You may be asked to enter some specific values. This is to check that you really have run YOUR program) h, w, x and y are expressed in terms of squares, i.e., integer multiples of L. The upper-left corner of the plate is numbered (0,0) and the lower-right corner is (y-1,x-1). 2. Life displays a 40x64 grid of squares, each either filled or empty. For the thermo program, we'd like to see information more detailed than just filled/not-filled. We'd like to see a map which visually shows the temperatures of the various regions, much like a weather map. The procedure drawBox() in the Life program selects the size of the dot it draws according to the third parameter, ranging from 0 (blank) to 9 (full). The Life program uses 0 for empty and 6 for filled, but you can modify the program so it uses all 10 values. 3. Since you can only use 10 "colors" to show a continuous temperature range, you have to use ranges, just like a weather map. The program should ask the user for a "resolution" -- how many degrees in each part of the range. For instance, if the ambient temperature is 300K and the resolution is 20K, then the value 0 is used for any temperature from 300K up to (but not including) 320K; 1 is used for anything from 320K up to 340K, etc. Anything too high (above 9) should be drawn using dot size 9. 4. Pay attention to what happens at the perimeter of the plate. The Life program just assumes that things fall off the edge. But notice that the arrays that hold the state of the Life game have an extra row on the top and the bottom, and an extra column on each side. These start out at 0 and are never changed, so they are always treated as empty squares. You don't want to do this for the heat problem, because then they would behave as if they were at Absolute Zero! You will need to check for when you are at the border, and avoid doing diffusion calculations with those squares outside the real plate. 5. You can make the following assumptions: A. Convection and radiation only happen on the faces of the plate. Ignore the edges. B. Both sides of the plate convect and radiate, so be sure to multiply the convection and radiation energies by 2. You can also do this for the square with the hot component. Assume the component will radiate/convect the same energy to the environment. REQUIREMENTS You need to send the program to HCA7. For output, this assignment is a special case, because the output is graphical, not text. Clearly identify the section of the C source code you have changed. Use something like the following lines to bracket your section. /************ Beginning of My Modifications ********************/ Your "new" program goes here. /************ End of My Modifications ********************/ You should test your program on different cases to convince yourself that it works. Then you should run it on the following case: using the values for k, h, c, and e for aluminum given above, simulate a 15cm * 30cm * 1mm aluminum plate in which L is 5mm (which means the plate is 30 squares by 60 squares). Assume Ta is 27C (300K). Use a time-step of 1 second. Assume a 40W device is near the middle (row 15, column 30). Run the simulation until it reaches equilibrium (wait until the picture stops changing, then wait a few more steps for the changes to decay to 0. Now what is the temperature of the upper-left corner (0,0)? (To get this, you should print out the value when you leave the loop. Note that main() has a printf at the end which prints out gencount; you can modify this to print one of the array values.) Include this output with your code when you send it to HCA7.