Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 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. int probe_row, int probe_col);
  67. void set_plate_cell(double plate[][MAX_COLS], int row, int col,
  68. double temp);
  69. void create_line(char line[], int n_cols);
  70. void update_plate (double plate1[MAX_ROWS][MAX_COLS],
  71. double ambien_temp,double trans_coefficient,
  72. double max_change, double probe_temp,
  73. int row, int col);
  74.  
  75.  
  76.  
  77. /*Define this globally because it will be used in update_plate*/
  78. FILE *output_file = NULL;
  79.  
  80. /*=============================================================*/
  81. int main(void)
  82. {
  83.  
  84. /*Plate Variables*/
  85. double plate[MAX_ROWS][MAX_COLS];
  86.  
  87. /*Variables that are scanned from input file*/
  88. double initial_temp = 0;
  89. double transfer_coeff = 0;
  90. double ambient_temp = 0;
  91. double probe_temp = 0;
  92. int probe_r = 0;
  93. int probe_c = 0;
  94. double max_change_value = 0;
  95.  
  96. /*Variables needed for the input and output file*/
  97. char filename1[MAX_FILENAME_LEN] = "";
  98. char outputfile[MAX_FILENAME_LEN] = "";
  99. FILE *infile1 = NULL;
  100.  
  101. printf("Welcome to the Heat Plate Simulation Program\n");
  102. printf("\n");
  103. printf("The purpose of this program is to simulate heat\n");
  104. printf("transfer across a plate generated by a 2D array\n");
  105. printf("based upon given data provided an input file.\n");
  106. printf("Note: An output file is also generated.\n\n");
  107.  
  108. /* Find out what files should be involved */
  109. scanf("%s", filename1);
  110. scanf("%s", outputfile);
  111.  
  112. printf("Reading from %s, writing to %s",filename1,outputfile);
  113. printf("\n\n");
  114.  
  115. /*Opens the specified file*/
  116. infile1 = fopen(filename1, "r");
  117. output_file = fopen(outputfile, "w");
  118.  
  119. /*Scans the input file*/
  120. fscanf(infile1,"%lf%lf%lf%lf%d%d%lf", &initial_temp,
  121. &transfer_coeff, &ambient_temp, & probe_temp,
  122. &probe_r, &probe_c,
  123. &max_change_value);
  124.  
  125. /*Prints the scanned values to the specific output file*/
  126. fprintf(output_file,
  127. "Plate Temperature: %.2f\n"
  128. "Heat Transfer Coefficient: %.2f\n"
  129. "Ambient Temperature: %.2f\n"
  130. "Probe temperature: %.2f\n"
  131. "Probe Location: [%d,%d]\n"
  132. "Critical max change: %.2f\n",
  133. initial_temp, transfer_coeff, ambient_temp, probe_temp,
  134. probe_c, probe_r, max_change_value);
  135.  
  136.  
  137. /*Prints the scanned values to the screen*/
  138. printf("Plate Temperature: %.2f\n",
  139. initial_temp);
  140. printf("Heat Transfer Coefficient: %.2f\n",
  141. transfer_coeff);
  142. printf("Ambient Temperature: %.2f\n",
  143. ambient_temp);
  144. printf("Probe temperature: %.2f\n",
  145. probe_temp);
  146. printf("Probe Location: [%d,%d]\n",
  147. probe_c, probe_r);
  148. printf("Critical max change: %.2f\n",
  149. max_change_value);
  150. printf("\n\n\n");
  151.  
  152.  
  153.  
  154.  
  155. /* Close the files */
  156. fclose(infile1);
  157.  
  158. /*Initialize table*/
  159. initialize_plate(plate, initial_temp);
  160. set_plate_cell(plate, probe_r,probe_c, probe_temp);
  161.  
  162. /*Print Initial Table*/
  163. printf("#### INITIAL PLATE ####\n");
  164. printf("\n");
  165. print_plate(plate, MAX_ROWS, MAX_COLS,probe_r, probe_c);
  166. printf("\n");
  167.  
  168. /*Print Tables until max/min change value is reached*/
  169. update_plate (plate, ambient_temp,
  170. transfer_coeff, max_change_value, probe_temp,
  171. probe_r, probe_c);
  172.  
  173.  
  174. /*Closes output file*/
  175. fclose(output_file);
  176.  
  177. return 0;
  178. }
  179.  
  180. /*=======================================================*/
  181.  
  182. void initialize_plate(double plate[][MAX_COLS], double temp)
  183.  
  184. {
  185. int i;
  186. int j;
  187.  
  188. for (i=0; i <=7; i++)
  189. {
  190.  
  191. for(j= 0; j <6; j++)
  192. {
  193. plate [j][i] = temp;
  194.  
  195. }
  196. }
  197.  
  198. }
  199.  
  200.  
  201. void print_plate(double plate[][MAX_COLS], int n_rows, int n_cols,
  202. int probe_row, int probe_col)
  203. {
  204. int j = 0;
  205. int i = 0;
  206. char separator_line[MAX_STRING_LEN] = "";
  207.  
  208. /* Create the line that goes between each row of values */
  209. create_line(separator_line, n_cols);
  210.  
  211. /* Print the plate */
  212. printf("%s\n", separator_line);
  213.  
  214.  
  215. for (i=0; i < n_rows; i++)
  216. {
  217.  
  218. for(j= 0; j < n_cols; j++)
  219. {
  220.  
  221. if (probe_row !=i || probe_col != j)
  222. {
  223. printf("| %6.2f ", plate[i][j]);
  224.  
  225. }
  226. else
  227. {
  228. printf("|*%6.2f*", plate[i][j]);
  229. }
  230.  
  231. }
  232. printf("|");
  233. printf("\n");
  234. printf("%s\n", separator_line);
  235.  
  236. }
  237. printf("\n");
  238. }
  239.  
  240. /*=======================================================*/
  241. void set_plate_cell(double plate[][MAX_COLS], int row, int col,
  242. double temp)
  243. {
  244. plate[row][col] = temp;
  245. }
  246. /*=======================================================*/
  247. void create_line(char line[], int n_cols)
  248. {
  249. int i;
  250. char cell_portion[] = "+--------";
  251.  
  252. /* Make sure the string starts out empty */
  253. line[0] = '\0';
  254.  
  255. /* Keep adding cell_portion onto the line */
  256. for (i = 0; i < n_cols; i++)
  257. {
  258. strcat(line, cell_portion);
  259. }
  260.  
  261. /* Add the final '+' */
  262. strcat(line, "+");
  263. }
  264.  
  265. /*=======================================================*/
  266.  
  267. void update_plate (double plate1[MAX_ROWS][MAX_COLS],
  268. double ambien_temp,double H_coef,
  269. double max_change, double probe_temp,
  270. int row, int col)
  271. {
  272. int n = 1;
  273. int i;
  274. int j;
  275. double delta_temp;
  276. double min_delta = 0.0;
  277. double max_delta = 100000000.0;
  278. int min_delta_row;
  279. int min_delta_col;
  280. int max_delta_row;
  281. int max_delta_col;
  282. double plate2[MAX_ROWS][MAX_COLS];
  283.  
  284.  
  285. /*Print outputfile header*/
  286. fprintf(output_file,
  287. "\n"
  288. "Max Temp. Row Col\n"
  289. "---------+-----+-----\n");
  290.  
  291. while (fabs(min_delta)<max_change && max_change<fabs(max_delta))
  292. {
  293. min_delta = 100000000.0;
  294. max_delta = 0.0;
  295.  
  296.  
  297. /*Set Probe Temp*/
  298. plate1 [row][col]= probe_temp;
  299.  
  300. /*Calculation for edges*/
  301.  
  302. plate2 [0][0]= (plate1[0][0] + H_coef*plate1[1][0]
  303. + H_coef*ambien_temp + H_coef*ambien_temp
  304. + H_coef*plate1[0][1] )/(1+ (4*H_coef));
  305. plate2 [0][5]= (plate1[0][5] + H_coef*plate1[0][4]
  306. + H_coef*ambien_temp + H_coef*ambien_temp
  307. + H_coef*plate1[1][5] )/(1+ (4*H_coef));
  308. plate2 [7][0]= (plate1[7][0] + H_coef*plate1[6][0]
  309. + H_coef*ambien_temp + H_coef*ambien_temp
  310. + H_coef*plate1[7][1] )/(1+ (4*H_coef));
  311. plate2 [7][5]= (plate1[7][5] + H_coef*plate1[7][4]
  312. + H_coef*ambien_temp + H_coef*ambien_temp
  313. + H_coef*plate1[6][5] )/(1+ (4*H_coef));
  314.  
  315. /*Calculation for sides*/
  316.  
  317.  
  318. /*Left Side*/
  319. for (i=0; i <=(0); i++)
  320. {
  321. for(j= 1; j <=(6); j++)
  322. {
  323. plate2 [j][i]= (plate1[j][i]
  324. + H_coef*plate1[j+1][i]
  325. + H_coef*plate1[j-1][i]
  326. + H_coef*ambien_temp
  327. + H_coef*plate1[j][i+1] )
  328. /(1+ (4*H_coef));
  329. }
  330. }
  331.  
  332.  
  333. /*Right Side*/
  334. for (i=5; i <=(5); i++)
  335. {
  336. for(j= 1; j <=(6); j++)
  337. {
  338. plate2 [j][i]= (plate1[j][i]
  339. + H_coef*plate1[j+1][i]
  340. + H_coef*plate1[j-1][i]
  341. + H_coef*plate1[j][i-1]
  342. + H_coef*ambien_temp )
  343. /(1+ (4*H_coef));
  344. }
  345. }
  346.  
  347. /*top side*/
  348. for (i=1; i <=(4); i++)
  349. {
  350. for(j= 0; j <=(0); j++)
  351. {
  352. plate2 [j][i]= (plate1[j][i]
  353. + H_coef*plate1[j+1][i]
  354. + H_coef*plate1[j][i-1]
  355. + H_coef*plate1[j][i+1]
  356. + H_coef*ambien_temp )
  357. /(1+ (4*H_coef));
  358. }
  359. }
  360.  
  361. /*bottom side*/
  362. for (i=1; i <=(4); i++)
  363. {
  364. for(j= 7; j <=(7); j++)
  365. {
  366. plate2 [j][i]= (plate1[j][i]
  367. + H_coef*plate1[j-1][i]
  368. + H_coef*plate1[j][i+1]
  369. + H_coef*plate1[j][i-1]
  370. + H_coef*ambien_temp )
  371. /(1+ (4*H_coef));
  372. }
  373. }
  374.  
  375. /*Calculations for "center" array elements*/
  376. for (i=1; i <=(4); i++)
  377. {
  378. for(j= 1; j <=(6); j++)
  379. {
  380. plate2 [j][i]= (plate1[j][i]
  381. + H_coef*plate1[j+1][i]
  382. + H_coef*plate1[j-1][i]
  383. + H_coef*plate1[j][i-1]
  384. + H_coef*plate1[j][i+1] )
  385. /(1+ (4*H_coef));
  386. }
  387. }
  388.  
  389. plate2 [row][col]= probe_temp;
  390.  
  391.  
  392. /*Calculates min and max*/
  393. for (i = 0; i < 8; i++)
  394. for (j = 0; j < 6; j++)
  395. {
  396.  
  397. if (row !=i || col != j)
  398. {
  399.  
  400. delta_temp = (plate2[i][j] - plate1[i][j]);
  401. if (fabs(delta_temp) > fabs(max_delta))
  402. {
  403. max_delta = delta_temp;
  404. max_delta_row = i;
  405. max_delta_col = j;
  406. }
  407.  
  408. if (fabs(delta_temp) < fabs(min_delta))
  409. {
  410. min_delta = delta_temp;
  411. min_delta_row = i;
  412. min_delta_col = j;
  413. }
  414. plate1[i][j] = plate2[i][j];
  415. }
  416. }
  417.  
  418. printf("#### PLATE %d ####\n", n++);
  419. printf("\n");
  420. printf(" %f ",
  421. ambien_temp);
  422. printf("\n");
  423. print_plate(plate1, MAX_ROWS, MAX_COLS, row, col);
  424.  
  425. /*copy data for max values to file*/
  426. fprintf(output_file,
  427. " %8.4f %2d %2d \n",
  428. max_delta, max_delta_row, max_delta_col);
  429.  
  430. /*prints min and max*/
  431. printf("Minimum change of %.2f at [%d,%d]\n", min_delta, min_delta_row, min_delta_col);
  432. printf("Maximum change of %.2f at [%d,%d]\n\n", max_delta, max_delta_row, max_delta_col);
  433.  
  434. }
  435.  
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement