array problem

Hi everyone.My code is simulation of heat flow in a square plate ,using the method of finite differences.At the beginning plate in each node has a temperature of 273 K(this value u can change if u want to). The plate was divided for 10x10 nodes.Each node is an element of array T[i][j][k] where the i is row index ,j is column index and k is index of increment of the time.Coefficient of thermal conductivity, density and Cp are the same for plate and surroundings.

After compilation,I saw that the value of temperature of node T[0][DimY][k + 1] is the same as temperature of T[1][0][k + 1] and symmetrically and similarly T[DimX][0][k + 1] is the same as T[DimX-1][DimY][k+1].

I found the place in my code where are discrepancies in algorithm. That place I marked by a //comment in code and method of checking by instruction "if".How is that possible that assigning for element T[0][DimY][k + 1] for example value 999 that value is also assigning for T[1][0][k + 1]? I think on this for a long time. PLZ HELP ME FIND MISTAKE! :( :( :(

My code:
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
 #include<iostream>
#include<cmath>
#include <fstream>

using namespace std;

#define DimX 9	//number of X nodes 
#define DimY 9	//number of Y nodes
#define Dimt 10000 //time step

typedef long double real;

real T[DimX][DimY][Dimt];
int k;

void save_array_to_TXT(std::string nazwaPliku)
{

	std::fstream plik;

	plik.open(nazwaPliku, std::ios::out);
	plik.precision(10);
	for (k = 0; k < Dimt; k++)
	{

		for (int j = 0; j < DimY + 1; j++)
		{
			for (int i = 0; i < DimX + 1; i++)
			{

				plik << T[i][j][k] << " ";

			}
			plik << endl;
		}
	}
	plik << endl;
	plik.close();
}
void show_MAP()
{
	
		for (int j = 0; j < DimY + 1; j++)
		{
			for (int i = 0; i < DimX + 1; i++)
			{
				cout.precision(17);
				cout << T[i][j][k] << " ";

			}
			cout << endl;
		}
    
	cout << "after your click the button current map will dissapear" << endl;
	system("pause");
	system("cls");
}
void The_BEGINNING_CONDITION()
{
	    //THE BEGINNING CONDITION FOR TIIME t=0 , each node has temperature T[i][j][k]=273K = 0 Celcius degree
		for (int i = 0; i < DimX + 1; i++)
		{
			for (int j = 0; j < DimY + 1; j++)
			{
				T[i][j][0] = 273; 
			}

		}
	
}

