Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*===================================================*/
- /**************************************************************
- * NAME: John Jordan
- * ASSIGNMENT: 7
- * SECTION: 1
- *
- * Purpose:
- * The purpose of this program is to simulate heat transfer across
- * metal plates which are being modeled by a 2D array. The program
- * will transverse the entire 2D array. For each transversal, each
- * element will be visited and a new temperature will be computed
- * based on the original temperature in the following manner:
- * the average current temperature of an element will be taken
- * along with those of its four neighbors (north, south east
- * and west using the formula T(t+1) = (T(t) + north•H + east•H
- * + south•H + west•H) / (1 + 4•H). Edge and corner elements will
- * implement a different formula involving the ambient
- * temperatures which will be treated as neighbors outside of the
- * plate.
- *
- *
- * Program Input:
- * Two items will come from standard input 1) the name of the
- * input file and 2) the name of the output file. The remaining
- * input values will come from the input file. The input file
- * will include 7 values. These include the following:
- * 1) The initial plate temperature (reported as a double).
- * 2) Plate transfer coefficient H (double).
- * 3) Ambient temperature (double).
- * 4) Probe temperature (double).
- * 5) Probe Row index (integer).
- * 6) Probe column index (integer).
- * 7) Critical max change value at which simulation should
- * stop (double).
- *
- *
- * Program Output:
- * First, the input data will be reported to both the screen
- * and to the specific output file. The initial temperatures
- * will then be displayed to the screen. An updated plate
- * will then be displayed for each time step, including
- * iteration number of min and max temp. change for each step.
- * When the max or min change is found, both the change value
- * and the index of one of those cells with that value must be
- * printed (if more than one cell has this value, only cell needs
- * to be printed).
- *
- *
- *
- ***************************************************************/
- /*=============================================================*/
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #define MAX_STRING_LEN 80
- #define MAX_ROWS 8
- #define MAX_COLS 6
- #define MAX_FILENAME_LEN 128
- #define MAX_LINE_LEN 80
- void initialize_plate(double plate[][MAX_COLS], double temp);
- void print_plate(double plate[][MAX_COLS],
- int n_rows, int n_cols);
- void set_plate_cell(double plate[][MAX_COLS], int row, int col,
- double temp);
- void create_line(char line[], int n_cols);
- void update_plate (double plate1[MAX_ROWS][MAX_COLS], double ambien_temp,
- double trans_coefficient, double max_change, double probe_temp,
- int row, int col);
- /*=============================================================*/
- int main(void)
- {
- /*Plate Variables*/
- double plate[MAX_ROWS][MAX_COLS];
- /*Variables that are scanned from input file*/
- double initial_temp = 0;
- double transfer_coeff = 0;
- double ambient_temp = 0;
- double probe_temp = 0;
- int probe_r = 0;
- int probe_c = 0;
- double max_change_value = 0;
- /*Variables needed for the input and output file*/
- char filename1[MAX_FILENAME_LEN] = "";
- char outputfile[MAX_FILENAME_LEN] = "";
- FILE *infile1 = NULL;
- FILE *output_file = NULL;
- printf("Welcome to the Heat Plate Simulation Program\n");
- printf("\n");
- printf("The purpose of this program is to simulate heat\n");
- printf("transfer across a plate generated by a 2D array\n");
- printf("based upon given data provided an input file.\n");
- printf("Note: An output file is also generated.\n\n");
- /* Find out what files should be involved */
- scanf("%s", filename1);
- scanf("%s", outputfile);
- printf("Reading from %s, writing to %s",filename1,outputfile);
- printf("\n\n");
- /*Opens the specified file*/
- infile1 = fopen(filename1, "r");
- output_file = fopen(outputfile, "w");
- /*Scans the input file*/
- fscanf(infile1,"%lf%lf%lf%lf%d%d%lf", &initial_temp,
- &transfer_coeff, &ambient_temp, & probe_temp,
- &probe_r, &probe_c,
- &max_change_value);
- /*Prints the scanned values to the specific output file*/
- fprintf(output_file,
- "Plate Temperature: %.2f\n"
- "Heat Transfer Coefficient: %.2f\n"
- "Ambient Temperature: %.2f\n"
- "Probe temperature: %.2f\n"
- "Probe Location: [%d,%d]\n"
- "Critical max change: %.2f\n",
- initial_temp, transfer_coeff, ambient_temp, probe_temp,
- probe_c, probe_r, max_change_value);
- /*Prints the scanned values to the screen*/
- printf("Plate Temperature: %.2f\n", initial_temp);
- printf("Heat Transfer Coefficient: %.2f\n", transfer_coeff);
- printf("Ambient Temperature: %.2f\n", ambient_temp);
- printf("Probe temperature: %.2f\n", probe_temp);
- printf("Probe Location: [%d,%d]\n", probe_c, probe_r);
- printf("Critical max change: %.2f\n", max_change_value);
- printf("\n\n\n");
- /* Close the files */
- fclose(infile1);
- fclose(output_file);
- /*Initialize table*/
- initialize_plate(plate, initial_temp);
- set_plate_cell(plate, probe_r,probe_c, probe_temp);
- /*Print Initial Table*/
- printf("#### INITIAL PLATE ####\n");
- printf("\n");
- print_plate(plate, MAX_ROWS, MAX_COLS);
- printf("\n");
- /*Print Tables until max/min change value is reached*/
- update_plate (plate, ambient_temp,
- transfer_coeff, max_change_value, probe_temp,
- probe_r, probe_c);
- return 0;
- }
- /*=======================================================*/
- void initialize_plate(double plate[][MAX_COLS], double temp)
- {
- int i;
- int j;
- for (i=0; i <=7; i++)
- {
- for(j= 0; j <=7; j++)
- {
- plate [j][i] = temp;
- }
- }
- }
- void print_plate(double plate[][MAX_COLS], int n_rows, int n_cols)
- {
- int j = 0;
- int i = 0;
- char separator_line[MAX_STRING_LEN] = "";
- /* Create the line that goes between each row of values */
- create_line(separator_line, n_cols);
- /* Print the plate */
- printf("%s\n", separator_line);
- for (i=0; i < n_rows; i++)
- {
- for(j= 0; j < n_cols; j++)
- {
- printf("| %6.2f ", plate[i][j]);
- }
- printf("|");
- printf("\n");
- printf("%s\n", separator_line);
- }
- printf("\n");
- }
- /*=======================================================*/
- void set_plate_cell(double plate[][MAX_COLS], int row, int col,
- double temp)
- {
- plate[row][col] = temp;
- }
- /*=======================================================*/
- void create_line(char line[], int n_cols)
- {
- int i;
- char cell_portion[] = "+--------";
- /* Make sure the string starts out empty */
- line[0] = '\0';
- /* Keep adding cell_portion onto the line */
- for (i = 0; i < n_cols; i++)
- {
- strcat(line, cell_portion);
- }
- /* Add the final '+' */
- strcat(line, "+");
- }
- /*=======================================================*/
- void update_plate (double plate1[MAX_ROWS][MAX_COLS], double ambien_temp,
- double H_coef, double max_change, double probe_temp,
- int row, int col)
- {
- int z;
- int i;
- int j;
- double min;
- double max;
- double delta_temp;
- double new_temp;
- double old_temp;
- double plate2[MAX_ROWS][MAX_COLS];
- for (z=1; z<3; z++)
- {
- /*Set Probe Temp*/
- plate1 [row][col]= probe_temp;
- /*Calculation for edges*/
- plate2 [0][0]= (plate1[0][0] + H_coef*plate1[1][0]
- + H_coef*ambien_temp + H_coef*ambien_temp
- + H_coef*plate1[0][1] )/(1+ (4*H_coef));
- plate2 [0][5]= (plate1[0][5] + H_coef*plate1[0][4]
- + H_coef*ambien_temp + H_coef*ambien_temp
- + H_coef*plate1[1][5] )/(1+ (4*H_coef));
- plate2 [7][0]= (plate1[7][0] + H_coef*plate1[6][0]
- + H_coef*ambien_temp + H_coef*ambien_temp
- + H_coef*plate1[7][1] )/(1+ (4*H_coef));
- plate2 [7][5]= (plate1[7][5] + H_coef*plate1[7][4]
- + H_coef*ambien_temp + H_coef*ambien_temp
- + H_coef*plate1[6][5] )/(1+ (4*H_coef));
- /*Calculation for sides*/
- /*Left Side*/
- for (i=0; i <=(0); i++)
- {
- for(j= 1; j <=(6); j++)
- {
- plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
- + H_coef*plate1[j-1][i] + H_coef*ambien_temp
- + H_coef*plate1[j][i+1] )/(1+ (4*H_coef));
- }
- }
- /*Right Side*/
- for (i=5; i <=(5); i++)
- {
- for(j= 1; j <=(6); j++)
- {
- plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
- + H_coef*plate1[j-1][i] + H_coef*plate1[j][i-1]
- + H_coef*ambien_temp )/(1+ (4*H_coef));
- }
- }
- /*top side*/
- for (i=1; i <=(4); i++)
- {
- for(j= 0; j <=(0); j++)
- {
- plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
- + H_coef*plate1[j][i-1] + H_coef*plate1[j][i+1]
- + H_coef*ambien_temp )/(1+ (4*H_coef));
- }
- }
- /*bottom side*/
- for (i=1; i <=(4); i++)
- {
- for(j= 7; j <=(7); j++)
- {
- plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j-1][i]
- + H_coef*plate1[j][i+1] + H_coef*plate1[j][i-1]
- + H_coef*ambien_temp )/(1+ (4*H_coef));
- }
- }
- /*Calculations for "center" array elements*/
- for (i=1; i <=(4); i++)
- {
- for(j= 1; j <=(6); j++)
- {
- plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
- + H_coef*plate1[j-1][i] + H_coef*plate1[j][i-1]
- + H_coef*plate1[j][i+1] )/(1+ (4*H_coef));
- }
- }
- for (i = 0; i < 8; i++)
- {
- for (j = 0; j < 6; j++)
- plate1[i][j] = plate2[i][j];
- }
- plate1 [row][col]= probe_temp;
- print_plate(plate1, MAX_ROWS, MAX_COLS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement