-
Notifications
You must be signed in to change notification settings - Fork 0
/
icim.cpp
2902 lines (2224 loc) · 78.9 KB
/
icim.cpp
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
#include "icim.h"
#include "finitediff.h"
#include "interface.h"
#include "global.h"
#include "numerics.h"
#include <unordered_map>
#include <list>
#include <iterator>
using namespace std;
#define DEBUG
int CUBENBR = 2;
struct Info{
double d[3];
double f[3][5][5][5];
double M[3][3];
double d2u[3];
double sign;
};
unordered_map<int, Info> D2uMap;
Info SaveInfo(double** M, double* d, double**** f){
Info info;
for(int r = 0; r < 3; r++){
info.d[r] = d[r];
for(int i = 0 ; i <= 4; i++){
for(int j = 0 ; j <= 4; j++){
for(int k = 0 ; k <= 4; k++){
info.f[r][i][j][k] = f[r][i][j][k];
}
}
}
}
for(int i = 0 ; i <= 2; i++){
for(int j = 0 ; j <= 2; j++){
info.M[i][j] = M[i][j];
}
}
return info;
};
// Retreve info
void GetInfo(double** M, double* d, double**** f, const Info& info){
for(int r = 0; r < 3; r++){
d[r] = info.d[r];
for(int i = 0 ; i <= 4; i++){
for(int j = 0 ; j <= 4; j++){
for(int k = 0 ; k <= 4; k++){
f[r][i][j][k] = info.f[r][i][j][k];
}
}
}
}
for(int i = 0 ; i <= 2; i++){
for(int j = 0 ; j <= 2; j++){
M[i][j] = info.M[i][j];
}
}
};
// equation (12) in ICIM paper, return ind[r] = 3 if the r-dim touch boundary
array<int,3> Indicator(Index ind, double*** sign_surf, GridData &grid){
array<int,3> g = {0,0,0};
for(int r = 0; r < 3; r ++){
if (outofbound(UnitIncrement(ind,r,1), grid) || outofbound(UnitIncrement(ind,r,-1), grid) ){
// if outofbound, 3
g[r] = 3;
}else{
// if not out of bound
if( SameSign(evalarray(sign_surf,UnitIncrement(ind,r,1)),evalarray(sign_surf,ind))
&& SameSign(evalarray(sign_surf,UnitIncrement(ind,r,-1)),evalarray(sign_surf,ind)) ){
g[r] = 2;
}else if( !SameSign(evalarray(sign_surf,UnitIncrement(ind,r,1)),evalarray(sign_surf,ind)) &&
!SameSign(evalarray(sign_surf,UnitIncrement(ind,r,-1)),evalarray(sign_surf,ind)) ){
g[r] = 0;
}else{
g[r] = 1;
}
}
}
return g;
}
// check if is type 1 exception
bool istype1(Index ind, double *** sign_surf, GridData &grid){
for( int k = 0; k < 3; k++){
Index tind = ind; // temporary index
array<int,3> g0 = Indicator(ind, sign_surf, grid);
tind[k] = ind[k]-1;
array<int,3> gm = Indicator(tind, sign_surf, grid); //g(x_{i-e_k})
tind[k] = ind[k]+1;
array<int,3> gp = Indicator(tind, sign_surf, grid); //g(x_{i+e_k})
// type 1 exception
if (g0[k]==0 || gm[k]==0 || gp[k] == 0){
return true;
}
}
return false;
}
// check if is interior point
bool isinterior(Index ind, double*** S, GridData &grid){
array<int,3> g = Indicator(ind, S, grid);
return (g[0]==2 && g[1]==2 && g[2]==2);
}
// check if is type 2 exception
bool istype2(Index ind, double*** S, GridData &grid){
if (istype1(ind, S, grid)){
return false;
}
for (int m = 0; m < grid.dim; m++){
for (int n = m+1; n < grid.dim; n++){
int sk2[4];
char has_sk2 = yessk2(sk2, m, n, ind.data(), S, grid);
//
// if(ind[0]==eindex[0] && ind[1]==eindex[1] && ind[2]==eindex[2]){
// printf("in %i %i plane, sk2=%i\n",m,n,has_sk2);
// printPlane(S, grid, ind.data(), m, n, 2);
// }
//
if (!has_sk2){
return true;
}
}
}
return false;
}
// type 0 for interior, type 1 and 2 as defined in paper, eqn(12), 3 for regular cim, 4 for boundary
int GetType( Index ind, double ***S, GridData &grid ){
if(atbound(ind, grid)){
return 4;
}
if (isinterior(ind, S, grid)){
return 0;
}
if(istype1(ind, S, grid)){
return 1;
}
if(istype2(ind, S, grid)){
return 2;
}
return 3;
}
void AtomFlip(Index ind, double*** S, GridData& grid){
S[ind[0]][ind[1]][ind[2]] = -1 * S[ind[0]][ind[1]][ind[2]];
vector<Index> nbrs = GetDirectNeighbors(ind, grid);
// reset do-not-flip flag for nbrs
for(auto nbr : nbrs){
if( abs(S[nbr[0]][nbr[1]][nbr[2]])>1.5){
S[nbr[0]][nbr[1]][nbr[2]] = S[nbr[0]][nbr[1]][nbr[2]]/2.0;
}
}
// debug info
//
// int type = GetType(ind, S, grid);
// printf("flip at (%i,%i,%i) type %i\n", ind[0],ind[1],ind[2], type);
// print_surf(S,ind,2);
//
}
// flip ind and its negbhors upto certain recursion depth
// if if flipbydet is true, use determinant as threshold for flipping
// if marked as do-not-flip, set value to +/- 2
void LocalFlip(Index ind, double *** S, double *** flipping_S, PBData&pb, GridData &grid, int depth, int &counter, bool flipbydet){
if (depth == 0){
return;
}
vector<Index> nbrs = GetDirectNeighbors(ind, grid);
int type = GetType( ind, flipping_S, grid); // use flip S
if (type == 4){
// skip boundary
}else if (type == 1){
//if type 1, see table 1
array<int,3> g = Indicator(ind, flipping_S, grid);
Index gsort = g;
std::sort(gsort.begin(),gsort.end());
if (gsort == array<int,3>{0,0,0}){
//flip
AtomFlip(ind, flipping_S, grid);
++counter;
LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}if(gsort == array<int,3>{0,0,1}){ // estimate determinant and flip
if (abs(flipping_S[ind[0]][ind[1]][ind[2]])>1.5){
// if marked as do-not-flip, condinue
return;
}
bool fliphere = true;
AtomFlip(ind, flipping_S, grid); // first time flip
if(flipbydet){
// calculate condition number
double det = DetCouplingMatrix(ind, flipping_S, S, pb, grid);
if(det < 0.1265){
printf("det = %f at (%i,%i,%i) Do not flip.\n",det, ind[0],ind[1],ind[2] );
AtomFlip(ind, flipping_S, grid); // flip back
fliphere = false;
flipping_S[ind[0]][ind[1]][ind[2]] = 2 * flipping_S[ind[0]][ind[1]][ind[2]]; // mark as -2 or 2
}
}
if(fliphere){
// if it is actually fliped, flip nbr
++counter;
LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}
}else if(gsort == array<int,3>{0,1,1} ){
Index dummy_idx;
int dummy_r;
int r = std::distance(g.begin(), std::find(g.begin(), g.end(), 0)); //find with dimension is 0
// -- first shift then flip
vector<Index> nbr_for_d2u = GetShiftD2uNbr(ind, r, flipping_S, grid);
if( nbr_for_d2u.empty()){
AtomFlip(ind, flipping_S, grid);
++counter;
LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}
// -- flip no matter what
// bool fliphere = true;
// AtomFlip(ind, flipping_S, grid); // first time flip
// if(flipbydet){
// // calculate condition number
// double det = DetCouplingMatrix(ind, flipping_S, S, pb, grid);
// if(det < 0.1265){
// printf("det = %f at (%i,%i,%i) Do not flip.\n",det, ind[0],ind[1],ind[2] );
// AtomFlip(ind, flipping_S, grid); // flip back
// fliphere = false;
// }
// }
// if(fliphere){
// // if it is actually fliped, flip nbr
// ++counter;
// LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
// }
}
//for dim r where g[r] == 0, if can not find shift, then have to flip
for(int r = 0; r < 3; r ++){
if(g[r] == 0){
vector<Index> nbr_shift_d2u = GetShiftD2uNbr(ind, r, flipping_S, grid);
if( nbr_shift_d2u.empty()){
printf("g = (%i,%i,%i) at (%i,%i,%i). No shift for D2u[%i] , has to flip.\n", g[0], g[1], g[2], ind[0], ind[1], ind[2], r);
AtomFlip(ind, flipping_S, grid);
++counter;
LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}
}
}
}else if(type == 2){
// if type 2, see figure 6
// if case 4 5 6, either more than 2 nbr(up down left right) has G = 0, flip
for(int k = 0; k < 3; k++){
for(int l = k+1; l < 3; l++){
// iterater over plane-k,l
int count_plane = 0;
array<int,2> dim = {k,l};
array<int,2> dir = {-1,1};
vector< Index > index_to_flip; // list of index to flip
for(int r: dim){
for(int s: dir){
Index nbr_idx = UnitIncrement(ind, r, s); // neighbor in k l plane
array<int,3> G = Indicator(nbr_idx, S, grid);
int other_dim = (r == dim[0])? dim[1] : dim[0];
if (G[other_dim] == 0){
count_plane++;
index_to_flip.push_back( nbr_idx );
}
}
}
if (count_plane>=2){
for( auto idx: index_to_flip){
AtomFlip(idx, flipping_S, grid);
++counter;
LocalFlip(idx, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}
AtomFlip(ind, flipping_S, grid); // flip current
++counter;
LocalFlip(ind, S, flipping_S, pb, grid, --depth, counter, flipbydet);
}
}
} // end of kl loop
}// end of type 2
return;
}
// method to flip iteratively
void flip(double*** sign_surf, double*** original_surf, PBData &pb, GridData &grid, int maxIter, int depth, bool cond){
if (cond){
cout<<"Flipping with coupling matrix determinant"<<endl;
}else{
cout<<"Simple flipping without considering determinant"<<endl;
}
int total_flip = 0;
int iter_num = maxIter; // number of iteration
for(int iter = 0; iter < maxIter; iter++){
int count_iter = 0; // counter for flip action
for(int i = 0 ; i <= grid.nx[0]; i++){
for(int j = 0 ; j <= grid.nx[1]; j++){
for(int k = 0 ; k <= grid.nx[2]; k++){
Index ind = {i,j,k};
LocalFlip(ind, original_surf, sign_surf, pb, grid, depth, count_iter, cond);
}
}
}//end of ijk loop
total_flip += count_iter;
if (count_iter == 0){
// if no flip in this iteration, break out of max iter
iter_num = iter;
break;
}
}
int total_sign_change = 0;
for(int i = 0 ; i <= grid.nx[0]; i++){
for(int j = 0 ; j <= grid.nx[1]; j++){
for(int k = 0 ; k <= grid.nx[2]; k++){
if (!SameSign(sign_surf[i][j][k], original_surf[i][j][k])){
total_sign_change ++;
}
}
}
}//end of ijk loop
cout<< "Flip " << total_flip << " in "<< iter_num << " iterations"<< endl;
cout<< "Total sign change = " << total_sign_change <<endl;
return;
}
bool HasTwoNeighborBothSide(Index ind, int dim, int s, double*** S){
double a[4];
a[0] = evalarray(S, UnitIncrement(ind,dim,-s));//1 step back
a[1] = evalarray(S, ind);// here
a[2] = evalarray(S, UnitIncrement(ind,dim,s));//1 step forward
a[3] = evalarray(S, UnitIncrement(ind,dim,2*s));// 2step forward
return SameSign(a[0],a[1]) && !SameSign(a[1],a[2]) && SameSign(a[2],a[3]);
}
bool HasTwoNeighborWithGap(Index ind, int dim, int s, double*** S){
double a[4];
a[0] = evalarray(S, UnitIncrement(ind,dim,-s));//1 step back
a[1] = evalarray(S, ind);// here
a[2] = evalarray(S, UnitIncrement(ind,dim,s));//1 step forward
a[3] = evalarray(S, UnitIncrement(ind,dim,2*s));// 2step forward
return SameSign(a[0],a[1]) && !SameSign(a[1],a[2]) && SameSign(a[1],a[3]);
}
bool HasTwoNeighbor(Index ind, int dim, double*** S){
double a[3];
int s = 1;
a[0] = evalarray(S, UnitIncrement(ind,dim,-s));//1 step back
a[1] = evalarray(S, ind);// here
a[2] = evalarray(S, UnitIncrement(ind,dim,s));//1 step forward
return SameSign(a[0],a[1]) && SameSign(a[1],a[2]);
}
// approx second derivative by it nbr, used for coupling equation
// return a vector of Index that can be used to approximate D2u
vector<Index> GetShiftD2uNbr(Index ind, int dur, double*** S, GridData &grid){
vector<int> dim = {0,1,2};
vector<Index> result;
vector<Index> nbr_order;
vector<pair<double,Index> > dist_ind_pairs;
Index range{CUBENBR,CUBENBR,CUBENBR}; // only look at cube nbr
range[dur] = CUBENBR - 1; // along dur range can only be 1, if 2, then may step out of 5-point stencil
for( int i = -range[0]; i <= range[0]; i ++){
for( int j = -range[1]; j <= range[1]; j ++){
for( int k = -range[2]; k <= range[2]; k ++){
Index nbr = Add(ind, Index {i,j,k});
if (SameSide(S, ind, nbr) && HasTwoNeighbor(nbr, dur, S)){
// same side with two nbr
double dist = Norm2(Minus(nbr, ind));
dist_ind_pairs.push_back(make_pair(dist, nbr));
}
}
}
}
// sort by distance
sort(dist_ind_pairs.begin(),dist_ind_pairs.end());
for(auto e : dist_ind_pairs){
result.push_back(e.second);
}
return result;
}
// struct FD{
// vector<double> coef;
// vector<Index> stencil;
// Index center;
// };
// std::vector<FD> GetUsualMixD2u(double*** u, Index ind, int k, int l, double*** S, GridData& grid){
// vector<FD> fds;
// int combimation[9][4] = {
// {-1, 1, -1, 1},
// {-1, 1, -1, 0},
// {-1, 1, 0, 1},
// {-1, 0, -1, 1},
// { 0, 1, -1, 1},
// {-1, 0, -1, 0},
// {-1, 0, 0, 1},
// { 0, 1, -1, 0},
// { 0, 1, 0, 1} };
// for( int i = 0; i < 9; i++){
// }
// }
// has usual mixed derivative, four point squre stencile
bool HasUsualMixD2u(double*** u, Index ind, int k, int l, double*** S, GridData& grid){
int mid = 2;
int t, N = 2*mid, sindex[grid.dim], rindex[grid.dim], nindex[grid.dim], sk2[4];
if (yessk2(sk2,k,l,ind.data(),S,grid))//if D2u in (m,n) plane at index has approximation in same side
{
for (int t = 0; t < grid.dim; t++)
{
sindex[t] = mid;
nindex[t] = N;
}
if(u){
// if pointer is not null, write
SetMatrix(u, 0.0, N, N, N);
getD2(u,k,l,sk2,sindex,nindex,grid);// approximate D2u in terms of u-value only
}
return true;
}
return false;
}
// get O(h) apprx of Du from nbr points
bool HasShiftMixD2u(double*** ucoef, Index ind, int k, int l, double*** S, GridData& grid){
int mid = 2;
int N = 2 * mid;
SetMatrix(ucoef, 0.0, N, N, N);
bool found = false;
array<int,3> dim = {0,1,2};
vector<Index> nbr_order;
auto it = find_if(dim.begin(), dim.end(), [k,l](int v)->bool{return v!=k &&v!=l;});
int o = *it; // out of pane dimension
Index range = {mid - 1, mid - 1, mid - 1};
range[o] = mid;
// push back same side, order by distance, prefer out of plane
for(int i = -range[0]; i <= range[0]; i ++ ){
for(int j = -range[1]; j <= range[1]; j ++ ){
for(int k = -range[2]; k <= range[2]; k ++ ){
Index nbr_idx = Add(ind, Index{i,j,k});
if(SameSide(S, ind, nbr_idx)){
nbr_order.push_back(nbr_idx);
}
}
}
}
auto comp = [ind,o](Index &a, Index &b){
double da = Norm2(Minus(a,ind));
double db = Norm2(Minus(b,ind));
if(abs(da-db) > 1e-9){
// return smaller 2 norm
return da < db ;
}else{
// tie breaker, return out of plane
return abs(a[o] - b[o]) > 0;
}
};
std::sort( nbr_order.begin(), nbr_order.end(), comp);
double*** temp = matrix(N,N,N);
for(Index nbr_idx : nbr_order ){
if( !found && HasUsualMixD2u(temp, nbr_idx, k, l, S, grid)){
// shift result by grid in dim r, dir s
ExpandCoeff(1.0, ind, ucoef, nbr_idx, temp, N);
found = true;
}
if(found){
break;
}
}
free_matrix(temp, N, N, N);
return found;
}
bool HasCoupleMixD2u(double*** u, double* uxxcoef, Index ind, int k, int l, double*** S, GridData& grid){
int mid = 2;
int N = 2 * mid;
SetMatrix(u, 0.0, N, N, N);
SetMatrix(uxxcoef, 0.0, 2);
double hsqured = (grid.dx[k]*grid.dx[l]);
for ( int sk = -1; sk <=1; sk +=2){
for ( int sl = -1; sl <=1; sl +=2){
//restart every time
SetMatrix(u, 0.0, N, N, N);
SetMatrix(uxxcoef, 0.0, 2);
Index center = {mid,mid,mid};
if ( SameSide(S, ind, UnitIncrement(ind,k,sk)) && SameSide(S, ind, UnitIncrement(ind,l,sl)) ){
Index local_stepk = UnitIncrement(center,k,sk); // 1 step in k
Index local_stepl = UnitIncrement(center,l,sl); // 1 step in l
Index local_stepkl = UnitIncrement(local_stepk,l,-sl); // 1 step in k, then opp step in l
Index local_steplk = UnitIncrement(local_stepl,k,-sk); // 1 step in l, then opp step in k
if (SameSide(S, ind, Add(ind, Minus(local_steplk,center)))) {
// couple with ukk
uxxcoef[k] = (sk*sl) ;
setvalarray(u, center, 1.0/hsqured/(sk*sl) );
setvalarray(u, local_stepk, -1.0/hsqured/(sk*sl) );
setvalarray(u, local_stepl, 1.0/hsqured/(sk*sl) );
setvalarray(u, local_steplk, -1.0/hsqured/(sk*sl) );
return true;
}
if (SameSide(S, ind, Add(ind, Minus(local_stepkl,center)))) {
uxxcoef[l] = (sk*sl);
setvalarray(u, center, 1.0/hsqured/(sk*sl) );
setvalarray(u, local_stepk, 1.0/hsqured/(sk*sl) );
setvalarray(u, local_stepl, -1.0/hsqured/(sk*sl) );
setvalarray(u, local_stepkl, -1.0/hsqured/(sk*sl) );
return true;
}
}
}
}
return false;
}
bool IsUsualCim2(Index ind, double***S, GridData&grid){
array<int,3> g = Indicator(ind, S, grid);
// for each dimension
for(int r = 0; r < 3; r++){
if(g[r] == 3){
cerr<<"pass boundary points to IsUsualCim2"<<endl;
exit(1);
}
// has two nbr
if(g[r] == 2){
continue;
}
// has interface both side
if(g[r] == 0){
return false;
}
// has interface on one side
if(g[r] == 1){
int s=1;
if(SameSign(evalarray(S,ind), evalarray(S,UnitIncrement(ind, r, s)))){
s = -1;
}
if (!HasTwoNeighborBothSide(ind, r, s, S)){
return false;
}
}
// find mixed derivative
for(int m = 0; m < 3; m++){
for(int n = m + 1; n < 3; n++){
if (!HasUsualMixD2u( nullptr, ind, m, n, S, grid)){
return false;
}
}
}
}
return true;
}
bool IsIcim2(Index ind, double***S, GridData&grid){
array<int,3> g = Indicator(ind, S, grid);
for(int r = 0; r < 3; r++){
if(g[r] == 3){
cerr<<"pass boundary points to IsUsualCim2"<<endl;
exit(1);
}
}
if (IsUsualCim2(ind, S, grid)){
return true;
}else{
int type = GetType(ind, S, grid);
if(type==1){
bool has_fix = true;
for(int r = 0; r < 3; r++){
// each dim should has a fix
if(g[r] == 0){
vector<Index> nbr_shift_d2u = GetShiftD2uNbr( ind, r, S, grid);
if (nbr_shift_d2u.empty()){
// if both side has interface, but not shift, then fail
return false;
}
}else if (g[r]==1){
// if one side has interface,
// interface side
int s = (SameSide(S, ind, UnitIncrement(ind, r, -1) ))? 1 : -1;
if ( ! (HasTwoNeighborBothSide(ind, r, s, S) || HasTwoNeighborWithGap(ind, r, s, S))){
return false;
}
}else{
// both side no interface, not a problem
}
}
}else if (type==2){
// each plane should have a fix;
for(int m = 0; m < 3; m ++){
for(int n = m + 1; n < 3; n ++){
int dummy_s, dummy_r;
if (!HasMixD2u(nullptr, nullptr, m, n, ind, S, grid)) {
// if do not have couple mix deri or usual mix Du
return false;
}
}
}
}
}
return true;
}
// get mixed derivitive
bool HasMixD2u(double ***u, double *uxxcoef, int m, int n, Index index, double ***S, GridData &grid)
{
if (m>n){
swap(m, n);
}
int mid = 2;
int t, N = 2*mid, sindex[grid.dim], rindex[grid.dim], nindex[grid.dim], sk2[4];
double thesign;
if(u){
SetMatrix(u, 0.0, N, N, N);
}
if(uxxcoef){
SetMatrix(uxxcoef, 0.0, 2);
}
if (HasUsualMixD2u(u, index, m, n, S, grid)){
return true;
}else if (HasCoupleMixD2u(u, uxxcoef, index, m, n, S, grid)){
return true;
}else if(HasShiftMixD2u(u, index, m, n, S, grid)){
return true;
}else{
fprintf(stderr, "[HasMixD2u] no D2u[%i][%i] at (%i,%i,%i)\n", m, n, index[0],index[1],index[2]);
print_surf(S, index, 3);
}
return false;
}
// A is centered at a, B is centered at b, transfer coeff of B to A, assume both size N x N x N
// coeff of b should be copy to b-a
void ExpandCoeff(double coef, Index a, double *** A, Index b, double*** B, int N){
Index offset = Minus(b,a);
for( int i = 0; i <= N; i ++){
for( int j = 0; j <= N; j ++){
for( int k = 0; k <= N; k ++){
Index Bind = {i,j,k}; // Bind is an index in B
Index Aind = Add(Bind , offset); //Aind is Bind's local coord in A
if (all_of(Aind.begin(), Aind.end(), [N](int e) { return e<=N && e>=0; })){
// if Aind is in bound
A[Aind[0]][Aind[1]][Aind[2]] += coef * B[Bind[0]][Bind[1]][Bind[2]];
}else{
if (abs(evalarray(B,Bind))>1e-9){
cerr<<"loosing infomation when Expanding Coeff"<<endl;
exit(1);
}
}
}
}
}
}
// when approximating Du[dur]
// xnbr = x + sr h e_r
// D[dur](x) = D[dur](xnbr) - sr h D[dur][r](x) + O(h^2)
// D[dur](xnbr) = D^0 (xnbr) + O(h^2)
// or = D^s (xnbr) + 0.5 s h D[dur][dur](x) + O(h^2) , s = 1 backward diff, s = -1 forward diff
bool HasShiftDuAndMix(double ***ucoef, double* uxxcoef, Index ind, int dur, double*** S, GridData &grid){
int mid = 2;
int N = mid * 2;
Index center = {mid, mid, mid};
SetMatrix(ucoef, 0.0, N, N, N);
SetMatrix(uxxcoef, 0.0, 3);
double*** mix_d2u_ucoef = matrix(N, N, N);
double mix_d2u_uxxcoef[3];
bool found = false;
// find nbr with central diff
for(int r: {0, 1, 2}){
if(r == dur){
continue;
}
if( !found && HasMixD2u(mix_d2u_ucoef, mix_d2u_uxxcoef, r, dur, ind, S, grid)){
for(int s : {-1, 1}){
Index nbr = UnitIncrement(ind, r, s);
Index nbr_loc = UnitIncrement(center, r, s);
if(SameSide(S, ind, nbr)){
Index g = Indicator(nbr, S, grid);
if(g[dur] == 2){
// can do central diff
addarray(ucoef, UnitIncrement(nbr_loc, dur , 1), 1.0 / (2.0 * grid.dx[dur]));
addarray(ucoef, UnitIncrement(nbr_loc, dur , -1), -1.0 / (2.0 * grid.dx[dur]));
found = true;
}else if(g[dur] == 1){
// can do forward backward diff
int snbr = SameSide(S, UnitIncrement(nbr,dur,1), nbr)? -1: 1;// which side is interface
addarray(ucoef, nbr_loc, snbr / grid.dx[dur]);
addarray(ucoef, UnitIncrement(nbr_loc, dur , -snbr), - snbr / grid.dx[dur]);
uxxcoef[dur] += 0.5 * snbr * grid.dx[dur];
found = true;
}else{
// 2 interface, go to next nbr
continue;
}
}
if(found){
ExpandCoeff(-s * grid.dx[dur], ind, ucoef, ind, mix_d2u_ucoef, N);
break;
}
}
}
}
free_matrix(mix_d2u_ucoef, N, N, N);
if(ind[0]==eindex[0] && ind[1]==eindex[1] && ind[2]==eindex[2]){
printf("[HasShiftDuAndMix] (%i,%i,%i) \n",ind[0], ind[1], ind[2]);
// for each component of Du
double dummy_D1uxcoef[3] = {0};
double thesign = GetSign(S,ind);
double approx = evalcoef(0.0, ucoef,dummy_D1uxcoef,uxxcoef,ind.data(),0,0,0.0,mid,S,grid);
double exact_Du = getDu(ind.data(),dur,0,0,0,thesign,grid);
double err = approx - exact_Du;
printf("grid Du[%i] = %f, exact = %f, loc trunc err = %f\n", dur, approx, exact_Du, err);
}
return found;
}
// Approximate Du in dur dimension
// a nbr in rr-dim has central diff in dur and mix Du(rr,r), used to find Du at grid point
// xnbr = x + sr h e_r+ sr2 h e_r2 + sr3 h e_r3
// D[r](x) = D[r](xnbr) - sr h D[r][r](x) - sr2 h D[r][r2](x) - sr3 h D[r][r3](x) + O(h^2)
// D[r](xnbr) = D^0 (xnbr) + O(h^2)
// or D^s (xnbr) + 0.5 s h D[r][r](x) + O(h^2) , s = 1 backward diff, s = -1 forward diff
bool HasShiftDuAndMixGeneral(double ***ucoef, double* uxxcoef, Index ind, int dur, double*** S, GridData &grid){
int mid = 2;
int N = mid * 2;
Index center = {mid, mid, mid};
SetMatrix(ucoef, 0.0, N, N, N);
SetMatrix(uxxcoef, 0.0, 3);
double*** mix_d2u_ucoef = matrix(N, N, N);
double mix_d2u_uxxcoef[3];
bool found = false;
vector<Index> nbr_order;
Index range = {mid , mid , mid};
range[dur] = mid - 1; // smaller range in du dimension
// push back same side, order by distance, prefer central difference
for(int i = -range[0]; i <= range[0]; i ++ ){
for(int j = -range[1]; j <= range[1]; j ++ ){
for(int k = -range[2]; k <= range[2]; k ++ ){
Index nbr_idx = Add(ind, Index{i,j,k});
Index g = Indicator(nbr_idx, S, grid);
if(SameSide(S, ind, nbr_idx) && g[dur]!=0){
// same side and has neighbor
nbr_order.push_back(nbr_idx);
}
}
}
}
auto comp = [&](Index &a, Index &b){
double da = Norm2(Minus(a,ind));
double db = Norm2(Minus(b,ind));
if(abs(da-db) > 1e-9){
// return smaller 2 norm
return da < db ;
}else{
// prefer central diff
return HasTwoNeighbor(a, dur, S);
}
};
std::sort( nbr_order.begin(), nbr_order.end(), comp);
if(nbr_order.empty()){
fprintf(stderr, "no nbr candidates at (%i,%i,%i)\n", ind[0], ind[1], ind[2]);
print_surf(S, ind, mid);
exit(1);
}
// approximate second derivative
Index nbr_to_eval;
for(Index nbr: nbr_order){
SetMatrix(ucoef, 0.0, N, N, N);
SetMatrix(uxxcoef, 0.0, 3);
int count_valid = 0; // count if each dimension sucess;
Index s = Minus(nbr, ind);
for(int r: {0, 1, 2}){
if(s[r] == 0){
count_valid ++;
continue;
}else{
if(r == dur){
mix_d2u_uxxcoef[r] += -s[r];
count_valid ++;
}else{
if(HasMixD2u(mix_d2u_ucoef, mix_d2u_uxxcoef, r, dur, ind, S, grid)){
count_valid ++;
ExpandCoeff(-s[r] * grid.dx[dur], ind, ucoef, ind, mix_d2u_ucoef, N);
}
}
}
}
if (count_valid == 3){
nbr_to_eval = nbr;
found = true;
break;
}
}
if(!found){
fprintf(stderr, "[HasShiftDuAndMixGeneral] no D2u at (%i,%i,%i)\n", ind[0], ind[1], ind[2]);
print_surf(S, ind, mid);
exit(1);
}
// approximated Du[dur] at nbr
Index nbr_loc = Add(center, Minus(nbr_to_eval, ind));
if(HasTwoNeighbor(nbr_to_eval, dur, S)){
addarray(ucoef, UnitIncrement(nbr_loc, dur , 1), 1.0 / (2.0 * grid.dx[dur]));