Hot Plate simulation

So my part three wont show up for the output in my online textbook.

Part 3: Repeat update until Stable

After printing the results from the first iteration, continue to iterate (calculating the temperature for all interior cells) until no cell in the array changes by more than 0.1 degrees. Your program should monitor the largest change for any cell in the array in order to determine when to stop iterating. At the end of each iteration you will copy the values from the "new values" array into the "old values" array, to be ready for the next iteration.

Once all of the values in the plate have ceased to change by more than 0.1 degrees, print the plate, in the same comma separated form used above.


Print the values in the stable plate one more time but in tab separated form, like this:

0.0000 100.0000 100.0000 100.0000 0.0000
0.0000 49.8535 62.3047 49.8535 0.0000
0.0000 37.3047 49.7070 37.3047 0.0000
0.0000 49.8535 62.3047 49.8535 0.0000
0.0000 100.0000 100.0000 100.0000 0.0000
where there is a tab (created using the escape sequence "\t") between pairs of values, but not at the end of the lines. Fixed precision of 4 still applies, but the width of each item is not set for this part.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;
const int MAX_TEMPERATURE = 100;
const int MIN_TEMPERATURE = 0;
const int ROWS = 20;
const int COLUMNS = 20;
const int DIMENSIONS = 20;
bool update = true;
const double CHANGE = .1;
const int NEIGHBORS = 4;

void printHotPlate(double(&plate)[ROWS][COLUMNS], int dimension){
	for (int row = 0; row < dimension; row++){	
		for (int column = 0; column < dimension; column++){
			if (column == 19){
				cout << fixed << setprecision(4) << setw(10) << plate[row][column];
			}
			else{
				cout << fixed << setprecision(4) << setw(10) << plate[row][column] << ",";
			}
		}
		cout << endl;
	}
}

void initializeHotPlate(double(&plate)[ROWS][COLUMNS], int dimension, int MIN_TEMPERATURE, int MAX_TEMPERATURE){
	for (int row = 0; row < dimension; row++){	
		for (int column = 0; column < dimension; column++){
			if (row == 0 || row == 19){
				if (column == 0 || column == 19){
					plate[row][column] = MIN_TEMPERATURE;
				}
				else{
					plate[row][column] = MAX_TEMPERATURE;
				}
			}
			else{
				plate[row][column] = 0;
			}
		}
	}
}

void averageHotPlate(double(&oldPlate)[ROWS][COLUMNS], double(&plate)[ROWS][COLUMNS], bool& update, int dimension, double CHANGE, int NEIGHBORS){
	
	update = false;	
	for (int row = 1; row < dimension - 1; row++){
		for (int column = 1; column < dimension - 1; column++){
			
			plate[row][column] = (oldPlate[row + 1][column] + oldPlate[row - 1][column] + oldPlate[row][column + 1] + oldPlate[row][column - 1]) / NEIGHBORS;
			if (abs(plate[row][column] - oldPlate[row][column]) > CHANGE){	
				update = true;
			}
		}
	}
	for (int row = 1; row < dimension - 1; row++){	
		for (int column = 1; column < dimension - 1; column++){
			oldPlate[row][column] = plate[row][column];
		}
	}
}

void exportHotPlate(double(&plate)[ROWS][COLUMNS], int dimension){
	ofstream out_file;	
	out_file.open("lab6output.csv");	

	for (int row = 0; row < dimension; row++){	
		for (int column = 0; column < dimension; column++){
			if (column == 19){
				cout << fixed << setprecision(4) << plate[row][column];
			}
			else{
				cout << fixed << setprecision(4) << plate[row][column] << "\t";
			}
		}
		cout << endl;
	}
}

int main()
{
const int MAX_TEMPERATURE = 100;
const int MIN_TEMPERATURE = 0;
const int ROWS = 20;
const int COLUMNS = 20;
const int DIMENSIONS = 20;
bool update = true;
const double CHANGE = .1;
const int NEIGHBORS = 4;
	
double oldPlate[ROWS][COLUMNS];		
double plate[ROWS][COLUMNS];		

cout << "Hotplate simulator" << endl;
cout << endl;
cout << "Printing initial plate..." << endl;
	initializeHotPlate(oldPlate, DIMENSIONS, MIN_TEMPERATURE, MAX_TEMPERATURE);		
	initializeHotPlate(plate, DIMENSIONS, MIN_TEMPERATURE, MAX_TEMPERATURE);
	printHotPlate(oldPlate, DIMENSIONS);
	cout << endl;
	cout << "Printing plate after one iteration..." << endl;
	averageHotPlate(oldPlate, plate, update, ROWS, CHANGE, NEIGHBORS);
	printHotPlate(plate, DIMENSIONS);
	cout << endl<< endl;
	cout << "Printing final plate..." << endl;
	while (update){	
		averageHotPlate(oldPlate, plate, update, DIMENSIONS, CHANGE, NEIGHBORS);
	}
	printHotPlate(plate, DIMENSIONS);	
	cout << endl << endl;
	exportHotPlate(plate, DIMENSIONS);	

	return 0;
}

Last edited on
Not sure what you are asking. Your program runs perfectly well and solves the steady-state heat-conduction problem between two warmer boundaries.

If you are asking why it didn't go to file (in routine exportHotPlate) then it is because in that routine you sent it to standard output (cout) rather than the file output stream that you had just opened (out_file). I think your original question also expected you to separate items with commas (it's a CSV file, and could then be read as such into Excel).

Please try to phrase your question so that we can understand what your difficulty is.
Yea i know that wasnt too clear. So I submit this though Zybooks and for whatever reason it wont accept my part three. I dont know if it is the formatting or what?
I haven't a clue what Zybooks is, nor what you are referring to by "my part three", nor what precisely is happening when "it won't accept my ..."

The only reason that your output file (lab6output.csv) doesn't get any content is that in lines 75 and 78 of the code above you send your output to standard output (cout) and not to the intended file stream (out_file).
As lastchance told you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void exportHotPlate(double(&plate)[ROWS][COLUMNS], int dimension){
	ofstream out_file;	
	out_file.open("lab6output.csv");	// Not used

	for (int row = 0; row < dimension; row++){	
		for (int column = 0; column < dimension; column++){
			if (column == 19){
				cout << fixed << setprecision(4) << plate[row][column]; // All data is send to cout -> out_file
			}
			else{
				cout << fixed << setprecision(4) << plate[row][column] << "\t"; // All data is send to cout -> out_file
			}
		}
		cout << endl; // All data is send to cout -> out_file
	}
}
Topic archived. No new replies allowed.