Lab 2 Part 2: Ouch! Hot Plate!
Overview
This question is to simulate the above scenario.
Getting Started
As in part 1 of this lab, the iron plate can be simulated by a 8 x 8 2-dimensional float array. To simulate the flow of heat, we recalculate the new temperature for all the points on the plate every time step. Since the temperature of a point is influence by the heat of its neighbors (left, right, above and below), we define the following:
Heat (X, Y) = { Heat (X-1, Y) + Heat (X+1, Y) + Heat (X, Y-1) + Heat (X, Y+1) } / 4
where Heat(X,Y) means Temperature at row X, Column Y.
Time Step 0:
0 |
1 |
2 |
3 | |
0 |
10.00 |
10.00 |
10.00 | 10.00 |
1 |
0.00 |
0.00 |
0.00 |
0.00 |
2 | 0.00 | 0.00 |
0.00 |
0.00 |
3 | 90.00 | 90.00 | 90.00 | 90.00 |
Time Step 1:
0 |
1 |
2 |
3 | |
0 |
10.00 |
10.00 |
10.00 | 10.00 |
1 |
3.33 |
2.50 |
2.50 |
3.33 |
2 | 30.00 | 22.50 |
22.50 |
30.00 |
3 | 90.00 | 90.00 | 90.00 | 90.00 |
Heat at point (1, 1) = ( 10.00 + 0.00 + 0.00 + 0.00 ) / 4
= 2.50
Notice that points on the edge (i.e. points on first or last column) are calculated slightly differently:
Heat at point (2, 0) = (0.00 + 0.00 + 90.00) / 3
= 30.00
From this one step example, we can see that:
1. Points on edge are special cases which needs special handling. All other points simply follows the formula given.
2. The heat of the upper and lower edge does not change (i.e. no recalculation needed), to simulate the constant heating.
3. This is more subtle: a temporary plate of the same size is needed to store
the new temperature. For all points, we need the
current temperature of its neighbors to calculate its
temperature for the next time step. If we simply store the updated
temperature using the same plate, the calculation for later
points would be wrong.
e.g. If we store the value 2.50 into point (1, 1) right
after the calculation, then
point (1, 2) = ( 10.00 + 2.50 + 0.00 + 0.00) / 4
= 3.12
Hence, we need to store all the new
temperature into a temporary plate. Only after all points are calculated, then
we can update
the temperature. This consideration is reflected in the
design of the functions below.
Stopping condition
From the last section, you can see that the temperature can be recalculated repeatedly, time step after time step. However, it is obvious that after a certain number of time steps, the temperature of all the points would remain relatively stable. To use this phenomena as a stopping condition to the simulation, we define that Temperature Difference (TD)
TD(X, Y) = The Absolute difference between current and next time step temperature at point (X, Y)
For example:
TD(1, 1) = | 0.00 - 2.50 | = 2.50
TD(2, 1) = | 0.00 - 22.50 | = 22.50
between time step 0 and time step 1.
Then we can define the Maximum Temperature Difference (MTD) as the Maximum TD for all the points. You can see that this MTD indicates the stability of the temperature of the whole plate, where lower MTD indicates smaller fluctuation in temperature. So, when the MTD is smaller than a user defined value (a.k.a "Temperature Difference Threshold", TDT ) , the simulation can be stopped.
Bits and Pieces
Implements the following functions which would help you in defining the
final program. The purpose, input and output of the functions are given to help
you. You do not need to follow the skeleton program if you think you
have a better program design.
Note that a few of these functions have a close counterparts in Lab 2 Part 1
where you can just copy and modify a little.
We assume that following constant declaration (the size of the plate) in the
following description:
#define SIZE 8
Remember that you should use the constant "SIZE"
whenever necessary so that the solution is general enough to solve the same
problem for plate of any size.
void InitRow(float Plate[][SIZE], int RowNumber,
float initTemp)
Purpose: Init all points in row RowNumber
to temperature InitTemp
Input: Plate (the 2d
array that represents the iron plate)
RowNumber (indicate the row to initialize)
initTemp (indicate the initial temperature for that row)
Output: None. See remark.
Remark: Since the plate
is directly manipulated/changed by this
function, no return type is required.
void PrintPlate(float Plate[][SIZE])
Purpose: Print the Plate using the
following format
0 1 2 ..... 7
0 2.50
1
2
...
7
Each column is separated by a tab. The temperature is printed
with 2 places of precision.
Input:
Plate (the 2d array that represents the iron plate)
Output: None. Directly display on
screen.
float PropagateHeat(float
current[][SIZE],float next[][SIZE])
Purpose: Calculate the new temperature
for all the points. As new temperature
is calculated, the maximum temperature difference among all the points
is kept and return at the end of function.
Input: current (the 2d
array that represents the iron plate)
next (the temporary 2d array to keep the updated temperature)
Output: Return the Maximum
Temperature Difference
Remark: As mentioned in section
"Getting Started":
1. Points on edge are special cases which needs special handling.
All other points simply follows the formula given.
2. The heat of the upper and lower edge does not change
3. The new temperatures are stored in the temporary plate next
void CopyPlate(float
destination[][SIZE],float source[][SIZE])
Purpose: Copy every values (i.e.
temperatures) in source over to destination.
Input: destination (the
2d array that represents the iron plate)
source (the 2d array that represents the iron plate)
Output: None. The plate
destination is changed directly.
Remark: Since arrays, unlike basic
datatype variable, cannot get values by simple
assignment, this function helps to moves all the value in one plate to another.
Putting it together
With the help of the functions above, you can now design the hot plate main function. The basic steps is given as follows:
Basic Steps:
1. Get the upper edge initial temperature, UE from user.
2. Get the lower edge initial temperature, LE from user.
3. Get the temperature difference threshold, TDT from
user.
4. Initialize upper edge to
UE
5. Initialize lower edge
to LE
6. Initialize all
other rows to 0 degree.
7. Print the Initial Plate.
8. Calculate new temperature and get
the MTD.
9. Print the MTD and the
updated Plate.
10. If MTD is larger than TDT repeat step 8.
Skeleton Program
#include <stdio.h>
#define SIZE 8
void InitRow(float[][SIZE],int,float);
void CopyPlate(float[][SIZE],float[][SIZE]);
float PropagateHeat(float[][SIZE],float[][SIZE]);
void PrintPlate(float[][SIZE]);
main()
{
// A few basic declarations
float CurrentPlate[SIZE][SIZE],NextPlate[SIZE][SIZE];
float tempDiffThreshold;
int timeStep = 1;
// Get User Input
// Initialization
// Main Loop
}
void InitRow(float plate[][SIZE],int row, float temp)
{
}
void CopyPlate(float dest[][SIZE],float src[][SIZE])
{
}
float PropagateHeat(float current[][SIZE],float next[][SIZE])
{
}
void PrintPlate(float plate[][SIZE])
{
}
Sample Output 1
Note: User Input is in bold.
Initialize Upper Edge to: 10.0 Initialize Lower Edge to: 90.0 Temperature Difference Threshold: 25.0 Initial Temperature: 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Temperature At Time Step 1 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 3.33 2.50 2.50 2.50 2.50 2.50 2.50 3.33 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 6 30.00 22.50 22.50 22.50 22.50 22.50 22.50 30.00 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 30.00 Temperature At Time Step 2 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 4.17 3.96 3.75 3.75 3.75 3.75 3.96 4.17 2 1.11 0.62 0.62 0.62 0.62 0.62 0.62 1.11 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 10.00 5.62 5.62 5.62 5.62 5.62 5.62 10.00 6 37.50 35.62 33.75 33.75 33.75 33.75 35.62 37.50 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 13.12Sample Output 2
Initialize Upper Edge to: 10.0 Initialize Lower Edge to: 90.0 Temperature Difference Threshold: 0.5 Initial Temperature: 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Temperature At Time Step 1 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 3.33 2.50 2.50 2.50 2.50 2.50 2.50 3.33 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 6 30.00 22.50 22.50 22.50 22.50 22.50 22.50 30.00 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 30.00 Temperature At Time Step 2 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 4.17 3.96 3.75 3.75 3.75 3.75 3.96 4.17 2 1.11 0.62 0.62 0.62 0.62 0.62 0.62 1.11 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5 10.00 5.62 5.62 5.62 5.62 5.62 5.62 10.00 6 37.50 35.62 33.75 33.75 33.75 33.75 35.62 37.50 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 13.12 [....... other time steps not shown ......] Temperature At Time Step 36 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 17.75 17.60 17.50 17.45 17.45 17.50 17.60 17.75 2 26.22 25.96 25.77 25.69 25.69 25.77 25.96 26.22 3 36.01 35.67 35.46 35.34 35.34 35.46 35.67 36.01 4 47.42 47.10 46.87 46.76 46.76 46.87 47.10 47.42 5 60.50 60.22 60.05 59.96 59.96 60.05 60.22 60.50 6 74.88 74.73 74.63 74.58 74.58 74.63 74.73 74.88 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 0.50 Temperature At Time Step 37 *************************** 0 1 2 3 4 5 6 7 0 10.00 10.00 10.00 10.00 10.00 10.00 10.00 10.00 1 17.94 17.80 17.71 17.66 17.66 17.71 17.80 17.94 2 26.58 26.31 26.15 26.06 26.06 26.15 26.31 26.58 3 36.44 36.13 35.91 35.81 35.81 35.91 36.13 36.44 4 47.87 47.54 47.34 47.23 47.23 47.34 47.54 47.87 5 60.84 60.60 60.42 60.34 60.34 60.42 60.60 60.84 6 75.08 74.93 74.84 74.79 74.79 74.84 74.93 75.08 7 90.00 90.00 90.00 90.00 90.00 90.00 90.00 90.00 Maximum Difference Detected: 0.47