void numerical_method(real t, real alfa, real lambda1, real Cp1, real ro1, real LengthXandY, real Tair)
	//alfa is convection coefficient , for air(natural convection) alfa=5-25 W/(K*m^2)
{

	real a1 = lambda1 / (Cp1*ro1);
	real dl = LengthXandY / (DimX - 1); //dl=dx=dy
	real dt;
	dt = t / Dimt;
	cout << "dt= " << dt << endl;
	system("pause");
	//stability condition
	if (((lambda1*dt) / (ro1*Cp1*dl*dl)) > 0.25) // aby warunek byl spelniony Dimt musi byc dostatecznie duzy
	{
		cout << "(lambda1*dt) / (ro1*Cp1*dh*dh)= " << (lambda1*dt) / (ro1*Cp1*dl*dl) << endl;
		cout << "error" << endl;
		system("pause");
	}

	The_BEGINNING_CONDITION();
	show_MAP();

	real tal;
	tal = (dt*lambda1) / (dl*dl*ro1*Cp1);
	for ( k = 0; k < Dimt+1 ; k++)
	{
		for (int j = 0; j < DimY+1; j++)
		{
			for (int i = 0; i < DimX+1; i++)
			{
				//LEFT TOP COURNER
				if ((i == 0) && (j == 0))
				{		
					T[0][0][k + 1] = T[0][0][k]+tal*(Tair+Tair+T[0][1][k]+T[1][0][k]-4*T[0][0][k]) ;
					
				}
				//RIGHT TOP COURNER
				if ((i == DimX) && (j == 0))
				{
					
					T[DimX][0][k + 1] = T[DimX][0][k] + tal*(Tair + Tair + T[DimX-1][0][k] + T[DimX][1][k] - 4 * T[DimX][0][k]);
				}
				//LEFT BOT CURNER
				if ((i == 0) && (j == DimY))
				{
					T[0][DimY][k + 1] = T[0][DimY][k] + tal*(Tair + Tair + T[0][DimY - 1][k] + T[1][DimY][k] - 4*T[0][DimY][k]);
				 //THIS IS HERE!!!!!!!!!!!!!!!!!!
					cout << "T[0][DimY][k + 1]= " << T[0][DimY][k + 1] << endl;
					cout << "T[1][0][k + 1]= " << T[1][0][k + 1] << endl;
					T[0][DimY][k + 1] = 999;
					if (T[1][0][k + 1] == 999)
					{
						//How is that possible that assigning for element T[0][DimY][k + 1] for example value 999 that value is also assigning for T[1][0][k + 1]?
					
						cout << "blad" << endl;
						cout << "T[0][DimY][k + 1]= " << T[0][DimY][k + 1] << endl;
						cout << "T[1][0][k + 1]= " << T[1][0][k + 1] << endl;
						system("pause");
					}
					//T[0][DimY][k + 1] = T[0][DimY][k] + tal*(Tair + Tair + T[0][DimY - 1][k] + T[1][DimY][k] - 4 * T[0][DimY][k]);

				}
				//RIGHT BOT COURNER
				if ((i == DimX) && (j == DimY))
				{
					
					T[DimX][DimY][k + 1] = T[DimX][DimY][k] + tal*(Tair + Tair + T[DimX - 1][DimY][k] + T[DimX][DimY - 1][k] - 4 * T[DimX][DimY][k]);

				}
				//TOP LINE
				if ((j == 0) && (i != 0) && (i != DimX))
				{
					
					T[i][0][k + 1] = T[i][0][k] + tal*(Tair + T[i - 1][0][k] + T[i + 1][0][k] + T[i][1][k] - 4 * T[i][0][k]);

				}
				//BOT LINE
				if ((j == DimY) && (i != 0) && (i != DimX))
				{
					
					T[i][DimY][k + 1] = T[i][DimY][k] + tal*(Tair + T[i - 1][DimY][k] + T[i + 1][DimY][k] + T[i][DimY-1][k] - 4 * T[i][DimY][k]);

				}
				//LEFT LINE
				if ((i == 0) && (j != 0) && (j != DimY))
				{
					
					T[0][j][k + 1] = T[0][j][k] + tal*(Tair + T[0][j - 1][k] + T[0][j + 1][k] + T[1][j][k] - 4 * T[0][j][k]);

				}
				//RIGHT LINE
				if ((i == DimX) && (j != 0) && (j != DimY))
				{
					
					T[DimX][j][k + 1] = T[DimX][j][k] + tal*(Tair + T[DimX][j - 1][k] + T[DimX][j + 1][k] + T[DimX - 1][j][k] - 4 * T[DimX][j][k]);
				}
				//REST OF NODES BASED ON PLATE
				if ((i < DimX) && (i>0) && (j < DimY) && (j>0))
				{
					T[i][j][k + 1] = T[i][j][k] + tal*( T[i][j - 1][k] + T[i][j + 1][k] + T[i- 1][j][k]+T[i+1][j][k] - 4 * T[i][j][k]);
					/*cout << "node (" << i << ")(" << j << ")" << endl;
					cout << "T[i][j][k]=" << T[i][j][k] << endl;
					cout << "tal*( T[i][j - 1][k] + T[i][j + 1][k] + T[i- 1][j][k]+T[i+1][j][k] - 4 * T[i][j][k])" << tal*(T[i][j - 1][k] + T[i][j + 1][k] + T[i - 1][j][k] + T[i + 1][j][k] - 4 * T[i][j][k]) << endl;
					system("pause");
					*/
				}
				
			}
		}
		cout << "t= " << k*dt << endl;
		show_MAP();
		
	}
	save_array_to_TXT("results2d.txt");
}


int main()
{
	numerical_method(1000,0.025,0.03,1,1,10,300);
	
	system("pause");
}
You are mixing where to add or subtract one. You should declare array with the number of elements you wish it to have, so int i[10] is ten elements in array. But to access last element in array you use size-1, so i[9] is tenth element in array (because we count from 0). You are doing the opposite thing in the code above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define DimX 9	//number of X nodes  // this should be 10, because you want 10 rows
#define DimY 9	//number of Y nodes // this should also be 10
#define Dimt 10000 //time step

real T[DimX][DimY][Dimt]; // That is T[9][9][1000]!
// ...
for ( k = 0; k < Dimt+1 ; k++)  // You don't want +1 here
   {
      for (int j = 0; j < DimY+1; j++) // or here
      {
         for (int i = 0; i < DimX+1; i++) // or here
          {
            if ((i == 0) && (j == DimY)) //but you want -1 here: (j == DimY -1) 
             {
             //THIS IS HERE!!!
                 T[0][DimY][k + 1] = 999; // and -1 here: T[0][DimY-1][k+1] 


Also, look out for all cases of accessing element [k +1], because in the last iteration the value of k will be 999, so k+1 will be outside of array, and your program will crash.
Last edited on
Omg, what a HUGE Mistake I made! THX God for ppl like You!! Now it's all OK. Thx much for help!
Topic archived. No new replies allowed.