-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoisson_CG.cpp
171 lines (158 loc) · 3.7 KB
/
Poisson_CG.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
#include <fftw3.h>
#include "1D_BTCS.h"
void Poisson_CG()
{
double x_l = 0.0;
double x_r = 1.0;
int nx = 512;
double dx = (x_r - x_l) / nx;
vector<double> x(nx + 1, 0);
for (int i = 0; i < nx + 1; i++)
{
x[i] = i * dx + x_l;
}
double y_b = 0.0;
double y_t = 1.0;
int ny = 512;
double dy = (y_t - y_b) / ny;
vector<double> y(ny + 1, 0);
for (int i = 0; i < ny + 1; i++)
{
y[i] = i * dy + y_b;
}
double tolerance = 1.0e-4;
int max_iter = 10000;
double tiny = 1.0e-16;
vector<vector<double>> ue(ny + 1, vector<double>(nx + 1, 0.0));
vector<vector<double>> f(ny + 1, vector<double>(nx + 1, 0.0));
vector<vector<double>> un(ny + 1, vector<double>(nx + 1, 0.0));
// analytic solution and initial condition
for (int i = 0; i < ny + 1; i++)
{
for (int j = 0; j < nx + 1; j++)
{
ue[i][j] = (x[j] * x[j] - 1.0) * (y[i] * y[i] - 1.0);
f[i][j] = -2.0 * (2.0 - x[j] * x[j] - y[i] * y[i]);
}
}
for (int i = 0; i < ny + 1; i++)
{
un[i][0] = ue[i][0];
un[i][ny] = ue[i][ny];
}
for (int i = 0; i < nx + 1; i++)
{
un[0][i] = ue[0][i];
un[nx][i] = ue[nx][i];
}
vector<vector<double>> r(ny + 1, vector<double>(nx + 1, 0.0));
vector<vector<double>> p(ny + 1, vector<double>(nx + 1, 0.0));//matrix for direction
vector<vector<double>> del_p(ny + 1, vector<double>(nx + 1, 0.0));//matrix for direction
double init_rms = 0.0;
double rms = 0.0;
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
double d2udx2 = (un[i + 1][j] - 2 * un[i][j] + un[i - 1][j]) / dx / dx;
double d2udy2 = (un[i][j + 1] - 2 * un[i][j] + un[i][j - 1]) / dy / dy;
r[i][j] = f[i][j] - d2udx2 - d2udy2;
p[i][j] = r[i][j];
}
}
//compute residual
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
init_rms += r[i][j] * r[i][j];
}
}
init_rms = sqrt(init_rms / (nx - 1) / (ny - 1));
rms = init_rms;
int iter_count = 0;
double den = -2.0 / dx / dx - 2.0 / dy / dy;
double exp_rms = tolerance * init_rms;
for (iter_count = 0; iter_count < max_iter && rms>exp_rms; iter_count++)
{
//correct solution
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
del_p[i][j] = (p[i + 1][j] - 2 * p[i][j] + p[i - 1][j]) / dx / dx + (p[i][j + 1] - 2 * p[i][j] + p[i][j - 1]) / dy / dy;
}
}
//compute new residual
double aa = 0.0;
double bb = 0.0;
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
aa += r[i][j] * r[i][j];
bb += del_p[i][j] * p[i][j];
}
}
double cc = aa / (bb + tiny);
//update the numerical solution by adding some component of conjugate vector
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
un[i][j] += cc * p[i][j];
}
}
bb = aa;
aa = 0.0;
//update the residual by removing some component of previous residual
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
r[i][j] -= cc * del_p[i][j];
aa += r[i][j] * r[i][j];
}
}
cc = aa / (bb + tiny);
//update the conjugate vector
for (int i = 0; i < ny; i++)
{
for (int j = 0; j < nx; j++)
{
p[i][j] = r[i][j] + cc * p[i][j];
}
}
rms = 0.0;
for (int i = 1; i < ny; i++)
{
for (int j = 1; j < nx; j++)
{
rms += r[i][j] * r[i][j];
}
}
rms = sqrt(rms / (nx - 1) / (ny - 1));
cout << "iteration times " << iter_count << endl;
cout << "residual " << rms << endl;
}
cout << "iteration times until convergence:" << iter_count << endl;
//write
ofstream outfile("Poisson_CG.dat");
if (outfile.is_open())
{
for (int i = 0; i < ny + 1; i++)
{
for (int j = 0; j < nx + 1; j++)
{
outfile << un[i][j] << " ";
}
outfile << endl;
}
outfile << endl;
}
else
{
std::cerr << "Error: unable to open file for writing" << std::endl;
}
return;
}