forked from martinjrobins/SPH-DEM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataLL.cpp
executable file
·1288 lines (1169 loc) · 44.3 KB
/
dataLL.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 "dataLL.impl.h"
#include "sort.h"
const unsigned int KEY_SIZE = 30; // must be divisable by NDIM
const unsigned int KEY_DIM_BITS = KEY_SIZE/NDIM;
const unsigned int KEY_DIM_MAX = (unsigned int)floor(pow(2.0,(int)KEY_DIM_BITS)-1);
CdataLL::CdataLL(particleContainer &in_ps, CglobalVars &in_globals,bool opt): ps(in_ps),globals(in_globals) {
//initialise hmax, the maximum smoothing length
hmax = H;
sph_search_radius = H;
for (int i=0;i<NDIM;i++) {
cell_max[i] = globals.procDomain[i*2+1];
cell_min[i] = globals.procDomain[i*2];
}
cells.resize(static_cast<vectInt>((cell_max-cell_min)/(KERNAL_RADIUS*sph_search_radius))+8);
#ifdef LIQ_DEM
dem_cells.resize(static_cast<vectInt>((cell_max-cell_min)/(2*DEM_SEARCH_RADIUS))+2*2*LIQ_DEM_COUPLING_RADIUS/DEM_SEARCH_RADIUS);
#endif
cout << "size of particle array = "<<ps.capacity()*sizeof(Cparticle)/1024/1024<<" MB"<<endl;
// allocate buffers used for mpi communication
allocateBuffers();
//keep track of hmax for each neighbouring cpu
procGhostMax2H.resize(3);
procGhostMax2H = 1.1*KERNAL_RADIUS*H;
cout << "Processor "<<globals.mpiRank<<" of "<<globals.mpiSize<<" has domain ";
for (int i=0;i<NDIM*2;i++) {
cout<<globals.procDomain[i]<<' ';
}
// if prompted, init the sorted traverse order
if (opt) {
initTraverseOrder();
}
// reset all data (neighbours, ghosts, mpi comms etc)
reset();
}
/*
* allocate buffers used for mpi communication.
* TODO: these buffers take up quite a bit of memory. Some could be combined,
* for example the ones used to sync info and the normal ghost buffers
*/
void CdataLL::allocateBuffers() {
ghostBuffersSend.resize(3);
pBufferSizes.resize(3);
gBufferSizes.resize(3);
/*
* sending and receiving ghost buffers
*/
int totalSize = 0;
for (Array<CghostData*,NDIM>::iterator i=ghostBuffersSend.begin();i!=ghostBuffersSend.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new CghostData[calcBufferSize(coords)];
totalSize += calcBufferSize(coords)*sizeof(CghostData);
gBufferSizes(i.position()) = calcBufferSize(coords);
}
}
ghostBuffersRecv.resize(3);
for (Array<CghostData*,NDIM>::iterator i=ghostBuffersRecv.begin();i!=ghostBuffersRecv.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new CghostData[calcBufferSize(coords)];
totalSize += calcBufferSize(coords)*sizeof(CghostData);
}
}
/*
* ghostedParticles contains links to particles that have been ghosted
* (for later syncing purposes)
*/
ghostedParticles.resize(3);
for (Array<Cparticle**,NDIM>::iterator i=ghostedParticles.begin();i!=ghostedParticles.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new Cparticle*[calcBufferSize(coords)];
totalSize += calcBufferSize(coords)*sizeof(Cparticle*);
}
}
/*
* sending and receiving particle buffers
*/
particleBuffersSend.resize(3);
for (Array<Cparticle*,NDIM>::iterator i=particleBuffersSend.begin();i!=particleBuffersSend.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new Cparticle[calcPBufferSize(coords)];
totalSize += calcPBufferSize(coords)*sizeof(Cparticle);
pBufferSizes(i.position()) = calcPBufferSize(coords);
}
}
particleBuffersRecv.resize(3);
for (Array<Cparticle*,NDIM>::iterator i=particleBuffersRecv.begin();i!=particleBuffersRecv.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new Cparticle[calcPBufferSize(coords)];
totalSize += calcPBufferSize(coords)*sizeof(Cparticle);
}
}
/*
* sending and receiving buffers for syncing
*/
syncBuffersSend.resize(3);
for (Array<void*,NDIM>::iterator i=syncBuffersSend.begin();i!=syncBuffersSend.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new unsigned char[sizeof(CghostData)*calcBufferSize(coords)];
totalSize += calcBufferSize(coords)*sizeof(CghostData);
}
}
syncBuffersRecv.resize(3);
for (Array<void*,NDIM>::iterator i=syncBuffersRecv.begin();i!=syncBuffersRecv.end();i++) {
vectInt coords = i.position()-1;
if (globals.procNeighbrs(i.position()) >= 0) {
*i = new unsigned char[sizeof(CghostData)*calcBufferSize(coords)];
totalSize += calcBufferSize(coords)*sizeof(CghostData);
}
}
cout << "size of buffers = "<<totalSize/1024/1024<<" MB"<<endl;
sendSizesGhosts.resize(3);
recvSizesGhosts.resize(3);
}
/*
* reset particle positions, find neighbours, mpi communication to neighbor
* cpus etc.
*
* user will normally call this whenever particle positions are updated
*/
void CdataLL::reset() {
tagSortedParticlePointers.clear();
timeval t1,t2;
gettimeofday(&t1,NULL);
//clear bucket list
for (vector<vector<Cparticle *>*>::iterator dirty_cell = dirty_cells.begin();dirty_cell != dirty_cells.end();dirty_cell++) {
(*dirty_cell)->clear();
}
dirty_cells.clear();
/*
#ifdef LIQ_DEM
for (particleContainer::iterator p = ps.begin();p != ps.end();p++) {
if (p->iam==dem) {
const vectInt cellI = dem_getCellI(p->r);
dem_cells(cellI).clear();
}
}
for (vector<Cparticle>::iterator p = procGhostParticles.begin();p != procGhostParticles.end();p++) {
if (p->iam==dem) {
const vectInt cellI = dem_getCellI(p->r);
dem_cells(cellI).clear();
}
}
#endif
if (cells.size()>ps.size()+procGhostParticles.size()) {
for (particleContainer::iterator p = ps.begin();p != ps.end();p++) {
#ifdef LIQ_DEM
if (p->iam==dem) continue;
#endif
const vectInt cellI = getCellI(p->r);
cells(cellI).clear();
}
for (vector<Cparticle>::iterator p = procGhostParticles.begin();p != procGhostParticles.end();p++) {
#ifdef LIQ_DEM
if (p->iam==dem) continue;
#endif
const vectInt cellI = getCellI(p->r);
cells(cellI).clear();
}
} else {
for (Array<vector<Cparticle *>,NDIM>::iterator i=cells.begin();i!=cells.end();i++) {
i->clear();
}
}
*/
int psCapacity = ps.capacity();
// update domain and sync with neighbouring processes
updateDomain();
// check that the sizes of ps, pInfos and pInfoLinks are identical
if ((ps.size() != pInfos.size())||(ps.size() != pInfoLinks.size())) {
if (globals.sphStep != 0) {
cout <<"Error: ps has "<<ps.size()<<" particles, pInfos has "<<pInfos.size()<<" particles, pInfoLinks has "<<pInfoLinks.size()<<" partcles"<<endl;
exit(-1);
}
}
// ps should not reallocate its memory!
if (ps.size() > psCapacity) {
cerr << "Error: dataLL:reset(): ps has grown beyond its capacity! Bad things can happen......I'm outta here!"<<endl;
exit(-1);
}
n = ps.size();
// recalculate limits
calcDomainLimits();
// reinsert particles and ghost particles
if ((hmax > sph_search_radius)||(hmax < 0.9*sph_search_radius)) {
sph_search_radius = hmax;
cells.resize(static_cast<vectInt>((cell_max-cell_min)/(KERNAL_RADIUS*sph_search_radius))+8);
}
for (particleContainer::iterator p = ps.begin();p != ps.end();p++) {
insert(*p);
}
for (vector<Cparticle>::iterator p=procGhostParticles.begin();p!= procGhostParticles.end();p++) {
insert(*p);
}
gettimeofday(&t2,NULL);
globals.wtTotalReset.tv_sec += t2.tv_sec-t1.tv_sec;
globals.wtTotalReset.tv_usec += t2.tv_usec-t1.tv_usec;
}
void CdataLL::insertNewParticle(Cparticle &newP) {
ps.push_back(newP);
n++;
if (n>MAX_NUM_PARTICLES_PER_CPU) {
cout<<"insertNewParticle(): number of particles greater than max limit!!!"<<endl;
exit(-1);
}
/*
* This is the tricky bit. Make sure that the inserted particle info is also
*placed in pInfos and pInfoLinks
*/
Cparticle *p = &(ps.back());
CpInfo newpInfo;
newpInfo.p = p;
newpInfo.key = calcKey(newpInfo.p->r);
pInfos.push_back(newpInfo);
CpInfoLink newpInfoLink;
newpInfoLink.ppInfo = &(pInfos.back());
pInfoLinks.push_back(newpInfoLink);
pInfos.back().ppInfoLink = &(pInfoLinks.back());
insert(*p);
}
void CdataLL::markForDeletion(Cparticle &p) {
// using a custom tag for deletion. Could be better....
p.tag = -111;
}
/*
* deletes all particles with a tag = -111. This could be done better...
* (delete flag maybe)
*/
void CdataLL::deleteParticles() {
for (vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.begin();ppInfoLink!=pInfoLinks.end();) {
CpInfo *ppInfo = ppInfoLink->ppInfo;
Cparticle *thisP = ppInfo->p;
if (thisP->tag == -111) {
deleteParticle(ppInfoLink,ppInfo);
continue;
}
ppInfoLink++;
}
}
/*
* deletes particle from particle list (ps). Makes sure that the particle
* infomation is also deleted from pInfoLinks and pInfos
*/
void CdataLL::deleteParticle(vector<CpInfoLink>::iterator &ppInfoLink,CpInfo *ppInfo) {
Cparticle *thisP = ppInfo->p;
if (ppInfoLink != pInfoLinks.end()-1) {
*thisP = ps.back();
*ppInfoLink = pInfoLinks.back();
ppInfoLink->ppInfo->p = thisP;
ppInfoLink->ppInfo->ppInfoLink = &(*ppInfoLink);
ps.pop_back();
pInfoLinks.pop_back();
} else {
ps.pop_back();
pInfoLinks.pop_back();
ppInfoLink = pInfoLinks.end();
}
if (ppInfo != &(pInfos.back())) {
*ppInfo = pInfos.back();
ppInfo->ppInfoLink->ppInfo = ppInfo;
pInfos.pop_back();
} else {
pInfos.pop_back();
}
}
/*
* BIG TODO: This function handles ALL the mpi processing for each timestep.
* Need to break this up into sub-functions and tidy it all up
*/
void CdataLL::updateDomain() {
timeval t11,t22;
gettimeofday(&t11,NULL);
Array<int,NDIM> ghostIndicies(3);
Array<int,NDIM> particleIndicies(3);
ghostIndicies = 0;
particleIndicies = 0;
procGhostParticles.clear();
/*
* update relevant domain limits (for ghost particles etc) using
* globals.procDomain.
*/
vect dmin,dspace,dgmin,dgspace;
#ifdef LIQ_DEM
vect liq_dgspace,liq_dgmin,dem_dgspace,dem_dgmin;
#endif
for (int i=0;i<NDIM;i++) {
dmin[i] = globals.procDomain[i*2];
dspace[i] = globals.procDomain[i*2+1] - dmin[i];
vectInt coords = 1;
coords[i] = 0;
dgmin[i] = dmin[i] + procGhostMax2H(coords);
dgspace[i] = dspace[i] - procGhostMax2H(coords);
#ifdef LIQ_DEM
liq_dgmin[i] = dmin[i] + max(procGhostMax2H(coords),LIQ_DEM_COUPLING_RADIUS);
//dem_dgmin[i] = dmin[i] + 2.0*DEM_RADIUS;
dem_dgmin[i] = liq_dgmin[i];
liq_dgspace[i] = dspace[i] - max(procGhostMax2H(coords),LIQ_DEM_COUPLING_RADIUS);
//dem_dgspace[i] = dspace[i] - 4.0*DEM_RADIUS;
#endif
coords[i] = 2;
dgspace[i] -= procGhostMax2H(coords);
#ifdef LIQ_DEM
liq_dgspace[i] -= max(procGhostMax2H(coords),LIQ_DEM_COUPLING_RADIUS);
dem_dgspace[i] = liq_dgspace[i];
#endif
}
//cout <<"liq_dgmin = "<<liq_dgmin<<" liq_dgspace = "<<liq_dgspace<<" dem_dgmin = "<<dem_dgmin<<" dem_dgspace = "<<dem_dgspace<<endl;
/*
* loop thru particles and add all particle to be sent to
* particleBuffersSend. Delete these particles from this CPUs list
*/
for (vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.begin();ppInfoLink!=pInfoLinks.end();) {
CpInfo *ppInfo = ppInfoLink->ppInfo;
ppInfo->neighbrs.clear();
Cparticle *thisP = ppInfo->p;
if (thisP->tag == -111) {
deleteParticle(ppInfoLink,ppInfo);
continue;
}
vectInt outCoords = static_cast<vectInt>((thisP->r-dmin)/dspace+1);
if (any(outCoords>2)||any(outCoords<0)) {
cerr << "Particle out of range! Tag = "<<thisP->tag<<" iam = "<<thisP->iam<<" position = "<<thisP->r<<" velocityhat = "<<thisP->vhat<<" velocity = "<<thisP->v<<" density = "<<thisP->dens<<" total force = "<<thisP->f<<" pressure force = "<<thisP->fp<<" boundary force = "<<thisP->fb<<endl;
deleteParticle(ppInfoLink,ppInfo);
continue;
}
if (any(outCoords!=1)) {
if (globals.procNeighbrs(outCoords) >= 0) {
//send particle to neighbour
//add to real particle to send
if (particleIndicies(outCoords)>=pBufferSizes(outCoords)) {
cerr << "particleBuffers are full, exiting...."<<endl;
exit(-1);
}
particleBuffersSend(outCoords)[particleIndicies(outCoords)] = *thisP;
particleIndicies(outCoords)++;
} else {
cerr << "Particle out of range! Tag = "<<thisP->tag<<" iam = "<<thisP->iam<<" position = "<<thisP->r<<" velocityhat = "<<thisP->vhat<<" velocity = "<<thisP->v<<" density = "<<thisP->dens<<" total force = "<<thisP->f<<" pressure force = "<<thisP->fp<<" boundary force = "<<thisP->fb<<endl;
}
deleteParticle(ppInfoLink,ppInfo);
continue;
}
ppInfoLink++;
}
/*
* Send these particles to the correct neighbouring cpus
*/
Array<int,NDIM> particleSizesSend(3);
Array<int,NDIM> particleSizesRecv(3);
particleSizesSend = particleIndicies*sizeof(Cparticle);
int sendRecvArraySize = 4*int(pow(3.0,NDIM));
MPI_Request requestSendRecv[sendRecvArraySize];
MPI_Status statusSendRecv[sendRecvArraySize];
timeval t1,t2;
gettimeofday(&t1,NULL);
if (globals.mpiRank%2==0) {
int upper = int(pow(3.0,NDIM));
for (int i=0;i<upper;i++) {
vectInt split = 3;
vectInt coords = Nmisc::numToCoords(i,split);
if (globals.procNeighbrs(coords)>=0) {
sendRecvParticles(coords,i,requestSendRecv+i*4,&(particleSizesSend(coords)),&(particleSizesRecv(coords)));
}
else {
for (int j=0;j<4;j++) {
requestSendRecv[i*4+j] = MPI_REQUEST_NULL;
}
}
}
} else {
for (int i=int(pow(3.0,NDIM))-1;i>=0;i--) {
vectInt split = 3;
vectInt coords = Nmisc::numToCoords(i,split);
if (globals.procNeighbrs(coords)>=0) {
sendRecvParticles(coords,i,requestSendRecv+i*4,&(particleSizesSend(coords)),&(particleSizesRecv(coords)));
}
else {
for (int j=0;j<4;j++) {
requestSendRecv[i*4+j] = MPI_REQUEST_NULL;
}
}
}
}
MPI_Waitall(sendRecvArraySize, requestSendRecv, statusSendRecv);
gettimeofday(&t2,NULL);
globals.wtTotalMPI.tv_sec += t2.tv_sec-t1.tv_sec;
globals.wtTotalMPI.tv_usec += t2.tv_usec-t1.tv_usec;
/*
* All particles have arrived. Now loop thru them all and add them to
* my list. Also, process any special boundary conditions (periodic, ghost etc)
*/
particleIndicies = particleSizesRecv/sizeof(Cparticle);
for (Array<int,NDIM>::iterator ap = globals.procNeighbrs.begin();ap != globals.procNeighbrs.end();ap++) {
vectInt coords = ap.position();
int neighbr = globals.procNeighbrs(coords);
if (neighbr >= 0) {
double maxh = hmax;
for (int z=0;z<particleIndicies(coords);z++) {
Cparticle *recvP = &(particleBuffersRecv(coords)[z]);
ps.push_back(*recvP);
Cparticle *p = &(ps.back());
for (int i=0;i<NDIM;i++) {
if ((PERIODIC[i])&&(coords[i]==0)&&(RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)) {
p->r[i] = RMIN[i]-RMAX[i]+p->r[i];
if (p->iam==sph)
p->dens += DENS_DROP[i];
#ifdef SLK
p->currR[i] = RMIN[i]-RMAX[i]+p->currR[i];
#endif
}
if ((PERIODIC[i])&&(coords[i]==2)&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP)) {
p->r[i] = RMAX[i]-RMIN[i]+p->r[i];
if (p->iam==sph)
p->dens -= DENS_DROP[i];
#ifdef SLK
p->currR[i] = RMAX[i]-RMIN[i]+p->currR[i];
#endif
}
if ((GHOST[2*i])&&(coords[i]==0)&&(RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)) {
p->r[i] = 2.0*RMIN[i]-p->r[i];
if (GHOST[2*i]==1) {
p->v[i] = -p->v[i];
p->vhat[i] = -p->vhat[i];
} else {
p->v = -p->v;
p->vhat = -p->vhat;
}
}
if ((GHOST[2*i+1])&&(coords[i]==2)&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP)) {
p->r[i] = 2.0*RMAX[i]-p->r[i];
if (GHOST[2*i+1]==1) {
p->v[i] = -p->v[i];
p->vhat[i] = -p->vhat[i];
} else {
p->v = -p->v;
p->vhat = -p->vhat;
}
}
}
CpInfo newpInfo;
newpInfo.p = p;
newpInfo.key = calcKey(newpInfo.p->r);
pInfos.push_back(newpInfo);
CpInfoLink newpInfoLink;
newpInfoLink.ppInfo = &(pInfos.back());
pInfoLinks.push_back(newpInfoLink);
pInfos.back().ppInfoLink = &(pInfoLinks.back());
}
}
}
/*
* now loop thru all the particles again and find all the ghost particles
* that need to be sent to neighbouring cpus
*/
for (vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.begin();ppInfoLink!=pInfoLinks.end();ppInfoLink++) {
CpInfo *ppInfo = ppInfoLink->ppInfo;
ppInfo->neighbrs.clear();
Cparticle *thisP = ppInfo->p;
#ifdef LIQ_DEM
vectInt inCoords;
if (thisP->iam==dem) {
inCoords = static_cast<vectInt>((thisP->r-dem_dgmin)/dem_dgspace+1);
} else {
inCoords = static_cast<vectInt>((thisP->r-liq_dgmin)/liq_dgspace+1);
}
#else
vectInt inCoords = static_cast<vectInt>((thisP->r-dgmin)/dgspace+1);
#endif
for (int i=0;i<NDIM;i++) {
if (inCoords[i]>2) inCoords[i]=2;
if (inCoords[i]<0) inCoords[i]=0;
}
if (any(inCoords!=1)) {
vectInt lowerBounds;
vectInt upperBounds;
for (int i=0;i<NDIM;i++) {
if (inCoords[i]<=1) {
upperBounds[i] = 1;
lowerBounds[i] = inCoords[i];
} else {
upperBounds[i] = inCoords[i];
lowerBounds[i] = 1;
}
}
vectInt extent = upperBounds-lowerBounds+1;
Array<int,NDIM> dummy(lowerBounds,extent);
for (Array<int,NDIM>::iterator pD = dummy.begin();pD !=dummy.end();pD++) {
vectInt dCoords = pD.position();
if (globals.procNeighbrs(dCoords) >= 0) {
//if ((thisP->iam==11)&&((dCoords[1]==2)||(dCoords[1]==0))) cout <<"mpiRank = "<<globals.mpiRank<<". adding type "<<thisP->iam<<" at r = "<<thisP->r<<" to "<<dCoords<<endl;
//add to ghost particle array
if (ghostIndicies(dCoords)>=gBufferSizes(dCoords)) {
cerr << "ghostBuffers are full, exiting...."<<endl;
cerr << "buffer size is "<<gBufferSizes(dCoords) <<endl;
cerr << "buffer is "<<dCoords<<endl;
cerr << "mpirank is "<<globals.mpiRank<<endl;
exit(-1);
}
#ifdef BORE_SOLITON
if ((globals.mpiRank==ENDPERIODICCPU)&&(dCoords[0]==2)) {
Cparticle newP = *thisP;
vectInt coords = 1;
coords[0] = 2;
newP.r[0] += procGhostMax2H(coords);
procGhostParticles.push_back(*thisP);
}
#endif
ghostBuffersSend(dCoords)[ghostIndicies(dCoords)] = *thisP;
ghostedParticles(dCoords)[ghostIndicies(dCoords)] = thisP;
ghostIndicies(dCoords)++;
}
}
}
}
sendSizesGhosts = ghostIndicies;
/*
* ok. We have found all the ghost and put them in ghostBuffersSend. Now
* send the data to the correct neighbouring cpus
*/
Array<int,NDIM> ghostSizesSend(3);
Array<int,NDIM> ghostSizesRecv(3);
ghostSizesSend = ghostIndicies*sizeof(CghostData);
gettimeofday(&t1,NULL);
if (globals.mpiRank%2==0) {
int upper = int(pow(3.0,NDIM));
for (int i=0;i<upper;i++) {
vectInt split = 3;
vectInt coords = Nmisc::numToCoords(i,split);
if (globals.procNeighbrs(coords)>=0) {
sendRecvGhosts(coords,i,requestSendRecv+i*4,&(ghostSizesSend(coords)),&(ghostSizesRecv(coords)));
}
else {
for (int j=0;j<4;j++) {
requestSendRecv[i*4+j] = MPI_REQUEST_NULL;
}
}
}
} else {
for (int i=int(pow(3.0,NDIM))-1;i>=0;i--) {
vectInt split = 3;
vectInt coords = Nmisc::numToCoords(i,split);
if (globals.procNeighbrs(coords)>=0) {
sendRecvGhosts(coords,i,requestSendRecv+i*4,&(ghostSizesSend(coords)),&(ghostSizesRecv(coords)));
}
else {
for (int j=0;j<4;j++) {
requestSendRecv[i*4+j] = MPI_REQUEST_NULL;
}
}
}
}
MPI_Waitall(sendRecvArraySize, requestSendRecv, statusSendRecv);
gettimeofday(&t2,NULL);
globals.wtTotalMPI.tv_sec += t2.tv_sec-t1.tv_sec;
globals.wtTotalMPI.tv_usec += t2.tv_usec-t1.tv_usec;
/*
* all ghost data is now sent and received. Now loop thru the ghost
* particles obtained from each neighbour and add them to procGhostParticles, At
* the same time handle and special boundary conditions.
*/
ghostIndicies = ghostSizesRecv/sizeof(CghostData);
recvSizesGhosts = ghostIndicies;
for (Array<int,NDIM>::iterator ap = globals.procNeighbrs.begin();ap != globals.procNeighbrs.end();ap++) {
vectInt coords = ap.position();
int neighbr = globals.procNeighbrs(coords);
if (neighbr >= 0) {
#ifdef BORE_SOLITON
if ((globals.mpiRank==ENDPERIODICCPU)&&(coords[0]==2)) continue;
#endif
//work out ghost max h's
double maxh = hmax;
for (int z=0;z<ghostIndicies(coords);z++) {
Cparticle p;
p = ghostBuffersRecv(coords)[z];
for (int i=0;i<NDIM;i++) {
if ((PERIODIC[i])&&(coords[i]==0)&&(RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)) {
p.r[i] = RMIN[i]-RMAX[i]+p.r[i];
if (p.iam==sph)
p.dens += DENS_DROP[i];
#ifdef SLK
p.currR[i] = RMIN[i]-RMAX[i]+p.currR[i];
#endif
}
if ((PERIODIC[i])&&(coords[i]==2)&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP)) {
p.r[i] = RMAX[i]-RMIN[i]+p.r[i];
if (p.iam==sph)
p.dens -= DENS_DROP[i];
#ifdef SLK
p.currR[i] = RMAX[i]-RMIN[i]+p.currR[i];
#endif
}
if ((GHOST[2*i])&&(coords[i]==0)&&(RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)) {
p.r[i] = 2.0*RMIN[i]-p.r[i];
if (GHOST[2*i]==1) {
p.v[i] = -p.v[i];
p.vhat[i] = -p.vhat[i];
} else {
p.v = -p.v;
p.vhat = -p.vhat;
}
}
if ((GHOST[2*i+1])&&(coords[i]==2)&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP)) {
p.r[i] = 2.0*RMAX[i]-p.r[i];
if (GHOST[2*i+1]==1) {
p.v[i] = -p.v[i];
p.vhat[i] = -p.vhat[i];
} else {
p.v = -p.v;
p.vhat = -p.vhat;
}
}
}
if (p.h>maxh) maxh = p.h;
procGhostParticles.push_back(p);
}
//store max h for this neighbour
procGhostMax2H(coords) = KERNAL_RADIUS*maxh;
}
}
gettimeofday(&t22,NULL);
globals.wtTotalUpdateDomain.tv_sec += t22.tv_sec-t11.tv_sec;
globals.wtTotalUpdateDomain.tv_usec += t22.tv_usec-t11.tv_usec;
}
/*
* non-blocking mpi send of the particle data in the buffer
* particleBuffersSend. The function sends the number of particles to be sent before sending the data
*/
void CdataLL::sendRecvParticles(const vectInt coords,const int num,MPI_Request *request,int *particleSizesSend,int *particleSizesRecv) {
int neighbr = globals.procNeighbrs(coords);
vectInt middle = 1;
vectInt split = 3;
vectInt oppCoords = 2-coords;
for (int i=0;i<NDIM;i++) {
if (((coords[i]==2)&&(GHOST[2*i+1])&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP))||
((coords[i]==0)&&(GHOST[2*i])&&((RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)))) {
oppCoords[i] = coords[i];
}
}
int oppNum = Nmisc::coordsToNum(oppCoords,split);
MPI_Status status;
MPI_Isend(particleSizesSend,1,MPI_INT,neighbr,300+num,MPI_COMM_WORLD,request);
MPI_Irecv(particleSizesRecv,1,MPI_INT,neighbr,300+oppNum,MPI_COMM_WORLD,request+1);
MPI_Isend(particleBuffersSend(coords),*particleSizesSend,MPI_BYTE,neighbr,400+num,MPI_COMM_WORLD,request+2);
MPI_Irecv(particleBuffersRecv(coords),pBufferSizes(coords)*sizeof(Cparticle),MPI_BYTE,neighbr,400+oppNum,MPI_COMM_WORLD,request+3);
}
/*
* non-blocking mpi send of the ghost data in the buffer
* ghostBuffersSend. The function sends the number of particles to be sent before sending the data
*/
void CdataLL::sendRecvGhosts(const vectInt coords,const int num,MPI_Request *request,int *ghostSizesSend,int *ghostSizesRecv) {
int neighbr = globals.procNeighbrs(coords);
vectInt middle = 1;
vectInt split = 3;
vectInt oppCoords = 2-coords;
for (int i=0;i<NDIM;i++) {
if (((coords[i]==2)&&(GHOST[2*i+1])&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP))||
((coords[i]==0)&&(GHOST[2*i])&&((RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)))) {
oppCoords[i] = coords[i];
}
}
int oppNum = Nmisc::coordsToNum(oppCoords,split);
MPI_Status status;
MPI_Isend(ghostSizesSend,1,MPI_INT,neighbr,100+num,MPI_COMM_WORLD,request);
MPI_Irecv(ghostSizesRecv,1,MPI_INT,neighbr,100+oppNum,MPI_COMM_WORLD,request+1);
MPI_Isend(ghostBuffersSend(coords),*ghostSizesSend,MPI_BYTE,neighbr,200+num,MPI_COMM_WORLD,request+2);
MPI_Irecv(ghostBuffersRecv(coords),gBufferSizes(coords)*sizeof(CghostData),MPI_BYTE,neighbr,200+oppNum,MPI_COMM_WORLD,request+3);
}
/*
* for the user-initiated syncing, the number of ghost particles is already
* know. So no need to send sizes
*/
void CdataLL::sendRecvDataSync(const vectInt coords,const int num,MPI_Request *request,int sendSize, void *sendBuffer, int recvSize, void *recvBuffer) {
int neighbr = globals.procNeighbrs(coords);
vectInt middle = 1;
vectInt split = 3;
vectInt oppCoords = 2-coords;
for (int i=0;i<NDIM;i++) {
if (((coords[i]==2)&&(GHOST[2*i+1])&&(RMAX[i] >= globals.procDomain[i*2+1]-PSEP)&&(RMAX[i] <= globals.procDomain[i*2+1]+PSEP))||
((coords[i]==0)&&(GHOST[2*i])&&((RMIN[i] >= globals.procDomain[i*2]-PSEP)&&(RMIN[i] <= globals.procDomain[i*2]+PSEP)))) {
oppCoords[i] = coords[i];
}
}
int oppNum = Nmisc::coordsToNum(oppCoords,split);
MPI_Status status;
MPI_Isend(sendBuffer,sendSize,MPI_BYTE,neighbr,500+num,MPI_COMM_WORLD,request);
MPI_Irecv(recvBuffer,recvSize,MPI_BYTE,neighbr,500+oppNum,MPI_COMM_WORLD,request+1);
}
void CdataLL::setGlobalTimestep(double dt) {
double sendbuf = dt;
double recvbuf[globals.mpiSize];
MPI_Allgather(&sendbuf, 1, MPI_DOUBLE, recvbuf, 1, MPI_DOUBLE, MPI_COMM_WORLD);
globals.dt = dt;
for (int i=0;i<globals.mpiSize;i++) {
if (recvbuf[i]<globals.dt) globals.dt = recvbuf[i];
}
}
/*
* TODO: should make a general one: "function" over procs
* TODO: now have two functions for two types (vect and double).
* template this to one function
*/
void CdataLL::sumOverProcs(vect *data,int num) {
int newNum = num*sizeof(vect);
vect *sendbuf = data;
vect recvbuf[globals.mpiSize*num];
MPI_Gather(sendbuf,newNum,MPI_BYTE,recvbuf,newNum,MPI_BYTE,0,MPI_COMM_WORLD);
if (globals.mpiRank==0) {
for (int i=1;i<globals.mpiSize;i++) {
for (int j=0;j<num;j++) {
recvbuf[j] += recvbuf[i*num+j];
}
}
for (int i=1;i<globals.mpiSize;i++) {
for (int j=0;j<num;j++) {
recvbuf[i*num+j] = recvbuf[j];
}
}
}
MPI_Scatter(recvbuf,newNum,MPI_BYTE,sendbuf,newNum,MPI_BYTE,0,MPI_COMM_WORLD);
}
void CdataLL::sumOverProcs(double *data,int num) {
int newNum = num*sizeof(double);
double *sendbuf = data;
double recvbuf[globals.mpiSize*num];
MPI_Gather(sendbuf,newNum,MPI_BYTE,recvbuf,newNum,MPI_BYTE,0,MPI_COMM_WORLD);
if (globals.mpiRank==0) {
for (int i=1;i<globals.mpiSize;i++) {
for (int j=0;j<num;j++) {
recvbuf[j] += recvbuf[i*num+j];
}
}
for (int i=1;i<globals.mpiSize;i++) {
for (int j=0;j<num;j++) {
recvbuf[i*num+j] = recvbuf[j];
}
}
}
MPI_Scatter(recvbuf,newNum,MPI_BYTE,sendbuf,newNum,MPI_BYTE,0,MPI_COMM_WORLD);
}
unsigned long CdataLL::calcKey(vect r) {
unsigned int rint[NDIM];
unsigned long key = 0;
for (int i=0;i<NDIM;i++) {
rint[i] = (unsigned int)floor((r[i]-rmin[i])*KEY_DIM_MAX/(rmax[i]-rmin[i]));
}
unsigned int mask = 0x01<<(KEY_DIM_BITS-1);
for (int i=0;i<KEY_DIM_BITS;i++) {
for (int j=0;j<NDIM;j++) {
key <<= 1;
key |= (rint[j] & mask) >> (KEY_DIM_BITS-1-i);
}
mask >>= 1;
}
return key;
}
void CdataLL::updateParticles() {
initPinfos();
checkPinfos();
}
/*
* this function clears the particle information contained in pInfos and
* pInfoLinks and re-generates it from the particle list (ps)
*/
void CdataLL::initPinfos() {
pInfos.clear();
pInfoLinks.clear();
pInfos.reserve(ps.capacity());
pInfoLinks.reserve(ps.capacity());
cout << "size of pInfos and pInfoLinks = "<<(pInfos.capacity()*sizeof(CpInfo) + pInfoLinks.capacity()*sizeof(CpInfoLink))/1024/1024<<" MB"<<endl;
for (particleContainer::iterator thisP = ps.begin();thisP!=ps.end();thisP++) {
CpInfo newpInfo;
newpInfo.p = &(*thisP);
newpInfo.key = calcKey(thisP->r);
pInfos.push_back(newpInfo);
CpInfoLink newpInfoLink;
newpInfoLink.ppInfo = &(pInfos.back());
pInfoLinks.push_back(newpInfoLink);
pInfos.back().ppInfoLink = &(pInfoLinks.back());
vector<CpInfo>::iterator ppInfo = pInfos.end()-1;
vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.end()-1;
}
}
/*
* Sort pInfos using insertion sort. Make sure that the sorting
* maintains consistency with pInfoLink
*/
void CdataLL::sortPInfos() {
for (vector<CpInfo>::iterator i=pInfos.begin()+1;i!=pInfos.end();i++) {
for (vector<CpInfo>::iterator j=pInfos.begin();j!=i;j++) {
if (j->key > i->key) {
CpInfo tmpInfo = *j;
*j = *i;
j->ppInfoLink->ppInfo = &(*j);
vector<CpInfo>::iterator jplus1 = j+1;
for (vector<CpInfo>::iterator k=i;k!=jplus1;k--) {
*k = *(k-1);
k->ppInfoLink->ppInfo = &(*k);
}
*jplus1 = tmpInfo;
jplus1->ppInfoLink->ppInfo = &(*jplus1);
}
}
}
}
/*
* for each member of pInfoLinks, the particle information pointed to by ppInfo
* should have a class member ppInfoLink that points back to the original entry in pInfoLinks
*/
void CdataLL::checkPinfos() {
particleContainer::iterator p = ps.begin();
for (vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.begin();ppInfoLink!=pInfoLinks.end();ppInfoLink++) {
CpInfo *ppInfo = ppInfoLink->ppInfo;
if (ppInfo->ppInfoLink != &(*ppInfoLink)) {
cerr <<"Error: CdataLL::initTraverseOrder: ppInfo and ppInfoLink are not consistant!"<<endl;
exit(-1);
}
if (ppInfo->p != &(*p)) {
cerr <<"Error: CdataLL::initTraverseOrder: ppInfo, ppInfoLink and p are not consistant!"<<endl;
exit(-1);
}
p++;
}
}
void CdataLL::initTraverseOrder() {
initPinfos();
sortPInfos();
checkPinfos();
}
void CdataLL::calcTraverseOrder() {
particleContainer::iterator p = ps.begin();
for (vector<CpInfoLink>::iterator ppInfoLink = pInfoLinks.begin();ppInfoLink!=pInfoLinks.end();ppInfoLink++) {
CpInfo *ppInfo = ppInfoLink->ppInfo;
if (ppInfo->ppInfoLink != &(*ppInfoLink)) {
cerr <<"Error: CdataLL::calcTraverseOrder: ppInfo and ppInfoLink are not consistant!"<<endl;
exit(-1);
}
if (ppInfo->p != &(*p)) {
cerr <<"Error: CdataLL::calcTraverseOrder: ppInfo, ppInfoLink and p are not consistant!"<<endl;
exit(-1);
}
ppInfo->key = calcKey(ppInfo->p->r);
ppInfo->neighbrs.clear();
p++;
}
sortPInfos();
}
void CdataLL::calcDomainLimits() {
for (int i=0;i<NDIM;i++) {
rmin[i] = globals.procDomain[i*2];
rmax[i] = globals.procDomain[i*2+1];
}
hmax = -100000;
for (particleContainer::iterator thisP = ps.begin();thisP!=ps.end();thisP++) {
for (int j=0;j<NDIM;j++) {
if (thisP->r[j] < rmin[j]) { rmin[j] = thisP->r[j]; }
if (thisP->r[j] > rmax[j]) { rmax[j] = thisP->r[j]; }
}
if (thisP->h > hmax) hmax = thisP->h;
}
for (int i=0;i<NDIM;i++) {
vectInt coords = 1;
coords[i] = 0;
if (procGhostMax2H(coords)<hmax) procGhostMax2H(coords)=KERNAL_RADIUS*hmax;
coords[i] = 2;
if (procGhostMax2H(coords)<hmax) procGhostMax2H(coords)=KERNAL_RADIUS*hmax;
}
}
void CdataLL::insert(Cparticle &p) {
#ifdef LIQ_DEM
if (p.iam==dem) {
vector<Cparticle *> &cell = dem_cells(dem_getCellI(p.r));
if (cell.size()==0)
dirty_cells.push_back(&cell);
cell.push_back(&p);
} else {
vector<Cparticle *> &cell = cells(getCellI(p.r));
if (cell.size()==0)
dirty_cells.push_back(&cell);
cell.push_back(&p);
}
#else
vector<Cparticle *> &cell = cells(getCellI(p.r));
if (cell.size()==0)
dirty_cells.push_back(&cell);
cell.push_back(&p);
#endif
}
void CdataLL::addIfNeighbr(Cparticle &_p,vector<Cparticle *> &_neighbrs,vector<Cparticle *> &_toAdd) {
for (int i=0;i<_toAdd.size();i++) {
if (_p.is_neighbr(*(_toAdd[i]))) {
_neighbrs.push_back(_toAdd[i]);
}
}
}