-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedBlack_Serial.c
134 lines (113 loc) · 3.46 KB
/
RedBlack_Serial.c
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
//Serial Red Black Method
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<omp.h>
//Memory allocation for matrices
double** allocate2DArray(int rows, int cols) {
double **arr = (double **)malloc(rows * sizeof(double *));
for (int i = 0; i < rows; i++) {
arr[i] = (double *)malloc(cols * sizeof(double));
}
return arr;
}
//Memory deallocation for matrices
void deallocate2DArray(double **arr, int rows) {
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
}
int main(){
double t1,t2;
t1 = omp_get_wtime();
//Mesh Parameters
int xmin = 0, xmax = 1, ymin = 0, ymax = 1;
int N = 33;
double delta = (double)(xmax - xmin)/(N-1);
//Memory Allocation
double **phi, **phi_exact;
double **q;
phi = allocate2DArray(N, N);
phi_exact = allocate2DArray(N, N);
q = allocate2DArray(N, N);
//Initial Guesses and Boundary Conditions
int i,j,k; double x,y;
for(i = 0; i<N ;i++){
for(j = 0; j<N; j++){
x = xmin + delta*j;
y = ymin + delta*i;
phi[i][j] = 0;
if (i == 0)
phi[i][j] = exp(x);
if (i == N-1)
phi[i][j] = exp(x-2);
if (j == 0)
phi[i][j] = exp(-2*y);
if (j == N-1)
phi[i][j] = exp(1-2*y);
}
}
//RHS of Poisson equation
for(i = 1; i<N-1 ;i++){
for(j = 1; j<N-1; j++){
x = xmin + j*delta;
y = ymin + i*delta;
q[i][j] = 5*exp(x)*exp(-2*y);
}
}
//Exact Solution
for(i = 0; i<N; i++){
for(j = 0; j<N; j++){
x = xmin + delta*j;
y = ymin + delta*i;
phi_exact[i][j] = exp(x)*exp(-2*y);
}
}
//Norm of exact solution
double norm_exact = 0;
for(i = 0; i<N; i++){
for(j = 0; j<N; j++){
norm_exact += phi_exact[i][j]*phi_exact[i][j];
}
}
norm_exact = sqrt(norm_exact);
int iter = 0;
double err = 1;
//Main loop
while(err>0.0001){
//Updating odd elements
for(i = 1; i<N-1; i++){
for(j = 1; j<N-1; j++){
if((i+j)%2 == 1)
phi[i][j] = 0.25*(phi[i][j+1] + phi[i][j-1] + phi[i+1][j] + phi[i-1][j] - delta*delta*q[i][j]);
}
}
//Updating even elements
for(i=1; i<N-1; i++){
for(j=1; j<N-1; j++){
if((i+j)%2 == 0)
phi[i][j] = 0.25*(phi[i][j+1] + phi[i][j-1] + phi[i+1][j] + phi[i-1][j] - delta*delta*q[i][j]);
}
}
//Error Calculation
err = 0;
for (i = 0; i<N; i++){
for(j = 0; j<N; j++){
err += (phi_exact[i][j] - phi[i][j])*(phi_exact[i][j] - phi[i][j]);
}
}
err = sqrt(err)/norm_exact;
iter += 1;
}
t2 = omp_get_wtime();
printf("Number of iterations for Serial Red Black Method are : %d\n",iter);
printf("The problem size is : %d x %d\n",N,N);
printf("The error is : %lf\n",err);
printf("The time taken is : %lf\n",t2-t1);
//Freeing the allocated memory
deallocate2DArray(phi, N);
deallocate2DArray(phi_exact, N);
deallocate2DArray(q, N);
return 0;
}