-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
74 lines (67 loc) · 2.19 KB
/
main.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
#include <iostream>
#include <armadillo>
#include <cfloat>
#include <time.h>
#include <matrixSolvers.hpp>
using namespace std;
using namespace arma;
int main() {
const int n = pow(10,1);
std::cout << "n: 10^" << log10(n) << endl << endl;
const float h = 1.0f / ((float) n + 1.0f); // h is the spacing between discretized points
const float h2 = h*h;
float x; // x is a spatial coordinate
Col<float> b(n), u(n), v(n); // as per assignment
clock_t start, finish;
// Find the RHS of the Poisson eq 'b' (up to a constant multiple)
// and the LHS's analytic solution 'u'.
for (int i=0; i<n; ++i) {
x = (float) (i+1) * h;
b(i) = h2*100.0f * exp(-10.0f * x);
u(i) = 1.0f - (1.0f - exp(-10.0f))*x - exp(-10.0f*x);
}
// Calculate discrete solution (and time it).
start = clock();
v = poissonSolver(b);
finish = clock();
cout << "My poissonSolver()" << endl;
cout << "Time: " << finish-start << "us" << endl;
// Find common log of the solver's largest deviation from the analytic solution.
float maxError = -FLT_MAX;
float temp;
for (int i=0; i<n; ++i) {
temp = abs((v(i) - u(i))/u(i));
if (maxError < temp) {
maxError = temp;
}
}
maxError = log10(maxError);
cout << "maxError: " << maxError << endl << endl;
// Compare with Armadillo.
if (log10(n) <= 4.1) { // Without sparse matrices, Armadillo cannot handle large 'n'.
cout << "Armadillo's solve()" << endl;
mat A(n,n);
A.zeros();
A.diag( 0) += 2.0f;
A.diag(-1) += -1.0f;
A.diag( 1) += -1.0f;
colvec B(n), X(n);
for (int i=0; i<n; ++i) {
B(i) = b(i); // Easiest way around an annoying type error.
}
start = clock();
X = solve(A, B);
finish = clock();
cout << "Time: " << (finish-start) << "us" << endl;
maxError = -FLT_MAX;
for (int i=0; i<n; ++i) {
temp = abs((X(i) - u(i))/u(i));
if (maxError < temp) {
maxError = temp;
}
}
cout << "maxError: " << log10(maxError);
}
cout << endl << endl;
return 0;
}