-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLevelSetReinitializer.cpp
More file actions
2034 lines (1641 loc) · 74.6 KB
/
Copy pathLevelSetReinitializer.cpp
File metadata and controls
2034 lines (1641 loc) · 74.6 KB
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************
* Copyright © 2020 The Multiphysics Modeling and Computation (M2C) Lab
* <kevin.wgy@gmail.com> <kevinw3@vt.edu>
************************************************************************/
#include <LevelSetReinitializer.h>
#include <GradientCalculatorCentral.h>
#include <cfloat> //DBL_MAX
#include <set>
extern int verbose;
extern double domain_diagonal;
int large_int = 31415927;
//--------------------------------------------------------------------------
LevelSetReinitializer::LevelSetReinitializer(MPI_Comm &comm_, DataManagers3D &dm_all_,
LevelSetSchemeData &iod_ls_, SpaceVariable3D &coordinates_,
SpaceVariable3D &delta_xyz_, vector<GhostPoint> &ghost_nodes_inner_,
vector<GhostPoint> &ghost_nodes_outer_)
: comm(comm_), iod_ls(iod_ls_), coordinates(coordinates_),
delta_xyz(delta_xyz_), ghost_nodes_inner(ghost_nodes_inner_),
ghost_nodes_outer(ghost_nodes_outer_),
Tag(comm_, &(dm_all_.ghosted1_1dof)),
NormalDir(comm_, &(dm_all_.ghosted1_3dof)),
R(comm_, &(dm_all_.ghosted1_1dof)),
Phibk(comm_, &(dm_all_.ghosted1_1dof)),
Phi0(comm_, &(dm_all_.ghosted1_1dof)),
Phi1(comm_, &(dm_all_.ghosted1_1dof)),
Sign(comm_, &(dm_all_.ghosted1_1dof)),
PhiG2(comm_, &(dm_all_.ghosted2_1dof)),
phi_max(-domain_diagonal), phi_min(domain_diagonal)
{
coordinates.GetCornerIndices(&i0, &j0, &k0, &imax, &jmax, &kmax);
coordinates.GetGhostedCornerIndices(&ii0, &jj0, &kk0, &iimax, &jjmax, &kkmax);
Tag.SetConstantValue(0, true);
cfl = iod_ls.reinit.cfl;
/*
min_dxyz = std::min(delta_xyz.CalculateGlobalMin(0,false),
std::min(delta_xyz.CalculateGlobalMin(1,false), delta_xyz.CalculateGlobalMin(2,false)));
max_dxyz = std::max(delta_xyz.CalculateGlobalMax(0,false),
std::max(delta_xyz.CalculateGlobalMax(1,false), delta_xyz.CalculateGlobalMax(2,false)));
*/
}
//--------------------------------------------------------------------------
LevelSetReinitializer::~LevelSetReinitializer()
{
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::Destroy()
{
Tag.Destroy();
NormalDir.Destroy();
R.Destroy();
Phibk.Destroy();
Phi0.Destroy();
Phi1.Destroy();
Sign.Destroy();
PhiG2.Destroy();
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::ReinitializeFullDomain(SpaceVariable3D &Phi, int special_maxIts)
{
// Step 1: Prep: Tag first layer nodes & store the sign function
vector<FirstLayerNode> firstLayer;
bool detected = TagFirstLayerNodes(Phi, firstLayer); //also calculates the associated coefficients
if(!detected) return; //possible, especially in sims w/ phase transition
print("- Reinitializing the level set function (material id: %d).\n", iod_ls.materialid);
EvaluateSignFunctionFullDomain(Phi, 1.0/*smoothing coefficient*/);
// Step 2: Reinitialize first layer nodes (no iterations needed)
if(/*iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::UNCONSTRAINED||*/
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED1 ||
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED2) {
Phi1.AXPlusBY(0.0, 1.0, Phi, true); //Phi1 = Phi
ReinitializeFirstLayerNodes(Phi1, Phi, firstLayer); //Phi is updated
ApplyBoundaryConditions(Phi);
}
// Step 3: Main loop -- 3rd-order Runge-Kutta w/ spatially varying dt
//Store Phi (set Phibk = Phi) for failsafe
Phibk.AXPlusBY(0.0, 1.0, Phi,true);
RETRY_FullDomain:
double residual = 0.0, dphi_max = 0.0;
int iter;
int maxIts = (special_maxIts>0) ? special_maxIts : iod_ls.reinit.maxIts;
for(iter = 0; iter < maxIts; iter++) {
Phi0.AXPlusBY(0.0, 1.0, Phi);
//************** Step 1 of RK3 *****************
residual = ComputeResidualFullDomain(Phi, R, cfl); //R = R(Phi)
if(verbose>1)
print(" o Iter. %d: Residual = %e, Relative Error = %e, Tol = %e.\n", iter, residual,
dphi_max, iod_ls.reinit.convergence_tolerance);
if(residual < iod_ls.reinit.convergence_tolerance) {//residual itself is nondimensional
if(verbose==1)
print(" o Completed (%d iter.): Residual = %e, Rel. Error = %e, Tol = %e.\n",
iter, residual, dphi_max, iod_ls.reinit.convergence_tolerance);
break;
}
Phi1.AXPlusBY(0.0, 1.0, Phi); //Phi1 = Phi
Phi1.AXPlusBY(1.0, 1.0, R); //Phi1 = Phi + R(Phi)
ApplyBoundaryConditions(Phi1);
//*********************************************
//************** Step 2 of RK3 *****************
ComputeResidualFullDomain(Phi1, R, cfl);
Phi1.AXPlusBY(0.25, 0.75, Phi);
Phi1.AXPlusBY(1.0, 0.25, R);
ApplyBoundaryConditions(Phi1);
//*********************************************
//************** Step 3 of RK3 *****************
ComputeResidualFullDomain(Phi1, R, cfl);
Phi.AXPlusBY(1.0/3.0, 2.0/3.0, Phi1);
Phi.AXPlusBY(1.0, 2.0/3.0, R);
ApplyBoundaryConditions(Phi);
//*********************************************
//*********************************************
// Apply corrections to first layer nodes (HCR-1 or HCR-2)
ApplyCorrectionToFirstLayerNodes(Phi, firstLayer, cfl); //HCR-1 or HCR-2 (also apply b.c.)
//*********************************************
dphi_max = CalculateMaximumRelativeErrorFullDomain(Phi0, Phi);
if(dphi_max < iod_ls.reinit.convergence_tolerance) {//converged (using the same tolerance)
if(verbose==1)
print(" o Completed (%d iter.): Residual = %e, Rel. Error = %e, Tol = %e.\n",
iter, residual, dphi_max, iod_ls.reinit.convergence_tolerance);
break;
}
}
// apply failsafe
if(iter==maxIts) {
print_warning(" o Warning: L-S Reinitialization failed to converge. Residual = %e, Rel.Error = %e, "
"Tol = %e.", residual, dphi_max, iod_ls.reinit.convergence_tolerance);
if(dphi_max<0.02) {//ok...
print_warning("\n");
return;
}
if(cfl < 0.02) {//failed...
print_error("\n*** Error: L-S Reinitialization failed to converge. Residual = %e, Rel.Error = %e, Tol = %e.\n",
residual, dphi_max, iod_ls.reinit.convergence_tolerance);
exit_mpi();
} else {
print_warning(" Retrying.\n");
cfl /= 1.5;
Phi.AXPlusBY(0.0, 1.0, Phibk,true);
goto RETRY_FullDomain;
}
}
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::ReinitializeInBand(SpaceVariable3D &Phi, SpaceVariable3D &Level,
SpaceVariable3D &UsefulG2,SpaceVariable3D &Active,
vector<Int3> &useful_nodes, vector<Int3> &active_nodes,
int special_maxIts)
{
// update phi_max and phi_min (only for use in updating new useful nodes)
UpdatePhiMaxAndPhiMinInBand(Phi, useful_nodes);
// Step 1: Prep: Tag first layer nodes & store the sign function, and update the narrow band
vector<FirstLayerNode> firstLayer;
vector<Int3> firstLayerIncGhost;
bool detected = TagFirstLayerNodesInBand(Phi, useful_nodes, firstLayer, firstLayerIncGhost);
if(!detected) return; //possible, especially in sims w/ phase transition
print("- Reinitializing the level set function (material id: %d), bandwidth = %d.\n",
iod_ls.materialid, iod_ls.bandwidth);
UpdateNarrowBand(Phi, firstLayerIncGhost, Level, UsefulG2, Active, useful_nodes, active_nodes);
EvaluateSignFunctionInBand(Phi, useful_nodes, 1.0/*smoothing coefficient*/);
// Step 2: Reinitialize first layer nodes (no iterations needed)
if(/*iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::UNCONSTRAINED||*/
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED1 ||
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED2) {
AXPlusBYInBandPlusOne(0.0, Phi1, 1.0, Phi, true); //Phi1 = Phi
//Phi1.AXPlusBY(0.0, 1.0, Phi, true); //Phi1 = Phi
ReinitializeFirstLayerNodes(Phi1, Phi, firstLayer, &UsefulG2, &useful_nodes); //Phi is updated
ApplyBoundaryConditions(Phi, &UsefulG2);
}
// Step 3: Main loop -- 3rd-order Runge-Kutta w/ spatially varying dt
//Store Phi (set Phibk = Phi) for failsafe
AXPlusBYInBandPlusOne(0.0, Phibk, 1.0, Phi);
RETRY_NarrowBand:
double residual = 0.0, dphi_max = 0.0;
int iter;
int maxIts = (special_maxIts>0) ? special_maxIts : iod_ls.reinit.maxIts;
for(iter = 0; iter < maxIts; iter++) {
AXPlusBYInBandPlusOne(0.0, Phi0, 1.0, Phi);
//************** Step 1 of RK3 *****************
residual = ComputeResidualInBand(Phi, UsefulG2, useful_nodes, R, cfl); //R = R(Phi)
if(verbose>1)
print(" o Iter. %d: Residual = %e, Relative Error = %e, Tol = %e.\n", iter, residual,
dphi_max, iod_ls.reinit.convergence_tolerance);
if(residual < iod_ls.reinit.convergence_tolerance) {//residual itself is nondimensional
if(verbose==1)
print(" o Completed (%d iter.): Residual = %e, Rel. Error = %e, Tol = %e.\n",
iter, residual, dphi_max, iod_ls.reinit.convergence_tolerance);
break;
}
AXPlusBYInBandPlusOne(0.0, Phi1, 1.0, Phi); //Phi1 = Phi
//Phi1.AXPlusBY(0.0, 1.0, Phi); //Phi1 = Phi
AXPlusBYInBandPlusOne(1.0, Phi1, 1.0, R); //Phi1 = Phi + R(Phi)
//Phi1.AXPlusBY(1.0, 1.0, R); //Phi1 = Phi + R(Phi)
ApplyBoundaryConditions(Phi1, &UsefulG2);
//*********************************************
//************** Step 2 of RK3 *****************
ComputeResidualInBand(Phi1, UsefulG2, useful_nodes, R, cfl);
AXPlusBYInBandPlusOne(0.25, Phi1, 0.75, Phi);
//Phi1.AXPlusBY(0.25, 0.75, Phi);
AXPlusBYInBandPlusOne(1.0, Phi1, 0.25, R);
//Phi1.AXPlusBY(1.0, 0.25, R);
ApplyBoundaryConditions(Phi1, &UsefulG2);
//*********************************************
//************** Step 3 of RK3 *****************
ComputeResidualInBand(Phi1, UsefulG2, useful_nodes, R, cfl);
AXPlusBYInBandPlusOne(1.0/3.0, Phi, 2.0/3.0, Phi1);
//Phi.AXPlusBY(1.0/3.0, 2.0/3.0, Phi1);
AXPlusBYInBandPlusOne(1.0, Phi, 2.0/3.0, R);
//Phi.AXPlusBY(1.0, 2.0/3.0, R);
ApplyBoundaryConditions(Phi, &UsefulG2);
//*********************************************
//*********************************************
// Apply corrections to first layer nodes (HCR-1 or HCR-2)
ApplyCorrectionToFirstLayerNodes(Phi, firstLayer, cfl, &UsefulG2); //HCR-1 or HCR-2 (also apply b.c.)
//*********************************************
dphi_max = CalculateMaximumRelativeErrorInBand(Phi0, Phi, useful_nodes);
if(dphi_max < iod_ls.reinit.convergence_tolerance) {//converged (using the same tolerance)
if(verbose==1)
print(" o Completed (%d iter.): Residual = %e, Rel. Error = %e, Tol = %e.\n",
iter, residual, dphi_max, iod_ls.reinit.convergence_tolerance);
break;
}
}
//Failsafe
if(iter==maxIts) {
print_warning(" o Warning: L-S Reinitialization failed to converge. Residual = %e, Rel.Error = %e, "
"Tol = %e.", residual, dphi_max, iod_ls.reinit.convergence_tolerance);
if(dphi_max<0.02) {//ok...
print_warning("\n");
return;
}
if(cfl < 0.02) {//failed...
print_error("\n*** Error: L-S Reinitialization failed to converge. Residual = %e, Rel.Error = %e, Tol = %e.\n",
residual, dphi_max, iod_ls.reinit.convergence_tolerance);
exit_mpi();
} else {
print_warning(" Retrying.\n");
cfl /= 1.5;
AXPlusBYInBandPlusOne(0.0, Phi, 1.0, Phibk); //set Phi = Phibk and retry
goto RETRY_NarrowBand;
}
}
}
//--------------------------------------------------------------------------
bool
LevelSetReinitializer::TagFirstLayerNodes(SpaceVariable3D &Phi, vector<FirstLayerNode> &firstLayer)
{
//*****************************************************************************
// "Tag" is populated within both domain interior and the ghost boundary outside
// the physical domain (except corners).
// "firstLayer" stores all the first-layer nodes within the physical domain (i.e.
// Gamma in Hartmann et al. (2010))
// For each node in Gamma, S is constructed to contain nodes on the opposite side
// of the interface (excluding nodes with phi == 0!). For each node on the opposite
// side, r_alpha, and \tilde{r} are computed.
// Return value: true if an interface is detected in the entire domain. false if not.
//*****************************************************************************
firstLayer.clear();
double*** phi = Phi.GetDataPointer();
double*** tag = Tag.GetDataPointer();
for(int k=kk0; k<kkmax; k++)
for(int j=jj0; j<jjmax; j++)
for(int i=ii0; i<iimax; i++) {
tag[k][j][i] = 0; //default
if(Phi.OutsidePhysicalDomainAndUnpopulated(i,j,k))
continue;
if(i-1>=ii0 && phi[k][j][i]*phi[k][j][i-1]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(i+1<iimax && phi[k][j][i]*phi[k][j][i+1]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(j-1>=jj0 && phi[k][j][i]*phi[k][j-1][i]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(j+1<jjmax && phi[k][j][i]*phi[k][j+1][i]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(k-1>=kk0 && phi[k][j][i]*phi[k-1][j][i]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(k+1<kkmax && phi[k][j][i]*phi[k+1][j][i]<=0) {
tag[k][j][i] = 1;
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
}
//Now, calculate s and r for each node in firstLayer (Gamma)
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
double sum(0.0);
int i(it->i), j(it->j), k(it->k);
if(phi[k][j][i]*phi[k][j][i-1]<0) { //left neighbor
it->s[0] = true;
it->r[0] = phi[k][j][i]/phi[k][j][i-1];
it->ns++;
sum += phi[k][j][i-1];
}
if(phi[k][j][i]*phi[k][j][i+1]<0) { //right neighbor
it->s[1] = true;
it->r[1] = phi[k][j][i]/phi[k][j][i+1];
it->ns++;
sum += phi[k][j][i+1];
}
if(phi[k][j][i]*phi[k][j-1][i]<0) { //bottom neighbor
it->s[2] = true;
it->r[2] = phi[k][j][i]/phi[k][j-1][i];
it->ns++;
sum += phi[k][j-1][i];
}
if(phi[k][j][i]*phi[k][j+1][i]<0) { //top neighbor
it->s[3] = true;
it->r[3] = phi[k][j][i]/phi[k][j+1][i];
it->ns++;
sum += phi[k][j+1][i];
}
if(phi[k][j][i]*phi[k-1][j][i]<0) { //back neighbor
it->s[4] = true;
it->r[4] = phi[k][j][i]/phi[k-1][j][i];
it->ns++;
sum += phi[k-1][j][i];
}
if(phi[k][j][i]*phi[k+1][j][i]<0) { //front neighbor
it->s[5] = true;
it->r[5] = phi[k][j][i]/phi[k+1][j][i];
it->ns++;
sum += phi[k+1][j][i];
}
it->r0 = (sum!=0) ? phi[k][j][i]/sum : 0.0;
}
Tag.RestoreDataPointerAndInsert();
Phi.RestoreDataPointerToLocalVector(); //no changes made
// Finally, figure out whether the interface exists. (It may not exist at some time steps,
// especially in a simulation w/ phase transition.)
int interface_exists = firstLayer.size()>0 ? 1 : 0;
MPI_Allreduce(MPI_IN_PLACE, &interface_exists, 1, MPI_INT, MPI_MAX, comm);
if(interface_exists>0)
return true;
return false;
}
//--------------------------------------------------------------------------
bool
LevelSetReinitializer::TagFirstLayerNodesInBand(SpaceVariable3D &Phi, vector<Int3> &useful_nodes,
vector<FirstLayerNode> &firstLayer,
vector<Int3> &firstLayerIncGhost)
{
//*****************************************************************************
// "Tag" is populated within both domain interior and the ghost boundary outside
// the physical domain (except corners).
// "firstLayer" stores all the first-layer nodes within the physical domain (i.e.
// Gamma in Hartmann et al. (2010))
// For each node in Gamma, S is constructed to contain nodes on the opposite side
// of the interface (excluding nodes with phi == 0!). For each node on the opposite
// side, r_alpha, and \tilde{r} are computed.
// Return value: true if an interface is detected in the entire domain. false if not.
//*****************************************************************************
firstLayer.clear();
firstLayerIncGhost.clear();
double*** phi = Phi.GetDataPointer();
double*** tag = Tag.GetDataPointer();
for(auto it = useful_nodes.begin(); it != useful_nodes.end(); it++) {
int i((*it)[0]), j((*it)[1]), k((*it)[2]);
tag[k][j][i] = 0; //default
if(Phi.OutsidePhysicalDomainAndUnpopulated(i,j,k))
continue;
if(i-1>=ii0 && !Phi.OutsidePhysicalDomainAndUnpopulated(i-1,j,k) && phi[k][j][i]*phi[k][j][i-1]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(i+1<iimax && !Phi.OutsidePhysicalDomainAndUnpopulated(i+1,j,k) && phi[k][j][i]*phi[k][j][i+1]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(j-1>=jj0 && !Phi.OutsidePhysicalDomainAndUnpopulated(i,j-1,k) && phi[k][j][i]*phi[k][j-1][i]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(j+1<jjmax && !Phi.OutsidePhysicalDomainAndUnpopulated(i,j+1,k) && phi[k][j][i]*phi[k][j+1][i]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(k-1>=kk0 && !Phi.OutsidePhysicalDomainAndUnpopulated(i,j,k-1) && phi[k][j][i]*phi[k-1][j][i]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
if(k+1<kkmax && !Phi.OutsidePhysicalDomainAndUnpopulated(i,j,k+1) && phi[k][j][i]*phi[k+1][j][i]<=0) {
tag[k][j][i] = 1;
firstLayerIncGhost.push_back(Int3(i,j,k));
if(Phi.IsHere(i,j,k,false)) //only push nodes in the subdomain interior into firstLayer
firstLayer.push_back(FirstLayerNode(i,j,k));
continue;
}
}
Tag.RestoreDataPointerAndInsert();
//Update firstLayerIncGhost to account for the exchange between subdomains
tag = Tag.GetDataPointer();
for(auto it = ghost_nodes_inner.begin(); it != ghost_nodes_inner.end(); it++) {
int i(it->ijk[0]), j(it->ijk[1]), k(it->ijk[2]);
if(tag[k][j][i]==1 &&
std::find(firstLayerIncGhost.begin(), firstLayerIncGhost.end(), it->ijk) == firstLayerIncGhost.end())
firstLayerIncGhost.push_back(it->ijk);
}
for(auto it = ghost_nodes_outer.begin(); it != ghost_nodes_outer.end(); it++) {
int i(it->ijk[0]), j(it->ijk[1]), k(it->ijk[2]);
if(tag[k][j][i]==1 &&
std::find(firstLayerIncGhost.begin(), firstLayerIncGhost.end(), it->ijk) == firstLayerIncGhost.end())
firstLayerIncGhost.push_back(it->ijk);
}
Tag.RestoreDataPointerToLocalVector();
//Now, calculate s and r for each node in firstLayer (Gamma)
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
double sum(0.0);
int i(it->i), j(it->j), k(it->k);
if(phi[k][j][i]*phi[k][j][i-1]<0) { //left neighbor
it->s[0] = true;
it->r[0] = phi[k][j][i]/phi[k][j][i-1];
it->ns++;
sum += phi[k][j][i-1];
}
if(phi[k][j][i]*phi[k][j][i+1]<0) { //right neighbor
it->s[1] = true;
it->r[1] = phi[k][j][i]/phi[k][j][i+1];
it->ns++;
sum += phi[k][j][i+1];
}
if(phi[k][j][i]*phi[k][j-1][i]<0) { //bottom neighbor
it->s[2] = true;
it->r[2] = phi[k][j][i]/phi[k][j-1][i];
it->ns++;
sum += phi[k][j-1][i];
}
if(phi[k][j][i]*phi[k][j+1][i]<0) { //top neighbor
it->s[3] = true;
it->r[3] = phi[k][j][i]/phi[k][j+1][i];
it->ns++;
sum += phi[k][j+1][i];
}
if(phi[k][j][i]*phi[k-1][j][i]<0) { //back neighbor
it->s[4] = true;
it->r[4] = phi[k][j][i]/phi[k-1][j][i];
it->ns++;
sum += phi[k-1][j][i];
}
if(phi[k][j][i]*phi[k+1][j][i]<0) { //front neighbor
it->s[5] = true;
it->r[5] = phi[k][j][i]/phi[k+1][j][i];
it->ns++;
sum += phi[k+1][j][i];
}
it->r0 = (sum!=0) ? phi[k][j][i]/sum : 0.0;
}
Phi.RestoreDataPointerToLocalVector(); //no changes made
// Finally, figure out whether the interface exists. (It may not exist at some time steps,
// especially in a simulation w/ phase transition.)
int interface_exists = firstLayer.size()>0 ? 1 : 0;
MPI_Allreduce(MPI_IN_PLACE, &interface_exists, 1, MPI_INT, MPI_MAX, comm);
if(interface_exists>0)
return true;
return false;
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::EvaluateSignFunctionFullDomain(SpaceVariable3D &Phi, double eps)
{
double*** phi = Phi.GetDataPointer();
Vec3D*** dxyz = (Vec3D***)delta_xyz.GetDataPointer();
double*** sign = Sign.GetDataPointer();
double factor;
for(int k=kk0; k<kkmax; k++)
for(int j=jj0; j<jjmax; j++)
for(int i=ii0; i<iimax; i++) {
factor = eps*std::min(dxyz[k][j][i][0], std::min(dxyz[k][j][i][1], dxyz[k][j][i][2]));
sign[k][j][i] = phi[k][j][i] / sqrt(phi[k][j][i]*phi[k][j][i] + factor*factor);
}
Phi.RestoreDataPointerToLocalVector();
delta_xyz.RestoreDataPointerToLocalVector();
Sign.RestoreDataPointerAndInsert();
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::EvaluateSignFunctionInBand(SpaceVariable3D &Phi, vector<Int3> &useful_nodes,
double eps)
{
double*** phi = Phi.GetDataPointer();
Vec3D*** dxyz = (Vec3D***)delta_xyz.GetDataPointer();
double*** sign = Sign.GetDataPointer();
double factor;
for(auto it = useful_nodes.begin(); it != useful_nodes.end(); it++) {
int i((*it)[0]), j((*it)[1]), k((*it)[2]);
factor = eps*std::min(dxyz[k][j][i][0], std::min(dxyz[k][j][i][1], dxyz[k][j][i][2]));
sign[k][j][i] = phi[k][j][i] / sqrt(phi[k][j][i]*phi[k][j][i] + factor*factor);
}
Phi.RestoreDataPointerToLocalVector();
delta_xyz.RestoreDataPointerToLocalVector();
Sign.RestoreDataPointerAndInsert();
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::ReinitializeFirstLayerNodes(SpaceVariable3D &Phi0, SpaceVariable3D &Phi,
vector<FirstLayerNode> &firstLayer,
SpaceVariable3D *UsefulG2, vector<Int3> *useful_nodes)
{
//Note: This function is designed to work for both full-domain and narrow-band level set methods
//****************************************
// Implementing the RSU, CR-1 and CR-2 algos
// in Hartmann et al. (2008)
//****************************************
PopulatePhiG2(Phi0, useful_nodes);
int NX, NY, NZ;
Phi.GetGlobalSize(&NX, &NY, &NZ);
double*** phi = Phi.GetDataPointer();
double*** phig = PhiG2.GetDataPointer();
double*** tag = Tag.GetDataPointer();
Vec3D*** dxyz = (Vec3D***)delta_xyz.GetDataPointer();
Vec3D*** coords = (Vec3D***)coordinates.GetDataPointer();
int i,j,k;
Vec3D gradphi;
double gradphi_norm;
double epsx, epsy, epsz;
for(int n=0; n<(int)firstLayer.size(); n++) {
// This must be a node in the interior of the subdomain (see how firstLayer is populated)
i = firstLayer[n].i;
j = firstLayer[n].j;
k = firstLayer[n].k;
// set phi = phi / |grad(phi)|, using tagged nodes to calculate the derivatives
// ref: Hartmann et al. 2008, Eqs.(20)(21) (which cites another paper)
//Eq.(21a) of Hartmann et al., 2008
epsx = 1.0e-3*dxyz[k][j][i][0];
epsy = 1.0e-3*dxyz[k][j][i][1];
epsz = 1.0e-3*dxyz[k][j][i][2];
gradphi[0] = DifferentiateInFirstLayer(coords[k][j][i-1][0], coords[k][j][i][0], coords[k][j][i+1][0],
tag[k][j][i-1], tag[k][j][i], tag[k][j][i+1],
phig[k][j][i-1], phig[k][j][i], phig[k][j][i+1],
(i-2>=-1) ? phig[k][j][i-2] : phig[k][j][i-1],
(i+2<=NX) ? phig[k][j][i+2] : phig[k][j][i+1], epsx);
gradphi[1] = DifferentiateInFirstLayer(coords[k][j-1][i][1], coords[k][j][i][1], coords[k][j+1][i][1],
tag[k][j-1][i], tag[k][j][i], tag[k][j+1][i],
phig[k][j-1][i], phig[k][j][i], phig[k][j+1][i],
(j-2>=-1) ? phig[k][j-2][i] : phig[k][j-1][i],
(j+2<=NY) ? phig[k][j+2][i] : phig[k][j+2][i], epsy);
gradphi[2] = DifferentiateInFirstLayer(coords[k-1][j][i][2], coords[k][j][i][2], coords[k+1][j][i][2],
tag[k-1][j][i], tag[k][j][i], tag[k+1][j][i],
phig[k-1][j][i], phig[k][j][i], phig[k+1][j][i],
(k-2>=-1) ? phig[k-2][j][i] : phig[k-1][j][i],
(k+2<=NZ) ? phig[k+2][j][i] : phig[k+1][j][i], epsz);
gradphi_norm = gradphi.norm();
firstLayer[n].nphi0 = gradphi/gradphi.norm();
if(gradphi_norm == 0) {
fprintf(stdout,"Warning: (%d,%d,%d)(%e,%e,%e): Updating first layer node led to zero gradient.\n",
i,j,k,coords[k][j][i][0],coords[k][j][i][1],coords[k][j][i][2]);
phi[k][j][i] = phig[k][j][i];
} else
phi[k][j][i] = phig[k][j][i]/gradphi_norm;
}
Phi.RestoreDataPointerAndInsert(); //update Phi
Tag.RestoreDataPointerToLocalVector();
delta_xyz.RestoreDataPointerToLocalVector();
coordinates.RestoreDataPointerToLocalVector();
PhiG2.RestoreDataPointerToLocalVector();
//**************************************************************************************************
// Applying the averaging algorithm to correct a subset of the first-layer nodes in (CR-1 or CR-2)
//**************************************************************************************************
if(iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED1 ||
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED2) {
ComputeNormalDirectionBeyondFirstLayer(Phi, firstLayer, UsefulG2, useful_nodes);
// Step 1: Find nodes in the correction set (C) --- Eq. (14) of Hartmann et al. (2010)
double curvature_x, curvature_y, curvature_z, curvature;
coords = (Vec3D***)coordinates.GetDataPointer();
phig = PhiG2.GetDataPointer();
Vec3D*** normal = (Vec3D***)NormalDir.GetDataPointer();
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) { //inside domain interior
i = it->i;
j = it->j;
k = it->k;
// calculate curvature (normal is calculated only within domain interior)
if(i-1>=0 && i+1<NX)
curvature_x = CentralDifferenceLocal(normal[k][j][i-1][0], normal[k][j][i][0], normal[k][j][i+1][0],
coords[k][j][i-1][0], coords[k][j][i][0], coords[k][j][i+1][0]);
else if(i-1>=0)
curvature_x = (normal[k][j][i][0] - normal[k][j][i-1][0])/(coords[k][j][i][0] - coords[k][j][i-1][0]);
else if(i+1<NX)
curvature_x = (normal[k][j][i+1][0] - normal[k][j][i][0])/(coords[k][j][i+1][0] - coords[k][j][i][0]);
else
curvature_x = 0.0;
if(j-1>=0 && j+1<NY)
curvature_y = CentralDifferenceLocal(normal[k][j-1][i][1], normal[k][j][i][1], normal[k][j+1][i][1],
coords[k][j-1][i][1], coords[k][j][i][1], coords[k][j+1][i][1]);
else if(j-1>=0)
curvature_y = (normal[k][j][i][1] - normal[k][j-1][i][1])/(coords[k][j][i][1] - coords[k][j-1][i][1]);
else if(j+1<NY)
curvature_y = (normal[k][j+1][i][1] - normal[k][j][i][1])/(coords[k][j+1][i][1] - coords[k][j][i][1]);
else
curvature_y = 0.0;
if(k-1>=0 && k+1<NZ)
curvature_z = CentralDifferenceLocal(normal[k-1][j][i][2], normal[k][j][i][2], normal[k+1][j][i][2],
coords[k-1][j][i][2], coords[k][j][i][2], coords[k+1][j][i][2]);
else if(k-1>=0)
curvature_z = (normal[k][j][i][2] - normal[k-1][j][i][2])/(coords[k][j][i][2] - coords[k-1][j][i][2]);
else if(k+1<NZ)
curvature_z = (normal[k+1][j][i][2] - normal[k][j][i][2])/(coords[k+1][j][i][2] - coords[k][j][i][2]);
else
curvature_z = 0.0;
curvature = curvature_x + curvature_y + curvature_z;
if(curvature*phig[k][j][i]<0 || (curvature==0.0 && phig[k][j][i]<0))
it->f = 0.0;
else
it->f = DBL_MAX;
}
// Step 2: Calculate the correction
double*** phi = Phi.GetDataPointer();
if(iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED1) {
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
if(it->f != 0.0)
continue; //not in the correction set --- see above
i = it->i;
j = it->j;
k = it->k;
double sum(0.0);
if(it->s[0]) sum += phi[k][j][i-1]/phig[k][j][i-1];
if(it->s[1]) sum += phi[k][j][i+1]/phig[k][j][i+1];
if(it->s[2]) sum += phi[k][j-1][i]/phig[k][j-1][i];
if(it->s[3]) sum += phi[k][j+1][i]/phig[k][j+1][i];
if(it->s[4]) sum += phi[k-1][j][i]/phig[k-1][j][i];
if(it->s[5]) sum += phi[k+1][j][i]/phig[k+1][j][i];
if(it->ns != 0)
it->f = phig[k][j][i]*sum/(double)(it->ns);
else
it->f = 0.0;
}
}
else if(iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED2) {
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
if(it->f != 0.0)
continue; //not in the correction set --- see above
i = it->i;
j = it->j;
k = it->k;
double sum1(0.0), sum2(0.0);
if(it->s[0]) {sum1 += phi[k][j][i-1]; sum2 += phig[k][j][i-1];}
if(it->s[1]) {sum1 += phi[k][j][i+1]; sum2 += phig[k][j][i+1];}
if(it->s[2]) {sum1 += phi[k][j-1][i]; sum2 += phig[k][j-1][i];}
if(it->s[3]) {sum1 += phi[k][j+1][i]; sum2 += phig[k][j+1][i];}
if(it->s[4]) {sum1 += phi[k-1][j][i]; sum2 += phig[k-1][j][i];}
if(it->s[5]) {sum1 += phi[k+1][j][i]; sum2 += phig[k+1][j][i];}
if(sum2 != 0)
it->f = phig[k][j][i]*sum1/sum2;
else //this would happen only when s[0] = ... = s[5] = false, meaning phig[k][j][i]=0.
it->f = 0.0;
}
}
// Step 3: Apply the correction
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
if(it->f == DBL_MAX)
continue;
i = it->i;
j = it->j;
k = it->k;
phi[k][j][i] = it->f;
it->f = 0.0; //reset
}
/*
//DEBUG
Tag.SetConstantValue(0.0, true);
tag = Tag.GetDataPointer();
for(auto it = firstLayer.begin(); it != firstLayer.end(); it++) {
if(it->f == DBL_MAX)
continue;
i = it->i;
j = it->j;
k = it->k;
tag[k][j][i] = 1.0;
}
Tag.RestoreDataPointerAndInsert();
Tag.WriteToVTRFile("tagged_nodes.vtr");
exit_mpi();
*/
Phi.RestoreDataPointerAndInsert();
PhiG2.RestoreDataPointerToLocalVector();
NormalDir.RestoreDataPointerToLocalVector();
coordinates.RestoreDataPointerToLocalVector();
}
}
//--------------------------------------------------------------------------
void
LevelSetReinitializer::PopulatePhiG2(SpaceVariable3D &Phi0, vector<Int3> *useful_nodes)
{
//This function is designed to work for both full-domain and narrow-band level set methods
//In the former case, useful_nodes is a NULL pointer.
double*** phig2 = PhiG2.GetDataPointer();
double*** phi0 = Phi0.GetDataPointer();
if(!useful_nodes) {
for(int k=kk0; k<kkmax; k++)
for(int j=jj0; j<jjmax; j++)
for(int i=ii0; i<iimax; i++) {
phig2[k][j][i] = phi0[k][j][i];
}
} else {
for(auto it = useful_nodes->begin(); it != useful_nodes->end(); it++) {
int i((*it)[0]), j((*it)[1]), k((*it)[2]);
phig2[k][j][i] = phi0[k][j][i];
}
}
Phi0.RestoreDataPointerToLocalVector();
PhiG2.RestoreDataPointerAndInsert();
}
//--------------------------------------------------------------------------
//Eq.(21a) of Hartmann et al., 2008, simplified
double
LevelSetReinitializer::DifferentiateInFirstLayer(double x0, double x1, double x2,
double tag0, [[maybe_unused]] double tag1, double tag2,
double phi0, double phi1, double phi2,
double phi00, double phi3, double eps)
{
bool phi0_useful = true, phi2_useful = true;
//identify cases in which phi0 is not useful
if(tag0==0) phi0_useful = false;
if(tag2==0) phi2_useful = false;
if(!phi0_useful && !phi2_useful)
return 0.0;
double dphi0 = phi1 - phi0;
double dphi1 = phi2 - phi1;
bool condB = dphi0*dphi1<0 || phi0*phi00<0 || phi2*phi3<0;
if(condB) {
if(phi0_useful) {
bool condA = (phi0*phi2<0) && fabs(dphi0 + eps)<fabs(dphi1);
if(condA)
phi0_useful = false;
}
if(phi2_useful) {
bool condA = (phi0*phi2<0) && fabs(dphi1 + eps)<fabs(dphi0);
if(condA)
phi2_useful = false;
}
}
if(!phi0_useful && !phi2_useful)
return 0.0;
if(phi0_useful) {
if(phi2_useful) { //central differencing
return CentralDifferenceLocal(phi0, phi1, phi2, x0, x1, x2);
}
else //backward difference
return (phi1-phi0)/(x1-x0);
}
else if(phi2_useful) { //forward difference
return (phi2-phi1)/(x2-x1);
}
else //neigher phi0 nor phi2 useful
return 0.0;
return 0.0;
}
//--------------------------------------------------------------------------
double
LevelSetReinitializer::ComputeResidualFullDomain(SpaceVariable3D &Phi, SpaceVariable3D &R, double cfl)
{
int NX, NY, NZ;
Phi.GetGlobalSize(&NX, &NY, &NZ);
// get data
double*** tag = Tag.GetDataPointer();
double*** sign = Sign.GetDataPointer();
double*** phi = Phi.GetDataPointer();
Vec3D*** dxyz = (Vec3D***)delta_xyz.GetDataPointer();
Vec3D*** coords = (Vec3D***)coordinates.GetDataPointer();
double*** res = R.GetDataPointer();
// fix first layer nodes?
bool fix_first_layer = (iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::FIXED ||
/*iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::UNCONSTRAINED||*/
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED1 ||
iod_ls.reinit.firstLayerTreatment == LevelSetReinitializationData::CONSTRAINED2);
// loop through the interior of the subdomain
double max_residual = 0.0, dt, dx, local_res;
double a,b,c,d,e,f, ap,am, bp,bm, cp,cm, dp,dm, ep,em, fp,fm;
for(int k=k0; k<kmax; k++)
for(int j=j0; j<jmax; j++)
for(int i=i0; i<imax; i++) {
if(fix_first_layer) {
if(tag[k][j][i]!=0) {//fixed node (first layer)
res[k][j][i] = 0.0;
continue;
}
}
//dx = min_dxyz;
dx = std::min(dxyz[k][j][i][0], std::min(dxyz[k][j][i][1], dxyz[k][j][i][2]));
dt = cfl*dx;
a = (phi[k][j][i]-phi[k][j][i-1])/(coords[k][j][i][0]-coords[k][j][i-1][0]);
b = (phi[k][j][i+1]-phi[k][j][i])/(coords[k][j][i+1][0]-coords[k][j][i][0]);
c = (phi[k][j][i]-phi[k][j-1][i])/(coords[k][j][i][1]-coords[k][j-1][i][1]);