-
Notifications
You must be signed in to change notification settings - Fork 0
/
poisson.c
194 lines (163 loc) · 6.08 KB
/
poisson.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "matrix.h"
#include "poisson.h"
#ifdef DEBUG
#define PRINT_RES_DEBUG 1
#else
#define PRINT_RES_DEBUG 0
#endif
int getIndexInSystemMatrix(int i, int j, int jmax) {
return i * jmax + j;
}
double *create2DpoissonMatrix(double xlength, double ylength, int imax, int jmax) {
int matrixDim = imax * jmax;
double *matrix = (double*)calloc(matrixDim * matrixDim, sizeof(double));
if (matrix == NULL) {
printf ("Fehler bei Spiecherallokation für Matrix\n");
return NULL;
}
double oneOverDeltaXSquared = ((imax + 1) * (imax + 1)) / (xlength * xlength);
double oneOverDeltaYSquared = ((jmax + 1) * (jmax + 1)) / (ylength * ylength);
/*for (i = 0; i < matrixDim; i++) {
matrix[i] = (double*)calloc(matrixDim, sizeof(double));
if (matrix[i] == NULL) {
printf ("Fehler bei Spiecherallokation für Matrixzeile %i\n", i);
freeMatrix(matrix, i);
return NULL;
}
}*/
int rowOffset = 0;
for (int i = 0; i < imax; i++) {
for (int j = 0; j < jmax; j++) {
//matrix[POS2D(i, j, matrixDim)] = ++rowOffset;
double* currentRow = &matrix[rowOffset++ * matrixDim];
currentRow[getIndexInSystemMatrix(i, j, jmax)] = 2 * (oneOverDeltaXSquared + oneOverDeltaYSquared);
if (i > 0)
currentRow[getIndexInSystemMatrix(i-1, j, jmax)] = -oneOverDeltaXSquared;
if (i < imax-1)
currentRow[getIndexInSystemMatrix(i+1, j, jmax)] = -oneOverDeltaXSquared;
if (j > 0)
currentRow[getIndexInSystemMatrix(i, j-1, jmax)] = -oneOverDeltaYSquared;
if (j < jmax-1)
currentRow[getIndexInSystemMatrix(i, j+1, jmax)] = -oneOverDeltaYSquared;
}
}
return matrix;
}
void solveSOR(double *A, double* x, double *b, int rows, int cols, double omega, double epsilon, int itermax) {
double error = 1;
int iter = 0;
while (error >= epsilon && iter < itermax) {
error = 0;
for (int j = 0; j < rows; j++) {
double xnew = b[j];
for (int i = 0; i < cols; i++) {
if (j != i)
xnew -= A[POS2D(i, j, cols)] * x[i];
}
xnew *= omega / A[POS2D(j, j, cols)];
xnew += (1 - omega) * x[j];
error = fmax(error, fabs(x[j]-xnew));
x[j] = xnew;
}
if (PRINT_RES_DEBUG && ((iter < 50 && iter % 5 == 0) || (iter < 500 && iter % 50 == 0) || (iter < 1000 && iter % 100 == 0) || iter % 1000 == 0))
printf("#%i: %f\n", iter, error);
iter++;
}
if (iter == itermax)
printf("Abgebrochen nach %i iterationen mit einem Fehler von %f\n", iter, error);
}
/* p ist Gitter mit (imax x jmax) inneren Zellen */
void applyHomogenousNeumannBC(double *p, int imax, int jmax) {
const int imaxPlus2 = imax+2;
const int imaxPlus1 = imax+1;
const int jmaxPlus1 = jmax+1;
const int lowerBound = (imaxPlus1 < jmaxPlus1) ? imaxPlus1 : jmaxPlus1;
int k;
for (k = 1; k < lowerBound; k++) {
p[POS2D(k, 0, imaxPlus2)] = p[POS2D(k, 1, imaxPlus2)];
p[POS2D(k, jmaxPlus1, imaxPlus2)] = p[POS2D(k, jmax, imaxPlus2)];
p[POS2D(0, k, imaxPlus2)] = p[POS2D(1, k, imaxPlus2)];
p[POS2D(imaxPlus1, k, imaxPlus2)] = p[POS2D(imax, k, imaxPlus2)];
}
for (k = lowerBound; k < imaxPlus1; k++) {
p[POS2D(k, 0, imaxPlus2)] = p[POS2D(k, 1, imaxPlus2)];
p[POS2D(k, jmaxPlus1, imaxPlus2)] = p[POS2D(k, jmax, imaxPlus2)];
}
for (k = lowerBound; k < jmaxPlus1; k++) {
p[POS2D(0, k, imaxPlus2)] = p[POS2D(1, k, imaxPlus2)];
p[POS2D(imaxPlus1, k, imaxPlus2)] = p[POS2D(imax, k, imaxPlus2)];
}
}
void applyHomogenousDirichletBC(double *p, int imax, int jmax) {
const int imaxPlus2 = imax+2;
const int imaxPlus1 = imax+1;
const int jmaxPlus1 = jmax+1;
const int lowerBound = (imaxPlus1 < jmaxPlus1) ? imaxPlus1 : jmaxPlus1;
int k;
for (k = 1; k < lowerBound; k++) {
p[POS2D(k, 0, imaxPlus2)] = -p[POS2D(k, 1, imaxPlus2)];
p[POS2D(k, jmaxPlus1, imaxPlus2)] = -p[POS2D(k, jmax, imaxPlus2)];
p[POS2D(0, k, imaxPlus2)] = -p[POS2D(1, k, imaxPlus2)];
p[POS2D(imaxPlus1, k, imaxPlus2)] = -p[POS2D(imax, k, imaxPlus2)];
}
for (k = lowerBound; k < imaxPlus1; k++) {
p[POS2D(k, 0, imaxPlus2)] = -p[POS2D(k, 1, imaxPlus2)];
p[POS2D(k, jmaxPlus1, imaxPlus2)] = -p[POS2D(k, jmax, imaxPlus2)];
}
for (k = lowerBound; k < jmaxPlus1; k++) {
p[POS2D(0, k, imaxPlus2)] = -p[POS2D(1, k, imaxPlus2)];
p[POS2D(imaxPlus1, k, imaxPlus2)] = -p[POS2D(imax, k, imaxPlus2)];
}
}
void solveSORforPoisson(double *p, double *rhs, double omega, double epsilon, int itermax,
int useNeumannBC, double xlength, double ylength, int imax, int jmax) {
const double oneMinusOmega = 1 - omega;
const double deltaX = xlength / imax;
const double deltaY = ylength / jmax;
const double oneOverDeltaXSquared = 1 / (deltaX * deltaX);
const double oneOverDeltaYSquared = 1 / (deltaY * deltaY);
const double omegaRelaxation = omega / (2 * (oneOverDeltaXSquared + oneOverDeltaYSquared));
const int imaxPlus2 = imax+2;
int ijlocation = 0;
double error = 1;
double sum = 0;
double twoPij = 0;
int iter = 0;
int i,j;
while (error >= epsilon && iter < itermax) {
error = 0;
if (useNeumannBC)
applyHomogenousNeumannBC(p, imax, jmax);
else
applyHomogenousDirichletBC(p, imax, jmax);
for (j = 1; j <= jmax; j++) {
for (i = 1; i <= imax; i++) {
ijlocation = POS2D(i, j, imaxPlus2);
p[ijlocation] = oneMinusOmega * p[ijlocation] + omegaRelaxation * (
oneOverDeltaXSquared * (p[ijlocation-1] + p[ijlocation+1]) +
oneOverDeltaYSquared * (p[ijlocation-imaxPlus2] + p[ijlocation+imaxPlus2]) -
rhs[ijlocation]);
}
}
for (i = 1; i <= imax; i++) {
for (j = 1; j <= jmax; j++) {
ijlocation = POS2D(i, j, imaxPlus2);
twoPij = 2 * p[ijlocation];
sum = oneOverDeltaXSquared * (p[ijlocation+1] + p[ijlocation-1] - twoPij) +
oneOverDeltaYSquared * (p[ijlocation+imaxPlus2] + p[ijlocation-imaxPlus2] - twoPij) -
rhs[ijlocation];
error += sum * sum;
}
}
error /= imax * jmax;
error = sqrt(error);
if (PRINT_RES_DEBUG && ((iter < 50 && iter % 5 == 0) || (iter < 500 && iter % 50 == 0) || (iter < 1000 && iter % 100 == 0) || iter % 1000 == 0))
printf("#%i: %f\n", iter, error);
iter++;
}
if (iter == itermax)
printf("Abgebrochen nach %i iterationen mit einem Fehler von %f\n", iter, error);
}