-
Notifications
You must be signed in to change notification settings - Fork 1
/
Schwarz_Main.cpp
137 lines (90 loc) · 3.65 KB
/
Schwarz_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
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
#include <bits/stdc++.h>
#include <omp.h>
#include <limits>
#include <chrono>
#include <cmath>
#include <sys/time.h>
#include "Functions.h"
#include "Functions.cpp"
#include "Jacobi.cpp"
#include "Gauss_Seidel.cpp"
#include "Schwarz_DD.cpp"
#include "Finite_Differences.cpp"
#define THREAD_COUNT 4
int N;
typedef std::numeric_limits<double> dbl;
int main(void) {
Finite_Differences finite_differences;
Jacobi jacobi;
Gauss_Seidel gauss_seidel;
Schwarz_DD schwarz_dd(jacobi, gauss_seidel);
std::cout.precision(dbl::max_digits10);
// Max iterations and TOL
unsigned long max_iterations = 1000;
double TOL = 0.000000001f;
double h = 1.0/(pow(2.0, 8.0)); // h step
double interval[2] = {0.0, 10 + h}; // Interval
double phi = 10.0 * M_PI;
double b = std::cos(2.0 * phi);
auto u = [phi](double x) { return std::cos(phi * x); };
auto f = [phi](double x) { return (3.0 * std::pow(phi, 2.0) * std::cos(phi * x)); };
double boundary_conditions[2] = {1.0, b}; // Boundary conditions
// Create x points in the interval
std::vector<double> X_points = finite_differences.arange(interval[0], interval[1] + h, h);
N = X_points.size(); finite_differences.set_N(N);
printf("N: %d\n", N);
printf("h: %lf\n",h);
int DDBounds[THREAD_COUNT][2] = {
{0,630},
{640,1280},
{1260,2000},
{1980,2559},
};
int block_size = (N-2) / THREAD_COUNT;
int number_of_blocks;
try {
if(block_size == 0) throw std::overflow_error("");
number_of_blocks = pow((N-2)/block_size, 2);
}
catch(const std::overflow_error& e) {
printf("\nCheck the value of the variable <THREAD_COUNT>. Division by zero attempted.\n");
std::cerr << e.what() << '\n';
return 1;
}
// Create tridiagonal matrix
double* A = finite_differences.create_tridiagonal_matrix(h,
[phi](double x) { return (2.0 * (phi * phi)); },
[](double x) { return x; },
X_points);
// Set initial X solution vector
double* X = create_vector(N-2); initialize_vector(X, 0.0, N-2);
// Create right side vector F
double* F = finite_differences.create_vector_F(h, f, boundary_conditions, X_points);
// Create non overlapping blocks
/* double* A_Blocks = create_non_overlapping_blocks(A, N-2, THREAD_COUNT, block_size, 1);
if(A_Blocks == nullptr) {
return 0;
} */
// Create the main inverse diagonal block of A
//double* inv_main_block = create_inverse_diagonal_block(A_Blocks, THREAD_COUNT, (N-2)/THREAD_COUNT);
auto t_start = std::chrono::steady_clock::now(); // Start timer
// Swartz Methods
//int iterations = schwarz_dd.ABJASM(X, TOL, max_iterations, N-2, THREAD_COUNT, block_size, A_Blocks, inv_main_block, F);
//int iterations = schwarz_dd.MBJASM(X, TOL, max_iterations, N-2, THREAD_COUNT, block_size, A, F);
int iterations = schwarz_dd.MBJRASM(X, TOL, max_iterations, N-2, THREAD_COUNT, DDBounds, A, F);
auto t_end = std::chrono::steady_clock::now(); // End timer
// Calculate the time elapsed and print it
std::cout << "Computation Time: " << std::chrono::duration<double>(t_end - t_start).count() << std::endl;
printf("Iteration: %d\n", iterations); // Print iterations
// L2 Norm Error
std::cout << "L2 Error: " << finite_differences.L2_NormError(u, h, X, X_points) << std::endl;
// Compare the solution to the exact solution
//finite_differences.compare_solutions(u, X, X_points, 10);
// Deallocate memory
free(X);
free(F);
free(A);
//free(A_Blocks);
//free(inv_main_block);
return 0;
}