-
Notifications
You must be signed in to change notification settings - Fork 7
/
advection.c
888 lines (822 loc) · 33.8 KB
/
advection.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
///////////////////////////////////////////////////////////////////////////////
///
/// \file advection.c
///
/// \brief Solver for advection step
///
/// \author Mingang Jin, Qingyan Chen
/// Purdue University
/// Jin55@purdue.edu, YanChen@purdue.edu
/// Wangda Zuo
/// University of Miami
/// W.Zuo@miami.edu
///
/// \date 8/3/2013
///
/// This file provides functions that used for the advection step of FFD method.
/// The advection starts with \c advect(). Then different subroutines are
/// called according to the properties of the variables that are sorted by
/// the location of variables assigned in the control volume.
/// Velocities at X, Y and Z directions are locatted
/// on the surface of the control volume. They are computed using
/// subroutines: \c trace_vx(), \c trace_vy() and \c trace_vz().
/// Scalar variables are in the center of control volume and they are computed
/// using \c trace_scalar().
///
///////////////////////////////////////////////////////////////////////////////
#include "advection.h"
///////////////////////////////////////////////////////////////////////////////
/// Entrance of advection step
///
/// Specific method for advection will be selected according to the variable
/// type.
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type The type of variable for advection solver
///\param index Index of trace substances or species
///\param d Pointer to the computed variables at previous time step
///\param d0 Pointer to the computed variables for current time step
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int advect(PARA_DATA *para, REAL **var, int var_type, int index,
REAL *d, REAL *d0, int **BINDEX) {
int flag;
switch (var_type) {
case VX:
flag = trace_vx(para, var, var_type, d, d0, BINDEX);
if(flag!=0)
ffd_log("advect(): Failed in advection for X-velocity.",
FFD_ERROR);
break;
case VY:
flag = trace_vy(para, var, var_type, d, d0, BINDEX);
if(flag!=0)
ffd_log("advect(): Failed in advection for Y-velocity.",
FFD_ERROR);
break;
case VZ:
flag = trace_vz(para, var, var_type, d, d0, BINDEX);
if(flag!=0)
ffd_log("advect(): Failed in advection for Z-velocity.",
FFD_ERROR);
break;
case TEMP:
case TRACE:
flag = trace_scalar(para, var, var_type, index, d, d0, BINDEX);
if(flag!=0) {
sprintf(msg,
"advect(): Failed in advection for scalar variable type %d.",
var_type);
ffd_log(msg, FFD_ERROR);
}
break;
default:
flag = 1;
sprintf(msg, "advect(): Advection function not defined for variable "
"type %d.", var_type);
ffd_log(msg, FFD_ERROR);
}
return flag;
} // End of advect( )
///////////////////////////////////////////////////////////////////////////////
/// Advection for velocity at X-direction
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type The type of variable for advection solver
///\param d Pointer to the computed variables at previous time step
///\param d0 Pointer to the computed variables for current time step
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int trace_vx(PARA_DATA *para, REAL **var, int var_type, REAL *d, REAL *d0,
int **BINDEX) {
int i, j, k;
int it;
int itmax = 20000; // Max number of iterations for backward tracing
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL x_1, y_1, z_1;
REAL dt = para->mytime->dt;
REAL u0, v0, w0;
REAL *x = var[X], *y = var[Y], *z = var[Z];
REAL *gx = var[GX];
REAL *u = var[VX], *v = var[VY], *w = var[VZ];
REAL *flagu = var[FLAGU];
int COOD[3], LOC[3];
REAL OL[3];
int OC[3];
FOR_U_CELL
if(flagu[IX(i,j,k)]>=0) continue;
/*-----------------------------------------------------------------------
| Step 1: Tracing Back
-----------------------------------------------------------------------*/
// Get velocities at the location of VX
u0 = u[IX(i,j,k)];
v0 = (REAL) 0.5
* ((v[IX(i, j,k)]+v[IX(i, j-1,k)])*( x[IX(i+1,j,k)]-gx[IX(i,j,k)])
+(v[IX(i+1,j,k)]+v[IX(i+1,j-1,k)])*(gx[IX(i, j,k)]- x[IX(i,j,k)]))
/ (x[IX(i+1,j,k)]-x[IX(i,j,k)]);
w0 = (REAL) 0.5
* ((w[IX(i, j,k)]+w[IX(i ,j, k-1)])*( x[IX(i+1,j,k)]-gx[IX(i,j,k)])
+(w[IX(i+1,j,k)]+w[IX(i+1,j, k-1)])*(gx[IX(i, j,k)]- x[IX(i,j,k)]))
/ (x[IX(i+1,j,k)]-x[IX(i,j,k)]);
// Find the location at previous time step
OL[X] =gx[IX(i,j,k)] - u0*dt;
OL[Y] = y[IX(i,j,k)] - v0*dt;
OL[Z] = z[IX(i,j,k)] - w0*dt;
// Initialize the coordinates of previous step
OC[X] = i;
OC[Y] = j;
OC[Z] = k; //OC is the trace back coodinates
// Initialize the signs for track process
// Completed: 0; In process: 1
COOD[X] = 1;
COOD[Y] = 1;
COOD[Z] = 1;
// Initialize the signs for recording if the track back hits the boundary
// Hit the boundary: 0; Not hit the boundary: 1
LOC[X] = 1;
LOC[Y] = 1;
LOC[Z] = 1;
//Initialize the number of iterations
it=1;
// Trace back more if the any of the trace is still in process
while(COOD[X]==1 || COOD[Y] ==1 || COOD[Z] ==1)
{
it++;
// If trace in X is in process and donot hit the boundary
if(COOD[X]==1 && LOC[X]==1)
set_x_location(para, var, flagu, gx, u0, i, j, k, OL, OC, LOC, COOD);
// If trace in Y is in process and donot hit the boundary
if(COOD[Y]==1 && LOC[Y]==1)
set_y_location(para, var, flagu, y, v0, i, j, k, OL, OC, LOC, COOD);
// If trace in Z is in process and donot hit the boundary
if(COOD[Z]==1 && LOC[Z]==1)
set_z_location(para, var, flagu, z, w0, i, j, k, OL, OC, LOC, COOD);
if(it>itmax)
{
printf("Error: advection.c, can not track the location for VX(%d, %d,%d)",
i, j, k);
printf("after %d iterations.\n", it);
return 1;
}
} // End of while() for backward tracing
// Set the coordinates of previous location if it is as boundary
if(u0>0 && LOC[X]==0) OC[X] -=1;
if(v0>0 && LOC[Y]==0) OC[Y] -=1;
if(w0>0 && LOC[Z]==0) OC[Z] -=1;
// Fixme: Do not understand here. Should it be
// if(u0<0 && LOC[X] = 0) OC[X] += 1;
if(u0<0 && LOC[X]==1) OC[X] -=1;
if(v0<0 && LOC[Y]==1) OC[Y] -=1;
if(w0<0 && LOC[Z]==1) OC[Z] -=1;
/*-------------------------------------------------------------------------
| Interpolate
-------------------------------------------------------------------------*/
x_1 = (OL[X]-gx[IX(OC[X],OC[Y],OC[Z])])
/ (gx[IX(OC[X]+1,OC[Y],OC[Z])]-gx[IX(OC[X],OC[Y],OC[Z])]);
y_1 = (OL[Y]-y[IX(OC[X],OC[Y],OC[Z])])
/ (y[IX(OC[X],OC[Y]+1,OC[Z])]-y[IX(OC[X],OC[Y],OC[Z])]);
z_1 = (OL[Z]-z[IX(OC[X],OC[Y],OC[Z])])
/ (z[IX(OC[X],OC[Y],OC[Z]+1)]-z[IX(OC[X],OC[Y],OC[Z])]);
d[IX(i,j,k)] = interpolation(para, d0, x_1, y_1, z_1, OC[X], OC[Y], OC[Z]);
END_FOR // End of loop for all cells
/*---------------------------------------------------------------------------
| define the b.c.
---------------------------------------------------------------------------*/
set_bnd(para, var, var_type, 0, d, BINDEX);
return 0;
} // End of trace_vx()
///////////////////////////////////////////////////////////////////////////////
/// Advection for velocity at Y-direction
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type The type of variable for advection solver
///\param d Pointer to the computed variables at previous time step
///\param d0 Pointer to the computed variables for current time step
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int trace_vy(PARA_DATA *para, REAL **var, int var_type, REAL *d, REAL *d0,
int **BINDEX) {
int i, j, k;
int it;
int itmax = 20000; // Max number of iterations for backward tracing
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL x_1, y_1, z_1;
REAL dt = para->mytime->dt;
REAL u0, v0, w0;
REAL *x = var[X], *y = var[Y], *z = var[Z];
REAL *gy = var[GY];
REAL *u = var[VX], *v = var[VY], *w = var[VZ];
REAL *flagv = var[FLAGV];
int COOD[3], LOC[3];
REAL OL[3];
int OC[3];
FOR_V_CELL
// Do not trace for boundary cells
if(flagv[IX(i,j,k)]>=0) continue;
/*-------------------------------------------------------------------------
| Step 1: Tracing Back
-------------------------------------------------------------------------*/
// Get velocities at the location of VY
u0 = (REAL) 0.5
* ((u[IX(i,j,k)]+u[IX(i-1,j, k)])*(y [IX(i,j+1,k)]-gy[IX(i,j,k)])
+(u[IX(i,j+1,k)]+u[IX(i-1,j+1,k)])*(gy[IX(i,j, k)]-y[IX(i,j,k)]))
/ (y[IX(i,j+1,k)]-y[IX(i,j,k)]);
v0 = v[IX(i,j,k)];
w0 = (REAL) 0.5
* ((w[IX(i,j,k)]+w[IX(i,j,k-1)])*(y[IX(i,j+1,k)]-gy[IX(i,j,k)])
+(w[IX(i,j+1,k)]+w[IX(i,j+1,k-1)])*(gy[IX(i,j,k)]-y[IX(i,j,k)]))
/ (y[IX(i,j+1,k)]-y[IX(i,j,k)]);
// Find the location at previous time step
OL[X] = x[IX(i,j,k)] - u0*dt;
OL[Y] = gy[IX(i,j,k)] - v0*dt;
OL[Z] = z[IX(i,j,k)] - w0*dt;
// Initialize the coordinates of previous step
OC[X] = i;
OC[Y] = j;
OC[Z] = k;
// Initialize the signs for track process
// Completed: 0; In process: 1
COOD[X] = 1;
COOD[Y] = 1;
COOD[Z] = 1;
// Initialize the signs for recording if the track back hits the boundary
// Hit the boundary: 0; Not hit the boundary: 1
LOC[X] = 1;
LOC[Y] = 1;
LOC[Z] = 1;
//Initialize the number of iterations
it=1;
// Trace back more if the any of the trace is still in process
while(COOD[X]==1 || COOD[Y] ==1 || COOD[Z] == 1)
{
it++;
if(COOD[X]==1 && LOC[X]==1)
set_x_location(para, var, flagv, x, u0, i, j, k, OL, OC, LOC, COOD);
if(COOD[Y]==1 && LOC[Y]==1)
set_y_location(para, var, flagv, gy, v0, i, j, k, OL, OC, LOC, COOD);
if(COOD[Z]==1 && LOC[Z]==1)
set_z_location(para, var, flagv, z, w0, i, j, k, OL, OC, LOC, COOD);
if(it>itmax) {
printf("Error: advection.c can not track the location for VY(%d, %d,%d)",
i, j, k);
printf("after %d iterations.\n", it);
return 1;
}
} // End of while() loop
// Set the coordinates of previous location if it is as boundary
if(u0>=0 && LOC[X] == 0) OC[X] -=1;
if(v0>=0 && LOC[Y] == 0) OC[Y] -=1;
if(w0>=0 && LOC[Z] == 0) OC[Z] -=1;
// Fixme: Do not understand here. Should it be
// if(u0<0 && LOC[X] = 0) OC[X] += 1;
if(u0<0 && LOC[X]==1) OC[X] -=1;
if(v0<0 && LOC[Y]==1) OC[Y] -=1;
if(w0<0 && LOC[Z]==1) OC[Z] -=1;
/*-------------------------------------------------------------------------
| Interpolating for all variables
-------------------------------------------------------------------------*/
x_1 = (OL[X]-x[IX(OC[X],OC[Y],OC[Z])])
/ (x[IX(OC[X]+1,OC[Y],OC[Z])]-x[IX(OC[X],OC[Y],OC[Z])]);
y_1 = (OL[Y]-gy[IX(OC[X],OC[Y],OC[Z])])
/ (gy[IX(OC[X],OC[Y]+1,OC[Z])]-gy[IX(OC[X],OC[Y],OC[Z])]);
z_1 = (OL[Z]-z[IX(OC[X],OC[Y],OC[Z])])
/ (z[IX(OC[X],OC[Y],OC[Z]+1)]-z[IX(OC[X],OC[Y],OC[Z])]);
d[IX(i,j,k)] = interpolation(para, d0, x_1, y_1, z_1, OC[X],OC[Y],OC[Z]);
END_FOR // End of For() loop for each cell
/*---------------------------------------------------------------------------
| define the b.c.
---------------------------------------------------------------------------*/
set_bnd(para, var, var_type, 0, d, BINDEX);
return 0;
} // End of trace_vy()
///////////////////////////////////////////////////////////////////////////////
/// Advection for velocity at Z-direction
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type The type of variable for advection solver
///\param d Pointer to the computed variables at previous time step
///\param d0 Pointer to the computed variables for current time step
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int trace_vz(PARA_DATA *para, REAL **var, int var_type, REAL *d, REAL *d0,
int **BINDEX) {
int i, j, k;
int it;
int itmax = 20000; // Max number of iterations for backward tracing
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL x_1, y_1, z_1;
REAL dt = para->mytime->dt;
REAL u0, v0, w0;
REAL *x = var[X], *y = var[Y], *z = var[Z];
REAL *gz = var[GZ];
REAL *u = var[VX], *v = var[VY], *w = var[VZ];
REAL *flagw = var[FLAGW];
int COOD[3], LOC[3];
REAL OL[3];
int OC[3];
FOR_W_CELL
// Do not trace for boundary cells
if(flagw[IX(i,j,k)]>=0) continue;
/*-------------------------------------------------------------------------
| Step 1: Tracing Back
-------------------------------------------------------------------------*/
// Get velocities at the location of VZ
u0 = (REAL) 0.5
* ((u[IX(i,j,k )]+u[IX(i-1,j,k )])*(z [IX(i,j,k+1)]-gz[IX(i,j,k)])
+(u[IX(i,j,k+1)]+u[IX(i-1,j,k+1)])*(gz[IX(i,j,k )]- z[IX(i,j,k)]))
/ (z[IX(i,j,k+1)]-z[IX(i,j,k)]);
v0 = (REAL) 0.5
* ((v[IX(i,j,k )]+v[IX(i,j-1,k )])*(z [IX(i,j,k+1)]-gz[IX(i,j,k)])
+(v[IX(i,j,k+1)]+v[IX(i,j-1,k+1)])*(gz[IX(i,j,k )]-z [IX(i,j,k)]))
/ (z[IX(i,j,k+1)]-z[IX(i,j,k)]);
w0 = w[IX(i,j,k)];
// Find the location at previous time step
OL[X] = x[IX(i,j,k)] - u0*dt;
OL[Y] = y[IX(i,j,k)] - v0*dt;
OL[Z] = gz[IX(i,j,k)] - w0*dt;
// Initialize the coordinates of previous step
OC[X] = i;
OC[Y] = j;
OC[Z] = k;
// Initialize the signs for track process
// Completed: 0; In process: 1
COOD[X] = 1;
COOD[Y] = 1;
COOD[Z] = 1;
// Initialize the signs for recording if the track back hits the boundary
// Hit the boundary: 0; Not hit the boundary: 1
LOC[X] = 1;
LOC[Y] = 1;
LOC[Z] = 1;
//Initialize the number of iterations
it=1;
// Trace back more if the any of the trace is still in process
while(COOD[X]==1 || COOD[Y] ==1 || COOD[Z] == 1) {
it++;
if(COOD[X]==1 && LOC[X]==1)
set_x_location(para, var, flagw, x, u0, i, j, k, OL, OC, LOC, COOD);
if(COOD[Y]==1 && LOC[Y]==1)
set_y_location(para, var, flagw, y, v0, i, j, k, OL, OC, LOC, COOD);
if(COOD[Z]==1 && LOC[Z]==1)
set_z_location(para, var, flagw, gz, w0, i, j, k, OL, OC, LOC, COOD);
if(it>itmax) {
printf("Error: advection.c can not track the location for VY(%d, %d,%d)",
i, j, k);
printf("after %d iterations.\n", it);
return 1;
}
} // End of while() loop
// Set the coordinates of previous location if it is as boundary
if(u0>=0 && LOC[X] == 0) OC[X] -=1;
if(v0>=0 && LOC[Y] == 0) OC[Y] -=1;
if(w0>=0 && LOC[Z] == 0) OC[Z] -=1;
// Fixme: Do not understand here. Should it be
// if(u0<0 && LOC[X] = 0) OC[X] += 1;
if(u0<0 && LOC[X]==1) OC[X] -=1;
if(v0<0 && LOC[Y]==1) OC[Y] -=1;
if(w0<0 && LOC[Z]==1) OC[Z] -=1;
/*-------------------------------------------------------------------------
| Interpolating for all variables
-------------------------------------------------------------------------*/
x_1 = (OL[X]- x[IX(OC[X],OC[Y],OC[Z])])
/ ( x[IX(OC[X]+1,OC[Y], OC[Z] )]- x[IX(OC[X],OC[Y],OC[Z])]);
y_1 = (OL[Y]- y[IX(OC[X],OC[Y],OC[Z])])
/ ( y[IX(OC[X], OC[Y]+1, OC[Z] )]- y[IX(OC[X],OC[Y],OC[Z])]);
z_1 = (OL[Z]-gz[IX(OC[X],OC[Y],OC[Z])])
/ (gz[IX(OC[X], OC[Y], OC[Z]+1)]-gz[IX(OC[X],OC[Y],OC[Z])]);
d[IX(i,j,k)] = interpolation(para, d0, x_1, y_1, z_1, OC[X], OC[Y], OC[Z]);
END_FOR
/*---------------------------------------------------------------------------
| define the b.c.
---------------------------------------------------------------------------*/
set_bnd(para, var, var_type, 0, d, BINDEX);
return 0;
} // End of trace_vz()
///////////////////////////////////////////////////////////////////////////////
/// Advection for scalar variables located in the center of control volume
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param var_type The type of variable for advection solver
///\param index Index of trace substances or species
///\param d Pointer to the computed variables at previous time step
///\param d0 Pointer to the computed variables for current time step
///\param BINDEX Pointer to boundary index
///
///\return 0 if no error occurred
///////////////////////////////////////////////////////////////////////////////
int trace_scalar(PARA_DATA *para, REAL **var, int var_type, int index,
REAL *d, REAL *d0, int **BINDEX) {
int i, j, k;
int it;
int itmax = 20000; // Max number of iterations for backward tracing
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL x_1, y_1, z_1;
REAL dt = para->mytime->dt;
REAL u0, v0, w0;
REAL *x = var[X], *y = var[Y], *z = var[Z];
REAL *u = var[VX], *v = var[VY], *w = var[VZ];
REAL *flagp = var[FLAGP];
int COOD[3], LOC[3];
REAL OL[3];
int OC[3];
FOR_EACH_CELL
// Do not trace for boundary cells
if(flagp[IX(i,j,k)]>=0) continue;
/*-------------------------------------------------------------------------
| Step 1: Tracing Back
-------------------------------------------------------------------------*/
// Get velocities at the location of scalar variable
u0 = (REAL) 0.5 * (u[IX(i,j,k)]+u[IX(i-1,j,k )]);
v0 = (REAL) 0.5 * (v[IX(i,j,k)]+v[IX(i,j-1,k )]);
w0 = (REAL) 0.5 * (w[IX(i,j,k)]+w[IX(i,j ,k-1)]);
// Find the location at previous time step
OL[X] = x[IX(i,j,k)] - u0*dt;
OL[Y] = y[IX(i,j,k)] - v0*dt;
OL[Z] = z[IX(i,j,k)] - w0*dt;
// Initialize the coordinates of previous step
OC[X] = i;
OC[Y] = j;
OC[Z] = k;
// Initialize the signs for tracing process
// Completed: 0; In process: 1
COOD[X] = 1;
COOD[Y] = 1;
COOD[Z] = 1;
// Initialize the flags for recording if the tracing back hits the boundary
// Hit the boundary: 0; Not hit the boundary: 1
LOC[X] = 1;
LOC[Y] = 1;
LOC[Z] = 1;
//Initialize the number of iterations
it=1;
// Trace back more if the any of the trace is still in process
while(COOD[X]==1 || COOD[Y] ==1 || COOD[Z] == 1) {
it++;
// If trace in X is in process and donot hit the boundary
if(COOD[X]==1 && LOC[X]==1)
set_x_location(para, var, flagp, x, u0, i, j, k, OL, OC, LOC, COOD);
// If trace in Y is in process and donot hit the boundary
if(COOD[Y]==1 && LOC[Y]==1)
set_y_location(para, var, flagp, y, v0, i, j, k, OL, OC, LOC, COOD);
// If trace in Z is in process and donot hit the boundary
if(COOD[Z]==1 && LOC[Z]==1)
set_z_location(para, var, flagp, z, w0, i, j, k, OL, OC, LOC, COOD);
if(it>itmax) {
sprintf(msg, "trace_scalar(): Could not track the location for scalar "
"variable %d at cell(%d, %d,%d) after %d interations",
var_type, i, j, k, it);
ffd_log(msg, FFD_ERROR);
return 1;
}
} // End of while() for backward tracing
// Set the coordinates of previous location if it is as boundary
if(u0>=0 && LOC[X]==0) OC[X] -=1;
if(v0>=0 && LOC[Y]==0) OC[Y] -=1;
if(w0>=0 && LOC[Z]==0) OC[Z] -=1;
// Fixme: Do not understand here. Should it be
// if(u0<0 && LOC[X] = 0) OC[X] += 1;
if(u0<0 && LOC[X]==1) OC[X] -=1;
if(v0<0 && LOC[Y]==1) OC[Y] -=1;
if(w0<0 && LOC[Z]==1) OC[Z] -=1;
//Store the local minium and maximum values
var[LOCMIN][IX(i,j,k)]=check_min(para, d0, OC[X], OC[Y], OC[Z]);
var[LOCMAX][IX(i,j,k)]=check_max(para, d0, OC[X], OC[Y], OC[Z]);
/*-------------------------------------------------------------------------
| Interpolate
-------------------------------------------------------------------------*/
x_1 = (OL[X]- x[IX(OC[X],OC[Y],OC[Z])])
/ ( x[IX(OC[X]+1,OC[Y], OC[Z] )] - x[IX(OC[X],OC[Y],OC[Z])]);
y_1 = (OL[Y]- y[IX(OC[X],OC[Y],OC[Z])])
/ ( y[IX(OC[X], OC[Y]+1, OC[Z] )] - y[IX(OC[X],OC[Y],OC[Z])]);
z_1 = (OL[Z]- z[IX(OC[X],OC[Y],OC[Z])])
/ ( z[IX(OC[X], OC[Y], OC[Z]+1)] - z[IX(OC[X],OC[Y],OC[Z])]);
d[IX(i,j,k)] = interpolation(para, d0, x_1, y_1, z_1, OC[X], OC[Y], OC[Z]);
END_FOR // End of loop for all cells
/*---------------------------------------------------------------------------
| Define the b.c.
---------------------------------------------------------------------------*/
set_bnd(para, var, var_type, index, d, BINDEX);
return 0;
} // End of trace_scalar()
///////////////////////////////////////////////////////////////////////////////
/// Find the X-location and coordinates at previous time step
///
/// Conducting backward tracing for the particle's X-location and
/// corresponding coordinates at the previous time step.
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param flag Pointer to the property of the cell
///\param x Pointer to the current position x(t) of particle
///\param u0 X-velocity at time (t-1) in location x(t)
///\param i I-index for cell at time t at x(t)
///\param j J-index for cell at time t at x(t)
///\param k K-index for cell at time t at x(t)
///\param OL Pointer to the locations of particle at time (t-1)
///\param OC Pointer to the coordinates of particle at time (t-1)
///\param LOC Pointer to flags recording if tracing back hits the boundary
///\param COOD Pointer to record the status of tracing back process
///
///\return void No return needed
///////////////////////////////////////////////////////////////////////////////
void set_x_location(PARA_DATA *para, REAL **var, REAL *flag, REAL *x, REAL u0,
int i, int j, int k,
REAL *OL, int *OC, int *LOC, int *COOD) {
int imax = para->geom->imax, jmax = para->geom->jmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
REAL *u = var[VX];
/****************************************************************************
| If the previous location is equal to current position
| stop the process (COOD[X] = 0)
****************************************************************************/
if(OL[X]==x[IX(OC[X],OC[Y],OC[Z])])
COOD[X]=0;
/****************************************************************************
| Otherwise, if previous location is on the west of the current position
****************************************************************************/
else if(OL[X]<x[IX(OC[X],OC[Y],OC[Z])]) {
// If donot reach the boundary yet, move to west
if(OC[X]>0)
OC[X] -=1;
// If the previous position is on the east of new location, stop the process
if(OL[X]>=x[IX(OC[X],OC[Y],OC[Z])])
COOD[X]=0;
// If the new position is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use the east cell for new location
OL[X] = x[IX(OC[X]+1,OC[Y],OC[Z])];
OC[X] +=1;
// Hit the boundary
LOC[X] = 0;
// Stop the trace process
COOD[X] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use new position
OL[X] = x[IX(OC[X],OC[Y],OC[Z])];
// use east cell for coordinate
OC[X] += 1;
// Hit the boundary
LOC[X] = 0;
// Stop the trace process
COOD[X]=0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the west of new position
/****************************************************************************
| Otherwise, if previous location is on the east of the current positon
****************************************************************************/
else {
// If not at the east boundary
if(OC[X]<=imax)
// Move to east
OC[X] +=1;
// If the previous position is on the west of new position
if(OL[X]<=x[IX(OC[X],OC[Y],OC[Z])])
// Stop the trace process
COOD[X]=0;
// If the cell is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use west cell
OL[X] = x[IX(OC[X]-1,OC[Y],OC[Z])];
OC[X] -= 1;
// Hit the boundary
LOC[X] = 0;
// Stop the trace process
COOD[X] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use the current cell for previous location
OL[X] = x[IX(OC[X],OC[Y],OC[Z])];
// Use the west cell for coordinate
OC[X] -=1;
// Hit the boundary
LOC[X] = 0;
// Stop the trace process
COOD[X]=0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the east of new position
} // End of set_x_location()
///////////////////////////////////////////////////////////////////////////////
/// Find the Y-location and coordinates at previous time step
///
/// Conducting backward tracing for the particle's Y-location and
/// corresponding coordinates at the previous time step.
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param flag Pointer to the property of the cell
///\param y Pointer to the current position y(t) of particle
///\param v0 Y-velocity at time (t-1) in location y(t)
///\param i I-index for cell at time t at y(t)
///\param j J-index for cell at time t at y(t)
///\param k K-index for cell at time t at y(t)
///\param OL Pointer to the locations of particle at time (t-1)
///\param OC Pointer to the coordinates of particle at time (t-1)
///\param LOC Pointer to flags recording if tracing back hits the boundary
///\param COOD Pointer to record the status of tracing back process
///
///\return void No return needed
///////////////////////////////////////////////////////////////////////////////
void set_y_location(PARA_DATA *para, REAL **var, REAL *flag, REAL *y, REAL v0,
int i, int j, int k,
REAL *OL, int *OC, int *LOC, int *COOD) {
int imax = para->geom->imax, jmax = para->geom->jmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
/****************************************************************************
| If the previous location is equal to current position,
| stop the process (COOD[X] = 0)
****************************************************************************/
if(OL[Y]==y[IX(OC[X],OC[Y],OC[Z])])
COOD[Y] = 0;
/****************************************************************************
| Otherwise, if previous location is on the south of the current positon
****************************************************************************/
else if(OL[Y]<y[IX(OC[X],OC[Y],OC[Z])]) {
// If donot reach the boundary yet
if(OC[Y]>0)
OC[Y] -= 1;
// If the previous position is on the north of new location
if(OL[Y]>=y[IX(OC[X],OC[Y],OC[Z])])
// Stop the process
COOD[Y] = 0;
// If the new position is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use the north cell for new location
OL[Y] = y[IX(OC[X],OC[Y]+1,OC[Z])];
OC[Y] += 1;
// Hit the boundary
LOC[Y] = 0;
// Stop the trace process
COOD[Y] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use new position
OL[Y] = y[IX(OC[X],OC[Y],OC[Z])];
// Use north cell for coordinate
OC[Y] += 1;
// Hit the boundary
LOC[Y] = 0;
// Stop the trace process
COOD[Y] = 0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the south of new position
/****************************************************************************
| Otherwise, if previous location is on the north of the current positon
****************************************************************************/
else {
// If not at the north boundary
if(OC[Y]<=jmax)
// Move to north
OC[Y] +=1;
// If the previous position is on the south of new position
if(OL[Y]<=y[IX(OC[X],OC[Y],OC[Z])])
// Stop the trace process
COOD[Y] = 0;
// If the cell is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use south cell
OL[Y] = y[IX(OC[X],OC[Y]-1,OC[Z])];
OC[Y] -= 1;
// Hit the boundary
LOC[Y] = 0;
// Stop the trace process
COOD[Y] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use the current cell for previous location
OL[Y] = y[IX(OC[X],OC[Y],OC[Z])];
// Use the south cell for coordinate
OC[Y] -= 1;
// Hit the boundary
LOC[Y]=0;
// Stop the trace process
COOD[Y]=0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the east of new position
} // End of set_x_location()
///////////////////////////////////////////////////////////////////////////////
/// Find the Z-location and coordinates at previous time step
///
/// Conducting backward tracing for the particle's Z-location and
/// corresponding coordinates at the previous time step.
///
///\param para Pointer to FFD parameters
///\param var Pointer to FFD simulation variables
///\param flag Pointer to the property of the cell
///\param Z Pointer to the current position y(t) of particle
///\param w0 Z-velocity at time (t-1) in location z(t)
///\param i I-index for cell at time t at z(t)
///\param j J-index for cell at time t at z(t)
///\param k K-index for cell at time t at z(t)
///\param OL Pointer to the locations of particle at time (t-1)
///\param OC Pointer to the coordinates of particle at time (t-1)
///\param LOC Pointer to flags recording if tracing back hits the boundary
///\param COOD Pointer to record the status of tracing back process
///
///\return void No return needed
///////////////////////////////////////////////////////////////////////////////
void set_z_location(PARA_DATA *para, REAL **var, REAL *flag, REAL *z, REAL w0,
int i, int j, int k,
REAL *OL, int *OC, int *LOC, int *COOD) {
int imax = para->geom->imax, jmax = para->geom->jmax;
int kmax = para->geom->kmax;
int IMAX = imax+2, IJMAX = (imax+2)*(jmax+2);
/****************************************************************************
| If the previous location is equal to current position,
| stop the process (COOD[Z] = 0)
****************************************************************************/
if(OL[Z]==z[IX(OC[X],OC[Y],OC[Z])])
COOD[Z] = 0;
/****************************************************************************
| Otherwise, if previous location is on the floor of the current positon
****************************************************************************/
else if(OL[Z]<z[IX(OC[X],OC[Y],OC[Z])]) {
// If donot reach the boundary yet
if(OC[Z]>0)
OC[Z] -= 1;
// If the previous position is on the ceiling of new location
if(OL[Z]>=z[IX(OC[X],OC[Y],OC[Z])])
// Stop the process
COOD[Z] = 0;
// If the new position is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use the ceiling cell for new location
OL[Z] = z[IX(OC[X],OC[Y],OC[Z]+1)];
OC[Z] += 1;
// Hit the boundary
LOC[Z] = 0;
// Stop the trace process
COOD[Z] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use new position
OL[Z] = z[IX(OC[X],OC[Y],OC[Z])];
// Use ceiling cell for coordinate
OC[Z] += 1;
// Hit the boundary
LOC[Z] = 0;
// Stop the trace process
COOD[Z] = 0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the floor of new position
/****************************************************************************
| Otherwise, if previous location is on the ceiling of the current positon
-***************************************************************************/
else {
// If not at the ceiling boundary
if(OC[Z]<=kmax)
// Move to ceiling
OC[Z] += 1;
// If the previous position is on the floor of new position
if(OL[Z] <=z[IX(OC[X],OC[Y],OC[Z])])
// Stop the trace process
COOD[Z] = 0;
// If the cell is solid
if(flag[IX(OC[X],OC[Y],OC[Z])]==1) {
// Use floor cell
OL[Z] = z[IX(OC[X],OC[Y],OC[Z]-1)];
OC[Z] -= 1;
// Hit the boundary
LOC[Z] = 0;
// Stop the trace process
COOD[Z] = 0;
} // End of if() for solid
// If the new position is inlet or outlet
if(flag[IX(OC[X],OC[Y],OC[Z])]==0||flag[IX(OC[X],OC[Y],OC[Z])]==2) {
// Use the current cell for previous location
OL[Z]=z[IX(OC[X],OC[Y],OC[Z])];
// Use the floor cell for coordinate
OC[Z] -= 1;
// Hit the boundary
LOC[Z]=0;
// Stop the trace process
COOD[Z]=0;
} // End of if() for inlet or outlet
} // End of if() for previous position is on the east of new position
} // End of set_z_location()