Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. /*===================================================*/
  2.  
  3. /**************************************************************
  4. * NAME: John Jordan
  5. * ASSIGNMENT: 7
  6. * SECTION: 1
  7. *
  8. * Purpose:
  9. * The purpose of this program is to simulate heat transfer across
  10. * metal plates which are being modeled by a 2D array. The program
  11. * will transverse the entire 2D array. For each transversal, each
  12. * element will be visited and a new temperature will be computed
  13. * based on the original temperature in the following manner:
  14. * the average current temperature of an element will be taken
  15. * along with those of its four neighbors (north, south east
  16. * and west using the formula T(t+1) = (T(t) + north•H + east•H
  17. * + south•H + west•H) / (1 + 4•H). Edge and corner elements will
  18. * implement a different formula involving the ambient
  19. * temperatures which will be treated as neighbors outside of the
  20. * plate.
  21. *
  22. *
  23. * Program Input:
  24. * Two items will come from standard input 1) the name of the
  25. * input file and 2) the name of the output file. The remaining
  26. * input values will come from the input file. The input file
  27. * will include 7 values. These include the following:
  28. * 1) The initial plate temperature (reported as a double).
  29. * 2) Plate transfer coefficient H (double).
  30. * 3) Ambient temperature (double).
  31. * 4) Probe temperature (double).
  32. * 5) Probe Row index (integer).
  33. * 6) Probe column index (integer).
  34. * 7) Critical max change value at which simulation should
  35. * stop (double).
  36. *
  37. *
  38. * Program Output:
  39. * First, the input data will be reported to both the screen
  40. * and to the specific output file. The initial temperatures
  41. * will then be displayed to the screen. An updated plate
  42. * will then be displayed for each time step, including
  43. * iteration number of min and max temp. change for each step.
  44. * When the max or min change is found, both the change value
  45. * and the index of one of those cells with that value must be
  46. * printed (if more than one cell has this value, only cell needs
  47. * to be printed).
  48. *
  49. *
  50. *
  51. ***************************************************************/
  52.  
  53. /*=============================================================*/
  54. #include <stdio.h>
  55. #include <math.h>
  56. #include <string.h>
  57. #define MAX_STRING_LEN 80
  58. #define MAX_ROWS 8
  59. #define MAX_COLS 6
  60. #define MAX_FILENAME_LEN 128
  61. #define MAX_LINE_LEN 80
  62.  
  63. void initialize_plate(double plate[][MAX_COLS], double temp);
  64. void print_plate(double plate[][MAX_COLS],
  65. int n_rows, int n_cols);
  66. void set_plate_cell(double plate[][MAX_COLS], int row, int col,
  67. double temp);
  68. void create_line(char line[], int n_cols);
  69. void update_plate (double plate1[MAX_ROWS][MAX_COLS], double ambien_temp,
  70. double trans_coefficient, double max_change, double probe_temp,
  71. int row, int col);
  72.  
  73.  
  74. /*=============================================================*/
  75. int main(void)
  76. {
  77.  
  78. /*Plate Variables*/
  79. double plate[MAX_ROWS][MAX_COLS];
  80.  
  81. /*Variables that are scanned from input file*/
  82. double initial_temp = 0;
  83. double transfer_coeff = 0;
  84. double ambient_temp = 0;
  85. double probe_temp = 0;
  86. int probe_r = 0;
  87. int probe_c = 0;
  88. double max_change_value = 0;
  89.  
  90. /*Variables needed for the input and output file*/
  91. char filename1[MAX_FILENAME_LEN] = "";
  92. char outputfile[MAX_FILENAME_LEN] = "";
  93. FILE *infile1 = NULL;
  94. FILE *output_file = NULL;
  95.  
  96. printf("Welcome to the Heat Plate Simulation Program\n");
  97. printf("\n");
  98. printf("The purpose of this program is to simulate heat\n");
  99. printf("transfer across a plate generated by a 2D array\n");
  100. printf("based upon given data provided an input file.\n");
  101. printf("Note: An output file is also generated.\n\n");
  102.  
  103. /* Find out what files should be involved */
  104. scanf("%s", filename1);
  105. scanf("%s", outputfile);
  106.  
  107. printf("Reading from %s, writing to %s",filename1,outputfile);
  108. printf("\n\n");
  109.  
  110. /*Opens the specified file*/
  111. infile1 = fopen(filename1, "r");
  112. output_file = fopen(outputfile, "w");
  113.  
  114. /*Scans the input file*/
  115. fscanf(infile1,"%lf%lf%lf%lf%d%d%lf", &initial_temp,
  116. &transfer_coeff, &ambient_temp, & probe_temp,
  117. &probe_r, &probe_c,
  118. &max_change_value);
  119.  
  120. /*Prints the scanned values to the specific output file*/
  121. fprintf(output_file,
  122. "Plate Temperature: %.2f\n"
  123. "Heat Transfer Coefficient: %.2f\n"
  124. "Ambient Temperature: %.2f\n"
  125. "Probe temperature: %.2f\n"
  126. "Probe Location: [%d,%d]\n"
  127. "Critical max change: %.2f\n",
  128. initial_temp, transfer_coeff, ambient_temp, probe_temp,
  129. probe_c, probe_r, max_change_value);
  130.  
  131.  
  132. /*Prints the scanned values to the screen*/
  133. printf("Plate Temperature: %.2f\n", initial_temp);
  134. printf("Heat Transfer Coefficient: %.2f\n", transfer_coeff);
  135. printf("Ambient Temperature: %.2f\n", ambient_temp);
  136. printf("Probe temperature: %.2f\n", probe_temp);
  137. printf("Probe Location: [%d,%d]\n", probe_c, probe_r);
  138. printf("Critical max change: %.2f\n", max_change_value);
  139. printf("\n\n\n");
  140.  
  141.  
  142.  
  143.  
  144. /* Close the files */
  145. fclose(infile1);
  146. fclose(output_file);
  147.  
  148. /*Initialize table*/
  149. initialize_plate(plate, initial_temp);
  150. set_plate_cell(plate, probe_r,probe_c, probe_temp);
  151.  
  152. /*Print Initial Table*/
  153. printf("#### INITIAL PLATE ####\n");
  154. printf("\n");
  155. print_plate(plate, MAX_ROWS, MAX_COLS);
  156. printf("\n");
  157.  
  158. /*Print Tables until max/min change value is reached*/
  159. update_plate (plate, ambient_temp,
  160. transfer_coeff, max_change_value, probe_temp,
  161. probe_r, probe_c);
  162.  
  163.  
  164.  
  165. return 0;
  166. }
  167.  
  168. /*=======================================================*/
  169.  
  170. void initialize_plate(double plate[][MAX_COLS], double temp)
  171.  
  172. {
  173. int i;
  174. int j;
  175.  
  176. for (i=0; i <=7; i++)
  177. {
  178.  
  179. for(j= 0; j <=7; j++)
  180. {
  181. plate [j][i] = temp;
  182.  
  183. }
  184. }
  185.  
  186. }
  187.  
  188.  
  189. void print_plate(double plate[][MAX_COLS], int n_rows, int n_cols)
  190. {
  191. int j = 0;
  192. int i = 0;
  193. char separator_line[MAX_STRING_LEN] = "";
  194.  
  195. /* Create the line that goes between each row of values */
  196. create_line(separator_line, n_cols);
  197.  
  198. /* Print the plate */
  199. printf("%s\n", separator_line);
  200.  
  201.  
  202. for (i=0; i < n_rows; i++)
  203. {
  204.  
  205. for(j= 0; j < n_cols; j++)
  206. {
  207. printf("| %6.2f ", plate[i][j]);
  208. }
  209. printf("|");
  210. printf("\n");
  211. printf("%s\n", separator_line);
  212.  
  213. }
  214. printf("\n");
  215. }
  216.  
  217. /*=======================================================*/
  218. void set_plate_cell(double plate[][MAX_COLS], int row, int col,
  219. double temp)
  220. {
  221. plate[row][col] = temp;
  222. }
  223. /*=======================================================*/
  224. void create_line(char line[], int n_cols)
  225. {
  226. int i;
  227. char cell_portion[] = "+--------";
  228.  
  229. /* Make sure the string starts out empty */
  230. line[0] = '\0';
  231.  
  232. /* Keep adding cell_portion onto the line */
  233. for (i = 0; i < n_cols; i++)
  234. {
  235. strcat(line, cell_portion);
  236. }
  237.  
  238. /* Add the final '+' */
  239. strcat(line, "+");
  240. }
  241.  
  242. /*=======================================================*/
  243.  
  244. void update_plate (double plate1[MAX_ROWS][MAX_COLS], double ambien_temp,
  245. double H_coef, double max_change, double probe_temp,
  246. int row, int col)
  247. {
  248. int z;
  249. int i;
  250. int j;
  251. double min;
  252. double max;
  253. double delta_temp;
  254. double new_temp;
  255. double old_temp;
  256. double plate2[MAX_ROWS][MAX_COLS];
  257.  
  258.  
  259.  
  260. for (z=1; z<3; z++)
  261.  
  262. {
  263.  
  264. /*Set Probe Temp*/
  265. plate1 [row][col]= probe_temp;
  266.  
  267. /*Calculation for edges*/
  268. plate2 [0][0]= (plate1[0][0] + H_coef*plate1[1][0]
  269. + H_coef*ambien_temp + H_coef*ambien_temp
  270. + H_coef*plate1[0][1] )/(1+ (4*H_coef));
  271. plate2 [0][5]= (plate1[0][5] + H_coef*plate1[0][4]
  272. + H_coef*ambien_temp + H_coef*ambien_temp
  273. + H_coef*plate1[1][5] )/(1+ (4*H_coef));
  274. plate2 [7][0]= (plate1[7][0] + H_coef*plate1[6][0]
  275. + H_coef*ambien_temp + H_coef*ambien_temp
  276. + H_coef*plate1[7][1] )/(1+ (4*H_coef));
  277. plate2 [7][5]= (plate1[7][5] + H_coef*plate1[7][4]
  278. + H_coef*ambien_temp + H_coef*ambien_temp
  279. + H_coef*plate1[6][5] )/(1+ (4*H_coef));
  280.  
  281.  
  282. /*Calculation for sides*/
  283.  
  284.  
  285. /*Left Side*/
  286. for (i=0; i <=(0); i++)
  287. {
  288.  
  289. for(j= 1; j <=(6); j++)
  290. {
  291. plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
  292. + H_coef*plate1[j-1][i] + H_coef*ambien_temp
  293. + H_coef*plate1[j][i+1] )/(1+ (4*H_coef));
  294.  
  295. }
  296. }
  297.  
  298.  
  299. /*Right Side*/
  300. for (i=5; i <=(5); i++)
  301. {
  302.  
  303. for(j= 1; j <=(6); j++)
  304. {
  305. plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
  306. + H_coef*plate1[j-1][i] + H_coef*plate1[j][i-1]
  307. + H_coef*ambien_temp )/(1+ (4*H_coef));
  308.  
  309. }
  310. }
  311.  
  312. /*top side*/
  313. for (i=1; i <=(4); i++)
  314. {
  315.  
  316. for(j= 0; j <=(0); j++)
  317. {
  318. plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
  319. + H_coef*plate1[j][i-1] + H_coef*plate1[j][i+1]
  320. + H_coef*ambien_temp )/(1+ (4*H_coef));
  321.  
  322. }
  323. }
  324.  
  325.  
  326. /*bottom side*/
  327. for (i=1; i <=(4); i++)
  328. {
  329.  
  330. for(j= 7; j <=(7); j++)
  331. {
  332. plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j-1][i]
  333. + H_coef*plate1[j][i+1] + H_coef*plate1[j][i-1]
  334. + H_coef*ambien_temp )/(1+ (4*H_coef));
  335.  
  336. }
  337. }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344. /*Calculations for "center" array elements*/
  345. for (i=1; i <=(4); i++)
  346. {
  347.  
  348. for(j= 1; j <=(6); j++)
  349. {
  350. plate2 [j][i]= (plate1[j][i] + H_coef*plate1[j+1][i]
  351. + H_coef*plate1[j-1][i] + H_coef*plate1[j][i-1]
  352. + H_coef*plate1[j][i+1] )/(1+ (4*H_coef));
  353.  
  354. }
  355. }
  356.  
  357. for (i = 0; i < 8; i++)
  358. {
  359. for (j = 0; j < 6; j++)
  360. plate1[i][j] = plate2[i][j];
  361. }
  362. plate1 [row][col]= probe_temp;
  363.  
  364.  
  365.  
  366. print_plate(plate1, MAX_ROWS, MAX_COLS);
  367.  
  368. }
  369.  
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement