-
Notifications
You must be signed in to change notification settings - Fork 7
/
gridsearch.c
1776 lines (1539 loc) · 58.2 KB
/
gridsearch.c
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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <fftw3.h>
#include <mpi.h>
#include <hdf5.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sort_long.h>
#include "raytrace.h"
static SourceGal *distribute_gals_to_nodes(long NumGalsForThisPlane, long start, long *NumGals);
static void gridsearch_gals(SourceGal *gals, long NumGals, double wpm1, double wpm2, HEALPixRay *bufferRays, long NumBufferRays);
static void gridsearch_gals_bornapprx(SourceGal *gals, long NumGals, double wpm1, double wpm2);
static void gridsearch_gals_nobornapprx(SourceGal *gals, long NumGals, double wpm1, double wpm2, HEALPixRay *bufferRays, long NumBufferRays);
static int tritest_getbarycoords(double a[2], double b[2], double c[2], double q[2], double barycoords[3]);
static double trisarea(double a[2], double b[2], double c[2]);
static HEALPixRay *get_buffer_rays(long *NumBufferRays);
static void destroy_buffer_rays(HEALPixRay *bufferRays);
static void rayprop_gridsearch(HEALPixRay *ray, double wp, double wpm1, double wpm2);
static int interp_invmagmat_to_point(double ivec[3], double galRad, double wpm1, double wpm2, double A[2][2]);
void gridsearch(double wpm1, double wpm2)
{
SourceGal *galsForThisPlane;
long i,NumGalsForThisPlane,start;
double rad,binL;
long bind;
long TotNumImageGalsGlobal,TotNumGalsForThisPlane;
double time,t;
HEALPixRay *bufferRays;
long NumBufferRays;
time = -MPI_Wtime();
if(ThisTask == 0)
fprintf(stderr,"finding galaxy images with grid search.\n");
//get gals for this plane
binL = rayTraceData.maxComvDistance/rayTraceData.NumLensPlanes;
NumGalsForThisPlane = 0;
start = -1;
for(i=NumSourceGalsGlobal-1;i>=0;--i)
{
rad = sqrt(SourceGalsGlobal[i].pos[0]*SourceGalsGlobal[i].pos[0] +
SourceGalsGlobal[i].pos[1]*SourceGalsGlobal[i].pos[1] +
SourceGalsGlobal[i].pos[2]*SourceGalsGlobal[i].pos[2]);
bind = (long) (rad/binL);
//catch gals past last plane within 1 kpc/h of edge for last plane
if(bind == rayTraceData.NumLensPlanes && fabs(rad-rayTraceData.maxComvDistance) < 1e-3)
bind = rayTraceData.NumLensPlanes-1;
if(bind == rayTraceData.CurrentPlaneNum)
++NumGalsForThisPlane;
if(bind > rayTraceData.CurrentPlaneNum)
{
start = i+1; //go back one index - an increment because we are working from the back of the list
break;
}
}
if(i == -1)
start = 0;
if(NumGalsForThisPlane == 0)
start = 0;
assert(start >= 0);
#ifdef DEBUG
#if DEBUG_LEVEL > 1
fprintf(stderr,"%05d: # of gals for this plane = %ld (of %ld), start = %ld\n",ThisTask,NumGalsForThisPlane,NumSourceGalsGlobal,start);
#endif
#endif
MPI_Allreduce(&NumGalsForThisPlane,&TotNumGalsForThisPlane,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
if(ThisTask == 0)
fprintf(stderr,"found %ld source galaxies for this plane.\n",TotNumGalsForThisPlane);
//do grid search if there are any gals on any task that need images
if(TotNumGalsForThisPlane > 0)
{
/*
Do this as follows
1) get buffer rays
2) loop through gal chunks and do grid search
3) write images to disk
4) destroy buffer rays
*/
// 1)
//get buffer rays
logProfileTag(PROFILETAG_GRIDSEARCH);
logProfileTag(PROFILETAG_RAYBUFF);
t = -MPI_Wtime();
if(ThisTask == 0)
fprintf(stderr,"starting to get buffer rays.\n");
bufferRays = get_buffer_rays(&NumBufferRays);
t += MPI_Wtime();
if(ThisTask == 0)
fprintf(stderr,"got buffer rays in %lf seconds.\n",t);
logProfileTag(PROFILETAG_RAYBUFF);
logProfileTag(PROFILETAG_GRIDSEARCH);
// 2)
//get right galaxies for this plane
logProfileTag(PROFILETAG_GRIDSEARCH_GALMOVE);
t = -MPI_Wtime();
galsForThisPlane = distribute_gals_to_nodes(NumGalsForThisPlane,start,&i);
NumGalsForThisPlane = i;
t += MPI_Wtime();
//if(ThisTask == 0)
//fprintf(stderr,"distributing gals to tasks took %lf seconds.\n",t);
logProfileTag(PROFILETAG_GRIDSEARCH_GALMOVE);
//reorder gals by their nest index - should help with cache misses during grid search
if(NumGalsForThisPlane > 0)
{
logProfileTag(PROFILETAG_GRIDSEARCH_GALGRIDSEARCH);
reorder_gals_nest(galsForThisPlane,NumGalsForThisPlane);
}
else
{
galsForThisPlane = NULL;
NumGalsForThisPlane = 0;
}
if(ThisTask == 0)
fprintf(stderr,"found %ld gals for this plane on this task.\n",NumGalsForThisPlane);
//do grid search
gridsearch_gals(galsForThisPlane,NumGalsForThisPlane,wpm1,wpm2,bufferRays,NumBufferRays);
#ifdef DEBUG
#if DEBUG_LEVEL > 1
if(NumGalsForThisPlane > 0)
fprintf(stderr,"%05d: finished grid search, found %ld images\n",ThisTask,NumImageGalsGlobal);
#endif
#endif
if(NumGalsForThisPlane > 0)
logProfileTag(PROFILETAG_GRIDSEARCH_GALGRIDSEARCH);
MPI_Allreduce(&NumImageGalsGlobal,&TotNumImageGalsGlobal,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
if(ThisTask == 0)
fprintf(stderr,"found %ld image gals from %ld source gals.\n",TotNumImageGalsGlobal,TotNumGalsForThisPlane);
// 3) - write images to disk if needed
if(TotNumImageGalsGlobal > 0)
{
logProfileTag(PROFILETAG_GRIDSEARCH);
logProfileTag(PROFILETAG_GALIO);
logProfileTag(PROFILETAG_GRIDSEARCH_IMAGEGALIO);
write_gals2fits();
if(NumImageGalsGlobal > 0)
{
free(ImageGalsGlobal);
ImageGalsGlobal = NULL;
NumImageGalsGlobal = 0;
}
logProfileTag(PROFILETAG_GRIDSEARCH_IMAGEGALIO);
logProfileTag(PROFILETAG_GALIO);
logProfileTag(PROFILETAG_GRIDSEARCH);
}
#ifdef DEBUG
#if DEBUG_LEVEL > 1
fprintf(stderr,"%05d: wrote gals to disk if needed\n",ThisTask);
#endif
#endif
if(NumGalsForThisPlane > 0)
free(galsForThisPlane);
// 4) - free some mem
destroy_buffer_rays(bufferRays);
}
time += MPI_Wtime();
if(ThisTask == 0)
fprintf(stderr,"found galaxy images in %lg seconds.\n",time);
/*MPI_Reduce(&time,&minTime,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD);
MPI_Reduce(&time,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Reduce(&time,&totTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
avgTime = totTime/((double) NTasks);
if(ThisTask == 0)
fprintf(stderr,"grid search time max,min,avg = %lf|%lf|%lf - %.2f percent (total time %lf)\n"
,maxTime,minTime,avgTime,(maxTime-avgTime)/avgTime*100.0,totTime);
*/
}
//util function for finding rays with nest value
static int compHEALPixRayNest(const void *a, const void *b)
{
if(((const HEALPixRay*)a)->nest > ((const HEALPixRay*)b)->nest)
return 1;
else if(((const HEALPixRay*)a)->nest < ((const HEALPixRay*)b)->nest)
return -1;
else
return 0;
}
//macro for error checking grid search prints out info for galaxy with ind CHECK_GS_IND
//#define CHECK_GS
#define CHECK_GS_IND 1370
static int interp_invmagmat_to_point(double ivec[3], double galRad, double wpm1, double wpm2, double A[2][2])
{
long n;
long fnd;
double nvec[3],ttens[2][2],rttens[2][2];
double theta,phi,wgt[4];
long Nwgt,wgtpix[4];
HEALPixRay wgtRays[4];
long snest,bnest,bundleRayShift,roffset;
HEALPixRay key,*fndRay;
bundleRayShift = 2*(rayTraceData.rayOrder - rayTraceData.bundleOrder);
//get shear matrix
A[0][0] = 0.0;
A[0][1] = 0.0;
A[1][0] = 0.0;
A[1][1] = 0.0;
vec2ang(ivec,&theta,&phi);
Nwgt = 4;
get_interpol(theta,phi,wgtpix,wgt,rayTraceData.rayOrder);
fnd = 1;
for(n=0;n<Nwgt;++n)
{
//find the ray you need
snest = ring2nest(wgtpix[n],rayTraceData.rayOrder);
bnest = snest >> bundleRayShift;
if(ISSETBITFLAG(bundleCells[bnest].active,PRIMARY_BUNDLECELL) && bundleCells[bnest].Nrays > 0)
{
roffset = snest - (bnest << bundleRayShift);
wgtRays[n] = bundleCells[bnest].rays[roffset]; //makes a copy of the ray via a structure assignemnt
assert(wgtRays[n].nest == snest);
}
else if(ISSETBITFLAG(bundleCells[bnest].active,RAYBUFF_BUNDLECELL) && bundleCells[bnest].Nrays > 0)
{
key.nest = snest;
fndRay = (HEALPixRay*)bsearch(&key,bundleCells[bnest].rays,(size_t) (bundleCells[bnest].Nrays),sizeof(HEALPixRay),compHEALPixRayNest);
if(fndRay != NULL)
{
wgtRays[n] = *fndRay; //makes a copy of the ray via a structure assignemnt
assert(wgtRays[n].nest == snest);
}
else
{
fnd = 0;
break;
}
}
else if(bundleCellsNest2RestrictedPeanoInd[bnest] == -1) //never made ray in first place, so move on
{
fnd = 0;
break;
}
else
{
#ifndef CHECK_GS
fprintf(stderr,"%d: ray buffer regions for grid search are not large enough\n",ThisTask);
MPI_Abort(MPI_COMM_WORLD,999);
#else
fnd = -1;
break;
#endif
}
//prop to right spot in plane
rayprop_gridsearch(&(wgtRays[n]),galRad,wpm1,wpm2);
//para trans to image spot
rttens[0][0] = wgtRays[n].A[2*0+0];
rttens[0][1] = wgtRays[n].A[2*0+1];
rttens[1][0] = wgtRays[n].A[2*1+0];
rttens[1][1] = wgtRays[n].A[2*1+1];
nest2vec(snest,nvec,rayTraceData.rayOrder);
paratrans_tangtensor(rttens,wgtRays[n].n,nvec,ttens);
//para trans to galaxy
paratrans_tangtensor(ttens,nvec,ivec,rttens);
//interp
A[0][0] += rttens[0][0]*wgt[n];
A[0][1] += rttens[0][1]*wgt[n];
A[1][0] += rttens[1][0]*wgt[n];
A[1][1] += rttens[1][1]*wgt[n];
}
return fnd;
}
static void gridsearch_gals(SourceGal *gals, long NumGals, double wpm1, double wpm2, HEALPixRay *bufferRays, long NumBufferRays)
{
int useborn;
#ifdef BORNAPPRX
useborn = 1;
#else
useborn = 0;
#endif
if(useborn)
gridsearch_gals_bornapprx(gals,NumGals,wpm1,wpm2);
else
gridsearch_gals_nobornapprx(gals,NumGals,wpm1,wpm2,bufferRays,NumBufferRays);
}
static void gridsearch_gals_bornapprx(SourceGal *gals, long NumGals, double wpm1, double wpm2)
{
long i;
long NumImageGalsGlobalAlloc;
ImageGal *tmpImageGal;
double vec[3];
double galRad;
long fnd;
double ivec[3],ra,dec;
double ttens_interp[2][2],Aradec[2][2];
if(ThisTask == 0)
fprintf(stderr,"doing grid search w/ born apprx.\n");
double timeBCSTestInterp;
timeBCSTestInterp = 0.0;
if(NumGals > 0)
{
//alloc image gals
NumImageGalsGlobalAlloc = NumGals;
ImageGalsGlobal = (ImageGal*)malloc(sizeof(ImageGal)*NumImageGalsGlobalAlloc);
assert(ImageGalsGlobal != NULL);
NumImageGalsGlobal = 0;
//get the images
timeBCSTestInterp -= MPI_Wtime();
for(i=0;i<NumGals;++i)
{
//get gal pos on sky
vec[0] = gals[i].pos[0];
vec[1] = gals[i].pos[1];
vec[2] = gals[i].pos[2];
galRad = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
vec[0] = vec[0]/galRad;
vec[1] = vec[1]/galRad;
vec[2] = vec[2]/galRad;
ivec[0] = vec[0]/galRad;
ivec[1] = vec[1]/galRad;
ivec[2] = vec[2]/galRad;
//do shear interp - if works, then record an image gal
fnd = interp_invmagmat_to_point(ivec,galRad,wpm1,wpm2,ttens_interp);
if(fnd == 1)
{
//rotate gal to ra dec coords
vec2radec(ivec,&ra,&dec);
Aradec[0][0] = ttens_interp[1][1];
Aradec[0][1] = -ttens_interp[1][0];
Aradec[1][0] = -ttens_interp[0][1];
Aradec[1][1] = ttens_interp[0][0];
//add image to list
ImageGalsGlobal[NumImageGalsGlobal].index = gals[i].index;
ImageGalsGlobal[NumImageGalsGlobal].ra = ra;
ImageGalsGlobal[NumImageGalsGlobal].dec = dec;
ImageGalsGlobal[NumImageGalsGlobal].A00 = Aradec[0][0];
ImageGalsGlobal[NumImageGalsGlobal].A01 = Aradec[0][1];
ImageGalsGlobal[NumImageGalsGlobal].A10 = Aradec[1][0];
ImageGalsGlobal[NumImageGalsGlobal].A11 = Aradec[1][1];
++NumImageGalsGlobal;
if(NumImageGalsGlobal >= NumImageGalsGlobalAlloc)
{
NumImageGalsGlobalAlloc += 10;
tmpImageGal = (ImageGal*)realloc(ImageGalsGlobal,sizeof(ImageGal)*NumImageGalsGlobalAlloc);
assert(tmpImageGal != NULL);
ImageGalsGlobal = tmpImageGal;
}
}
}//for(i=0;i<NumGals;++i)
timeBCSTestInterp += MPI_Wtime();
//realloc image gals if needed
if(NumImageGalsGlobal < NumImageGalsGlobalAlloc && NumImageGalsGlobal > 0)
{
tmpImageGal = (ImageGal*)realloc(ImageGalsGlobal,sizeof(ImageGal)*NumImageGalsGlobal);
assert(tmpImageGal != NULL);
ImageGalsGlobal = tmpImageGal;
}
else if(NumImageGalsGlobal == 0)
{
free(ImageGalsGlobal);
ImageGalsGlobal = NULL;
}
}
else
{
NumImageGalsGlobal = 0;
ImageGalsGlobal = NULL;
}
if(ThisTask == 0)
fprintf(stderr,"galaxy image interp took %lg seconds.\n",timeBCSTestInterp);
}
static void gridsearch_gals_nobornapprx(SourceGal *gals, long NumGals, double wpm1, double wpm2, HEALPixRay *bufferRays, long NumBufferRays)
{
long i,j,k,n;
long NumImageGalsGlobalAlloc;
ImageGal *tmpImageGal;
long ring;
double rvec[3],vec[3],tvec[3],pvec[3],npvec;
HEALPixTreeData *tdvec[2];
long tdInd;
HEALPixRay *raysVec[2];
NNbrData *NNbrs;
long maxNumNNbrs,NumNNbrs;
double galRad;
long Ntri,tri[4][3];
HEALPixRay triRays[3],tmpRay;
long fnd;
double area,bcs[3],galPos[2],triPos[3][2],triPosCurr[3][2],x,y;
double ivec[3],ra,dec;
double ttens_interp[2][2],Aradec[2][2];
double cosangCurr[3];
long snest,bnest,bundleRayShift,roffset;
bundleRayShift = 2*(rayTraceData.rayOrder - rayTraceData.bundleOrder);
HEALPixRay keyHEALPixRay,*fndHEALPixRay;
#ifdef CHECK_GS
long inval;
FILE *fp;
char fname[MAX_FILENAME];
sprintf(fname,"%s/gridsearchinfo_ind%d.txt",rayTraceData.OutputPath,CHECK_GS_IND);
#endif
if(ThisTask == 0)
fprintf(stderr,"doing grid search.\n");
double timeTreeBuild,timeTreeSearch,timeBCSTestInterp;
timeTreeBuild = 0.0;
timeTreeSearch = 0.0;
timeBCSTestInterp = 0.0;
if(NumGals > 0)
{
//build a tree for each bundle cell
timeTreeBuild -= MPI_Wtime();
tdvec[0] = buildHEALPixTree(NumAllRaysGlobal,AllRaysGlobal);
raysVec[0] = AllRaysGlobal;
tdvec[1] = buildHEALPixTree(NumBufferRays,bufferRays);
raysVec[1] = bufferRays;
timeTreeBuild += MPI_Wtime();
NumImageGalsGlobalAlloc = NumGals;
ImageGalsGlobal = (ImageGal*)malloc(sizeof(ImageGal)*NumImageGalsGlobalAlloc);
assert(ImageGalsGlobal != NULL);
NumImageGalsGlobal = 0;
NNbrs = NULL;
maxNumNNbrs = 0;
galPos[0] = 0.0;
galPos[1] = 0.0;
for(i=0;i<NumGals;++i)
{
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
fp = fopen(fname,"w");
assert(fp != NULL);
}
#endif
//get gal pos on sky and init basis vecs
timeBCSTestInterp -= MPI_Wtime();
vec[0] = gals[i].pos[0];
vec[1] = gals[i].pos[1];
vec[2] = gals[i].pos[2];
galRad = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
vec[0] = vec[0]/galRad;
vec[1] = vec[1]/galRad;
vec[2] = vec[2]/galRad;
npvec = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
pvec[0] = -vec[1]/npvec;
pvec[1] = vec[0]/npvec;
pvec[2] = 0.0;
tvec[0] = vec[2]*vec[0]/npvec;
tvec[1] = vec[2]*vec[1]/npvec;
tvec[2] = -1.0*(vec[0]*vec[0] + vec[1]*vec[1])/npvec;
timeBCSTestInterp += MPI_Wtime();
for(tdInd=0;tdInd<2;++tdInd)
{
//find all rays near it
timeTreeSearch -= MPI_Wtime();
NumNNbrs = nnbrsHEALPixTree(vec,rayTraceData.galImageSearchRad,wpm1,raysVec[tdInd],tdvec[tdInd],&NNbrs,&maxNumNNbrs);
timeTreeSearch += MPI_Wtime();
timeBCSTestInterp -= MPI_Wtime();
#ifdef DEBUG
#if DEBUG_LEVEL > 2
fprintf(stderr,"%05d: %ld of %ld, # of nbrs = %ld, pos = %f|%f|%f, theta,phi = %e|%e, x,y = %f|%f, phi,theta vec norm = %lg|%lg\n",
ThisTask,i,NumGals,NumNNbrs,vec[0],vec[1],vec[2],theta,phi,galPos[0],galPos[1],
sqrt(pvec[0]*pvec[0]+pvec[1]*pvec[1]+pvec[2]*pvec[2]),
sqrt(tvec[0]*tvec[0]+tvec[1]*tvec[1]+tvec[2]*tvec[2]));
#endif
#endif
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, # of nbrs = %ld, pos = %f|%f|%f, x,y = %f|%f, phi,theta vec norm = %lg|%lg, galRad = %lg\n",
ThisTask,i,NumGals,NumNNbrs,vec[0],vec[1],vec[2],galPos[0],galPos[1],
sqrt(pvec[0]*pvec[0]+pvec[1]*pvec[1]+pvec[2]*pvec[2]),
sqrt(tvec[0]*tvec[0]+tvec[1]*tvec[1]+tvec[2]*tvec[2]),galRad);
#endif
//search around each gal
for(j=0;j<NumNNbrs;++j)
{
ring = nest2ring(raysVec[tdInd][NNbrs[j].ind].nest,rayTraceData.rayOrder);
Ntri = ring2triangle(ring,tri,rayTraceData.rayOrder);
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, %ld triangles\n",
ThisTask,i,NumGals,j,NumNNbrs,Ntri);
#endif
if(Ntri > 0)
{
assert(tri[0][0] == ring);
triRays[0] = raysVec[tdInd][NNbrs[j].ind]; //makes a copy of the ray via a structure assignemnt
//propagate the ray to the galaxy's comoving location
rayprop_gridsearch(&(triRays[0]),galRad,wpm1,wpm2);
//get ray's projected loc near galaxy
cosangCurr[0] = (triRays[0].n[0]*vec[0] + triRays[0].n[1]*vec[1] + triRays[0].n[2]*vec[2])/galRad;
triPosCurr[0][0] = (triRays[0].n[0]*tvec[0] + triRays[0].n[1]*tvec[1] + triRays[0].n[2]*tvec[2])/galRad/cosangCurr[0];
triPosCurr[0][1] = (triRays[0].n[0]*pvec[0] + triRays[0].n[1]*pvec[1] + triRays[0].n[2]*pvec[2])/galRad/cosangCurr[0];
//get ray's starting location in same coords
nest2vec(triRays[0].nest,rvec,rayTraceData.rayOrder);
triPos[0][0] = rvec[0]*tvec[0] + rvec[1]*tvec[1] + rvec[2]*tvec[2];
triPos[0][1] = rvec[0]*pvec[0] + rvec[1]*pvec[1] + rvec[2]*pvec[2];
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld (index = %ld), nbr %ld of %ld, base ray start flat pos = %lg|%lg, base ray img flat pos = %lg|%lg, ray norm = %lg\n",
ThisTask,i,NumGals,gals[i].index,j,NumNNbrs,triPos[0][0],triPos[0][1],triPosCurr[0][0],triPosCurr[0][1],
sqrt(triRays[0].n[0]*triRays[0].n[0] + triRays[0].n[1]*triRays[0].n[1] + triRays[0].n[2]*triRays[0].n[2]));
#endif
}
for(k=0;k<Ntri;++k)
{
assert(tri[k][0] == ring);
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
vec2ang(vec,&theta,&phi);
fprintf(fp,"%.20le %.20le 0.0 0.0 ",theta,phi);
vec2ang(triRays[0].n,&theta,&phi);
fprintf(fp,"%.20le %.20le %.20le %.20le ",theta,phi,triPosCurr[0][0],triPosCurr[0][1]);
}
#endif
//find the rays for the triangle
for(n=1;n<3;++n)
{
snest = ring2nest(tri[k][n],rayTraceData.rayOrder);
bnest = snest >> bundleRayShift;
if(ISSETBITFLAG(bundleCells[bnest].active,PRIMARY_BUNDLECELL) && bundleCells[bnest].Nrays > 0)
{
fnd = 1;
roffset = snest - (bnest << bundleRayShift);
triRays[n] = bundleCells[bnest].rays[roffset]; //makes a copy of the ray via a structure assignemnt
assert(triRays[n].nest == snest);
}
else if(ISSETBITFLAG(bundleCells[bnest].active,RAYBUFF_BUNDLECELL) && bundleCells[bnest].Nrays > 0)
{
keyHEALPixRay.nest = snest;
fndHEALPixRay = (HEALPixRay*)bsearch(&keyHEALPixRay,bundleCells[bnest].rays,(size_t) (bundleCells[bnest].Nrays),sizeof(HEALPixRay),compHEALPixRayNest);
if(fndHEALPixRay != NULL)
{
fnd = 1;
triRays[n] = *fndHEALPixRay; //makes a copy of the ray via a structure assignemnt
assert(triRays[n].nest == snest);
}
else
{
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, did not find vert %ld buffer ray search\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n);
#endif
fnd = 0;
break;
}
}
else
{
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, did not find vert %ld\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n);
#endif
fnd = 0;
break;
}
//propagate the ray to the galaxy's comoving location
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
vec2ang(triRays[n].n,&theta,&phi);
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, sphere pos before prop %ld = %lg|%lg, norm = %lg\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n,theta,phi,
sqrt(triRays[n].n[0]*triRays[n].n[0] + triRays[n].n[1]*triRays[n].n[1] + triRays[n].n[2]*triRays[n].n[2]));
}
#endif
rayprop_gridsearch(&(triRays[n]),galRad,wpm1,wpm2);
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
vec2ang(triRays[n].n,&theta,&phi);
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, sphere pos after prop %ld = %lg|%lg, norm = %lg\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n,theta,phi,
sqrt(triRays[n].n[0]*triRays[n].n[0] + triRays[n].n[1]*triRays[n].n[1] + triRays[n].n[2]*triRays[n].n[2]));
}
#endif
//get ray's projected loc near galaxy
cosangCurr[n] = (triRays[n].n[0]*vec[0] + triRays[n].n[1]*vec[1] + triRays[n].n[2]*vec[2])/galRad;
triPosCurr[n][0] = (triRays[n].n[0]*tvec[0] + triRays[n].n[1]*tvec[1] + triRays[n].n[2]*tvec[2])/galRad/cosangCurr[n];
triPosCurr[n][1] = (triRays[n].n[0]*pvec[0] + triRays[n].n[1]*pvec[1] + triRays[n].n[2]*pvec[2])/galRad/cosangCurr[n];
//get ray's starting location in same coords
nest2vec(triRays[n].nest,rvec,rayTraceData.rayOrder);
triPos[n][0] = rvec[0]*tvec[0] + rvec[1]*tvec[1] + rvec[2]*tvec[2];
triPos[n][1] = rvec[0]*pvec[0] + rvec[1]*pvec[1] + rvec[2]*pvec[2];
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, start flat pos %ld = %lg|%lg, img flat pos = %lg|%lg\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n,triPos[n][0],triPos[n][1],triPosCurr[n][0],triPosCurr[n][1]);
#endif
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
vec2ang(triRays[n].n,&theta,&phi);
fprintf(fp,"%.20le %.20le %.20le %.20le ",theta,phi,triPosCurr[n][0],triPosCurr[n][1]);
if(n == 2)
fprintf(fp,"\n");
}
#endif
}
//this catches the case if we do not have all the rays in the triangle
if(fnd == 0)
continue;
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, found all verts\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri);
#endif
//test if the triangle is oriented properly - it needs to be in counter-clockwise order
// if area is zero, continue
area = trisarea(triPosCurr[0],triPosCurr[1],triPosCurr[2]);
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, area = %e\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,area);
#endif
if(area == 0.0) //triangle has zero area - cannot get barycoords and do interpolation so skip
{
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, area = 0.0\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri);
#endif
continue;
}
else if(area < 0) //in clockwise order, swap last two rays to get to counter-clockwise order - needed for interpolation below
{
tmpRay = triRays[2];
triRays[2] = triRays[1];
triRays[1] = tmpRay;
x = triPos[2][0];
y = triPos[2][1];
triPos[2][0] = triPos[1][0];
triPos[2][1] = triPos[1][1];
triPos[1][0] = x;
triPos[1][1] = y;
x = triPosCurr[2][0];
y = triPosCurr[2][1];
triPosCurr[2][0] = triPosCurr[1][0];
triPosCurr[2][1] = triPosCurr[1][1];
triPosCurr[1][0] = x;
triPosCurr[1][1] = y;
x = cosangCurr[2];
cosangCurr[2] = cosangCurr[1];
cosangCurr[1] = x;
}
//make sure in counter-clockwise order
if(!(trisarea(triPosCurr[0],triPosCurr[1],triPosCurr[2]) > 0))
{
fprintf(stderr,"%05d: gal %ld of %ld, index = %ld, area = %lg\n",
ThisTask,i,NumGals,gals[i].index,area);
assert(trisarea(triPosCurr[0],triPosCurr[1],triPosCurr[2]) > 0);
}
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
{
inval = tritest_getbarycoords(triPosCurr[0],triPosCurr[1],triPosCurr[2],galPos,bcs);
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, testing triangle, bcs = %lg|%lg|%lg, interp = %ld\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,bcs[0],bcs[1],bcs[2],inval);
}
#endif
//now see if the galaxy is in the triangle - if it is, then record the image
if(tritest_getbarycoords(triPosCurr[0],triPosCurr[1],triPosCurr[2],galPos,bcs))
{
//finally found an image! woot woot!
//get final barycoords
bcs[0] *= cosangCurr[0];
bcs[1] *= cosangCurr[1];
bcs[2] *= cosangCurr[2];
//get gal pos
x = triPos[0][0]*bcs[0];
y = triPos[0][1]*bcs[0];
for(n=1;n<3;++n)
{
x += triPos[n][0]*bcs[n];
y += triPos[n][1]*bcs[n];
}
ivec[0] = vec[0] + x*tvec[0] + y*pvec[0];
ivec[1] = vec[1] + x*tvec[1] + y*pvec[1];
ivec[2] = vec[2] + x*tvec[2] + y*pvec[2];
//get gal shear matrix
/// NOT using this code
//ttens_interp[0][0] = 0.0;
//ttens_interp[0][1] = 0.0;
//ttens_interp[1][0] = 0.0;
//ttens_interp[1][1] = 0.0;
//for(n=0;n<3;++n)
//{
//para trans to image spot
// rttens[0][0] = triRays[n].A[2*0+0];
// rttens[0][1] = triRays[n].A[2*0+1];
// rttens[1][0] = triRays[n].A[2*1+0];
// rttens[1][1] = triRays[n].A[2*1+1];
// nest2vec(triRays[n].nest,nvec,rayTraceData.rayOrder);
// paratrans_tangtensor(rttens,triRays[n].n,nvec,ttens);
//para trans to galaxy
// paratrans_tangtensor(ttens,nvec,ivec,rttens);
//interp
// ttens_interp[0][0] += rttens[0][0]*bcs[n]/cosangCurr[n];
// ttens_interp[0][1] += rttens[0][1]*bcs[n]/cosangCurr[n];
// ttens_interp[1][0] += rttens[1][0]*bcs[n]/cosangCurr[n];
// ttens_interp[1][1] += rttens[1][1]*bcs[n]/cosangCurr[n];
//}
fnd = interp_invmagmat_to_point(ivec,galRad,wpm1,wpm2,ttens_interp);
if(fnd == 1)
{
//rotate gal to ra dec coords
vec2radec(ivec,&ra,&dec);
Aradec[0][0] = ttens_interp[1][1];
Aradec[0][1] = -ttens_interp[1][0];
Aradec[1][0] = -ttens_interp[0][1];
Aradec[1][1] = ttens_interp[0][0];
//add image to list
ImageGalsGlobal[NumImageGalsGlobal].index = gals[i].index;
ImageGalsGlobal[NumImageGalsGlobal].ra = ra;
ImageGalsGlobal[NumImageGalsGlobal].dec = dec;
ImageGalsGlobal[NumImageGalsGlobal].A00 = Aradec[0][0];
ImageGalsGlobal[NumImageGalsGlobal].A01 = Aradec[0][1];
ImageGalsGlobal[NumImageGalsGlobal].A10 = Aradec[1][0];
ImageGalsGlobal[NumImageGalsGlobal].A11 = Aradec[1][1];
++NumImageGalsGlobal;
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, found an image! vec = %lg|%lg|%lg, ivec = %lg|%lg|%lg\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,
vec[0],vec[1],vec[2],ivec[0],ivec[1],ivec[2]);
#endif
if(NumImageGalsGlobal >= NumImageGalsGlobalAlloc)
{
NumImageGalsGlobalAlloc += 10;
tmpImageGal = (ImageGal*)realloc(ImageGalsGlobal,sizeof(ImageGal)*NumImageGalsGlobalAlloc);
assert(tmpImageGal != NULL);
ImageGalsGlobal = tmpImageGal;
}
}
#ifdef CHECK_GS
else if(fnd == -1)
{
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"%05d: gal %ld of %ld, nbr %ld of %ld, tri %ld of %ld, did not find wgt ray %ld\n",
ThisTask,i,NumGals,j,NumNNbrs,k,Ntri,n);
fprintf(stderr,"%d: ray buffer regions for grid search are not large enough\n",ThisTask);
MPI_Abort(MPI_COMM_WORLD,999);
}
#endif
}//if(tritest_getbarycoords(triPosCurr[0],triPosCurr[1],triPosCurr[2],galPos,bcs))
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fprintf(stderr,"\n");
#endif
}//for(k=0;k<Ntri;++k)
}//for(j=0;j<NumNNbrs;++j)
timeBCSTestInterp += MPI_Wtime();
}//for(tdInd=0;tdInd<2;++tdInd)
#ifdef CHECK_GS
if(CHECK_GS_IND == gals[i].index)
fclose(fp);
#endif
}//for(i=0;i<NumGals;++i)
if(NumImageGalsGlobal < NumImageGalsGlobalAlloc && NumImageGalsGlobal > 0)
{
tmpImageGal = (ImageGal*)realloc(ImageGalsGlobal,sizeof(ImageGal)*NumImageGalsGlobal);
assert(tmpImageGal != NULL);
ImageGalsGlobal = tmpImageGal;
}
else if(NumImageGalsGlobal == 0)
{
free(ImageGalsGlobal);
ImageGalsGlobal = NULL;
}
if(maxNumNNbrs > 0)
free(NNbrs);
destroyHEALPixTree(tdvec[0]);
destroyHEALPixTree(tdvec[1]);
}
else
{
NumImageGalsGlobal = 0;
ImageGalsGlobal = NULL;
}
if(ThisTask == 0)
fprintf(stderr,"tree build+search took %lg seconds.\ngalaxy image interp took %lg seconds.\n",
timeTreeBuild+timeTreeSearch,timeBCSTestInterp);
/* //print out some profiling info
double minTime,maxTime,totTime,avgTime;
MPI_Reduce(&timeTreeBuild,&minTime,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD);
MPI_Reduce(&timeTreeBuild,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Reduce(&timeTreeBuild,&totTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
avgTime = totTime/((double) NTasks);
#ifdef DEBUG
if(ThisTask == 0)
fprintf(stderr,"tree build time max,min,avg = %lf|%lf|%lf - %.2f (total time %lf)\n",
maxTime,minTime,avgTime,(maxTime-avgTime)/avgTime*100.0,totTime);
#endif
MPI_Reduce(&timeTreeSearch,&minTime,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD);
MPI_Reduce(&timeTreeSearch,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Reduce(&timeTreeSearch,&totTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
avgTime = totTime/((double) NTasks);
#ifdef DEBUG
if(ThisTask == 0)
fprintf(stderr,"tree search time max,min,avg = %lf|%lf|%lf - %.2f (total time %lf)\n",
maxTime,minTime,avgTime,(maxTime-avgTime)/avgTime*100.0,totTime);
#endif
MPI_Reduce(&timeBCSTestInterp,&minTime,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD);
MPI_Reduce(&timeBCSTestInterp,&maxTime,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
MPI_Reduce(&timeBCSTestInterp,&totTime,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
avgTime = totTime/((double) NTasks);
#ifdef DEBUG
if(ThisTask == 0)
fprintf(stderr,"BCs test interp time max,min,avg = %lf|%lf|%lf - %.2f (total time %lf)\n",
maxTime,minTime,avgTime,(maxTime-avgTime)/avgTime*100.0,totTime);
#endif
*/
}
#undef CHECK_GS
#undef CHECK_GS_IND
//FIXME - need to test routine to distribute gals to tasks
static SourceGal *distribute_gals_to_nodes(long NumGalsForThisPlane, long start, long *NumGals)
{
MPI_Status Stat;
SourceGal *galsForThisPlane,*buffGals,*tmpSourceGal;
long NumGalsAlloc,NumBuffGals;
long i,Nsend,Nrecv,nest,NumGalsToSend,TotNumGalsToSend,round;
double vec[3];
long log2NTasks;
long level,sendTask,recvTask;
if(ThisTask == 0)
fprintf(stderr,"sending gals to correct tasks.\n");
log2NTasks = 0;
while(NTasks > (1 << log2NTasks))
++log2NTasks;
NumGalsAlloc = NumGalsForThisPlane;
if(NumGalsAlloc == 0)
NumGalsAlloc = 10000;
galsForThisPlane = (SourceGal*)malloc(sizeof(SourceGal)*NumGalsAlloc);
assert(galsForThisPlane != NULL);
*NumGals = 0;
NumBuffGals = 50.0*1024l*1024l/sizeof(SourceGal);
buffGals = (SourceGal*)malloc(sizeof(SourceGal)*NumBuffGals);
assert(buffGals != NULL);
//get total # of gals to send - move gals to keep to other vector
NumGalsToSend = 0;
for(i=0;i<NumGalsForThisPlane;++i)
{
vec[0] = SourceGalsGlobal[start+i].pos[0];
vec[1] = SourceGalsGlobal[start+i].pos[1];
vec[2] = SourceGalsGlobal[start+i].pos[2];
nest = vec2nest(vec,rayTraceData.bundleOrder);
if(!(ISSETBITFLAG(bundleCells[nest].active,PRIMARY_BUNDLECELL)))
++NumGalsToSend;
else
{
if((*NumGals) == NumGalsAlloc)
{
if(NumGalsForThisPlane*0.1 < 10000)
NumGalsAlloc += 10000;
else
NumGalsAlloc += NumGalsForThisPlane*0.1;
tmpSourceGal = (SourceGal*)realloc(galsForThisPlane,sizeof(SourceGal)*NumGalsAlloc);
assert(tmpSourceGal != NULL);
galsForThisPlane = tmpSourceGal;
}
galsForThisPlane[*NumGals] = SourceGalsGlobal[start+i];
++(*NumGals);
SourceGalsGlobal[start+i] = SourceGalsGlobal[start+NumGalsForThisPlane-1];
--i;
--NumGalsForThisPlane;
}
}
MPI_Allreduce(&NumGalsToSend,&TotNumGalsToSend,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
//realloc global gals vector down to size
if((*NumGals) > 0)
{
NumSourceGalsGlobal -= (*NumGals);
if(NumSourceGalsGlobal > 0)
{
tmpSourceGal = (SourceGal*)realloc(SourceGalsGlobal,sizeof(SourceGal)*NumSourceGalsGlobal);
assert(tmpSourceGal != NULL);
SourceGalsGlobal = tmpSourceGal;
}
else
{