-
Notifications
You must be signed in to change notification settings - Fork 2
/
fluid_dynamics.cc
450 lines (367 loc) · 10.2 KB
/
fluid_dynamics.cc
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
#include "nr3.h"
using namespace std;
struct Flow {
const int dim = 65;
const double v0 = 1.0;
double w;
const double delta = 1.0/double(dim-1);
const double visc = 0.1;
ofstream outfile;
const double plate_upstream = 0.25;
const double plate_downstream = 0.375;
const double plate_top = 0.25;
MatDoub psi;
MatDoub xi;
MatDoub residuals_psi;
MatDoub residuals_xi;
Flow(double w_in) {
w = w_in;
xi.assign(dim, dim, 0.0);
psi.assign(dim, dim, 0.0);
residuals_psi.assign(dim, dim, 0.0);
residuals_xi.assign(dim, dim, 0.0);
//
//Initialize Psi to free flow state.
//
for (int ell=0; ell<dim; ell++) {
double y = delta*double(ell);
double v0_y = v0 * y;
for (int j=0; j<dim; j++) {
double x = delta*double(j);
//psi also 0 along and over plate surface
if(x >= plate_upstream && x <= plate_downstream && y <= plate_top){
v0_y = 0.0;
}else{
v0_y = v0*y;
}
psi[j][ell] = v0_y;
}
}
}
~Flow() {}
void printPsi() {
cout << "\n\nPrinting Psi . . . \n";
for (int j=dim-1; j>-1; j--) {
for (int ell=0; ell<dim; ell++) {
cout << setw(8)<< setprecision(2) << psi[ell][j];
}
cout << endl;
}
}
void printXi() {
cout << "\n\nPrinting Xi . . .\n";
for (int j=dim-1; j>-1; j--) {
for (int ell=0; ell<dim; ell++) {
cout << setw(8)<< setprecision(2) << xi[ell][j];
}
cout << endl;
}
}
void printResiduals() {
cout<< "\n\nResiduals for Psi: \n";
for (int j=dim-1; j>-1; j--) {
for (int ell=0; ell<dim; ell++) {
cout << setw(8)<< setprecision(2) << residuals_psi[ell][j];
}
cout << endl;
}
cout << "\n\nResiduals for Xi: \n";
for (int j=dim-1; j>-1; j--) {
for (int ell=0; ell<dim; ell++) {
cout << setw(8)<< setprecision(2) << residuals_xi[ell][j];
}
cout << endl;
}
}
void psi_update() {
for (int j=1; j<dim-1; j++) {
double x = delta*j;
for (int ell=1; ell<dim; ell++) {
double y = delta * ell;
//
//Make sure surface is set to
// zero for all points.
//
if(x >= plate_upstream && x <= plate_downstream && y <= plate_top){
psi[j][ell] = 0.0;
}
//
//Implementing upper boundary
// condition.
//
else if (ell == dim-1) {
psi[j][ell] = v0 * y;
}
//
//Update interior points...
//
else {
psi[j][ell] = w * .25 * ( psi[j+1][ell] + psi[j-1][ell]
+ psi[j][ell+1] + psi[j][ell-1] - delta * delta * xi[j][ell] )
+ (1.0 - w ) * psi[j][ell];
//
//Upstream bdry: - dpsi/dx = 0.
//
if (j==1) {
psi[0][ell] = psi[1][ell];
}
//
//Downstream bdry: dpsi/dx = 0.
//
else if (j == dim-2) {
psi[j+1][ell] = psi[j][ell];
}
}
}
}
}
void xi_update() {
//
//Top of obstruction bdry condition.
//
int el = int(plate_top/delta);
for (int j=int(plate_upstream/delta); j < int(plate_downstream/delta); j++){
xi[j][el] = psi[j][el+1] * 2.0 / (delta * delta);
}
//
//Left and right obstruction bdry condition.
//
int j_left = int(plate_upstream / delta);
int j_right = int(plate_downstream / delta);
for (int ell = 0; ell <= int(plate_top / delta); ell++) {
xi[j_left][ell] = psi[j_left - 1][ell] * (2.0 / (delta*delta) );
xi[j_right][ell] = psi[j_right+1][ell] * (2.0 / (delta*delta) );
}
//
//Update interior points
//
for (int j=1; j<dim-1; j++) {
double x = delta*j;
for (int ell=1; ell<dim-1; ell++) {
double y = delta * ell;
//
//If point is ON obstruction boundary,
// continue.
//
if ( ( (x == plate_upstream || x == plate_downstream ) && y <= plate_top)) {
continue;
}
if (x >= plate_upstream && x <= plate_downstream && y == plate_top) {
continue;
}
double dpsi_dx = (psi[j+1][ell]-psi[j-1][ell]) / (2.0 * delta);
double dpsi_dy = (psi[j][ell+1]-psi[j][ell-1]) / (2.0 * delta);
double dxi_dx = (xi[j+1][ell] - xi[j-1][ell]) / (2.0 * delta);
double dxi_dy = (xi[j][ell+1] - xi[j][ell-1]) / (2.0 * delta);
double source = (1.0 / visc) * (dpsi_dy * dxi_dx - dpsi_dx * dxi_dy);
if(x > plate_upstream && x < plate_downstream && y < plate_top){
xi[j][ell] = 0.0;
}
else {
xi[j][ell] = w * .25 * ( xi[j+1][ell] + xi[j-1][ell]
+ xi[j][ell+1] + xi[j][ell-1] - delta * delta * source )
+ (1.0 - w ) * xi[j][ell];
}
}
}
//
//Downstream bdry condition:
// dxi / dx = 0.
//
for (int ell=0; ell<dim; ell++) {
xi[dim-1][ell] = xi[dim-2][ell];
}
}
void residuals() {
double residual_norm;
int counter = 0;
for (int j=0; j<dim; j++) {
for (int ell=0; ell<dim; ell++) {
double x = delta*double(j);
double y = delta*double(ell);
//cout << "inner iterated: ell = " << ell << endl;
//
//Centerline boundaries:
//
if (ell==0){
residuals_psi[j][ell] = psi[j][ell];
residuals_xi[j][ell] = xi[j][ell];
}
//
//Downstream obstruction bdry.
//
else if (j == int( plate_downstream / delta)
&& ell < int(plate_top / delta) ) {
residuals_psi[j][ell] = psi[j][ell];
residuals_xi[j][ell] = xi[j][ell] - (2.0 / (delta * delta) ) *
psi[j+1][ell];
}
//
//Upstream obstruction bdry.
//
else if (j == int( plate_upstream / delta)
&& ell < int(plate_top / delta) ) {
residuals_psi[j][ell] = psi[j][ell];
residuals_xi[j][ell] = xi[j][ell] - (2.0 / (delta * delta) ) *
psi[j-1][ell];
}
//
//Top obstruction bdry.
//
else if (ell == int(plate_top / delta) && j >= int(plate_upstream / delta)
&& j <= int(plate_downstream / delta) ) {
residuals_psi[j][ell] = psi[j][ell];
residuals_xi[j][ell] = xi[j][ell] - (2.0 / (delta*delta) ) *
psi[j][ell+1];
}
//
//Upstream Bdry.
//
//Should we be dividing by
// 2.0 to get the derivative?!
//
else if (j == 0) {
//cout << "on upstream. iteration no.: " << counter << "\n";
counter++;
residuals_psi[j][ell] = - (psi[j+1][ell] - psi[j][ell]) / ( delta);
residuals_xi[j][ell] = xi[j][ell];
}
//
//Downstream Bdry.
//
else if (j == dim-1) {
residuals_psi[j][ell] = - (psi[j-1][ell] - psi[j][ell]) / (delta);
residuals_xi[j][ell] = xi[j][ell];
}
//
//Top bdry.
//
else if (ell == dim-1) {
residuals_psi[j][ell] = (psi[j][ell] - psi[j][ell-1]) / (delta)
- v0;
residuals_xi[j][ell] = xi[j][ell];
}
//
//Inside of obstruction.
//
else if (x > plate_upstream && x < plate_downstream && y < plate_top) {
residuals_xi[j][ell] = xi[j][ell];
residuals_psi[j][ell]=psi[j][ell];
}
//
//Interior points
//
else {
//
//Compute second derivatives...
//
double ddpsi_ddx = (psi[j+1][ell] - 2.0*psi[j][ell] + psi[j-1][ell])
/ (delta*delta);
double ddpsi_ddy = (psi[j][ell+1] - 2.0*psi[j][ell] + psi[j][ell-1])
/ (delta*delta);
double ddxi_ddx = (xi[j+1][ell] - 2.0*xi[j][ell] + xi[j-1][ell])
/ (delta*delta);
double ddxi_ddy = (xi[j][ell+1] - 2.0*xi[j][ell] + xi[j][ell-1])
/ (delta*delta);
//
//...and first derivatives.
//
double dpsi_dx = (psi[j+1][ell]-psi[j-1][ell]) / (2.0 * delta);
double dpsi_dy = (psi[j][ell+1]-psi[j][ell-1]) / (2.0 * delta);
double dxi_dx = (xi[j+1][ell] - xi[j-1][ell]) / (2.0 * delta);
double dxi_dy = (xi[j][ell+1] - xi[j][ell-1]) / (2.0 * delta);
double source = (1.0 / visc) * (dpsi_dy * dxi_dx - dpsi_dx * dxi_dy);
double laplace_psi = ddpsi_ddx + ddpsi_ddy;
residuals_psi[j][ell] = - xi[j][ell] + laplace_psi;
double laplace_xi = ddxi_ddx + ddxi_ddy;
residuals_xi[j][ell] = laplace_xi - source;
}
}
}
}
double residualNorm() {
double norm_psi_reimann=0.0;
double norm_xi_reimann=0.0;
//
//Calculate integral using reimann sum.
//
double integral=0.0;
for (int j=0; j<dim-1; j++) {
for (int ell=0; ell<dim-1; ell++) {
double rave = 0.25*(residuals_psi[j][ell] + residuals_psi[j+1][ell]
+ residuals_psi[j][ell+1] + residuals_psi[j+1][ell+1]);
integral += delta*delta*rave*rave;
}
}
norm_psi_reimann = sqrt(integral);
cout << "Using reimann ... " << norm_psi_reimann << endl;
//
//Calculate integral using Root mean square.
//
double sum=0.0;
for (int j=0; j<dim; j++) {
for (int ell=0; ell<dim; ell++) {
sum += (residuals_psi[j][ell] * residuals_psi[j][ell])*delta*delta;
}
}
sum = sqrt(sum);
cout << "Using averaged... " << sum << endl;
return sum;
}
double sweep() {
psi_update();
xi_update();
cout << "finished updates...\n";
residuals();
return residualNorm();
}
void data_out() {
outfile.open("data.dat");
int width = 16;
outfile.setf(ios::left);
outfile << "#================================"
<< "================================"
<< "================================\n";
outfile << "#" << setw(width) << "x" << setw(width) << "y"
<< setw(width) << "Psi" << setw(width) << "Res_Psi"
<< setw(width) << "Xi" << "Res_Xi\n";
outfile << "#================================"
<< "================================"
<< "================================\n";
for (int j = 0; j<dim; j++) {
double x = delta*double(j);
for (int ell=0; ell<dim; ell++) {
double y = delta*double(ell);
outfile << setw(width) << x << setw(width) << y
<< setw(width) << psi[j][ell] << setw(width) << residuals_psi[j][ell]
<< setw(width) << xi[j][ell] << setw(width) << residuals_xi [j][ell]
<< endl;
}
outfile << endl;
}
outfile.close();
}
};
int main() {
Flow flow(1.6);
//flow.printPsi();
cout << endl;
//flow.printXi();
cout<< endl;
for(int patrick = 0; patrick < 10000; patrick++){
cout << flow.sweep() << endl << endl;
//flow.printPsi();
cout << endl << endl;
//flow.printXi();
cout << "\n\n";
//flow.printResiduals();
}
//flow.printPsi();
//flow.printXi();
flow.data_out();
//flow.printPsi();
cout<< endl;
//flow.printXi();
flow.printResiduals();
return 0;
}