Advertisement
Guest User

rewritten

a guest
Nov 25th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. /* Program to compute thermal equilibrium map of a heatbath bath has walls at T=0,
  2. two heat sources (T=10(50,50), T=7.2(40,60)) and a sink at T=-1.2(70,25). Bath can
  3. be approximated by 100x100 grid where each node's temperature evolves iteratively
  4. as the average of its 4 nearest neighbours and itself. Assume all undefined grid
  5. points to be T=0 on start. */
  6.  
  7. #include <stdio.h> // printf
  8. #include <stdlib.h> // calloc, free
  9. #include <math.h> // fabs
  10.  
  11. #define ARRAY_SIZE 100
  12. #define MAX_RELATIVE_ERROR 0.001 // 0.1%
  13.  
  14. /* Function prototypes */
  15. void setConstantCells(float **array, int size, int x, int y);
  16. void printArray(float **array, int size, int x, int y);
  17. void calcNewCellTemp(float **array, float **arrayTemp, int size, int x, int y);
  18. int equilibriumCheck(float **array, float **arrayTemp, int size, int x, int y);
  19. void updateTempArray(float **array, float **arrayTemp, int size, int x, int y);
  20. void printResult(float **array, int size, int i);
  21. void deallocateArrays(float **array, float **arrayTemp, int size, int i);
  22.  
  23. int main(int argc, char *argv[]){
  24.     /* Wide scope variables */
  25.     int x, y, i;
  26.     int success = 0;
  27.     /* Pointer setup and array allocation */
  28.     float **array, **arrayTemp; // pointer to pointer to ints
  29.     array = (float **)malloc(sizeof(float *) * (ARRAY_SIZE + 1)); // column 0
  30.     arrayTemp = (float **)malloc(sizeof(float *) * (ARRAY_SIZE + 1));
  31.     for(i = 0; i < ARRAY_SIZE + 1; i++){
  32.         array[i] = (float *)malloc(sizeof(float) * (ARRAY_SIZE + 1)); // generate rows
  33.         arrayTemp[i] = (float *)malloc(sizeof(float) * (ARRAY_SIZE + 1));
  34.     }
  35.  
  36.     /* Body of program */
  37.     setConstantCells(array, ARRAY_SIZE, x, y);
  38.     while(success != 1){
  39.         i++;
  40.         calcNewCellTemp(array, arrayTemp, ARRAY_SIZE, x, y);
  41.         setConstantCells(arrayTemp, ARRAY_SIZE, x, y);
  42.         success = equilibriumCheck(array, arrayTemp, ARRAY_SIZE, x, y);
  43.         updateTempArray(array, arrayTemp, ARRAY_SIZE, x, y);
  44.     }
  45.     printResult(array, ARRAY_SIZE, i);
  46.     deallocateArrays(array, arrayTemp, ARRAY_SIZE, i);
  47.     return 0;
  48. }
  49.  
  50. /* Resets the values which remain constant throughout */
  51. void setConstantCells(float **array, int size, int x, int y){
  52.     /* Set edges to 0 */
  53.     for(y = ARRAY_SIZE; y >= 0; y--){
  54.         array[0][y] = 0;
  55.         array[ARRAY_SIZE][y] = 0;
  56.     }
  57.     for(x = 0; x <= ARRAY_SIZE; x++){
  58.         array[x][0] = 0;
  59.         array[x][ARRAY_SIZE] = 0;
  60.     }
  61.  
  62.     /* Set heat sources / sinks */
  63.     array[40][60] = 7.2;
  64.     array[50][50] = 10;
  65.     array[70][25] = -1.2;
  66. }
  67.  
  68. /* Debugging print function */
  69. void printArray(float **array, int size, int x, int y){
  70.     for(y = ARRAY_SIZE; y >= 0; y--){
  71.         for(x = 0; x <= ARRAY_SIZE; x++){
  72.             //printf("(%d,%d) = %.2f\t", x, y, array[x][y]);
  73.             printf("%.2f\t", array[x][y]);
  74.         }
  75.         printf("\n");
  76.     }
  77.     printf("\n");
  78. }
  79.  
  80. /* Calculate temperature changes and assign to arrayTemp */
  81. void calcNewCellTemp(float **array, float **arrayTemp, int size, int x, int y){
  82.     int up, down, left, right;
  83.     for(y = ARRAY_SIZE; y >= 0; y--){
  84.         for(x = 0; x <= ARRAY_SIZE; x++){          
  85.             up = y + 1;
  86.             down = y - 1;
  87.             left = x - 1;
  88.             right = x + 1;
  89.             if(up > ARRAY_SIZE)
  90.                 up = ARRAY_SIZE;
  91.             if(down < 0)
  92.                 down = 0;
  93.             if(left < 0)
  94.                 left = 0;
  95.             if(right > ARRAY_SIZE)
  96.                 right = ARRAY_SIZE;
  97.             arrayTemp[x][y] = (array[x][y] + array[left][y] + array[right][y] + array[x][down] + array[x][up]) / 5;
  98.         }
  99.     }
  100. }
  101.  
  102. /* Check for thermal equilibrium */
  103. int equilibriumCheck(float **array, float **arrayTemp, int size, int x, int y){
  104.     float relativeError;
  105.     int totalThermalEquilibrium = 1;
  106.     for(y = ARRAY_SIZE; y >= 0; y--){
  107.         for(x = 0; x <= ARRAY_SIZE; x++){              
  108.             relativeError = fabs(array[x][y] - arrayTemp[x][y]) / arrayTemp[x][y];
  109.             if(relativeError > MAX_RELATIVE_ERROR)
  110.                 totalThermalEquilibrium = 0;
  111.         }
  112.     }
  113.     return totalThermalEquilibrium;
  114. }
  115.  
  116. /* Set array to updated values */
  117. void updateTempArray(float **array, float **arrayTemp, int size, int x, int y){
  118.     for(y = ARRAY_SIZE; y >= 0; y--){
  119.         for(x = 0; x <= ARRAY_SIZE; x++){
  120.             array[x][y] = arrayTemp[x][y];
  121.         }
  122.     }      
  123. }
  124.  
  125. void printResult(float **array, int size, int i){
  126.     float percentageAccuracy = MAX_RELATIVE_ERROR * 100;
  127.     printf("Finished at iteration #: %d\n", i);
  128.     printf("Thermal equilibrium attainted, with maximum %g%% change between iterations for any points\n", percentageAccuracy);
  129.     printf("Temperature @ (55,55) = %.2f\n", array[55][55]);
  130. }
  131.  
  132. void deallocateArrays(float **array, float **arrayTemp, int size, int i){
  133.     for(i = 0; i < ARRAY_SIZE + 1; i++){
  134.         free(array[i]);
  135.         free(arrayTemp[i]);
  136.     }
  137.     free(array);
  138.     free(arrayTemp);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement