-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoissonSOR.cpp
372 lines (336 loc) · 10.1 KB
/
poissonSOR.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "poissonSOR.h"
#define MAX_FNAME_SIZE 100
double getOmegaIdeal(int nx, int ny);
std::string typeToString(type t);
struct Dominio {
int x0, y0, x1, y1;
Dominio(int a, int b, int c, int d) {
x0 = a;
y0 = b;
x1 = c;
y1 = d;
}
};
poissonSOR::poissonSOR(int x0, int x1, int y0, int y1, double hx, double hy) {
this->dom = new Dominio(x0, y0, x1, y1);
this->resize(hx, hy);
t = NIL;
this->erro_vp = 0;
this->erro_ele = 0;
this->grndFunc = NULL;
this->maxIter = STD_MAX_ITER;
return;
}
poissonSOR::~poissonSOR() {
delete(this->dom);
return;
}
void poissonSOR::resize(double hx, double hy) {
this->hx = hx;
this->hy = hy;
this->nx = 1 + (abs(dom->x1 - dom->x0) / this->hx);
this->ny = 1 + (abs(dom->y1 - dom->y0) / this->hy);
this->w = getOmegaIdeal(nx, ny);
this->a = this->b = (-1.)/(hx*hx);
this->c = this->d = (-1.)/(hy*hy);
this->e = 2. * ((1. / (hx*hx)) + (1./(hy*hy)));
this->vecSize = nx * ny;
for(int i = 0; i < this->vecSize; i++) {
this->vp[i] = 0;
this->ep[i] = 0;
this->ground_ep[i] = 0;
this->fp[i] = 0;
}
return;
}
void poissonSOR::reset() {
for(int i = 0; i < this->vecSize; i++) {
this->vp[i] = 0;
this->ep[i] = 0;
this->ground_ep[i] = 0;
this->fp[i] = 0;
this->ground[i] = 0;
}
this->erro_ele = 0;
this->erro_vp = 0;
this->contornos.clear();
}
void poissonSOR::setType(type t) {
this->t = t;
return;
}
void poissonSOR::setFXY(double (*fxy) (double, double)) {
this->aproxFunc = fxy;
return;
}
void poissonSOR::calcFp() {
for(int j = 0; j < nx; j++) {
for(int i = 0; i < ny; i++) {
this->fp[nx*i + j] = this->aproxFunc(j * hx, i * hy);
}
}
checkContornos();
return;
}
// Essa função meio que fica "hardcoded" aqui mesmo
void poissonSOR::checkContornos() {
switch(this->t) {
case NIL:
printf("ERRO: O tipo de contorno não foi configurado!");
break;
case VALIDACAO:
// Casos de contorno no Fp
for(int j = 0; j < nx; j++) {
for(int i = 0; i < ny; i++) {
if(i * hy == 2.5)
fp[nx * i + j] = this->contornos[0](j * hx, i * hy);
if(i == 0 || j == 0 || j == nx-1 || i == ny-1)
fp[nx * i + j] = 0;
}
}
break;
case CAPACITORES:
for(int j = 0; j < nx; j++) {
for(int i = 0; i < ny; i++) {
if(i * hy == 3.0)
fp[nx*i + j] = 5;
else if(i * hy == 2)
fp[nx*i + j] = -5;
if(i == 0 || j == 0 || j == nx-1 || i == nx - 1)
fp[nx * i + j] = 0;
}
}
break;
}
return;
}
void poissonSOR::addContorno(double (*f) (double, double)) {
this->contornos.push_back(f);
return;
}
void poissonSOR::process() {
this->calcFp();
if(this->grndFunc) {
this->calcExact();
}
this->doSOR();
this->calcCampElet();
this->calcCampElet_e();
this->calcErr();
this->calcErrEle();
return;
}
void poissonSOR::calcExact() {
// Column Major - Ground - OK!
for(int j = 0; j < nx; j++) {
for(int i = 0; i < ny; i++) {
ground[nx*i + j] = this->grndFunc(j * hx, i * hy);
}
}
return;
}
void poissonSOR::setValFunc(double (*f) (double, double)) {
this->grndFunc = f;
return;
}
void poissonSOR::doSOR() {
for(int iter = 0; iter < maxIter; iter++) {
// 1ª iter
switch (this->t){
case VALIDACAO:
vp[0] = 0;
break;
case CAPACITORES:
vp[0] = 0;
break;
default:
vp[0] = (w/e) * (fp[0] - a * vp[1] - c * vp[nx]) + ((1-w) * vp[0]);
break;
}
for(int i = 1; i < vecSize-1; i++) {
switch(this->t) {
case VALIDACAO:
// Contornos
if(i % nx == 0 || i % (nx) == (nx-1) || i<nx || (ny-1) * nx < i) {
vp[i] = 0;
continue;
}
if((i / nx ) * hy == 2.5) {
vp[i] = fp[i];
continue;
}
break;
case CAPACITORES:
if(i % nx == 0 || i % (nx) == (nx-1) || i<nx || (ny-1) * nx < i) {
vp[i] = 0;
continue;
}
if((i / nx ) * hy == 3 || (i / nx ) * hy == 2) {
vp[i] = fp[i];
continue;
}
break;
default:
break;
}
if(i < nx)
vp[i] = (w/e) * (fp[i] - b * vp[i-1] - a * vp[i+1] - c * vp[i+nx]) + ((1-w) * vp[i]);
else if(i+nx >= vecSize)
vp[i] = (w/e) * (fp[i] - d * vp[i-nx] - b*vp[i-1] - a * vp[i+1]) + (1-w) * vp[i];
else
vp[i] = (w/e) * (fp[i] - d * vp[i-nx] - b*vp[i-1] - a * vp[i+1] - c * vp[i+nx]) + (1-w) * vp[i];
}
// Última iter
switch(this->t) {
case VALIDACAO:
vp[vecSize-1] = 0;
break;
case CAPACITORES:
vp[vecSize-1] = 0;
break;
default:
vp[vecSize-1] = (w/e) * (fp[vecSize-1] - d * vp[vecSize-1-nx] - b * vp[vecSize-1-1]) + (1-w) * vp[vecSize-1];
break;
}
}
return;
}
void poissonSOR::calcErr() {
double max = -1.0;
int idx = -1;
for(int i = 0; i < vecSize; i++) {
double diff = fabs(ground[i]-vp[i]);
if(diff > max) {
max = diff;
idx = i;
}
}
// printf("\nMAX ERRO: %.32lf\nComparing %lf and %lf, index %d (%d, %d)\n", max, ground[idx], vp[idx], idx, idx/ny, idx%ny);
this->erro_vp = max;
}
void poissonSOR::calcErrEle() {
double max = -1.0;
int idx = -1;
for(int i = 0; i < vecSize; i++) {
double diff = fabs(ground_ep[i]-ep[i]);
if(diff >= max) {
max = diff;
idx = i;
}
}
//printf("\nMAX ERRO: %.32lf\nComparing %lf and %lf, index %d (%d, %d)\n", max, ground[idx], vp[idx], idx, idx/ny, idx%ny);
this->erro_ele = max;
}
void poissonSOR::writeOutputData() {
std::string outputFolder = "output";
FILE* f;
char fname[MAX_FNAME_SIZE];
sprintf(fname, "%s/output_SOR_%s_%.4lf_%.4lf.txt", outputFolder.c_str(), typeToString(t).c_str(), hx, hy);
f = fopen(fname,"w");
for(int i = 0; i < this->vecSize; i++) {
fprintf(f, "%lf\n",vp[i]);
}
fclose(f);
sprintf(fname, "%s/elet_SOR_%s_%.4lf_%.4lf.txt", outputFolder.c_str(), typeToString(t).c_str(), hx, hy);
f = fopen(fname, "w");
for(int i = 0; i < this->vecSize; i++) {
fprintf(f, "%lf\n", ep[i]);
}
if(this->grndFunc) {
sprintf(fname, "%s/ground_SOR_%s_%.4lf_%.4lf.txt", outputFolder.c_str(), typeToString(t).c_str(), hx, hy);
f = fopen(fname, "w");
for(int i = 0; i < this->vecSize; i++) {
fprintf(f, "%lf\n", ground[i]);
}
fclose(f);
sprintf(fname, "%s/ground_e_SOR_%s_%.4lf_%.4lf.txt", outputFolder.c_str(), typeToString(t).c_str(), hx, hy);
f = fopen(fname, "w");
for(int i = 0; i < this->vecSize; i++) {
fprintf(f, "%lf\n", ground_ep[i]);
}
fclose(f);
}
}
void poissonSOR::debug() {
printf("\n\nOBJ DEBUG STUFF BELOW!\n\n");
printf("a = %d, b = %d, c = %d, d = %d, e = %d, w = %lf\n", this->a, this->b, this->c, this->d, this->e, this->w);
printf("hx = %.4lf hy = %.4lf nx = %d ny = %d\n", this->hx, this->hy, this->nx, this->ny);
printf("\"this\" is of the type \"");
switch(this->t) {
case VALIDACAO:
printf("validation");
break;
case CAPACITORES:
printf("capacitores");
break;
default:
printf("UNDEFINED");
break;
}
printf("\"\n");
printf("matriz fp (%dx%d)\n", ny, nx);
for(int i = 0; i < nx*ny; i++) {
if(i % nx == 0)
printf("\n");
printf("%08.5lf ", fp[i]);
}
printf("\n\nmatriz ground (%dx%d)\n", ny, nx);
for(int i = 0; i < nx*ny; i++) {
if(i % nx == 0)
printf("\n");
printf("%08.5lf ", ground[i]);
}
printf("\n\nmatriz vp (%dx%d)\n", ny, nx);
for(int i = 0; i < nx*ny; i++) {
if(i % nx == 0)
printf("\n");
printf("%08.5lf ", vp[i]);
}
printf("\n");
printf("\nDiferencial\n");
for(int i = 0; i < nx*ny; i++) {
if(i%nx == 0)
printf("\n");
printf("%08.5lf ", ground[i] - vp[i]);
}
printf("\n");
return;
}
// Calcula o ômega ideal de acordo com a equação para o método SOR
double getOmegaIdeal(int nx, int ny) {
double t = cos(M_PI/nx) + cos(M_PI/ny);
double t2 = t*t;
return ((8 - (sqrt(64-16*t2)))/t2);
}
std::string typeToString(type t) {
switch(t) {
case VALIDACAO:
return "validacao";
break;
case CAPACITORES:
return "capacitores";
break;
default:
throw("Invalid type toString()ed!");
break;
}
}
void poissonSOR::calcCampElet() {
for(int j = 1; j < nx-1; j++) {
for(int i = 1; i < ny-1; i++) {
double dvdx = (vp[nx*i + (j + 1)] - vp[nx*i + (j-1)])/2*hx;
double dvdy = (vp[nx * (i+1) + j] - vp[nx*(i-1) + j])/2*hy;
ep[nx*i + j] = (dvdx + dvdy);
}
}
return;
}
void poissonSOR::calcCampElet_e() {
for(int j = 1; j < nx-1; j++) {
for(int i = 1; i < ny-1; i++) {
double dvdx = (ground[nx*i + (j + 1)] - ground[nx*i + (j-1)])/2*hx;
double dvdy = (ground[nx * (i+1) + j] - ground[nx*(i-1) + j])/2*hy;
ground_ep[nx*i + j] = (dvdx + dvdy);
}
} return;
}