Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program to compute thermal equilibrium map of a heatbath bath has walls at T=0,
- two heat sources (T=10(50,50), T=7.2(40,60)) and a sink at T=-1.2(70,25). Bath can
- be approximated by 100x100 grid where each node's temperature evolves iteratively
- as the average of its 4 nearest neighbours and itself. Assume all undefined grid
- points to be T=0 on start. */
- #include <stdio.h> // printf
- #include <stdlib.h> // calloc, free
- #include <math.h> // fabs
- #define ARRAY_SIZE 100
- #define MAX_RELATIVE_ERROR 0.001 // 0.1%
- /* Function prototypes */
- void setConstantCells(float **array, int size, int x, int y);
- void printArray(float **array, int size, int x, int y);
- void calcNewCellTemp(float **array, float **arrayTemp, int size, int x, int y);
- int equilibriumCheck(float **array, float **arrayTemp, int size, int x, int y);
- void updateTempArray(float **array, float **arrayTemp, int size, int x, int y);
- void printResult(float **array, int size, int i);
- void deallocateArrays(float **array, float **arrayTemp, int size, int i);
- int main(int argc, char *argv[]){
- /* Wide scope variables */
- int x, y, i;
- int success = 0;
- /* Pointer setup and array allocation */
- float **array, **arrayTemp; // pointer to pointer to ints
- array = (float **)malloc(sizeof(float *) * (ARRAY_SIZE + 1)); // column 0
- arrayTemp = (float **)malloc(sizeof(float *) * (ARRAY_SIZE + 1));
- for(i = 0; i < ARRAY_SIZE + 1; i++){
- array[i] = (float *)malloc(sizeof(float) * (ARRAY_SIZE + 1)); // generate rows
- arrayTemp[i] = (float *)malloc(sizeof(float) * (ARRAY_SIZE + 1));
- }
- /* Body of program */
- setConstantCells(array, ARRAY_SIZE, x, y);
- while(success != 1){
- i++;
- calcNewCellTemp(array, arrayTemp, ARRAY_SIZE, x, y);
- setConstantCells(arrayTemp, ARRAY_SIZE, x, y);
- success = equilibriumCheck(array, arrayTemp, ARRAY_SIZE, x, y);
- updateTempArray(array, arrayTemp, ARRAY_SIZE, x, y);
- }
- printResult(array, ARRAY_SIZE, i);
- deallocateArrays(array, arrayTemp, ARRAY_SIZE, i);
- return 0;
- }
- /* Resets the values which remain constant throughout */
- void setConstantCells(float **array, int size, int x, int y){
- /* Set edges to 0 */
- for(y = ARRAY_SIZE; y >= 0; y--){
- array[0][y] = 0;
- array[ARRAY_SIZE][y] = 0;
- }
- for(x = 0; x <= ARRAY_SIZE; x++){
- array[x][0] = 0;
- array[x][ARRAY_SIZE] = 0;
- }
- /* Set heat sources / sinks */
- array[40][60] = 7.2;
- array[50][50] = 10;
- array[70][25] = -1.2;
- }
- /* Debugging print function */
- void printArray(float **array, int size, int x, int y){
- for(y = ARRAY_SIZE; y >= 0; y--){
- for(x = 0; x <= ARRAY_SIZE; x++){
- //printf("(%d,%d) = %.2f\t", x, y, array[x][y]);
- printf("%.2f\t", array[x][y]);
- }
- printf("\n");
- }
- printf("\n");
- }
- /* Calculate temperature changes and assign to arrayTemp */
- void calcNewCellTemp(float **array, float **arrayTemp, int size, int x, int y){
- int up, down, left, right;
- for(y = ARRAY_SIZE; y >= 0; y--){
- for(x = 0; x <= ARRAY_SIZE; x++){
- up = y + 1;
- down = y - 1;
- left = x - 1;
- right = x + 1;
- if(up > ARRAY_SIZE)
- up = ARRAY_SIZE;
- if(down < 0)
- down = 0;
- if(left < 0)
- left = 0;
- if(right > ARRAY_SIZE)
- right = ARRAY_SIZE;
- arrayTemp[x][y] = (array[x][y] + array[left][y] + array[right][y] + array[x][down] + array[x][up]) / 5;
- }
- }
- }
- /* Check for thermal equilibrium */
- int equilibriumCheck(float **array, float **arrayTemp, int size, int x, int y){
- float relativeError;
- int totalThermalEquilibrium = 1;
- for(y = ARRAY_SIZE; y >= 0; y--){
- for(x = 0; x <= ARRAY_SIZE; x++){
- relativeError = fabs(array[x][y] - arrayTemp[x][y]) / arrayTemp[x][y];
- if(relativeError > MAX_RELATIVE_ERROR)
- totalThermalEquilibrium = 0;
- }
- }
- return totalThermalEquilibrium;
- }
- /* Set array to updated values */
- void updateTempArray(float **array, float **arrayTemp, int size, int x, int y){
- for(y = ARRAY_SIZE; y >= 0; y--){
- for(x = 0; x <= ARRAY_SIZE; x++){
- array[x][y] = arrayTemp[x][y];
- }
- }
- }
- void printResult(float **array, int size, int i){
- float percentageAccuracy = MAX_RELATIVE_ERROR * 100;
- printf("Finished at iteration #: %d\n", i);
- printf("Thermal equilibrium attainted, with maximum %g%% change between iterations for any points\n", percentageAccuracy);
- printf("Temperature @ (55,55) = %.2f\n", array[55][55]);
- }
- void deallocateArrays(float **array, float **arrayTemp, int size, int i){
- for(i = 0; i < ARRAY_SIZE + 1; i++){
- free(array[i]);
- free(arrayTemp[i]);
- }
- free(array);
- free(arrayTemp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement