-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseidel.cpp
90 lines (74 loc) · 2.24 KB
/
seidel.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
#include <iostream>
#include <time.h>
#include <vector>
#include <cmath>
#include <fstream>
#include <Eigen/Dense>
using namespace std;
using Eigen::MatrixXd;
int main() {
ofstream fout, foutIter, foutTotalWallTime, foutIterWallTime;
clock_t start, end;
double time;
int numIter = 0;
int N = 100;
double error = 1;
double errorCap = pow(10, -13);
//cout << grid << endl;
//Gauss-Seidel
fout.open("seidelData.txt");
foutIter.open("seidelIter.txt");
foutTotalWallTime.open("seidelTotalWallTime.txt");
foutIterWallTime.open("seidelIterWallTime.txt");
MatrixXd grid = MatrixXd::Constant(N+2,N+2,0);
MatrixXd oldGrid = MatrixXd::Constant(N+2,N+2,0);
for(;N<=100;N+=10){
grid.resize(N+2,N+2);
oldGrid.resize(N+2,N+2);
for (int j = 0; j < N+2; j++){
grid(0,j) = 1.0;
oldGrid(0,j) = 1.0;
}
for(int i = 1; i < N+1; i++){
for(int j = 1; j < N+1; j++){
grid(i,j) = 0;
}
}
numIter = 0;
start = clock();
error = 1;
while(error > errorCap){
error = 0;
for(int i = 1; i < N+1; i++){
for(int j = 1; j < N+1; j++){
grid(i,j) = 0.25*(grid(i+1,j)+grid(i-1,j)+grid(i,j+1)+grid(i,j-1));
error += (grid(i,j)-oldGrid(i,j))*(grid(i,j)-oldGrid(i,j));
numIter++;
}
}
oldGrid = grid;
error = sqrt(error/(N*N));
}
end = clock();
time = double(end-start)/ (double)CLOCKS_PER_SEC;
foutTotalWallTime << N << " " << time << endl;
foutIterWallTime << N << " " << (double)time/(double)numIter << endl;
foutIter << N << " " << numIter << endl;
cout << N << endl;
}
N -= 10;
//fout << grid;
//cout << N << endl;
for(int i = N+1; i >= 0; i--){
for(int j = 0; j < N+2; j++){
fout << grid(i,j) << " ";
}
fout << endl;
}
fout.close();
foutIter.close();
foutTotalWallTime.close();
foutIterWallTime.close();
system("python graphSeidel.py");
return 0;
}