-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAFMM2D.hpp
2730 lines (2602 loc) · 102 KB
/
DAFMM2D.hpp
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
#ifndef _G_FMM2DTree_HPP__
#define _G_FMM2DTree_HPP__
double kappa;
#include <fstream>
#include <sstream>
#include <string>
#include <omp.h>
#include <cmath>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <chrono>
#include <filesystem>
struct orderedPair {
int x,y;
};
#include "ACA.hpp"
string currentDirectory;
double arctan(double y, double x) {//returns atan2 in range (0,2*PI)
double temp = atan2(y, x);
if (temp < 0.0)
return temp + 2*PI;
else
return temp;
}
const double machinePrecision = pow(10,-16);
using namespace std::chrono;
class FMM2DCone {
public:
bool outgoing_ILActive;
bool incoming_ILActive;
//Directional multipoles and locals of the box in this cone direction
Vec multipoles;
//defined only in HFR
//std::vector<pts2D> &outgoing_chargePoints;//equivalent points {y_{k}^{B,o,l}}
Vec outgoing_charges;//equivalent densities {f_{k}^{B,o,l}}
//std::vector<pts2D> &outgoing_checkPoints;//check points {x_{k}^{B,o,l}}
Vec outgoing_potential;//check potentials {u_{k}^{B,o,l}}
//outgoing_chargePoints and outgoing_checkPoints are considered to be same as incoming_checkPoints and incoming_chargePoints respectively; hence are not declared
//This is because of the assumption that the potentials are evaluated at the charge locations
std::vector<int> incoming_chargePoints;//equivalent points {y_{k}^{B,i,l}}
Vec incoming_charges;//equivalent densities {f_{k}^{B,i,l}}
std::vector<int> incoming_checkPoints;//check points {x_{k}^{B,i,l}}
std::vector<int> outgoing_chargePoints;//equivalent points {y_{k}^{B,i,l}}
std::vector<int> outgoing_checkPoints;//check points {x_{k}^{B,i,l}}
std::vector<int> user_checkPoints;
Vec incoming_potential;//check potentials {u_{k}^{B,i,l}}
ColPivHouseholderQR<Mat> outgoing_Atilde_dec;
ColPivHouseholderQR<Mat> incoming_Atilde_dec;
std::vector<orderedPair > InteractionList;
double angle;
Mat outgoing_Ar;
Mat L2L[4], M2M, Ktilde;
std::vector<Mat> M2L;
FMM2DCone () {}
};
class FMM2DBox {
public:
double radius;
int active;
bool outgoing_ILActive;
bool incoming_ILActive;
int level;
int boxNumber;
int parentNumber;
int childrenNumbers[4];
bool isLeaf;
std::vector<orderedPair > neighborNumbers;
int fineNeighbors[12];//12
int coarseNeighbors[12];//12
int separatedFineNeighbors[20];//20
int colleagueNeighbors[9];//9
std::vector<orderedPair > InteractionList;
std::vector<int > MFR_IL;
//defined only in LFR
//std::vector<pts2D> &outgoing_chargePoints;//equivalent points {y_{k}^{B,o}}
Vec outgoing_charges;//equivalent densities {f_{k}^{B,o}}
//std::vector<pts2D> &outgoing_checkPoints;//check points {x_{k}^{B,o}}
Vec outgoing_potential;//check potentials {u_{k}^{B,o}}
//outgoing_chargePoints and outgoing_checkPoints are considered to be same as incoming_checkPoints and incoming_chargePoints respectively; hence are not declared
//This is because of the assumption that the potentials are evaluated at the charge locations
std::vector<int> incoming_chargePoints;//equivalent points {y_{k}^{B,i}}
Vec incoming_charges;//equivalent densities {f_{k}^{B,i}}
std::vector<int> incoming_checkPoints;//check points {x_{k}^{B,i}}
std::vector<int> user_checkPoints;
std::vector<int> outgoing_chargePoints;//equivalent points {y_{k}^{B,i,l}}
std::vector<int> outgoing_checkPoints;//check points {x_{k}^{B,i,l}}
Vec incoming_potential;//check potentials {u_{k}^{B,i}}
ColPivHouseholderQR<Mat> outgoing_Atilde_dec;
ColPivHouseholderQR<Mat> incoming_Atilde_dec;
Mat outgoing_Ar;
Mat L2L, Ktilde;
std::vector<Mat> M2L;
FMM2DBox () {
boxNumber = -1;
parentNumber = -1;
for (int l=0; l<4; ++l) {
childrenNumbers[l] = -1;
}
isLeaf = false;
active = true;
for (size_t i = 0; i < 12; i++) {
fineNeighbors[i] = -1;
}
for (size_t i = 0; i < 12; i++) {
coarseNeighbors[i] = -1;
}
for (size_t i = 0; i < 20; i++) {
separatedFineNeighbors[i] = -1;
}
for (size_t i = 0; i < 9; i++) {
colleagueNeighbors[i] = -1;
}
}
Vec multipoles;
pts2D center;
std::vector<int> chargeLocations;
std::vector<pts2D> chebNodes;
std::vector<FMM2DCone> ConeTree;
};
class FMM2DTree: public LowRank {
public:
double timeIn_getMatrixEntry;//for time profiling
long NoIn_getMatrixEntry;
int nLevels; // Number of levels in the tree.
int nChebNodes; // Number of Chebyshev nodes along one direction.
int rank; // Rank of interaction, i.e., rank = nChebNodes*nChebNodes.
int N; // Number of particles.
double L; // Semi-length of the simulation box.
double smallestBoxSize; // This is L/2.0^(nLevels).
double a; // Cut-off for self-interaction. This is less than the length of the smallest box size.
const double A = 3.0; //higher A, higher level_FarField
const double B = 0.35;//4.0; //smaller B, higher level_LFR
int level_LFR;
int level_FarField;
int nCones_LFR;
Vec chargesAll;
double SFN_angles[20];
double N_angles[9];
double FN_angles[12];
double CN_angles[12];
std::vector<pts2D> bottomTopBoundary;//used for plotting field
std::vector<pts2D> leftRightBoundary;//used for plotting field
bool findPhi;
string NeighborFilename;
string MFilename;
std::vector<pts2D> gridPoints;
std::vector<int> nBoxesPerLevel; // Number of boxes at each level in the tree.
std::vector<double> boxRadius; // Box radius at each level in the tree assuming the box at the root is [-1,1]^2
std::vector<double> ConeAperture;
std::vector<int> nCones;
std::vector<double> boxHomogRadius; // Stores the value of boxRadius^{alpha}
std::vector<double> boxLogHomogRadius; // Stores the value of alpha*log(boxRadius)
std::vector<std::vector<FMM2DBox> > tree; // The tree storing all the information.
std::vector<std::vector<int> > indexTree;
int TOL_POW;
// Different Operators
int yes2DFMM;
std::vector<double> standardChebNodes1D;
std::vector<pts2D> standardChebNodes;
std::vector<pts2D> standardChebNodesChild;
std::vector<pts2D> leafChebNodes;
std::vector<orderedPair> leafNodes;
// Different Operators
Eigen::MatrixXd M2M[4]; // Transfer from multipoles of 4 children to multipoles of parent.
Eigen::MatrixXd L2L[4]; // Transfer from locals of parent to locals of 4 children.
std::vector<Mat > M; //M matrices computed during precomputations
Eigen::MatrixXd Q_pinv;
Eigen::MatrixXd Q_pinv2;
Mat colleagueNeighborInteraction[9][9];
Mat fineNeighborInteraction[9][12];
Mat separatedFineNeighborInteraction[9][20];
Mat coarseNeighborInteraction[9][12];
// public:
FMM2DTree(int nCones_LFR, int nChebNodes, double L, int yes2DFMM, int TOL_POW, std::vector<pts2D> particles_X, std::vector<pts2D> particles_Y, std::vector<int> row_indices, std::vector<int> col_indices):
LowRank(TOL_POW, particles_X, particles_Y, row_indices, col_indices) {
this->findPhi = true;
this->NoIn_getMatrixEntry = 0;
this->timeIn_getMatrixEntry = 0.0;
this->TOL_POW = TOL_POW;
this->nChebNodes = nChebNodes;
this->rank = nChebNodes*nChebNodes;
this->L = L;
this->nCones_LFR = nCones_LFR;
this->yes2DFMM = yes2DFMM;
nBoxesPerLevel.push_back(1);
boxRadius.push_back(L);
this->a = smallestBoxSize;
int k;
if (yes2DFMM == 1) {
if (kappa==0)
this->level_LFR = 2; //actually should be 2; but for checking the accuracy of DFMM i am making it 3; so that HFR code runs even for LFR; if that gives good result it means DFMM code is perfect
else {
this->level_LFR = floor(log(kappa*L/B)/log(2.0));
}
if (level_LFR < 2) level_LFR = 2;
}
else {
level_LFR = 2;
}
// cout << "level_LFR: " << level_LFR << endl;
}
void shift_scale_Nodes(std::vector<pts2D>& Nodes, std::vector<pts2D>& shifted_scaled_Nodes, double xShift, double yShift, double radius) {
for (int k=0; k < Nodes.size(); ++k) {
pts2D temp;
temp.x = radius*Nodes[k].x+xShift;
temp.y = radius*Nodes[k].y+yShift;
shifted_scaled_Nodes.push_back(temp);
}
}
// get_ChebPoly
double get_ChebPoly(double x, int n) {
return cos(n*acos(x));
}
// get_S
double get_S(double x, double y, int n) {
double S = 0.5;
for (int k=1; k<n; ++k) {
S+=get_ChebPoly(x,k)*get_ChebPoly(y,k);
}
return 2.0/n*S;
}
// set_Standard_Cheb_Nodes
void set_Standard_Cheb_Nodes() {
for (int k=0; k<nChebNodes; ++k) {
standardChebNodes1D.push_back(-cos((k+0.5)/nChebNodes*PI));
}
pts2D temp1;
for (int j=0; j<nChebNodes; ++j) {
for (int k=0; k<nChebNodes; ++k) {
temp1.x = standardChebNodes1D[k];
temp1.y = standardChebNodes1D[j];
standardChebNodes.push_back(temp1);
}
}
// Left Bottom child, i.e., Child 0
for (int j=0; j<rank; ++j) {
temp1 = standardChebNodes[j];
temp1.x = 0.5*temp1.x-0.5;
temp1.y = 0.5*temp1.y-0.5;
standardChebNodesChild.push_back(temp1);
}
// Right Bottom child, i.e., Child 1
for (int j=0; j<rank; ++j) {
temp1 = standardChebNodes[j];
temp1.x = 0.5*temp1.x+0.5;
temp1.y = 0.5*temp1.y-0.5;
standardChebNodesChild.push_back(temp1);
}
// Right Top child, i.e., Child 2
for (int j=0; j<rank; ++j) {
temp1 = standardChebNodes[j];
temp1.x = 0.5*temp1.x+0.5;
temp1.y = 0.5*temp1.y+0.5;
standardChebNodesChild.push_back(temp1);
}
// Left Top child, i.e., Child 3
for (int j=0; j<rank; ++j) {
temp1 = standardChebNodes[j];
temp1.x = 0.5*temp1.x-0.5;
temp1.y = 0.5*temp1.y+0.5;
standardChebNodesChild.push_back(temp1);
}
}
void get_Transfer_Matrix() {
for (int l=0; l<4; ++l) {
L2L[l] = Eigen::MatrixXd(rank,rank);
for (int j=0; j<rank; ++j) {
for (int k=0; k<rank; ++k) {
L2L[l](j,k) = get_S(standardChebNodes[k].x, standardChebNodesChild[j+l*rank].x, nChebNodes)*get_S(standardChebNodes[k].y, standardChebNodesChild[j+l*rank].y, nChebNodes);
}
}
}
for (int l=0; l<4; ++l) {
M2M[l] = L2L[l].transpose();
}
}
void createTree(int j, int k) {
//k: index of box in the vector tree[j]
//b: boxNumber of box, tree[j][k]
bool condition3Leaf = false;
if (kappa*tree[j][k].radius/2/PI <= 0.25) condition3Leaf = true; //each leaf shoould contain less than or equal to 1 wave cycle
//nChebnodes are the number of points in 1 wavecycle/Wavelength - in 1D.
if (condition3Leaf) {
tree[j][k].isLeaf = true;
orderedPair op;
op.x = j;
op.y = k;
leafNodes.push_back(op);
}
else {
if (int(tree.size()) == j+1) {
std::vector<FMM2DBox> level;
tree.push_back(level);
std::vector<int> index;
indexTree.push_back(index);
}
int n = tree[j+1].size();
int b = tree[j][k].boxNumber;
for (size_t c = 0; c < 4; c++) {
FMM2DBox box;
box.level = j+1;
box.boxNumber = b*4+c;
box.parentNumber = b;
box.radius = 0.5*tree[j][k].radius;
if (c==0) {
box.center.x = tree[j][k].center.x-0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y-0.5*tree[j][k].radius;
}
else if (c==1) {
box.center.x = tree[j][k].center.x+0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y-0.5*tree[j][k].radius;
}
else if (c==2) {
box.center.x = tree[j][k].center.x+0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y+0.5*tree[j][k].radius;
}
else {
box.center.x = tree[j][k].center.x-0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y+0.5*tree[j][k].radius;
}
tree[j+1].push_back(box);
indexTree[j+1].push_back(box.boxNumber);
}
for (size_t c = 0; c < 4; c++) {
createTree(j+1, n+c);
}
}
}
void createAdaptiveTree() {
FMM2DBox root;
root.level = 0;
root.boxNumber = 0;
root.parentNumber = -1;
root.radius = L;
root.center.x = 0.0;
root.center.y = 0.0;
std::vector<FMM2DBox> level;
level.push_back(root);
tree.push_back(level);
std::vector<int> index;
index.push_back(0);
indexTree.push_back(index);
createTree(0, 0);
nLevels = tree.size() - 1;
// cout << "nLevels: " << nLevels << endl;
for (size_t j = 1; j <= 10; j++) {
boxRadius.push_back(boxRadius[j-1]/2.0);
}
if (level_LFR >= nLevels) {
level_LFR = nLevels-1;
}
}
void getFutureGeneration_LFR(int bj, int bk, int pnj, int pni, std::vector<orderedPair>& futureGeneration) {
if (tree[pnj][pni].isLeaf) {
if ( fabs(tree[bj][bk].center.x - tree[pnj][pni].center.x) >= 3.0*boxRadius[bj] + boxRadius[pnj]-machinePrecision
|| fabs(tree[bj][bk].center.y - tree[pnj][pni].center.y) >= 3.0*boxRadius[bj] + boxRadius[pnj]-machinePrecision) {
}
else {
orderedPair op;
op.x = pnj;
op.y = pni;
futureGeneration.push_back(op);
}
}
else {
for (int nc=0; nc<4; ++nc) { //children of parents neighbors
int pnn = tree[pnj][pni].boxNumber;//boxNumber
int boxB = 4*pnn+nc;//its index=?
std::vector<int>::iterator indx = std::find(indexTree[pnj+1].begin(), indexTree[pnj+1].end(), boxB);
int boxB_index = indx-indexTree[pnj+1].begin();
getFutureGeneration_LFR(bj, bk, pnj+1, boxB_index, futureGeneration);
}
}
}
void assign_Child_Interaction_LFR(int c, int j, int k, std::vector<std::vector<std::vector<orderedPair> > >& Tree_Neighbors_LFR) {
int parentboxNumber = tree[j][k].boxNumber;
int boxA = 4*parentboxNumber+c;//child box number; its index=?
std::vector<int>::iterator indx = std::find(indexTree[j+1].begin(), indexTree[j+1].end(), boxA);
int boxA_index = indx-indexTree[j+1].begin();
for (int n=0; n<Tree_Neighbors_LFR[j][k].size(); ++n) {//parents neighbors; so u need its index to access it which is k
//children of neighbors of parent which are not neighbors to child=IL
int pnj = Tree_Neighbors_LFR[j][k][n].x;//level
int pni = Tree_Neighbors_LFR[j][k][n].y;//index
int pnn = tree[pnj][pni].boxNumber;//boxNumber
std::vector<orderedPair> futureGeneration;
getFutureGeneration_LFR(j+1, boxA_index, pnj, pni, futureGeneration);
for (size_t d = 0; d < futureGeneration.size(); d++) {
Tree_Neighbors_LFR[j+1][boxA_index].push_back(futureGeneration[d]);
}
}
}
// Assigns the interactions for the children of a box
void assign_Box_Interactions_LFR(int j, int k, std::vector<std::vector<std::vector<orderedPair> > >& Tree_Neighbors_LFR) {
if (!tree[j][k].isLeaf) {
for (int c=0; c<4; ++c) {
assign_Child_Interaction_LFR(c,j,k, Tree_Neighbors_LFR);
}
}
}
// Assigns the interactions for the children all boxes at a given level
void assign_Level_Interactions_LFR(int j, std::vector<std::vector<std::vector<orderedPair> > >& Tree_Neighbors_LFR) {
for (int k=0; k<tree[j].size(); ++k) {
assign_Box_Interactions_LFR(j,k, Tree_Neighbors_LFR);//k is index number of box in tree[j] vector
}
}
// Assigns the interactions for the children all boxes in the tree
void assign_Tree_Interactions_LFR(std::vector<std::vector<std::vector<orderedPair> > >& Tree_Neighbors_LFR) {
int J = 1;
for (int c=0; c<4; ++c) {
for (int n=0; n<4; ++n) {
orderedPair op;
op.x = J;
op.y = n;
Tree_Neighbors_LFR[J][c].push_back(op);
}
}
for (int j=1; j<=nLevels-1; ++j) {
assign_Level_Interactions_LFR(j, Tree_Neighbors_LFR);
}
}
void makeLevelRestriction_Tree() {
for (size_t n = 0; n < nLevels; n++) {
std::vector<std::vector<std::vector<orderedPair> > > Tree_Neighbors_LFR;
for (size_t j = 0; j <= nLevels; j++) {
std::vector<std::vector<orderedPair> > level;
for (size_t k = 0; k < tree[j].size(); k++) {
std::vector<orderedPair> box;
level.push_back(box);
}
Tree_Neighbors_LFR.push_back(level);
}
assign_Tree_Interactions_LFR(Tree_Neighbors_LFR);
std::vector<orderedPair> leafNodesOld = leafNodes;
for (size_t l = 0; l < leafNodesOld.size(); l++) {
makeLevelRestriction_Box(l, leafNodes[l], Tree_Neighbors_LFR, leafNodesOld);
}
int cnt = 0;
for (size_t l = 0; l < leafNodesOld.size(); l++) {//removing all the non-leaves from leafNodes vector
if (leafNodesOld[l].x == -1) {
leafNodes.erase(leafNodes.begin()+l-cnt);
cnt++;
}
}
for (size_t j = 0; j <= nLevels; j++) {
for (size_t k = 0; k < Tree_Neighbors_LFR[j].size(); k++) {
Tree_Neighbors_LFR[j][k].clear();
}
Tree_Neighbors_LFR[j].clear();
}
Tree_Neighbors_LFR.clear();
}
}
int getIndexOfInLeafNodes(orderedPair op) {
for (size_t l = 0; l < leafNodes.size(); l++) {
if (op.x == leafNodes[l].x && op.y == leafNodes[l].y) {
return l;
}
}
}
void makeLevelRestriction_Box(int l, orderedPair leaf, std::vector<std::vector<std::vector<orderedPair> > >& Tree_Neighbors_LFR, std::vector<orderedPair>& leafNodesOld) {
//l is index of leaf in leafNodes
int j = leaf.x;
int k = leaf.y;
for (size_t i = 0; i < Tree_Neighbors_LFR[j][k].size(); i++) {
int nj = Tree_Neighbors_LFR[j][k][i].x;
int nk = Tree_Neighbors_LFR[j][k][i].y;
std::vector<orderedPair> newLeaves;
if (j-nj >= 2 || nj-j >= 2) {
if (j-nj >= 2) {
int indexOfBox = getIndexOfInLeafNodes(Tree_Neighbors_LFR[j][k][i]);//index of Tree_Neighbors_LFR[j][k][i]
if (leafNodesOld[indexOfBox].x != -1) {
refineBox(indexOfBox, Tree_Neighbors_LFR[j][k][i], leafNodesOld);
}
}
else if(nj-j >= 2) {
if (leafNodesOld[l].x != -1) {
refineBox(l, leaf, leafNodesOld);
}
}
}
}
}
void refineBox(int l, orderedPair leaf, std::vector<orderedPair> &leafNodesOld) {
int j = leaf.x; //level
int k = leaf.y; //box index
int b = tree[j][k].boxNumber;
leafNodesOld[l].x = -1;//making it a non-leaf node
tree[j][k].isLeaf = false;
for (size_t c = 0; c < 4; c++) {
FMM2DBox box;
box.isLeaf = true;
box.level = j+1;
box.boxNumber = b*4+c;
box.parentNumber = b;
box.radius = 0.5*tree[j][k].radius;
if (c==0) {
box.center.x = tree[j][k].center.x-0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y-0.5*tree[j][k].radius;
}
else if (c==1) {
box.center.x = tree[j][k].center.x+0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y-0.5*tree[j][k].radius;
}
else if (c==2) {
box.center.x = tree[j][k].center.x+0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y+0.5*tree[j][k].radius;
}
else {
box.center.x = tree[j][k].center.x-0.5*tree[j][k].radius;
box.center.y = tree[j][k].center.y+0.5*tree[j][k].radius;
}
orderedPair op;
op.x = j+1;
op.y = tree[j+1].size();
leafNodes.push_back(op);
tree[j+1].push_back(box);
indexTree[j+1].push_back(box.boxNumber);
}
}
void assignLeafCharges() {
for (size_t k = 0; k < leafNodes.size(); k++) {
int j = leafNodes[k].x;
int b = leafNodes[k].y;
tree[j][b].multipoles = 0.5*(Vec::Ones(rank));//+Eigen::VectorXd::Random(rank));
}
}
void assignLeafCharges(Vec &charges) {
chargesAll = charges;
int start = 0;
for (size_t k = 0; k < leafNodes.size(); k++) {
int j = leafNodes[k].x;
int b = leafNodes[k].y;
tree[j][b].multipoles = charges.segment(start, rank);
start += rank;
}
}
void assignLeafChargeLocations(std::vector<pts2D> &particles_out) {
for (size_t k = 0; k < leafNodes.size(); k++) {
int j = leafNodes[k].x;
int b = leafNodes[k].y;
int startIndex = gridPoints.size();
for (size_t i = 0; i < rank; i++) {
tree[j][b].chargeLocations.push_back(startIndex+i);
}
shift_scale_Nodes(standardChebNodes, gridPoints, tree[j][b].center.x, tree[j][b].center.y, boxRadius[j]);
}
particles_X = gridPoints;//object of base class FMM_Matrix
particles_Y = gridPoints;
particles_out = gridPoints;
}
void collectBoundary() {
for (size_t t = 0; t < leafNodes.size(); t++) {
int j = leafNodes[t].x;
int k = leafNodes[t].y;
pts2D leftRightBoundaryTemp, bottomTopBoundaryTemp;
leftRightBoundaryTemp.x = tree[j][k].center.x - tree[j][k].radius;//left
leftRightBoundaryTemp.y = tree[j][k].center.x + tree[j][k].radius;//right
bottomTopBoundaryTemp.x = tree[j][k].center.y - tree[j][k].radius;//bottom
bottomTopBoundaryTemp.y = tree[j][k].center.y + tree[j][k].radius;//top
leftRightBoundary.push_back(leftRightBoundaryTemp);
bottomTopBoundary.push_back(bottomTopBoundaryTemp);
}
}
void assignNonLeafChargeLocations() {
for (int j=nLevels-1; j>1; --j) {
int J = j+1;
// #pragma omp parallel for
for (int k=0; k<tree[j].size(); ++k) {
if (!tree[j][k].isLeaf) {
int b = tree[j][k].boxNumber;
int KboxNumber;
int K[4];
std::vector<int>::iterator indx;
KboxNumber = 4*b+0;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[0] = indx-indexTree[J].begin();
KboxNumber = 4*b+1;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[1] = indx-indexTree[J].begin();
KboxNumber = 4*b+2;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[2] = indx-indexTree[J].begin();
KboxNumber = 4*b+3;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[3] = indx-indexTree[J].begin();
for (int c=0; c<4; ++c) {
//Block containing n elements, starting at position i: vector.segment(i,n)
for (int i = 0; i < tree[J][K[c]].chargeLocations.size(); i++) {
tree[j][k].chargeLocations.push_back(tree[J][K[c]].chargeLocations[i]);
}
}
}
}
}
}
void assignLeafChebNodes() {
for (size_t k = 0; k < leafNodes.size(); k++) {
int j = leafNodes[k].x;
int b = leafNodes[k].y;
shift_scale_Nodes(standardChebNodes, tree[j][b].chebNodes, tree[j][b].center.x, tree[j][b].center.y, boxRadius[j]);
}
}
double errInApproximation(Eigen::VectorXd& trueValue, Eigen::VectorXd& approximateValue) {
Eigen::VectorXd error(trueValue.size());
for (int k=0; k<trueValue.size(); ++k) {
error(k) = fabs(trueValue(k)-approximateValue(k));
//error(k) = fabs((trueValue-approximateValue)(k)/trueValue(k));
}
return error.maxCoeff();///trueValue.maxCoeff();
}
double errInApproximation(Vec& trueValue, Vec& approximateValue) {
Vec error;
error = trueValue - approximateValue;
VectorXd errorAbs = error.cwiseAbs();
VectorXd trueValueAbs = trueValue.cwiseAbs();
return errorAbs.maxCoeff();///trueValueAbs.maxCoeff();
}
void outputAdaptiveGrid(std::string filename) {
double xcenter = 0.0;
double ycenter = 0.0;
double Lx = L;
double Ly = L;
std::ofstream myfile;
myfile.open(filename.c_str());
myfile << "\\documentclass{standalone}" << std::endl;
myfile << "\\usepackage{tikz}" << std::endl;
myfile << "\\begin{document}" << std::endl;
myfile << "\\begin{tikzpicture}" << std::endl;
for (int k=0; k<(int)leafNodes.size(); ++k) {
int j = leafNodes[k].x;
int b = leafNodes[k].y;
myfile << "\\draw (" << tree[j][b].center.x-tree[j][b].radius << ",";
myfile << tree[j][b].center.y-tree[j][b].radius << ") rectangle (";
myfile << tree[j][b].center.x+tree[j][b].radius << ",";
myfile << tree[j][b].center.y+tree[j][b].radius << ");" << std::endl;
}
long double push = 0.125;
myfile<< "\\node at (" << xcenter-Lx-push << "," << ycenter-Ly-push << ") {\\tiny$(" << xcenter-Lx << "," << ycenter-Ly << ")$};" << std::endl;
myfile<< "\\node at (" << xcenter-Lx-push << "," << ycenter+Ly+push << ") {\\tiny$(" << xcenter-Lx << "," << ycenter+Ly << ")$};" << std::endl;
myfile<< "\\node at (" << xcenter+Lx+push << "," << ycenter-Ly-push << ") {\\tiny$(" << xcenter+Lx << "," << ycenter-Ly << ")$};" << std::endl;
myfile<< "\\node at (" << xcenter+Lx+push << "," << ycenter+Ly+push << ") {\\tiny$(" << xcenter+Lx << "," << ycenter+Ly << ")$};" << std::endl;
myfile << "\\end{tikzpicture}" << std::endl;
myfile << "\\end{document}" << std::endl;
myfile.close();
}
//////////////////////////////////////////////////////////////
double find_distance(int j1, int k1, int j2, int k2) {
pts2D r1 = tree[j1][k1].center;
pts2D r2 = tree[j2][k2].center;
return sqrt((r1.x-r2.x)*(r1.x-r2.x) + (r1.y-r2.y)*(r1.y-r2.y));
}
void assign_Child_Interaction(int c, int j, int k) {
// j: level
// k: parent box
int parentboxNumber = tree[j][k].boxNumber;
int boxA = 4*parentboxNumber+c;//child box number; its index=?
std::vector<int>::iterator indx = std::find(indexTree[j+1].begin(), indexTree[j+1].end(), boxA);
int boxA_index = indx-indexTree[j+1].begin();
for (int n=0; n<tree[j][k].neighborNumbers.size(); ++n) {//parents neighbors; so u need its index to access it which is k
//children of neighbors of parent which are not neighbors to child=IL
int pnj = tree[j][k].neighborNumbers[n].x;//level
int pni = tree[j][k].neighborNumbers[n].y;//index
int pnn = tree[pnj][pni].boxNumber;//boxNumber
if (j+1 >= level_LFR || (j+1 < level_LFR && tree[j+1][boxA_index].isLeaf)) { //LFR
if (tree[pnj][pni].isLeaf) {
if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj][pni].center.x) >= 3.0*boxRadius[j+1] + boxRadius[pnj]-machinePrecision
|| fabs(tree[j+1][boxA_index].center.y - tree[pnj][pni].center.y) >= 3.0*boxRadius[j+1] + boxRadius[pnj]-machinePrecision) {
orderedPair op;
op.x = pnj;
op.y = pni;
tree[j+1][boxA_index].InteractionList.push_back(op);
}
else {
orderedPair op;
op.x = pnj;
op.y = pni;
tree[j+1][boxA_index].neighborNumbers.push_back(op);
}
}
else {
for (int nc=0; nc<4; ++nc) { //children of parents neighbors
int boxB = 4*pnn+nc;//its index=?
std::vector<int>::iterator indx = std::find(indexTree[j+1].begin(), indexTree[j+1].end(), boxB);
int boxB_index = indx-indexTree[j+1].begin();
if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj+1][boxB_index].center.x) >= 3.0*boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision
|| fabs(tree[j+1][boxA_index].center.y - tree[pnj+1][boxB_index].center.y) >= 3.0*boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision) {
orderedPair op;
op.x = pnj+1;
op.y = boxB_index;
tree[j+1][boxA_index].InteractionList.push_back(op);
}
else {
if (!tree[j+1][boxA_index].isLeaf) {
orderedPair op;
op.x = pnj+1;
op.y = boxB_index;
tree[j+1][boxA_index].neighborNumbers.push_back(op);
}
else {
int pj = pnj+1;
int pk = boxB_index;
std::vector<orderedPair> futureGeneration;
getFutureGeneration_LFR(j+1, boxA_index, pj, pk, futureGeneration);
for (size_t d = 0; d < futureGeneration.size(); d++) {
tree[j+1][boxA_index].neighborNumbers.push_back(futureGeneration[d]);
}
}
}
}
}
}
else { //HFR
if (tree[pnj][pni].isLeaf) {
// if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj][pni].center.x) >= 3.0*boxRadius[j+1] + boxRadius[pnj]-machinePrecision
// || fabs(tree[j+1][boxA_index].center.y - tree[pnj][pni].center.y) >= 3.0*boxRadius[j+1] + boxRadius[pnj]-machinePrecision) {
if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj][pni].center.x) >= kappa*boxRadius[j+1]*boxRadius[j+1] + boxRadius[j+1] + boxRadius[pnj]-machinePrecision
|| fabs(tree[j+1][boxA_index].center.y - tree[pnj][pni].center.y) >= kappa*boxRadius[j+1]*boxRadius[j+1] + boxRadius[j+1] + boxRadius[pnj]-machinePrecision) {
double arg = atan2(tree[j+1][boxA_index].center.y-tree[pnj][pni].center.y, tree[j+1][boxA_index].center.x-tree[pnj][pni].center.x);
arg = fmod(arg+2*PI+PI, 2*PI);
int coneNum = int(arg/ConeAperture[j+1]);
orderedPair op;
op.x = pnj;
op.y = pni;
tree[j+1][boxA_index].ConeTree[coneNum].InteractionList.push_back(op);
}
else {
orderedPair op;
op.x = pnj;
op.y = pni;
tree[j+1][boxA_index].neighborNumbers.push_back(op);
}
}
else {
for (int nc=0; nc<4; ++nc) { //children of parents neighbors
int boxB = 4*pnn+nc;//its index=?
std::vector<int>::iterator indx = std::find(indexTree[j+1].begin(), indexTree[j+1].end(), boxB);
int boxB_index = indx-indexTree[j+1].begin();
// if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj+1][boxB_index].center.x) >= 3.0*boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision
// || fabs(tree[j+1][boxA_index].center.y - tree[pnj+1][boxB_index].center.y) >= 3.0*boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision) {
if ( fabs(tree[j+1][boxA_index].center.x - tree[pnj+1][boxB_index].center.x) >= kappa*boxRadius[j+1]*boxRadius[j+1] + boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision
|| fabs(tree[j+1][boxA_index].center.y - tree[pnj+1][boxB_index].center.y) >= kappa*boxRadius[j+1]*boxRadius[j+1] + boxRadius[pnj+1]-machinePrecision) {
double arg = atan2(tree[j+1][boxA_index].center.y-tree[pnj+1][boxB_index].center.y, tree[j+1][boxA_index].center.x-tree[pnj+1][boxB_index].center.x);
arg = fmod(arg+2*PI+PI, 2*PI);
int coneNum = int(arg/ConeAperture[j+1]);
orderedPair op;
op.x = pnj+1;
op.y = boxB_index;
tree[j+1][boxA_index].ConeTree[coneNum].InteractionList.push_back(op);
}
else {
orderedPair op;
op.x = pnj+1;
op.y = boxB_index;
tree[j+1][boxA_index].neighborNumbers.push_back(op);
}
}
}
}
}
}
// Assigns the interactions for the children of a box
void assign_Box_Interactions(int j, int k) {
if (!tree[j][k].isLeaf) {
// #pragma omp parallel for
for (int c=0; c<4; ++c) {
assign_Child_Interaction(c,j,k);
}
}
}
// Assigns the interactions for the children all boxes at a given level
void assign_Level_Interactions(int j) {
// #pragma omp parallel for
for (int k=0; k<tree[j].size(); ++k) {
assign_Box_Interactions(j,k);//k is index number of box in tree[j] vector
}
}
// Assigns the interactions for the children all boxes in the tree
void assign_Tree_Interactions() {
//j=0, no neighbors, no IL
//j=1, no IL, neighbors yes
//neighbor includes self
int j = 1;
for (int c=0; c<4; ++c) {
for (int n=0; n<4; ++n) {
orderedPair op;
op.x = 1;
op.y = n;
tree[j][c].neighborNumbers.push_back(op);
}
}
for (j=1; j<=nLevels-1; ++j) {
assign_Level_Interactions(j);
}
}
void createCones() {
N = leafNodes.size()*rank;
// cout << "level_LFR: " << level_LFR << endl;
// cout << "Number of particles: " << N << endl;
nCones.push_back(nCones_LFR*pow(2.0, level_LFR-1));
ConeAperture.push_back(2*PI/nCones[0]);
for (size_t j = 1; j <= level_LFR-1; j++) {
nCones.push_back(nCones[j-1]/2.0);
ConeAperture.push_back(ConeAperture[j-1]*2.0);
}
for (size_t j = 0; j <= level_LFR-1; j++) {
for (int k=0; k<tree[j].size(); ++k) {
if (!tree[j][k].isLeaf) {
tree[j][k].ConeTree.clear();////
for (int c=0; c<nCones[j]; ++c) {
FMM2DCone cone;
cone.angle = ConeAperture[j]/2.0 + c*ConeAperture[j];
tree[j][k].ConeTree.push_back(cone);
}
}
}
}
}
void assign_NonLeaf_Charges() {
for (int j=nLevels-1; j>1; --j) {
int J = j+1;
// #pragma omp parallel for
for (int k=0; k<tree[j].size(); ++k) {
if (!tree[j][k].isLeaf) {
int b = tree[j][k].boxNumber;
int KboxNumber;
int K[4];
std::vector<int>::iterator indx;
KboxNumber = 4*b+0;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[0] = indx-indexTree[J].begin();
KboxNumber = 4*b+1;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[1] = indx-indexTree[J].begin();
KboxNumber = 4*b+2;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[2] = indx-indexTree[J].begin();
KboxNumber = 4*b+3;
indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
K[3] = indx-indexTree[J].begin();
int NumCharges = tree[J][K[0]].chargeLocations.size() + tree[J][K[1]].chargeLocations.size() +tree[J][K[2]].chargeLocations.size() + tree[J][K[3]].chargeLocations.size();
tree[j][k].multipoles = Vec::Zero(NumCharges);
int start = 0;
for (int c=0; c<4; ++c) {
//Block containing n elements, starting at position i: vector.segment(i,n)
int NumElem = tree[J][K[c]].chargeLocations.size();
tree[j][k].multipoles.segment(start, NumElem) = tree[J][K[c]].multipoles;
start += NumElem;
}
}
}
}
}
void getParticlesFromChildrenLFR_outgoing_col(int j, int k, std::vector<int>& searchNodes) {
if (tree[j][k].isLeaf) {
searchNodes.insert(searchNodes.end(), tree[j][k].chargeLocations.begin(), tree[j][k].chargeLocations.end());
}
else {
int J = j+1;
int b = tree[j][k].boxNumber;
for (int c = 0; c < 4; c++) {
int KboxNumber = 4*b+c;
std::vector<int>::iterator indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
int K = indx-indexTree[J].begin();
if (J >= level_LFR || (J<level_LFR && tree[J][K].isLeaf)) { //LFR
// if (tree[J][K].incoming_checkPoints.size() == 0) {
// cout << "problem LFR_outgoing_row: " << j << ", " << k << ", " << J << ", " << K << endl;
// }
searchNodes.insert(searchNodes.end(), tree[J][K].outgoing_chargePoints.begin(), tree[J][K].outgoing_chargePoints.end());
}
else { //HFR
for (size_t cone = 0; cone < nCones[J]; cone++) {
searchNodes.insert(searchNodes.end(), tree[J][K].ConeTree[cone].outgoing_chargePoints.begin(), tree[J][K].ConeTree[cone].outgoing_chargePoints.end());
}
}
}
}
}
void getParticlesFromChildrenLFR_outgoing_row(int j, int k, std::vector<int>& searchNodes, int j1, int k1) {
if (tree[j][k].isLeaf) {
searchNodes.insert(searchNodes.end(), tree[j][k].chargeLocations.begin(), tree[j][k].chargeLocations.end());
}
else {
int J = j+1;
int b = tree[j][k].boxNumber;
for (int c = 0; c < 4; c++) {
int KboxNumber = 4*b+c;
std::vector<int>::iterator indx = std::find(indexTree[J].begin(), indexTree[J].end(), KboxNumber);
int K = indx-indexTree[J].begin();
if (J >= level_LFR || (J<level_LFR && tree[J][K].isLeaf)) { //LFR
searchNodes.insert(searchNodes.end(), tree[J][K].incoming_checkPoints.begin(), tree[J][K].incoming_checkPoints.end());
}
else { //HFR
for (size_t cone = 0; cone < nCones[J]; cone++) {
searchNodes.insert(searchNodes.end(), tree[J][K].ConeTree[cone].incoming_checkPoints.begin(), tree[J][K].ConeTree[cone].incoming_checkPoints.end());
}
}
}
}
}
void getNodes_LFR() {
for (int j=nLevels; j>=2; j--) {
for (int k=0; k<tree[j].size(); ++k) {
if (tree[j][k].incoming_chargePoints.size() > 0)
tree[j][k].incoming_chargePoints.clear();
if (tree[j][k].incoming_checkPoints.size() > 0)
tree[j][k].incoming_checkPoints.clear();
if (tree[j][k].outgoing_chargePoints.size() > 0)
tree[j][k].outgoing_chargePoints.clear();
if (tree[j][k].outgoing_checkPoints.size() > 0)
tree[j][k].outgoing_checkPoints.clear();
if (tree[j][k].user_checkPoints.size() > 0)
tree[j][k].user_checkPoints.clear();
}
}
// std::cout << "level_LFR: " << level_LFR << std::endl;
for (int j=nLevels; j>=level_LFR; j--) {
getNodes_LFR_outgoing_level(j);
getNodes_LFR_incoming_level(j);
}
}
void getNodes_LFR_outgoing_box(int j, int k, int &n_rows, int &n_cols, int &ComputedRank) {
int ILcheck = 0;
if (tree[j][k].active == true) {
std::vector<int> boxA_Nodes;
getParticlesFromChildrenLFR_outgoing_col(j, k, boxA_Nodes);
//sort( boxA_Nodes.begin(), boxA_Nodes.end() );
//boxA_Nodes.erase( unique( boxA_Nodes.begin(), boxA_Nodes.end() ), boxA_Nodes.end() );
std::vector<int> IL_Nodes;//indices
for (int l=0; l<tree[j][k].InteractionList.size(); ++l) {
int jIL = tree[j][k].InteractionList[l].x;