-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPoisson_GaussSeidel.cpp
133 lines (122 loc) · 3 KB
/
Poisson_GaussSeidel.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
#include <fftw3.h>
#include "1D_BTCS.h"
#include <complex>
void Poisson_GaussSeidel()
{
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;
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));
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;
}
}
//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++)
{
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;
un[i][j] += r[i][j] / den;
}
}
//compute new residual
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;
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_GaussSeidel.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;
}