-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2D.c
521 lines (444 loc) · 24.2 KB
/
main2D.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
Implementação de um método de refino de malha incorporada usando LBM. Baseado no artigo "A generic, mass conservative local grid refinement technique for lattice-Boltzmann schemes"
compilado usando a versão gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
comando para compilação:
gcc -o main2D main2D.c q2d9_library.c -lm
ou
make
O problema consiste basicamente em resolver duas matrizes separadas, cada uma referente a uma das malhas e lidar com os pontos de intersecção entre as mesmas.
*/
#include "q2d9_library.h"
void EscreveDirecoes(Celula **funcDistribuicao, char *nome, int tamX, int tamY)
{
FILE *arq;
arq = fopen(nome,"w");
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j++)
{
for(int c = 0; c < 9; c++)
{
fprintf(arq, " %f ", funcDistribuicao[i][j].f[c]);
}
fprintf(arq, "\n");
}
}
}
/*
Função usada para escrever as direções de cada célula da matriz refinada na ordem que elas coincidem com a matriz grosseira para o pós processamento
*/
void EscreveDirecoes_refinada(Celula **funcDistribuicao, char *nome,int tamX, int tamY, int razaoDeDivisao)
{
FILE *arq;
arq = fopen(nome,"w");
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j++)
{
for(int i2 = 0; i2 < razaoDeDivisao; i2++)
{
for(int j2 = 0; j2 < razaoDeDivisao; j2++)
{
for(int c = 0; c < 9; c++)
{
fprintf(arq, " %f ", funcDistribuicao[i*razaoDeDivisao + i2][j*razaoDeDivisao + j2].f[c]);
}
fprintf(arq, "\n");
}
}
}
}
}
void EscreveTudo(Celula **funcDistribuicao, char *nome, int tamX, int tamY)
{
FILE *arq;
arq = fopen(nome,"w");
fprintf(arq, "#ux uy rho\n");
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j ++)
{
fprintf(arq,"%f %f %f\n", funcDistribuicao[i][j].ux, funcDistribuicao[i][j].uy, funcDistribuicao[i][j].rho);
}
}
fclose(arq);
}
void escreveMatriz(Celula **funcDistribuicao, int tamX, int tamY)
{
FILE *arqX, *arqY, *arqRho;
arqX = fopen("Matriz da Velocidade em X.txt","w");
arqY = fopen("Matriz da Velocidade em Y.txt","w");
arqRho = fopen("Matriz da Densidade.txt","w");
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j++)
{
fprintf(arqX, " %f ", funcDistribuicao[i][j].ux);
fprintf(arqY, " %f ", funcDistribuicao[i][j].uy);
fprintf(arqRho, " %f ", funcDistribuicao[i][j].rho);
}
fprintf(arqX, "\n");
fprintf(arqY, "\n");
fprintf(arqRho, "\n");
}
fclose(arqX);
fclose(arqY);
fclose(arqRho);
}
void escreveMatriz_refinada(Celula **funcDistribuicao, int tamX, int tamY)
{
FILE *arqX, *arqY, *arqRho;
arqX = fopen("Matriz da Velocidade em X - refinada.txt","w");
arqY = fopen("Matriz da Velocidade em Y - refinada.txt","w");
arqRho = fopen("Matriz da Densidade - refinada.txt","w");
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j++)
{
fprintf(arqX, " %f ", funcDistribuicao[i][j].ux);
fprintf(arqY, " %f ", funcDistribuicao[i][j].uy);
fprintf(arqRho, " %f ", funcDistribuicao[i][j].rho);
}
fprintf(arqX, "\n");
fprintf(arqY, "\n");
fprintf(arqRho, "\n");
}
fclose(arqX);
fclose(arqY);
fclose(arqRho);
}
void condContornoPoiseuille_coarse(Celula **funcDistribuicao, int tamX, int tamY, double velX, double velY)
{
double rhoAux = 0;
for(int j = 0; j < tamY; j++)
{
rhoAux = 0;
//known velocity na face da entrada
rhoAux = funcDistribuicao[0][j].f[0] + funcDistribuicao[0][j].f[2] + funcDistribuicao[0][j].f[4];
rhoAux += 2*(funcDistribuicao[0][j].f[3] + funcDistribuicao[0][j].f[6] + funcDistribuicao[0][j].f[7]);
rhoAux = (double)(rhoAux / (1.0 - velX));
funcDistribuicao[0][j].f[1] = funcDistribuicao[0][j].f[3] + (2.0*rhoAux*velX/3.0);
funcDistribuicao[0][j].f[5] = funcDistribuicao[0][j].f[7] + (rhoAux*velX/6.0);
funcDistribuicao[0][j].f[8] = funcDistribuicao[0][j].f[6] + (rhoAux*velX/6.0);
//--------------------------------------------------------------------------------------------------------//
//open boundary na face da saída
if(j>0)//cuidar
{
funcDistribuicao[tamX-1][j].f[1] = 2.0*funcDistribuicao[tamX-2][j].f[1] - funcDistribuicao[tamX-3][j].f[1];
funcDistribuicao[tamX-1][j].f[5] = 2.0*funcDistribuicao[tamX-2][j].f[5] - funcDistribuicao[tamX-3][j].f[5];
funcDistribuicao[tamX-1][j].f[8] = 2.0*funcDistribuicao[tamX-2][j].f[8] - funcDistribuicao[tamX-3][j].f[8];
}
}
for(int i = 0; i < tamX; i++)
{
//bounce back na face do topo
funcDistribuicao[i][tamY-1].f[4] = funcDistribuicao[i][tamY-1].f[2];
funcDistribuicao[i][tamY-1].f[7] = funcDistribuicao[i][tamY-1].f[5];
funcDistribuicao[i][tamY-1].f[8] = funcDistribuicao[i][tamY-1].f[6];
}
}
void condContornoPoiseuille_fine(Celula **funcDistribuicao, int tamX, int tamY, double velX, double velY)
{
double rhoAux = 0;
for(int j = 0; j < tamY; j++)
{
rhoAux = 0;
//known velocity na face da entrada
rhoAux = funcDistribuicao[0][j].f[0] + funcDistribuicao[0][j].f[2] + funcDistribuicao[0][j].f[4];
rhoAux += 2*(funcDistribuicao[0][j].f[3] + funcDistribuicao[0][j].f[6] + funcDistribuicao[0][j].f[7]);
rhoAux = (double)(rhoAux / (1.0 - velX));
funcDistribuicao[0][j].f[1] = funcDistribuicao[0][j].f[3] + (2.0*rhoAux*velX/3.0);
funcDistribuicao[0][j].f[5] = funcDistribuicao[0][j].f[7] + (rhoAux*velX/6.0);
funcDistribuicao[0][j].f[8] = funcDistribuicao[0][j].f[6] + (rhoAux*velX/6.0);
//--------------------------------------------------------------------------------------------------------//
//open boundary na face da saída
if(j>0)
{
funcDistribuicao[tamX-1][j].f[1] = 2.0*funcDistribuicao[tamX-2][j].f[1] - funcDistribuicao[tamX-3][j].f[1];
funcDistribuicao[tamX-1][j].f[5] = 2.0*funcDistribuicao[tamX-2][j].f[5] - funcDistribuicao[tamX-3][j].f[5];
funcDistribuicao[tamX-1][j].f[8] = 2.0*funcDistribuicao[tamX-2][j].f[8] - funcDistribuicao[tamX-3][j].f[8];
}
}
for(int i = 0; i < tamX; i++)
{
//bounce back no fundo
funcDistribuicao[i][0].f[2] = funcDistribuicao[i][0].f[4];
funcDistribuicao[i][0].f[5] = funcDistribuicao[i][0].f[7];
funcDistribuicao[i][0].f[6] = funcDistribuicao[i][0].f[8];
}
}
void condContornoBordas(Celula **funcDistribuicao_c, Celula **funcEquilibrio_c, int tamX_c, int tamY_c, Celula **funcDistribuicao_f, Celula **funcEquilibrio_f, int tamX_f, int tamY_f)
{
double rho = 0.0;
double ux = 0.0;
//superior esquerdo
rho = funcDistribuicao_c[0][tamY_c-1].rho;
ux = funcDistribuicao_c[0][tamY_c-1].ux;
funcDistribuicao_c[0][tamY_c-1].f[1] = funcDistribuicao_c[0][tamY_c-1].f[3] + (funcEquilibrio_c[0][tamY_c-1].f[1] - funcEquilibrio_c[0][tamY_c-1].f[3]);
funcDistribuicao_c[0][tamY_c-1].f[4] = funcDistribuicao_c[0][tamY_c-1].f[2] + (funcEquilibrio_c[0][tamY_c-1].f[4] - funcEquilibrio_c[0][tamY_c-1].f[2]);
funcDistribuicao_c[0][tamY_c-1].f[8] = funcDistribuicao_c[0][tamY_c-1].f[6] + (funcEquilibrio_c[0][tamY_c-1].f[8] - funcEquilibrio_c[0][tamY_c-1].f[6]);
funcDistribuicao_c[0][tamY_c-1].f[7] = 0.5*(rho - rho*ux - 2*funcDistribuicao_c[0][tamY_c-1].f[6] - 2*funcDistribuicao_c[0][tamY_c-1].f[3] - funcDistribuicao_c[0][tamY_c-1].f[0] - funcDistribuicao_c[0][tamY_c-1].f[2] - funcDistribuicao_c[0][tamY_c-1].f[4]);
funcDistribuicao_c[0][tamY_c-1].f[5] = rho*ux + funcDistribuicao_c[0][tamY_c-1].f[7] + funcDistribuicao_c[0][tamY_c-1].f[6] + funcDistribuicao_c[0][tamY_c-1].f[3] - funcDistribuicao_c[0][tamY_c-1].f[8] - funcDistribuicao_c[0][tamY_c-1].f[1];
//superior direito
rho = funcDistribuicao_c[tamX_c-1][tamY_c-1].rho;
ux = funcDistribuicao_c[tamX_c-1][tamY_c-1].ux;
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[1] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[3] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[1]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[4] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[2] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[4] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[2]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[5] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[7] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[5]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[6] = 0.5*(rho - rho*ux - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[0] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[2] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[4] - 2*funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] - 2*funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[8] = rho*ux + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[6] + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7] + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[1] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[5];
//infeior direito
rho = funcDistribuicao_f[tamX_f-1][0].rho;
ux = funcDistribuicao_f[tamX_f-1][0].ux;
funcDistribuicao_f[tamX_f-1][0].f[3] = funcDistribuicao_f[tamX_f-1][0].f[1] + (funcEquilibrio_f[tamX_f-1][0].f[3] - funcEquilibrio_f[tamX_f-1][0].f[1]);
funcDistribuicao_f[tamX_f-1][0].f[2] = funcDistribuicao_f[tamX_f-1][0].f[4] + (funcEquilibrio_f[tamX_f-1][0].f[2] - funcEquilibrio_f[tamX_f-1][0].f[4]);
funcDistribuicao_f[tamX_f-1][0].f[6] = funcDistribuicao_f[tamX_f-1][0].f[8] + (funcEquilibrio_f[tamX_f-1][0].f[6] - funcEquilibrio_f[tamX_f-1][0].f[8]);
funcDistribuicao_f[tamX_f-1][0].f[7] = 0.5*(rho - rho*ux - 2*funcDistribuicao_f[tamX_f-1][0].f[6] - 2*funcDistribuicao_f[tamX_f-1][0].f[3] - funcDistribuicao_f[tamX_f-1][0].f[0] - funcDistribuicao_f[tamX_f-1][0].f[2] - funcDistribuicao_f[tamX_f-1][0].f[4]);
funcDistribuicao_f[tamX_f-1][0].f[5] = rho*ux + funcDistribuicao_f[tamX_f-1][0].f[7] + funcDistribuicao_f[tamX_f-1][0].f[6] + funcDistribuicao_f[tamX_f-1][0].f[3] - funcDistribuicao_f[tamX_f-1][0].f[8] - funcDistribuicao_f[tamX_f-1][0].f[1];
//inferior esquerdo
rho = funcDistribuicao_f[0][0].rho;
ux = funcDistribuicao_f[0][0].ux;
funcDistribuicao_f[0][0].f[2] = funcDistribuicao_f[0][0].f[4] + (funcDistribuicao_f[0][0].f[2] - funcDistribuicao_f[0][0].f[4]);
funcDistribuicao_f[0][0].f[1] = funcDistribuicao_f[0][0].f[3] + (funcDistribuicao_f[0][0].f[1] - funcDistribuicao_f[0][0].f[3]);
funcDistribuicao_f[0][0].f[5] = funcDistribuicao_f[0][0].f[7] + (funcDistribuicao_f[0][0].f[5] - funcDistribuicao_f[0][0].f[7]);
funcDistribuicao_f[0][0].f[6] = 0.5*(rho - rho*ux - funcDistribuicao_f[0][0].f[0] - funcDistribuicao_f[0][0].f[2] - funcDistribuicao_f[0][0].f[4] - 2*funcDistribuicao_f[0][0].f[3] - 2*funcDistribuicao_f[0][0].f[7]);
funcDistribuicao_f[0][0].f[8] = rho*ux + funcDistribuicao_f[0][0].f[6] + funcDistribuicao_f[0][0].f[7] + funcDistribuicao_f[0][0].f[3] - funcDistribuicao_f[0][0].f[1] - funcDistribuicao_f[0][0].f[5];
}
void condContornoBordas_coarse(Celula **funcDistribuicao_c, Celula **funcEquilibrio_c, int tamX_c, int tamY_c)
{
double rho = 0.0;
double ux = 0.0;
//superior esquerdo
rho = funcDistribuicao_c[0][tamY_c-1].rho;
ux = funcDistribuicao_c[0][tamY_c-1].ux;
funcDistribuicao_c[0][tamY_c-1].f[1] = funcDistribuicao_c[0][tamY_c-1].f[3] + (funcEquilibrio_c[0][tamY_c-1].f[1] - funcEquilibrio_c[0][tamY_c-1].f[3]);
funcDistribuicao_c[0][tamY_c-1].f[4] = funcDistribuicao_c[0][tamY_c-1].f[2] + (funcEquilibrio_c[0][tamY_c-1].f[4] - funcEquilibrio_c[0][tamY_c-1].f[2]);
funcDistribuicao_c[0][tamY_c-1].f[8] = funcDistribuicao_c[0][tamY_c-1].f[6] + (funcEquilibrio_c[0][tamY_c-1].f[8] - funcEquilibrio_c[0][tamY_c-1].f[6]);
funcDistribuicao_c[0][tamY_c-1].f[7] = 0.5*(rho - rho*ux - 2*funcDistribuicao_c[0][tamY_c-1].f[6] - 2*funcDistribuicao_c[0][tamY_c-1].f[3] - funcDistribuicao_c[0][tamY_c-1].f[0] - funcDistribuicao_c[0][tamY_c-1].f[2] - funcDistribuicao_c[0][tamY_c-1].f[4]);
funcDistribuicao_c[0][tamY_c-1].f[5] = rho*ux + funcDistribuicao_c[0][tamY_c-1].f[7] + funcDistribuicao_c[0][tamY_c-1].f[6] + funcDistribuicao_c[0][tamY_c-1].f[3] - funcDistribuicao_c[0][tamY_c-1].f[8] - funcDistribuicao_c[0][tamY_c-1].f[1];
//superior direito
rho = funcDistribuicao_c[tamX_c-1][tamY_c-1].rho;
ux = funcDistribuicao_c[tamX_c-1][tamY_c-1].ux;
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[1] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[3] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[1]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[4] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[2] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[4] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[2]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7] = funcDistribuicao_c[tamX_c-1][tamY_c-1].f[5] + (funcEquilibrio_c[tamX_c-1][tamY_c-1].f[7] -funcEquilibrio_c[tamX_c-1][tamY_c-1].f[5]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[6] = 0.5*(rho - rho*ux - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[0] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[2] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[4] - 2*funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] - 2*funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7]);
funcDistribuicao_c[tamX_c-1][tamY_c-1].f[8] = rho*ux + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[6] + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[7] + funcDistribuicao_c[tamX_c-1][tamY_c-1].f[3] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[1] - funcDistribuicao_c[tamX_c-1][tamY_c-1].f[5];
}
void condContornoBordas_fine(Celula **funcDistribuicao_f, Celula **funcEquilibrio_f, int tamX_f, int tamY_f)
{
double rho = 0.0;
double ux = 0.0;
//infeior direito
rho = funcDistribuicao_f[tamX_f-1][0].rho;
ux = funcDistribuicao_f[tamX_f-1][0].ux;
funcDistribuicao_f[tamX_f-1][0].f[3] = funcDistribuicao_f[tamX_f-1][0].f[1] + (funcEquilibrio_f[tamX_f-1][0].f[3] - funcEquilibrio_f[tamX_f-1][0].f[1]);
funcDistribuicao_f[tamX_f-1][0].f[2] = funcDistribuicao_f[tamX_f-1][0].f[4] + (funcEquilibrio_f[tamX_f-1][0].f[2] - funcEquilibrio_f[tamX_f-1][0].f[4]);
funcDistribuicao_f[tamX_f-1][0].f[6] = funcDistribuicao_f[tamX_f-1][0].f[8] + (funcEquilibrio_f[tamX_f-1][0].f[6] - funcEquilibrio_f[tamX_f-1][0].f[8]);
funcDistribuicao_f[tamX_f-1][0].f[7] = 0.5*(rho - rho*ux - 2*funcDistribuicao_f[tamX_f-1][0].f[6] - 2*funcDistribuicao_f[tamX_f-1][0].f[3] - funcDistribuicao_f[tamX_f-1][0].f[0] - funcDistribuicao_f[tamX_f-1][0].f[2] - funcDistribuicao_f[tamX_f-1][0].f[4]);
funcDistribuicao_f[tamX_f-1][0].f[5] = rho*ux + funcDistribuicao_f[tamX_f-1][0].f[7] + funcDistribuicao_f[tamX_f-1][0].f[6] + funcDistribuicao_f[tamX_f-1][0].f[3] - funcDistribuicao_f[tamX_f-1][0].f[8] - funcDistribuicao_f[tamX_f-1][0].f[1];
//inferior esquerdo
rho = funcDistribuicao_f[0][0].rho;
ux = funcDistribuicao_f[0][0].ux;
funcDistribuicao_f[0][0].f[2] = funcDistribuicao_f[0][0].f[4] + (funcDistribuicao_f[0][0].f[2] - funcDistribuicao_f[0][0].f[4]);
funcDistribuicao_f[0][0].f[1] = funcDistribuicao_f[0][0].f[3] + (funcDistribuicao_f[0][0].f[1] - funcDistribuicao_f[0][0].f[3]);
funcDistribuicao_f[0][0].f[5] = funcDistribuicao_f[0][0].f[7] + (funcDistribuicao_f[0][0].f[5] - funcDistribuicao_f[0][0].f[7]);
funcDistribuicao_f[0][0].f[6] = 0.5*(rho - rho*ux - funcDistribuicao_f[0][0].f[0] - funcDistribuicao_f[0][0].f[2] - funcDistribuicao_f[0][0].f[4] - 2*funcDistribuicao_f[0][0].f[3] - 2*funcDistribuicao_f[0][0].f[7]);
funcDistribuicao_f[0][0].f[8] = rho*ux + funcDistribuicao_f[0][0].f[6] + funcDistribuicao_f[0][0].f[7] + funcDistribuicao_f[0][0].f[3] - funcDistribuicao_f[0][0].f[1] - funcDistribuicao_f[0][0].f[5];
}
int confere(Celula **funcDistribuicao, int tamX, int tamY)
{
for(int i = 0; i < tamX; i++)
{
for(int j = 0; j < tamY; j++)
{
if(funcDistribuicao[i][j].rho >= 1000 || funcDistribuicao[i][j].rho <= -1)
{
return 1;
}
}
}
return 0;
}
int main()
{
//pesos
double centro = 4.0/9.0;
double retas = 1.0/9.0;
double diagonais = 1.0/36.0;
double pesos[9] = {centro, retas, retas, retas, retas, diagonais, diagonais, diagonais, diagonais};
//razão de divisão
int razaoDeDivisao = 2;
int tamX_c = 1000, tamY_c = 20;//malha grosseira
int tamX_f = tamX_c*razaoDeDivisao, tamY_f = tamY_c*razaoDeDivisao;//malha refinada
//tamanho do domínio
double dx = 1.0;
double dy = 1.0;
//características dos passos
double dt = 1.0;
double alpha = 0.01;
double a = 3*alpha;
double omega = (double)(1.0 / ( a + 0.5 ));//da onde vem esse termo exatamente
//tempo de relaxação
double tau_c = 1/omega;
double tau_f = 2*tau_c - 0.5;//correto, refiz a conta
printf("tau_c: %f\ntau_f: %f\n",tau_c,tau_f);
printf("tau_f / tau_c: %f\n", tau_f / tau_c);
//numero de passos
int numSteps = 10000;
//caracteristicas do fluido
double ux0 = 0.002;
double uy0 = 0.0;
double rho = 1.0;
double Re = ux0*tamX_c / alpha;
printf("Reynolds: %f\n", Re);
//alocação das matrizes que representam o domínio
Celula **funcDistribuicao_c, **funcEquilibrio_c;
Celula **funcDistribuicao_f, **funcEquilibrio_f;
funcDistribuicao_c = (Celula **)malloc(tamX_c*sizeof(Celula *));
funcEquilibrio_c = (Celula **)malloc(tamX_c*sizeof(Celula *));
funcDistribuicao_f = (Celula **)malloc(tamX_f*sizeof(Celula *));
funcEquilibrio_f = (Celula **)malloc(tamX_f*sizeof(Celula *));
for(int i = 0; i < tamX_c; i++)
{
funcDistribuicao_c[i] = (Celula *)malloc(tamY_c*sizeof(Celula));
funcEquilibrio_c[i] = (Celula *)malloc(tamY_c*sizeof(Celula));
}
//Aqui tenho que alocar a quantidade de colunas refinadas em Y mais a razão de divisão que representa as células da interface
for(int i = 0; i < tamX_f; i++)
{
funcDistribuicao_f[i] = (Celula *)malloc((tamY_f+razaoDeDivisao)*sizeof(Celula));
funcEquilibrio_f[i] = (Celula *)malloc((tamY_f+razaoDeDivisao)*sizeof(Celula));
}
//inicializo o problema para a malha grosseira
for(int i = 0; i < tamX_c; i++)
{
for(int j = 0; j < tamY_c; j ++)
{
funcDistribuicao_c[i][j].ux = ux0;
funcDistribuicao_c[i][j].uy = uy0;
funcDistribuicao_c[i][j].rho = rho;
}
}
//inicializo o problema para a malha refinada
for(int i = 0; i < tamX_f; i++)
{
for(int j = 0; j < tamY_f; j ++)
{
funcDistribuicao_f[i][j].ux = ux0;
funcDistribuicao_f[i][j].uy = uy0;
funcDistribuicao_f[i][j].rho = rho/(razaoDeDivisao*razaoDeDivisao);
}
}
//função distribuição original calculada como a função de equilíbrio no estado inicial
calculaFuncEquilibrio2D(funcDistribuicao_c,funcDistribuicao_c,pesos,tamX_c,tamY_c);
calculaFuncEquilibrio2D(funcDistribuicao_f,funcDistribuicao_f,pesos,tamX_f,tamY_f);
//laço principal
for(int k = 0; k < numSteps; k++)
{
//calcula a nova função de equilíbrio
calculaFuncEquilibrio2D(funcDistribuicao_c,funcEquilibrio_c,pesos,tamX_c,tamY_c);
calculaFuncEquilibrio2D(funcDistribuicao_f,funcEquilibrio_f,pesos,tamX_f,tamY_f);
//calcula a colisão | passo 1
calculaColisao2D(funcDistribuicao_c,funcEquilibrio_c,tau_c,tamX_c,tamY_c);
calculaColisao2D(funcDistribuicao_f,funcEquilibrio_f,tau_f,tamX_f,tamY_f);
//calcula a explosão | passo 2
calculaExplosao2D(funcDistribuicao_c,funcDistribuicao_f,tamX_c,tamY_c,tamX_f,tamY_f,razaoDeDivisao);
//propagação | passo 3
calculaPropagacao2D(funcDistribuicao_c,tamX_c,tamY_c);
calculaPropagacao2D(funcDistribuicao_f,tamX_f,tamY_f+razaoDeDivisao);//propaga na matriz refinada e na interface
//passo 4
for(int i = 0; i < (razaoDeDivisao)-1; i++)
{
//Adicionei essa rotina no dia 19 de janeiro como uma maneira de testar se haveria melhoria no resultado final do algoritmo
condContornoPoiseuille_fine(funcDistribuicao_f,tamX_f,tamY_f,ux0,uy0);
condContornoBordas_fine(funcDistribuicao_f, funcEquilibrio_f, tamX_f, tamY_f);
calculaDensidade2D(funcDistribuicao_f,tamX_f,tamY_f);
calculaVelocidade2D(funcDistribuicao_f,tamX_f,tamY_f);
for(int j = 1; j < tamY_f; j++)
{
funcDistribuicao_f[0][j].uy = 0.0;
funcDistribuicao_f[tamX_f-1][j].uy = 0.0;
}
//
calculaFuncEquilibrio2D(funcDistribuicao_f,funcEquilibrio_f,pesos,tamX_f,tamY_f);
calculaColisao2D(funcDistribuicao_f,funcEquilibrio_f,tau_f,tamX_f,tamY_f);//colide duas vezes somente na matriz refinada
calculaPropagacao2D(funcDistribuicao_f,tamX_f,tamY_f+razaoDeDivisao);//propaga na matriz refinada e na interface
}
//passo 5 | tá bem ruim isso daqui | não sei se precisa ser antes ou depois das condições de contorno
for(int i = 0; i < tamX_c; i++)
{
funcDistribuicao_c[i][0].f[2] = 0;
funcDistribuicao_c[i][0].f[5] = 0;
funcDistribuicao_c[i][0].f[6] = 0;
for(int i2 = 0; i2 < razaoDeDivisao; i2++)
{
for(int j2 = 0; j2 < razaoDeDivisao; j2++)
{
funcDistribuicao_c[i][0].f[2] += funcDistribuicao_f[i*razaoDeDivisao + i2][tamY_f + j2].f[2];
funcDistribuicao_c[i][0].f[5] += funcDistribuicao_f[i*razaoDeDivisao + i2][tamY_f + j2].f[5];
funcDistribuicao_c[i][0].f[6] += funcDistribuicao_f[i*razaoDeDivisao + i2][tamY_f + j2].f[6];
}
}
}
//Condições de contorno
//coarse
condContornoPoiseuille_coarse(funcDistribuicao_c,tamX_c,tamY_c,ux0,uy0);
condContornoBordas_coarse(funcDistribuicao_c, funcEquilibrio_c, tamX_c, tamY_c);
//fine
condContornoPoiseuille_fine(funcDistribuicao_f,tamX_f,tamY_f,ux0,uy0);
condContornoBordas_fine(funcDistribuicao_f, funcEquilibrio_f, tamX_f, tamY_f);
//Recalcula a densidade e a velocidade
calculaDensidade2D(funcDistribuicao_c,tamX_c,tamY_c);
calculaDensidade2D(funcDistribuicao_f,tamX_f,tamY_f);
calculaVelocidade2D(funcDistribuicao_c,tamX_c,tamY_c);
calculaVelocidade2D(funcDistribuicao_f,tamX_f,tamY_f);
for(int j = 1; j < tamY_c; j++)
{
funcDistribuicao_c[0][j].uy = 0.0;
funcDistribuicao_c[tamX_c-1][j].uy = 0.0;
}
for(int j = 1; j < tamY_f; j++)
{
funcDistribuicao_f[0][j].uy = 0.0;
funcDistribuicao_f[tamX_f-1][j].uy = 0.0;
}
//função para conferir se não está divergindo
if(k % 100 == 0)
{
printf("laço: %d\n",k );
if(confere(funcDistribuicao_c,tamX_c,tamY_c))
{
printf("Deu erro na matriz grosseira\n");
return 0;
}
if(confere(funcDistribuicao_f,tamX_f,tamY_f))
{
printf("Deu erro na matriz refinada\n");
return 0;
}
}
}
EscreveDirecoes(funcDistribuicao_c,"results/direções grosseira.txt",tamX_c,tamY_c);
EscreveDirecoes_refinada(funcDistribuicao_f,"results/direções refinadas.txt",tamX_c,tamY_c,razaoDeDivisao);
escreveMatriz(funcDistribuicao_c,tamX_c,tamY_c);
escreveMatriz_refinada(funcDistribuicao_f,tamX_f,tamY_f);
for(int i = 0; i < tamX_c; i++)
{
for(int j = 0; j < tamY_c; j++)
{
for(int c = 0; c < 9; c++)
{
funcDistribuicao_c[i][j].f[c] = funcDistribuicao_f[i*razaoDeDivisao][j*razaoDeDivisao].f[c] + funcDistribuicao_f[i*razaoDeDivisao+1][j*razaoDeDivisao].f[c] + funcDistribuicao_f[i*razaoDeDivisao][j*razaoDeDivisao+1].f[c] + funcDistribuicao_f[i*razaoDeDivisao+1][j*razaoDeDivisao+1].f[c];
}
}
}
calculaDensidade2D(funcDistribuicao_c,tamX_c,tamY_c);
calculaVelocidade2D(funcDistribuicao_c,tamX_c,tamY_c);
//escreveMatriz_refinada(funcDistribuicao_c,tamX_c,tamY_c);
EscreveDirecoes(funcDistribuicao_c,"results/direções Pós-Processamento.txt",tamX_c,tamY_c);
//limpa a memória alocada
limpa(funcDistribuicao_c,tamX_c,tamY_c);
limpa(funcDistribuicao_f,tamX_f,tamY_f);
return 0;
}