-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMPI_Refinement_without_Solver.cpp
1546 lines (1502 loc) · 42.2 KB
/
MPI_Refinement_without_Solver.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 <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <fstream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <mpi.h>
using namespace std;
class Vertex {
private:
/*Variables*/
double x; // x position
double y; // y position
public:
/*Constructors*/
Vertex() {
x = 0.;
y = 0.;
}
Vertex(double x1, double y1) {
x = x1;
y = y1;
}
/*Members*/
double getX() const{
return x;
}
double getY() const{
return y;
}
void setX(double xx) {
x = xx;
}
void setY(double yy) {
y = yy;
}
void setXY(double xx, double yy) {
x = xx;
y = yy;
}
double getLength(Vertex v2) {
double length;
length = (x - v2.getX())*(x - v2.getX()) + (y - v2.getY())*(y - v2.getY());
length = sqrt(length);
return length;
}
bool equals(Vertex v3) {
double met = pow(10,-8); //Metric
double xr = fabs(x-v3.getX());
double yr = fabs(y-v3.getY());
if ((xr<met) && (yr<met)) {
return true;
}
else {
return false;
}
}
bool operator<(const Vertex& v2) const{
double x2,y2;
x2 = v2.getX();
y2 = v2.getY();
if(x<x2){
return true;
}
else if(x==x2){
if(y<y2){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
};
Vertex r(Vertex v){ //Used to round off to 8 digits after decimal
//Causing an issue with keys
double x = v.getX();
double y = v.getY();
double xp = floor(x*pow(10,8))/pow(10,8);
double yp = floor(y*pow(10,8))/pow(10,8);
Vertex ver(xp,yp);
return ver;
}
/*Edge type can be used to implement different boundary conditions*/
class Edge{
private:
/*Variables*/
Vertex v1;
Vertex v2;
int type; // type used to identify where the edge is in the domain
// Assigning 0 to the edges which are in the interior
int count; //Used to delete obsolete edges during bisection of triangles
//Edge of type 0 becomes obsolete when count becomes 2
//Edge of type Non Zero becomes obsolte when count becomes 1
vector<Vertex> oppVer; //Opposite vertex to track the triangles of this edge
bool mpiEdge; // To detect if it is an MPI Interface Edge or not
//Only edges of type 0 can become MPI Interface Edges
int mpiProcessor; //To know the processor of the mpi edge
bool mpiEdgeBisected; // To track if the mpiEdge is bisected or not
int track; //This when set to 1 means it has communicated
// with the other processor about its bisection
public:
/*Constructors*/
Edge(Vertex vv1,Vertex vv2){
v1=vv1;
v2=vv2;
type =0;
count = 0;
mpiEdge = false;
mpiProcessor = 0;
mpiEdgeBisected = false;
track =0;
}
Edge(Vertex vv1,Vertex vv2, int t){
v1=vv1;
v2=vv2;
type=t;
count = 0;
mpiEdge = false;
mpiProcessor = 0;
mpiEdgeBisected = false;
track =0;
}
/*Members*/
int getType(){
return type;
}
void setType(int typ){
type=typ;
}
double getLength(){
double length=v1.getLength(v2);
return length;
}
vector<Vertex> getVertices(){
vector<Vertex> ver;
ver.push_back(v1);
ver.push_back(v2);
return ver;
}
Vertex getVertex1(){
return v1;
}
Vertex getVertex2(){
return v2;
}
Vertex getMidpoint(){
Vertex midpoint;
midpoint.setXY(0.5*(v1.getX()+v2.getX()),0.5*(v1.getY()+v2.getY()));
return midpoint;
}
void addOppVertices(Vertex ver){
if(oppVer.size()==0){
oppVer.push_back(ver);
}
else{
int l=1;
for(int d=0;d<oppVer.size();d++){
if(oppVer[d].equals(ver)){
l=0;
}
}
if(l==1){
oppVer.push_back(ver);
}
}
}
vector<Vertex> getOppVertices(){
return oppVer;
}
void incrementCount(){
count=count+1;
}
int getCount(){
return count;
}
void clearOppVertex(Vertex ver){
for(int i=0;i<oppVer.size();i++){
if(ver.equals(oppVer[i])){
oppVer.erase(oppVer.begin()+i);
}
}
}
bool equals(Edge e1){
Vertex ve1 = e1.getMidpoint();
Vertex vw = this->getMidpoint();
return ve1.equals(vw);
}
void setmpiEdge(bool des){
mpiEdge = des;
}
bool getmpiEdge(){
return mpiEdge;
}
void setmpiProcessor(int i1){
mpiProcessor =i1;
}
int getmpiProcessor(){
return mpiProcessor;
}
void bisectmpiEdge(bool dess){
mpiEdgeBisected = dess;
}
bool getBisectionStateOfmpiEdge(){
return mpiEdgeBisected;
}
void setTrack (int i1){
track = i1;
}
int getTrack(){
return track;
}
};
class Triangle{
private:
Vertex v1;
Vertex v2;
Vertex v3;
int newVertices; // Used during refinement
vector<Vertex> newNodes; // Used during refinement
bool refine; //Used during refinement
int count; //Used to delete triangles
//Obsolete triangles have count==1
public:
/*Constructor*/
Triangle(Vertex vv1,Vertex vv2,Vertex vv3){
v1=vv1;
v2=vv2;
v3=vv3;
newVertices =0;
refine = false;
count =0;
}
/*Members*/
vector<Vertex> getVertices(){
vector<Vertex> ver;
ver.push_back(v3);
ver.push_back(v1);
ver.push_back(v2);
return ver;
}
Vertex getCentroid(){
double x1,y1,x2,y2,x3,y3,xc,yc;
x1=v1.getX();
y1=v1.getY();
x2=v2.getX();
y2=v2.getY();
x3=v3.getX();
y3=v3.getY();
xc=(x1+x2+x3)/3.0;
yc=(y1+y2+y3)/3.0;
Vertex ver(xc,yc);
return ver;
}
vector<Edge> getEdges(){
Edge e12(v1,v2);
Edge e23(v2,v3);
Edge e31(v3,v1);
vector<Edge> ed;
ed.push_back(e12);
ed.push_back(e23);
ed.push_back(e31);
return ed;
}
double getArea(){
double x1,y1,x2,y2,x3,y3,area;
x1=v1.getX();
y1=v1.getY();
x2=v2.getX();
y2=v2.getY();
x3=v3.getX();
y3=v3.getY();
area=0.5*(x2*y3-x3*y2-x1*y3+x1*y2+x3*y1-x2*y1);
area =fabs(area);
return area;
}
Edge getLongestEdge(){
Edge e12(v1,v2);
Edge e23(v2,v3);
Edge e31(v3,v1);
Edge le = e12;
if(le.getLength() < e23.getLength()){
le=e23;
}
if(le.getLength() < e31.getLength()){
le=e31;
}
return le;
}
void addNewNode(Vertex v){
if(newNodes.size()==0){
newNodes.push_back(v);
}
else{
int l=1;
for(int j=0;j<newNodes.size();j++){
if(newNodes[j].equals(v)){
l=0;
}
}
if(l==1){
newNodes.push_back(v);
}
}
newVertices = newNodes.size();
}
int getNewVertices(){
return newVertices;
}
void setRefine(bool dec){
refine=dec;
}
bool getRefine(){
return refine;
}
Edge getEdgeOfPoint(Vertex ver){ //Used to find the edge on which a point (midpoint) lies
Vertex v12,v23,v31;
v12.setXY(0.5*(v1.getX()+v2.getX()),0.5*(v1.getY()+v2.getY()));
v23.setXY(0.5*(v2.getX()+v3.getX()),0.5*(v2.getY()+v3.getY()));
v31.setXY(0.5*(v3.getX()+v1.getX()),0.5*(v3.getY()+v1.getY()));
if(v12.equals(ver)){
Edge e12(v1,v2);
return e12;
}
if(v23.equals(ver)){
Edge e23(v2,v3);
return e23;
}
if(v31.equals(ver)){
Edge e31(v3,v1);
return e31;
}
}
Vertex getOppVertexofPoint(Vertex ver){ //Obtaining vertex of triangle which is opposite to an edge using the edge's midpoint
Vertex v12,v23,v31,v;
v12.setXY(0.5*(v1.getX()+v2.getX()),0.5*(v1.getY()+v2.getY()));
v23.setXY(0.5*(v2.getX()+v3.getX()),0.5*(v2.getY()+v3.getY()));
v31.setXY(0.5*(v3.getX()+v1.getX()),0.5*(v3.getY()+v1.getY()));
if(v12.equals(ver)){
v=v3;
}
if(v23.equals(ver)){
v=v1;
}
if(v31.equals(ver)){
v=v2;
}
return v;
}
vector<Vertex> getOrderedNewNodes(){ //Ordered in a way that the first node is the longest edge midpoint
vector<Vertex> ver;
if(newVertices>0){
Vertex longPoint=this->getLongestEdge().getMidpoint();
ver.push_back(longPoint);
for(int n=0;n<newNodes.size();n++){
if(!(longPoint.equals(newNodes[n]))){
ver.push_back(newNodes[n]);
}
}
}
return ver;
}
vector<Vertex> getMidpointsOfEdges(){ //Used to obtain midpoints of edges of a triangle
vector<Vertex> ver;
Vertex v12,v23,v31;
v12.setXY(0.5*(v1.getX()+v2.getX()),0.5*(v1.getY()+v2.getY()));
v23.setXY(0.5*(v2.getX()+v3.getX()),0.5*(v2.getY()+v3.getY()));
v31.setXY(0.5*(v3.getX()+v1.getX()),0.5*(v3.getY()+v1.getY()));
ver.push_back(v12);
ver.push_back(v23);
ver.push_back(v31);
return ver;
}
Vertex getThirdVertex(Vertex vv1,Vertex vv2){
Vertex ver;
ver.setXY(0.5*(vv1.getX()+vv2.getX()),0.5*(vv1.getY()+vv2.getY()));
Vertex ver1=this->getOppVertexofPoint(ver);
return ver1;
}
bool equals(Triangle t){
Vertex ver=t.getCentroid();
Vertex ver1=this->getCentroid();
return ver1.equals(ver);
}
void incrementCount(){
count =count+1;
}
int getCount(){
return count;
}
};
int main(int argc, char** argv){
MPI_Init(&argc, &argv);
int num_process; // Number of processes
MPI_Comm_size(MPI_COMM_WORLD, &num_process);
int process_num; // Process number
MPI_Comm_rank(MPI_COMM_WORLD, &process_num);
MPI_Status status;
vector<Vertex> vertices;
vector<Edge> edg,edg2;
vector<Triangle> trgl,trgl2;
map<Vertex,int> e2map; //For edges of edg2
ifstream inFile;
inFile.open("Mesh.msh");
if (!inFile.is_open()) {
cout << "File didn't open\n";
exit(EXIT_FAILURE);
}
string c;
while (inFile >> c) {
if (c.compare("$Nodes") == 0) {
int nn, i = 0, j; // Number of nodes-nn, j is dummy
inFile >> nn;
for (i = 0;i < nn;i++) {
inFile >> j;
double x, y, z;
inFile >> x >> y >> z;
Vertex v(x, y);
vertices.push_back(v);
}
}
if (c.compare("$Elements") == 0) {
int nel, i, j, k, l, q, m, n, p; // Number of elements - nel
inFile >> nel;
for (i = 0;i < nel;i++) {
inFile >> j >> k; //k - 1 is edge, 2 is triangle
if (k == 1) {
inFile >> q;
for (int w = 0;w < q;w++) {
int q1; //Dummy variable
if (w == 0) {
inFile >> l; //Type number of the edge
}
else {
inFile >> q1;
}
}
inFile >> m >> n;
Edge e(vertices[m - 1], vertices[n - 1], l);
edg2.push_back(e);
e2map[r(e.getMidpoint())] = edg2.size()-1;
}
else {
inFile >> q;
for (int w = 0;w < q;w++) {
int q1;
inFile >> q1;
}
inFile >> m >> n >> p;
Triangle tre(vertices[m - 1], vertices[n - 1], vertices[p - 1]);
trgl2.push_back(tre);
}
}
}
}
inFile.close();
vertices.clear();
//Test case setting two triangles to refine
for(int i=0;i<trgl2.size();i++){
trgl2[i].setRefine(true);
}
//trgl2[0].setRefine(true);
//trgl2[1].setRefine(true);
//trgl2[2].setRefine(true);
//trgl2[3].setRefine(true);
int triK, triL; //Used for distribution of triangle vector
triK = trgl2.size()/num_process;
triL = trgl2.size()%num_process;
//Adding triangles of this processor to trgl
if(process_num<triL){
for(int j=process_num*(triK+1);j<=process_num*(triK+1)+triK;j++){
Triangle tq = trgl2[j];
trgl.push_back(tq);
}
}
else{
for(int j=process_num*triK+triL;j<=process_num*triK+triL+triK-1;j++){
Triangle tq = trgl2[j];
trgl.push_back(tq);
}
}
trgl2.clear();
//Addding edges of this processor to edg
//Using maps for edges and triangles
map<Vertex,int> emap; //For edges
map<Vertex,int> tmap; //For triangles
for(int i = 0;i < trgl.size();i++) {
Triangle t = trgl[i];
tmap[r(t.getCentroid())] = i;
vector<Edge> ed = t.getEdges();
vector<Vertex> verr = t.getVertices();
for (int j = 0;j < 3;j++) {
Edge edd = ed[j];
Vertex veer = verr[j];
int l = 1;
/*for(int k = 0;k<edg.size();k++){
if(edd.equals(edg[k])){
l=0;
edg[k].addOppVertices(veer);
break;
}
}
for (int k = 0;k < edg2.size();k++) {
if (edd.equals(edg2[k])) {
l = 0;
edg2[k].addOppVertices(veer);
edg.push_back(edg2[k]);
emap[edg2[k].getMidpoint()] = edg.size()-1;
break;
}
}*/
if(emap.count(r(edd.getMidpoint()))==1){
l = 0;
int k = emap[r(edd.getMidpoint())];
edg[k].addOppVertices(veer);
}
if(e2map.count(r(edd.getMidpoint()))==1){
l = 0;
int k = e2map[r(edd.getMidpoint())];
edg2[k].addOppVertices(veer);
edg.push_back(edg2[k]);
emap[r(edg2[k].getMidpoint())] = edg.size()-1;
}
if (l == 1) {
vector<Vertex> ver = edd.getVertices();
Edge eg(ver[0], ver[1], 0); // Assigning type 0
eg.addOppVertices(veer);
edg.push_back(eg);
emap[r(eg.getMidpoint())] = edg.size()-1;
}
}
}
edg2.clear();
e2map.clear();
//Adding of opposite vertices to edges is done
/*//Updating emap
for(int i=0;i<edg.size();i++){
Vertex va = edg[i].getMidpoint();
emap[va] = i;
}
//Updating tmap
for(int i=0;i<trgl.size();i++){
Vertex va = trgl[i].getCentroid();
tmap[va] = i;
}*/
//Defining which edges of edg are mpiEdge and processor number in mpiProcessor
for(int i=0;i<edg.size();i++){
if(edg[i].getType()==0){
vector<Vertex> verces = edg[i].getOppVertices();
if(verces.size()==1){
edg[i].setmpiEdge(true);
edg[i].setmpiProcessor(process_num);
}
if(verces.size()==0){
cout<<"Wrong in mpiEdge of processor "<<process_num<<endl;
}
}
}
// mpiEdges have been detected
//Refinement of triangles in trgl
for (int i = 0;i < trgl.size();i++) {
if (trgl[i].getRefine()) {
vector<Vertex> ver = trgl[i].getMidpointsOfEdges();
for (int j = 0;j < 3;j++) {
trgl[i].addNewNode(ver[j]); //Adding the midpoints to triangles
//Loop to collect all triangles getting affected by this node
bool decision = true;
Triangle t = trgl[i];//Current triangle
while (decision) {
//Finding the edge from edg which contains this point
int k; //To track edge number
k = emap[r(ver[j])];
//Found the edge
//Cheking if the edge is an mpiEdge
//To set bisection to true
if(edg[k].getmpiEdge()){
edg[k].bisectmpiEdge(true);
}
//Getting opposite vertices from this edge to construct neighbouring triangle
vector<Vertex> oppVer = edg[k].getOppVertices();
if (oppVer.size() > 2) {
cout << "Opposite Vertices count is more than 2" << endl;
}
vector<Triangle> neigTri; //To store neighbouring triangle
if (oppVer.size() == 2) {
Triangle Tri1(edg[k].getVertex1(), edg[k].getVertex2(), oppVer[0]);
Triangle Tri2(edg[k].getVertex1(), edg[k].getVertex2(), oppVer[1]);
if (Tri1.equals(t)) {
neigTri.push_back(Tri2);
}
if (Tri2.equals(t)) {
neigTri.push_back(Tri1);
}
}
if (neigTri.size() == 0) {
decision = false;
}
//To find this triangle in trgl
int l; //To track triangle number
if (neigTri.size() == 1) {
Vertex va = neigTri[0].getCentroid();
l = tmap[r(va)];
trgl[l].addNewNode(ver[j]);
decision = !(ver[j].equals(trgl[l].getLongestEdge().getMidpoint()));
if (decision) {
//Perform bisection of the longest edge of this neighbouring triangle
trgl[l].addNewNode(trgl[l].getLongestEdge().getMidpoint());
//Update vertex ver[j] to the new point which has been created
ver[j] = trgl[l].getLongestEdge().getMidpoint();
//Update the current triangle to trgl[l]
t = trgl[l];
}
}
}
}
}
}
//Refinement of the triangles in trgl is done
if(process_num==0){
cout<<"New nodes have been added\n";
}
//Refinement of mpiEdges through exchange of information across processors
vector<Edge> mpiedg;
for(int i=0;i<edg.size();i++){
if(edg[i].getmpiEdge()){
mpiedg.push_back(edg[i]);
}
}
vector<Edge> mpiedgtotal; //Collection of all mpi edges
//This part has to be in a loop until
//there is no mpiedge which is bisected and has track = 0
bool deci =true;
while(deci){
//Adding mpiedges of processor 0 to mpiedgtotal
if(process_num==0){
for(int i=0;i<mpiedg.size();i++){
mpiedgtotal.push_back(mpiedg[i]);
}
}
if(process_num != 0 ){
int si = mpiedg.size();
//Sending size of mpiedg
MPI_Send(&si,1,MPI_INT,0,0,MPI_COMM_WORLD);
//Sending mpiedg to process 0
double* X;
X = (double*)malloc(si*7*sizeof(double));
for(int i=0;i<si;i++){
//Individually sending details of every edge
Vertex ve1 = mpiedg[i].getVertex1();
Vertex ve2 = mpiedg[i].getVertex2();
X[7*i+0] = ve1.getX();
X[7*i+1] = ve2.getX();
X[7*i+2] = ve1.getY();
X[7*i+3] = ve2.getY();
X[7*i+4] = (double)mpiedg[i].getmpiProcessor();
X[7*i+5] = (double)mpiedg[i].getTrack();
X[7*i+6]=0.;//For bisection state
if(mpiedg[i].getBisectionStateOfmpiEdge()){
X[7*i+6]=1.;
}
}
MPI_Send(&X[0],7*si,MPI_DOUBLE,0,1,MPI_COMM_WORLD);
free(X);
//Clearing mpiedg so that updated mpiedg can be received
mpiedg.clear();
//Receiving updated mpiedg vector
double* X1;
X1 = (double*)malloc(si*7*sizeof(double));
MPI_Recv(&X1[0],7*si,MPI_DOUBLE,0,2,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
for(int i=0;i<si;i++){
//Don't forget setting mpiEdge to true
Vertex ve1(X1[7*i+0],X1[7*i+2]),ve2(X1[7*i+1],X1[7*i+3]);
Edge ed(ve1,ve2,0);
ed.setmpiEdge(true);
ed.setmpiProcessor((int)X1[7*i+4]);
ed.setTrack((int)X1[7*i+5]);
if(X1[7*i+6]==1.){
ed.bisectmpiEdge(true);
}
mpiedg.push_back(ed);
}
free(X1);
}
else{
int* trk; //To track the no of mpiedgs received
//from each process
trk = (int*)malloc(num_process*sizeof(int));
trk[0] = mpiedgtotal.size()-1;
//Collecting mpiedgs from all processes
for(int i=1;i<num_process;i++){
int j;
MPI_Recv(&j,1,MPI_INT,i,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
double* X;
X = (double*)malloc(j*7*sizeof(double));
MPI_Recv(&X[0],7*j,MPI_DOUBLE,i,1,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
for(int k=0;k<j;k++){
//Don't forget setting mpiEdge to true
Vertex ve1(X[k*7+0],X[k*7+2]),ve2(X[k*7+1],X[k*7+3]);
Edge ed(ve1,ve2);
ed.setmpiEdge(true);
ed.setmpiProcessor((int)X[7*k+4]);
ed.setTrack((int)X[7*k+5]);
if(X[7*k+6]==1.){
ed.bisectmpiEdge(true);
}
mpiedgtotal.push_back(ed);
}
free(X);
trk[i] = mpiedgtotal.size()-1;
}
//Communication between similar mpiedgs
for(int i=0;i<mpiedgtotal.size();i++){
for(int j=i+1;j<mpiedgtotal.size();j++){
if(mpiedgtotal[i].equals(mpiedgtotal[j])){
bool d1 =mpiedgtotal[i].getBisectionStateOfmpiEdge();
bool d2 =mpiedgtotal[j].getBisectionStateOfmpiEdge();
if(d1 || d2){
mpiedgtotal[i].bisectmpiEdge(true);
mpiedgtotal[i].setTrack(1);
mpiedgtotal[j].bisectmpiEdge(true);
mpiedgtotal[j].setTrack(1);
}
}
}
}
//Redistribution of mpiedg from process 0 to respective processor
mpiedg.clear(); //This only clears mpiedg info of process 0
for(int i=0;i<=trk[0];i++){
mpiedg.push_back(mpiedgtotal[i]);
/*if(mpiedgtotal[i].getmpiProcessor()!=0){
cout<<"Error at line 759\n";
}*/
}
for(int i=1;i<num_process;i++){
int j,j1;
j1 = trk[i]-trk[i-1];
double* X;
X = (double*)malloc(j1*7*sizeof(double));
int k=0;
for(j=trk[i-1]+1;j<=trk[i];j++){
Vertex ve1 = mpiedgtotal[j].getVertex1();
Vertex ve2 = mpiedgtotal[j].getVertex2();
X[7*k+0] = ve1.getX();
X[7*k+1] = ve2.getX();
X[7*k+2] = ve1.getY();
X[7*k+3] = ve2.getY();
X[7*k+4] = (double)mpiedgtotal[j].getmpiProcessor();
X[7*k+5] = (double)mpiedgtotal[j].getTrack();
X[7*k+6]=0.;//For bisection state
if(mpiedgtotal[j].getBisectionStateOfmpiEdge()){
X[7*k+6]=1.;
}
/*if(mpiedgtotal[j].getmpiProcessor()!=i){
cout<<"Error in line 782\n";
}*/
k=k+1;
}
MPI_Send(&X[0],j1*7,MPI_DOUBLE,i,2,MPI_COMM_WORLD);
free(X);
}
/*for(int i=0;i<mpiedgtotal.size();i++){
int j;
j = mpiedgtotal[i].getmpiProcessor();
if(j==0){
mpiedg.push_back(mpiedgtotal[i]);
}
else{
//Individually sending details of every edge
Vertex ve1 = mpiedgtotal[i].getVertex1();
Vertex ve2 = mpiedgtotal[i].getVertex2();
double X[4];
X[0] = ve1.getX();
X[1] = ve2.getX();
X[2] = ve1.getY();
X[3] = ve2.getY();
int Y[3];
Y[0] = mpiedgtotal[i].getmpiProcessor();
Y[1] = mpiedgtotal[i].getTrack();
Y[2]=0;//For bisection state
if(mpiedgtotal[i].getBisectionStateOfmpiEdge()){
Y[2]=1;
}
MPI_Send(&X[0],4,MPI_DOUBLE,j,2,MPI_COMM_WORLD);
MPI_Send(&Y[0],3,MPI_INT,j,2,MPI_COMM_WORLD);
}
}*/
//Clearing mpiedgtotal
mpiedgtotal.clear();
free(trk);
}
//Relaying information from mpiedg to edg
//Changing mpiEdgeBisected and track
//Also creating map for mpiedgs
map<Vertex,int> mpmap;
for(int i=0;i<mpiedg.size();i++){
Vertex val = mpiedg[i].getMidpoint();
int j;
j = emap[r(val)];
mpmap[r(val)] = j;
if(mpiedg[i].getBisectionStateOfmpiEdge()){
edg[j].bisectmpiEdge(true);
edg[j].setTrack(1);
}
}
//Every processor going through its own mpiedg vector
//to start refinement based on bisected edges
for(int i=0;i<mpiedg.size();i++){
if(mpiedg[i].getBisectionStateOfmpiEdge()){
Vertex v1 = mpiedg[i].getMidpoint();
int ej;
ej = emap[r(v1)]; //Obtained corresponding edge
vector<Vertex> ever = edg[ej].getOppVertices();
Vertex ever1 = edg[ej].getVertex1();
Vertex ever2 = edg[ej].getVertex2();
Triangle tj(ever1,ever2,ever[0]);
Vertex vva = tj.getCentroid();
int jj;
jj = tmap[r(vva)];
trgl[jj].addNewNode(v1);
if(!(mpiedg[i].equals(trgl[jj].getLongestEdge()))){
Vertex v2 = trgl[jj].getLongestEdge().getMidpoint();
trgl[jj].addNewNode(v2);
//Process is similar to normal refinement
bool des = true;
Triangle tt = trgl[jj];
while(des){
int k;
k = emap[r(v2)];
if(edg[k].getmpiEdge()){
edg[k].bisectmpiEdge(true);
}
vector<Vertex> oppVer = edg[k].getOppVertices();
if(oppVer.size()>2){
cout<<"Opposite Vertices count is more than 2 in "<<process_num<<endl;
}
vector<Triangle> neigTri;
if(oppVer.size()==2){
Triangle Tri1(edg[k].getVertex1(), edg[k].getVertex2(), oppVer[0]);
Triangle Tri2(edg[k].getVertex1(), edg[k].getVertex2(), oppVer[1]);
if (Tri1.equals(tt)) {
neigTri.push_back(Tri2);
}
if (Tri2.equals(tt)) {
neigTri.push_back(Tri1);
}
}
if(neigTri.size()==0){
des = false;
}
int l;
if (neigTri.size() == 1) {
Vertex va1 = neigTri[0].getCentroid();
l = tmap[r(va1)];
trgl[l].addNewNode(v2);
des = !(v2.equals(trgl[l].getLongestEdge().getMidpoint()));
if(des){
trgl[l].addNewNode(trgl[l].getLongestEdge().getMidpoint());
v2 = trgl[l].getLongestEdge().getMidpoint();
tt = trgl[l];
}
}
}
}
}
}
//Clearing mpiedg and updating it
vector<Edge> mpiedg1(mpiedg);
mpiedg.clear();
for(int i=0;i<mpiedg1.size();i++){
Vertex ves = mpiedg1[i].getMidpoint();
int j = mpmap[r(ves)];
mpiedg.push_back(edg[j]);
}
mpiedg1.clear();
mpmap.clear();
deci = false;
for(int i=0;i<mpiedg.size();i++){
if(mpiedg[i].getBisectionStateOfmpiEdge()){
if(mpiedg[i].getTrack()==0){
//Denotes that new mpi Edge
//has been bisected only in this process
deci =true;
break;
}
}
}
//Every process sending its deci state to process 0
//Essential for all processes to run even when
//only few proesses come across deci==true
int gg=0;
if(deci==true){
gg=1;
}
if(process_num != 0){
MPI_Send(&gg,1,MPI_INT,0,4,MPI_COMM_WORLD);
MPI_Recv(&gg,1,MPI_INT,0,5,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
else{
int gg1=0;
vector<int> glist;
glist.push_back(gg);
for(int i=1;i<num_process;i++){
MPI_Recv(&gg1,1,MPI_INT,i,4,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
glist.push_back(gg1);
}
gg1=0;
for(int i=0;i<glist.size();i++){
if(glist[i]==1){
gg1=1;
break;
}
}
gg =gg1; //For process 0
for(int i=1;i<num_process;i++){
MPI_Send(&gg1,1,MPI_INT,i,5,MPI_COMM_WORLD);
}
}
if(gg==1){
deci = true;
}
else{
deci = false;
}
}
//Clearing map of triangles
tmap.clear();
if(process_num==0){
cout<<"MPI Edge communication is done\n";
}
//Updating all the triangles with bisected edges
for (int m = 0;m < trgl.size();m++) {
if (trgl[m].getNewVertices() > 0) {
//If only 1 new node has been added
if (trgl[m].getNewVertices() == 1) {
Vertex longVer = trgl[m].getLongestEdge().getMidpoint();
//Creating vertex opposite to the midpoint of the longest edge
Vertex OpplongVer = trgl[m].getOppVertexofPoint(longVer);
//Creating and add the new edge of type 0,which bisects the triangle to edg
Edge newEdge(longVer, OpplongVer, 0);
Edge longEdge = trgl[m].getLongestEdge();
newEdge.addOppVertices(longEdge.getVertex1());
newEdge.addOppVertices(longEdge.getVertex2());
edg.push_back(newEdge);
//Creating map location for newEdge
emap[r(newEdge.getMidpoint())] = edg.size()-1;
//Creating new triangles with refine as false,false is preassigned and deleting the old one
Triangle t1(longVer, OpplongVer, longEdge.getVertex1());
Triangle t2(longVer, OpplongVer, longEdge.getVertex2());
trgl.push_back(t1);
//Creating map location for Triangles
//tmap[t1.getCentroid()] = trgl.size()-1;
trgl.push_back(t2);
//Creating map location for Triangles
//tmap[t2.getCentroid()] = trgl.size()-1;
trgl.erase(trgl.begin() + m);
//Incrementing the count of triangle at m
//trgl[m].incrementCount();
//Creating new edges (if they aren't already in edg) with their type and adding Opposite Vertex
int n[3][2] = {0};//To keep track of the vector number and type of edge
Edge e1(longVer, longEdge.getVertex1());
Edge e2(longVer, longEdge.getVertex2());
//The 2nd column of n for pre-existing edges gives its type
//for new edges (bisected edges) stores checker, which indicates