-
Notifications
You must be signed in to change notification settings - Fork 1
/
linebylineTDMA.cpp
256 lines (203 loc) · 5.93 KB
/
linebylineTDMA.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
// Function to solve the tridiagonal matrix equation
//Solving a 2D grid of cells, with discrete equations of the form, ap*phi[p]=an*phi[n]+as*phi[s]+ae*phi[e]+aw*phi[w]+s, for each cell
//where p is cell P, n is cell N, s is cell S, e is cell E, w is cell W
//Imagine ap,an,as,ae,aw,s to be arrays of size (N+1)*(M+1), in the order given in the main function
void TDMAySWEEPx (double ap[],double an[],double as[],double ae[],double aw[],double s[],long long N,long long M)
{
double aP[N+1][M+1]={0};
double aN[N+1][M+1]={0};
double aS[N+1][M+1]={0};
double aE[N+1][M+1]={0};
double aW[N+1][M+1]={0};
double S[N+1][M+1]={0};
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
aP[i][j]=ap[(j-1)*N+i];
aN[i][j]=an[(j-1)*N+i];
aS[i][j]=as[(j-1)*N+i];
aE[i][j]=ae[(j-1)*N+i];
aW[i][j]=aw[(j-1)*N+i];
S[i][j]=s[(j-1)*N+i];
}
}
int noofsweeps= 0; //no of sweeps to be performed
double phi[N+2][M+2]={0};
double error=0;
do{
double phiold[N+2][M+2]={0};
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
phiold[i][j]=phi[i][j];
}
}
error=0;
for(int j=1;j<=M;j++)
{
double d[N+1]={0};
for (int i=1;i<=N;i++)
{
d[i]= aW[i][j]*phi[i][j-1]+aE[i][j]*phi[i][j+1]+S[i][j];
}
double P[N+1]={0};
double Q[N+1]={0};
P[1]=aN[1][j]/aP[1][j];
Q[1]=d[1]/aP[1][j];
for (int k=2;k<=N;k++)
{
P[k]=aN[k][j]/(aP[k][j]-aS[k][j]*P[k-1]);
Q[k]=(d[k]+aS[k][j]*Q[k-1])/(aP[k][j]-aS[k][j]*P[k-1]);
}
phi[N][j]=Q[N];
for(int k=N;k>=2;k--)
{
phi[k-1][j]=P[k-1]*phi[k][j]+Q[k-1];
}
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
error=error+pow((phi[i][j]-phiold[i][j]),2);
}
}
noofsweeps++;
}while(error>0.001);
cout<<"After "<<noofsweeps<<" sweeps, the solution is:"<<endl;
for(int j=1;j<=M;j++)
{
for(int i=1;i<=N;i++)
{
cout<<"phi["<<i<<"]["<<j<<"]="<<phi[i][j]<<" ";
}
cout<<endl;
}
cout<<"**Notation : phi[i][j] is the value of phi at cell at ith row and jth column**"<<endl;
}
void TDMAxSWEEPy (double ap[],double an[],double as[],double ae[],double aw[],double s[],long long N,long long M)
{
double aP[N+1][M+1]={0};
double aN[N+1][M+1]={0};
double aS[N+1][M+1]={0};
double aE[N+1][M+1]={0};
double aW[N+1][M+1]={0};
double S[N+1][M+1]={0};
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
aP[i][j]=ap[(j-1)*N+i];
aN[i][j]=an[(j-1)*N+i];
aS[i][j]=as[(j-1)*N+i];
aE[i][j]=ae[(j-1)*N+i];
aW[i][j]=aw[(j-1)*N+i];
S[i][j]=s[(j-1)*N+i];
}
}
int noofsweeps= 0; //no of sweeps to be performed
double phi[N+2][M+2]={0};
double error=0;
do{
double phiold[N+2][M+2]={0};
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
phiold[i][j]=phi[i][j];
}
}
error=0;
for(int i=1;i<=N;i++)
{
double d[M+1]={0};
for (int j=1;j<=M;j++)
{
d[j]= aN[i][j]*phi[i+1][j]+aS[i][j]*phi[i-1][j]+S[i][j];
}
double P[M+1]={0};
double Q[M+1]={0};
P[1]=aE[i][1]/aP[i][1];
Q[1]=d[1]/aP[i][1];
for (int k=2;k<=M;k++)
{
P[k]=aE[i][k]/(aP[i][k]-aW[i][k]*P[k-1]);
Q[k]=(d[k]+aW[i][k]*Q[k-1])/(aP[i][k]-aW[i][k]*P[k-1]);
}
phi[i][M]=Q[M];
for(int k=M;k>=2;k--)
{
phi[i][k-1]=P[k-1]*phi[i][k]+Q[k-1];
}
}
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
error=error+pow((phi[i][j]-phiold[i][j]),2);
}
}
noofsweeps++;
}while(error>0.01);
cout<<"After "<<noofsweeps<<" sweeps, the solution is:"<<endl;
for(int i=1;i<=N;i++)
{
for(int j=1;j<=M;j++)
{
cout<<"phi["<<i<<"]["<<j<<"]="<<phi[i][j]<<" ";
}
cout<<endl;
}
cout<<"**Notation : phi[i][j] is the value of phi at cell at ith row and jth column**"<<endl;
}
int main()
{
long long int N;
long long int M;
cout<<"Number of rows:";
cin>>N;
cout<<"Number of columns:";
cin>>M;
double ap[(N+1)*(M+1)]={0};
double an[(N+1)*(M+1)]={0};
double as[(N+1)*(M+1)]={0};
double ae[(N+1)*(M+1)]={0};
double aw[(N+1)*(M+1)]={0};
double s[(N+1)*(M+1)]={0};
for(int j=1;j<=M;j++)
{
for(int i=1;i<=N;i++)
{
cout<<"ap["<<i<<"]["<<j<<"]:";
cin>>ap[(j-1)*N+i];
cout<<"an["<<i<<"]["<<j<<"]:";
cin>>an[(j-1)*N+i];
cout<<"as["<<i<<"]["<<j<<"]:";
cin>>as[(j-1)*N+i];
cout<<"ae["<<i<<"]["<<j<<"]:";
cin>>ae[(j-1)*N+i];
cout<<"aw["<<i<<"]["<<j<<"]:";
cin>>aw[(j-1)*N+i];
cout<<"s["<<i<<"]["<<j<<"]:";
cin>>s[(j-1)*N+i];
}
}
//Test code
/*N=4;
M=3;
double ap[]={0,20,30,30,40,30,40,40,50,20,30,30,40,0,0,0,0,0,0,0};
double an[]={0,10,10,10,0,10,10,10,0,10,10,10,0,0,0,0,0,0,0,0};
double as[]={0,0,10,10,10,0,10,10,10,0,10,10,10,0,0,0,0,0,0,0};
double aw[]={0,0,0,0,0,10,10,10,10,10,10,10,10,0,0,0,0,0,0,0};
double ae[]={0,10,10,10,10,10,10,10,10,0,0,0,0,0,0,0,0,0,0,0};
double s[]={0,500,500,500,2500,0,0,0,2000,0,0,0,2000,0,0,0,0,0,0,0};*/
TDMAxSWEEPy(ap,an,as,ae,aw,s,N,M);
TDMAySWEEPx(ap,an,as,ae,aw,s,N,M);
return 0;
}