-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-weugene.h
executable file
·503 lines (468 loc) · 15 KB
/
utils-weugene.h
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
#ifndef NOT_ZERO
#define NOT_ZERO 1.e-30
#endif
void MinMaxValues(scalar * list, double * arr_eps) {// for each scalar min and max
double arr[10][2], small_val = 1e-10;
int ilist = 0;
for (scalar s in list) {
double mina= HUGE, maxa= -HUGE;
foreach( reduction(min:mina) reduction(max:maxa) ){
if (fabs(s[]) < mina) mina = fabs(s[]);
if (fabs(s[]) > maxa) maxa = fabs(s[]);
}
#if _MPI
MPI_Allreduce (MPI_IN_PLACE, &mina, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce (MPI_IN_PLACE, &maxa, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
#endif
arr[ilist][0] = mina;
arr[ilist][1] = maxa;
ilist++;
// fprintf(stderr, "arr for i=%d", ilist);
}
int i = 0;
for (scalar s in list){
#if EPS_MAXA == 1
if (arr[i][1] > small_val){
arr_eps[i] *=arr[i][1];
}
#elif EPS_MAXA == 2
if (arr[i][1] - arr[i][0] > small_val){
arr_eps[i] *= arr[i][1] - arr[i][0];
}
#else
if (0.5*(arr[i][0] + arr[i][1]) > small_val){
arr_eps[i] *= 0.5*(arr[i][0] + arr[i][1]);
}
#endif
else{
arr_eps[i] *= 1;
}
#ifdef DEBUG_MINMAXVALUES
fprintf(stderr, "MinMaxValues: name=%s, min=%g, max=%g, eps=%g\n", s.name, arr[i][0], arr[i][1], arr_eps[i]);
#endif
i++;
}
}
int count_cells(double t, int i){
int tnc = 0, maxlev = 0;
foreach( reduction(+:tnc) reduction(max:maxlev) ){
tnc++;
if (level > maxlev) maxlev = level;
}
#if _MPI
int nc = 0;
foreach(serial, noauto){
nc++;
}
int rank, h_len;
char hostname[MPI_MAX_PROCESSOR_NAME];
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(hostname, &h_len);
printf("i %d t %g hostname %s rank %d num cells %d total num cells %d compression rate %g\n", i, t, hostname, rank, nc, tnc, pow(2, dimension*maxlev)/tnc);
#else
printf("i %d t %g total num cells %d\n", i, t, tnc);
#endif
fflush(stdout);
return tnc;
}
// statistical values inside cells with liquid
stats statsf_weugene (scalar f, scalar fs)
{
double min = 1e100, max = -1e100, sum = 0., sum2 = 0., volume = 0.;
foreach(reduction(+:sum) reduction(+:sum2) reduction(+:volume)
reduction(max:max) reduction(min:min)){
double dvr = dv()*(1. - fs[]);
double val = f[]*(1. - fs[]);
volume += dvr;
sum += f[]*dvr;
sum2 += sq(f[])*dvr;
if (val > max) max = val;
if (val < min) min = val;
}
if (volume > 0.){
sum /= volume; sum2 /= volume;
}
sum2 -= sq(sum);
fprintf(ferr, "***: %g %g\n", sum, sum2);
stats s;
s.min = min, s.max = max, s.sum = sum, s.volume = volume; //modified by Weugene
s.stddev = sum2 > 0. ? sqrt(sum2) : 0.;
return s;
}
// statistical values inside pure liquid
stats statsf_weugene2 (scalar f, scalar fs)
{
double min = 1e100, max = -1e100, sum = 0., sum2 = 0., volume = 0.;
foreach(reduction(+:sum) reduction(+:sum2) reduction(+:volume)
reduction(max:max) reduction(min:min))
if (fs[] == 0.) {
double dvr = dv()*(1. - fs[]);
volume += dvr;
sum += dvr*f[];
sum2 += dvr*sq(f[]);
if (f[] > max) max = f[];
if (f[] < min) min = f[];
}
if (volume > 0.){
sum /= volume; sum2 /= volume;
}
sum2 -= sq(sum);
fprintf(ferr, "sum=%g\n", sum);
stats s;
s.min = min, s.max = max, s.sum = sum, s.volume = volume;
return s;
}
norm normf_weugene (scalar f, scalar fs)
{
double avg = 0., rms = 0., max = 0., volume = 0.;
foreach(reduction(max:max) reduction(+:avg)
reduction(+:rms) reduction(+:volume)){
double dvr = dv()*(1. - fs[]);
double v = fabs(f[])*(1. - fs[]);
if (v > max) max = v;
volume += dvr;
avg += dvr*v;
rms += dvr*sq(v);
}
norm n;
n.avg = volume ? avg/volume : 0.;
n.rms = volume ? sqrt(rms/volume) : 0.;
n.max = max;
n.volume = volume;
return n;
}
double change_weugene (scalar s, scalar sn, scalar fs)
{
double max = 0.;
foreach(reduction(max:max)) {
double ds = fabs (s[] - sn[])*(1. - fs[]);
if (ds > max)
max = ds;
sn[] = s[];
}
return max;
}
/**
* Smoothing function
* f insput scalar field
* sf - output smoothed scalar field
*/
void filter_scalar(scalar f, scalar sf){
#if dimension <= 2
foreach()
sf[] = (4.*f[] +
2.*(f[0,1] + f[0,-1] + f[1,0] + f[-1,0]) +
f[-1,-1] + f[1,-1] + f[1,1] + f[-1,1])/16.;
#else // dimension == 3
foreach()
sf[] = (8.*f[] +
4.*(f[-1] + f[1] + f[0,1] + f[0,-1] + f[0,0,1] + f[0,0,-1]) +
2.*(f[-1,1] + f[-1,0,1] + f[-1,0,-1] + f[-1,-1] +
f[0,1,1] + f[0,1,-1] + f[0,-1,1] + f[0,-1,-1] +
f[1,1] + f[1,0,1] + f[1,-1] + f[1,0,-1]) +
f[1,-1,1] + f[-1,1,1] + f[-1,1,-1] + f[1,1,1] +
f[1,1,-1] + f[-1,-1,-1] + f[1,-1,-1] + f[-1,-1,1])/64.;
#endif
#if TREE
sf.prolongation = refine_bilinear;
boundary ({sf});
#endif
}
void filter_scalar_N_times(scalar f, scalar sf, int N_smooth){
scalar sf_s[];
filter_scalar(f, sf);
for (int i_smooth=2; i_smooth<=N_smooth; i_smooth++){
filter_scalar(sf, sf_s);
foreach() sf[] = sf_s[];
}
}
/**
Calculate scalar from face vector. compute viscosity in a cell
*/
void calc_scalar_from_face(const face vector vf, scalar vs){
foreach() {
double vsum = 0;
foreach_dimension() {
vsum += vf.x[] + vf.x[1];
}
vs[] = vsum/(2.0*dimension);
}
boundary((scalar *){vs});
}
/**
A function to rescale normals so that they are unit vectors w.r.t. the
2-norm (by default, the 1-norm is adopted for efficiency purposes). */
coord normalize_coord(coord n){
double nn = NOT_ZERO;
foreach_dimension() nn += sq(n.x); // (sqrt(sq(nf.x[]) + sq(nf.y[])))
nn = sqrt(nn);
foreach_dimension() n.x /= nn;
return n;
}
//coord normal (Point point, scalar c) {
// coord n = mycs (point, c);
// return normalize_coord(n);
//}
//
//coord normal_face (Point point, scalar f){
// coord nf;
// foreach_dimension() nf.x = 0.;
// bool interface_plus = interfacial(point, f);
// bool interface_minus = interfacial(neighborp(-1), f);
// if (interface_minus || interface_plus) {
// if (interface_plus) {
// coord n = normal (point, f);
// foreach_dimension() nf.x += n.x;
// }
// if (interface_minus) {
// coord n = normal (neighborp(-1), f);
// nf.x += n.x;
// nf.y += n.y;
// #if dimension > 2
// nf.z += n.z;
// #endif
// }
//// double norm_nf = 0;
//// foreach_dimension() norm_nf += sq(nf.x); // (sqrt(sq(nf.x[]) + sq(nf.y[])))
//// norm_nf = sqrt(norm_nf);
//// foreach_dimension() nf.x /= norm_nf;
// nf = normalize_coord(nf);
// }
// return nf;
//}
//
///**
// *
// * @param point
// * @param f - volume fraction of fluid $1$
// * @param fs - volume fraction of solid
// * @return tau_w - is the tangential surface normal pointing along the surface, into the liquid.
// */
//coord tangential_wall_and_normal_CL_face (Point point, scalar f, scalar fs) {
// coord n_f = normal_face (point, f);
// coord n_fs = normal_face (point, fs);
// double n_f_dot_n_fs = 0;
// foreach_dimension() n_f_dot_n_fs += n_f.x*n_fs.x;
// coord tau_w;
// foreach_dimension() tau_w.x = n_f.x - n_f_dot_n_fs*n_fs.x;
// tau_w = normalize_coord(tau_w);
// return tau_w;
//}
//
//coord normal_face_correction (Point point, scalar f, scalar fs, double theta) {
// coord n_fs = normal_face (point, fs);
// coord tau_w = tangential_wall_and_normal_CL_face (point, f, fs);
// coord n_cor;
// foreach_dimension() n_cor.x = n_fs.x*cos(theta) - tau_w.x*sin(theta);
// return n_cor;
//}
//
///**
//A function to compute 2-norm cell-centered/face-centered normals in every cell/face. */
//
//void compute_normal (scalar f, vector normal_vector) {
// foreach() {
// coord n = normal (point, f);
// foreach_dimension()
// normal_vector.x[] = n.x;
// }
// boundary((scalar*){normal_vector});
//}
//
//void compute_normal_face (scalar f, face vector normal_vector_face) {
// foreach_face() {
// coord n = normal_face (point, f);
// foreach_dimension()
// normal_vector_face.x[] = n.x;
// }
// boundary((scalar*){normal_vector_face});
//}
//
//void compute_tangential_wall_and_normal_CL_face (scalar f, scalar fs, face vector tau_w) {
// foreach_face() {
// coord tau = tangential_wall_and_normal_CL_face (point, f, fs);
// foreach_dimension()
// tau_w.x[] = tau.x;
// }
// boundary((scalar*){tau_w});
//}
//
///**
//A function to suppress glitches after an advection. */
//
//void magnet (scalar f, double error) {
// foreach() {
// f[] = clamp(f[], 0., 1.);
// f[] = (f[] < error ? 0. : (f[] > 1. - error ? 1. : f[]));
// }
// boundary ({f});
//}
//
///**
//A function to compute in each point the divergence of a gradient based flux. */
//
//void my_laplacian (scalar f, scalar l, face vector D) {
// boundary({f, D});
// foreach() {
// l[] = 0.;
// foreach_dimension() l[] += (f[1] - f[0])*D.x[1] - (f[] - f[-1])*D.x[];
// l[] /= sq(Delta);
// }
// boundary({l});
//}
/**
A function to mesure the length of the interface in the cell. Warning: the
length is normalised by the size of the cell. To get the real interface length
you have to multiplie it by the cell size $\Delta$. */
//double interface_length (Point point, scalar c)
//{
// coord n = mycs (point, c);
// double alpha = line_alpha (c[], n);
// coord coord_centroid = {0, 0};
// return line_length_center(n, alpha, &coord_centroid);
//}
#define betwf(val) ((val > 0) && (val < 1))
#define F_LIQ_EPS 1e-6
//void correct_f(scalar f, scalar f_corr){
// double neighb, f1, f2;
// foreach(){
// f_corr[] = f[];
// }
// boundary((scalar *){f_corr});
//// // Correction step
// foreach(){
// if (interfacial (point, f)){
// neighb = f[-1,1];
// f1 = f[-1,0];
// f2 = f[0,1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[-1,0] = f[-1,0]*fabs(f[-1,0] - F_LIQ_EPS);
// f_corr[0, 1] = f[0, 1]*fabs(f[0, 1] - F_LIQ_EPS);
// }
// neighb = f[1,1];
// f1 = f[0,1];
// f2 = f[1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[0,1] = f[0,1]*fabs(f[0,1] - F_LIQ_EPS);
// f_corr[1,0] = f[1,0]*fabs(f[1,0] - F_LIQ_EPS);
// }
// neighb = f[1,-1];
// f1 = f[1,0];
// f2 = f[0,-1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[1,0] = f[1,0]*fabs(f[1,0] - F_LIQ_EPS);
// f_corr[0,-1] = f[0,-1]*fabs(f[0,-1] - F_LIQ_EPS);
// }
// neighb = f[-1,-1];
// f1 = f[0,-1];
// f2 = f[-1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[0,-1] = f[0,-1]*fabs(f[0,-1] - F_LIQ_EPS);
// f_corr[-1,0] = f[-1,0]*fabs(f[-1,0] - F_LIQ_EPS);
// }
// }
// }
// boundary((scalar *){f_corr});
//}
//
//double average_neighbors(Point point, scalar c, int i, int j, int k){
// double res = 0;
// int kk=0;
// for (int ii = -1; ii <= 1; ii++)
// for (int jj = -1; jj <= 1; jj++)
//#if dimension>2
// for (int kk = -1; kk <= 1; kk++)
//#endif
// res += c[i + ii, j + jj, k + kk];
// return res/pow(3, dimension);
//}
//void correct_f(scalar f, scalar f_corr){
// double avg1, avg2, neighb, f1, f2;
// foreach(){
// f_corr[] = f[];
// }
// boundary((scalar *){f_corr});
// // Correction step
// foreach(){
// if (interfacial (point, f)){
// neighb = f[-1,1];
// f1 = f[-1,0];
// f2 = f[0,1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// avg1 = average_neighbors(point, f, -1, 0, 0);
// avg2 = average_neighbors(point, f, 0, 1, 0);
// f_corr[-1,0] = (avg1/(avg1 + avg2));
// f_corr[0,1] = (avg2/(avg1 + avg2));
// }
// neighb = f[1,1];
// f1 = f[0,1];
// f2 = f[1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// avg1 = average_neighbors(point, f, 0, 1, 0);
// avg2 = average_neighbors(point, f, 1, 0, 0);
// f_corr[0,1] = (avg1/(avg1 + avg2));
// f_corr[1,0] = (avg2/(avg1 + avg2));
// }
// neighb = f[1,-1];
// f1 = f[1,0];
// f2 = f[0,-1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// avg1 = average_neighbors(point, f, 1, 0, 0);
// avg2 = average_neighbors(point, f, 0, -1, 0);
// f_corr[1,0] = (avg1/(avg1 + avg2));
// f_corr[0,-1] = (avg2/(avg1 + avg2));
// }
// neighb = f[-1,-1];
// f1 = f[0,-1];
// f2 = f[-1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// avg1 = average_neighbors(point, f, 0, -1, 0);
// avg2 = average_neighbors(point, f, -1, 0, 0);
// f_corr[0,-1] = (avg1/(avg1 + avg2));
// f_corr[-1,0] = (avg2/(avg1 + avg2));
// }
// }
// }
// boundary((scalar *){f_corr});
//}
//void correct_f(scalar f, scalar f_corr){
// double avg1, avg2, neighb, f1, f2;
// foreach(){
// f_corr[] = f[];
// }
// boundary((scalar *){f_corr});
// // Correction step
// foreach(){
// if (interfacial (point, f)){
// neighb = f[-1,1];
// f1 = f[-1,0];
// f2 = f[0,1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[-1,0] = fabs(f[-1,0] - F_LIQ_EPS);
// f_corr[0,1] = fabs(f[0,1] - F_LIQ_EPS);
// }
// neighb = f[1,1];
// f1 = f[0,1];
// f2 = f[1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[0,1] = fabs(f[0,1] - F_LIQ_EPS);
// f_corr[1,0] = fabs(f[1,0] - F_LIQ_EPS);
// }
// neighb = f[1,-1];
// f1 = f[1,0];
// f2 = f[0,-1];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[1,0] = fabs(f[1,0] - F_LIQ_EPS);
// f_corr[0,-1] = fabs(f[0,-1] - F_LIQ_EPS);
// }
// neighb = f[-1,-1];
// f1 = f[0,-1];
// f2 = f[-1,0];
// if ( !betwf(f1) && !betwf(f2) && betwf(neighb) ){
// f_corr[0,-1] = fabs(f[0,-1] - F_LIQ_EPS);
// f_corr[-1,0] = fabs(f[-1,0] - F_LIQ_EPS);
// }
// }
// }
// boundary((scalar *){f_corr});
//}