-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollecting_data.cc
552 lines (446 loc) · 18.4 KB
/
collecting_data.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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include "nr3.h"
using namespace std;
//test space for fluid to move through
struct Flow {
//number of verticies per side for grid of test space
static const int dim = 65;
//free flow velocity of fluid
double v0;
//relaxation factor
double w;
//will hold norm of all residuals for psi values
double psi_resid_norm;
//distance between verticies
static const double delta = 1.0/double(dim-1);
//viscosity value of fluid
double visc;
//location of rectangular obstruction in test space (given in x and y coordinates instead of j and l)
static const double plate_upstream = 0.25;
static const double plate_downstream = 0.375;
static const double plate_top = 0.25;
//matricies that will all psi, xi, and respective residual values
MatDoub psi;
MatDoub xi;
MatDoub residuals_psi;
MatDoub residuals_xi;
//Constructor
Flow(double w_in, double v0_in, double visc_in) {
//set all parameters
w = w_in;
v0 = v0_in;
visc= visc_in;
//makes all interior points 0 unless otherwise corrected later
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. (j and l correspond to grid)
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 (boundary condition)
if(x >= plate_upstream && x <= plate_downstream && y <= plate_top){
v0_y = 0.0;
}
//if not in/ on plate, make free flow
else{
v0_y = v0*y;
}
psi[j][ell] = v0_y;
}
}
}
//world's most thrilling deconstructor
~Flow() {}
/* Whenever running code withn new value of w/v/visc, means new test space (so
things need to be reset, which is the only time this is called).
Seemed less computationally expensive than making a new Flow object each time
(but this is literally the code from constructor) */
void reset_everything(){
//makes all interior points 0 unless otherwise corrected later
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. (j and l correspond to grid)
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 (boundary condition)
if(x >= plate_upstream && x <= plate_downstream && y <= plate_top){
v0_y = 0.0;
}
//if not in/ on plate, make free flow
else{
v0_y = v0*y;
}
psi[j][ell] = v0_y;
}
}
}
//for updating any of the parameters we're testing
void setW(double w_in){
w = w_in;
reset_everything();
}
void setVel(double v0_in){
v0 = v0_in;
reset_everything();
}
void setVisc(double visc_in){
visc = visc_in;
reset_everything();
}
//prints out all psi values
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;
}
}
//prints out all xi values
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;
}
}
//prints all local residual values for psi and xi
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;
}
}
//uses boundary conditions and SOR to update all local psi values
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 obstruction is set to
// zero for all points on it.
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.
//can't update this point independently so need to set it at same time
if (j==1) {
psi[0][ell] = psi[1][ell];
}
//Downstream bdry: dpsi/dx = 0.
//can't update this point independently so need to set it at same time
else if (j == dim-2) {
psi[j+1][ell] = psi[j][ell];
}
}
}
}
}
//uses boundary conditions and SOR to update all local xi values
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 (sides and top),
// continue.
if ( ( (x == plate_upstream || x == plate_downstream ) && y <= plate_top)) {
continue;
}
if (x >= plate_upstream && x <= plate_downstream && y == plate_top) {
continue;
}
//using centered finite differencing to find 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);
//xi is 0 inside obstruction always
if(x > plate_upstream && x < plate_downstream && y < plate_top){
xi[j][ell] = 0.0;
}
//using SOR
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];
}
}
/*calculates residual values for psi and xi by measuring
difference between lhs and rhs of equations given in write up*/
void residuals() {
for (int j=0; j<dim; j++) {
for (int ell=0; ell<dim; ell++) {
double x = delta*double(j);
double y = delta*double(ell);
//
//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.
//
else if (j == 0) {
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);
//and now finally appy that to getting residuals
//(see write up for where these equations come from)
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;
}
}
}
}
//finds norm of residuals of psi
void residualNormPsi() {
//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);
psi_resid_norm = sum;
}
//runs a single sweep
void one_sweep() {
psi_update();
xi_update();
residuals();
residualNormPsi();
}
//does the sweeps here so that way it's only one function call from main
double allTheSweeps() {
int repeats = 0;
double lastNorm;
for(int sweep = 0; sweep < 10000; sweep++){
lastNorm = psi_resid_norm;
one_sweep();
//checks for converging
if(psi_resid_norm == lastNorm){
repeats++;
//if residual has converged (avoiding extra computation time)
//stop doing sweeps and return value
if(repeats > 99){
cout << "converged early at " << sweep << " iteration\n";
return psi_resid_norm;
}
}
//reset amount of times value has repeated if residual has updated
else{
repeats = 0;
}
}
//otherwise current value is returned after 10,000 sweeps
return psi_resid_norm;
}
};
int main() {
//will hold current residual so it can be written to file
double resid;
//width for each collumn in data file
int width = 16;
//values from example
static const double w_eg = 1.5;
static const double vel_eg = 1.0;
static const double visc_eg = 0.1;
//creates original flow object with values from example
Flow flow(w_eg, vel_eg, visc_eg);
//evaluates just w values
ofstream outfile;
outfile.open("w_values.dat");
outfile.setf(ios::left);
outfile << "#================================"
<< "================================================================\n";
outfile << "#" << setw(width) << "w" << setw(width) << "residual norm\n";
for(double w = 1.0; w <= 2.0; w+= 0.1){
cout << "\n\n";
flow.setW(w);
resid = flow.allTheSweeps();
outfile << setw(width) << w << setw(width) << resid << endl;
}
outfile.close();
//return back to example value
Flow flow2(w_eg, vel_eg, visc_eg);
//evaluates just velocity values
ofstream out;
out.open("v_values.dat");
out.setf(ios::left);
out << "#================================"
<< "================================================================\n";
out << "#" << setw(width) << "velocity" << setw(width) << "residual norm\n";
for(double v = 0.5; v <= 10.0; v+= 0.5){
flow2.setVel(v);
resid = flow2.allTheSweeps();
out << setw(width) << v << setw(width) << resid << endl;
}
out.close();
//return back to example value
Flow flow3(w_eg, vel_eg, visc_eg);
//evaluates just viscosity values
ofstream outf;
outf.open("visc_values.dat");
outf.setf(ios::left);
outf << "#================================"
<< "================================================================\n";
outf << "#" << setw(width) << "viscosity" << setw(width) << "residual norm\n";
for(double visc = 0.5; visc <= 10.0; visc+= 0.5){
flow3.setVisc(visc);
resid = flow3.allTheSweeps();
outf << setw(width) << visc << setw(width) << resid << endl;
}
outf.close();
//return back to example value
Flow flow4(w_eg, vel_eg, visc_eg);
//now evaluates how all variables affect each other
//by looping through all possible combinations
ofstream otf;
otf.open("all_the_values.dat");
otf << "#================================"
<< "================================================================\n";
otf << "#" << setw(width) << "w" << setw(width) << "velocity" << setw(width) << "viscosity" << setw(width) << "residual norm\n";
for(double w = 1.0; w <= 2.0; w+= 0.1){
flow4.setW(w);
for(double v = 0.5; v <= 10.0; v+= 0.5){
flow4.setVel(v);
for(double visc = 0.5; visc <= 10.0; visc+= 0.5){
flow4.setVisc(visc);
resid = flow4.allTheSweeps();
otf << setw(width) << w << setw(width) << v << setw(width) << visc << setw(width) << resid << endl;
}
}
}
otf.close();
return 0;
}