-
Notifications
You must be signed in to change notification settings - Fork 11
/
spin_alm_tools.f90
7938 lines (6651 loc) · 289 KB
/
spin_alm_tools.f90
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
!mapping gradients of scalars and the exact and approx weak lensed CMB
!Antony Lewis 2004-2014
!Requires linking to Healpix libraries:
!See http://www.eso.org/science/healpix/
!Sign conventions follow Healpix/CMBFAST/CAMB
!Most easily used using HealpixObj.f90 wrapper routines
!Compile with -DMPIPIX -to use MPI
!Performance could be improved by changing the theta-CPU sharing
!However scaling is quite good up to 50 or so processors for high res transforms
!Temporary arrays use more memory than non-MPI routines
!For compatibility with Healpix input/output alm arrays are not packed (2*waste of mem)
!Jan 2005: improved/fixed polarization lens rotation factors. Minor fixes.
!Sept 2005: fixed bug in map2polalm
!Nov 2007: added bicubic interpolation, temp only, speedups
!Dec 2007: multiple map transforms, reduced memory requirements
!Jan 2008: further memory reductions for non-lensed; one MPI thread workarounds for scalar
!Oct 2010: corrected approximate handling of pole region interpolation (tiny area, virtually no effect)
!Nov 2010: fixes for bugs that only showed up in gfortran (thanks to Giancarlo de Gasperis)
!Apr 2011: Fixed wrap-around of phi during interp lensing
!Jul 2011: Changes for high pix number (consistent with healpix 2.2)
!Apr 2014: More use of MPI_IN_PLACE for latest MPI version compatibility
module MPIstuff
implicit none
double precision starttime
#ifdef MPIPIX
include "mpif.h"
integer :: DebugMsgs =1
integer MPIstatus(MPI_STATUS_SIZE), ierr
integer SP_MPI,CSP_MPI
#endif
contains
subroutine MpiBarrier
integer i
#ifdef MPIPIX
call MPI_BARRIER(MPI_COMM_WORLD,i)
#endif
end subroutine MpiBarrier
subroutine GetMpiStat(MpiId, MpiSize)
implicit none
integer MpiId,MpiSize
#ifdef MPIPIX
integer ierror
call mpi_comm_rank(mpi_comm_world,MpiId,ierror)
if (ierror/=MPI_SUCCESS) stop 'GetMpiDetail: MPI rank'
call mpi_comm_size(mpi_comm_world,MpiSize,ierror)
SP_MPI = MPI_REAL
CSP_MPI= MPI_COMPLEX
#else
MpiId=0
MpiSize=1
#endif
end subroutine GetMpiStat
subroutine SyncInts(i,j,k)
integer, intent(inout) :: i
integer, intent(inout), optional :: j,k
#ifdef MPIPIX
integer params(3),sz
params(1)=i
sz=1
if (present(j)) then
params(2)=j
sz=2
end if
if (present(k)) then
params(3)=k
sz=3
end if
call MPI_BCAST(params,sz,MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)
i= params(1)
if (present(j)) then
j=params(2)
end if
if (present(k)) then
k=params(3)
end if
#endif
end subroutine SyncInts
subroutine SyncReals(i,j,k)
real, intent(inout) :: i
real, intent(inout), optional :: j,k
#ifdef MPIPIX
real params(3)
integer sz
params(1)=i
sz=1
if (present(j)) then
params(2)=j
sz=2
end if
if (present(k)) then
params(3)=k
sz=3
end if
call MPI_BCAST(params,sz,MPI_REAL, 0, MPI_COMM_WORLD, ierr)
i= params(1)
if (present(j)) then
j=params(2)
end if
if (present(k)) then
k=params(3)
end if
#endif
end subroutine SyncReals
end module MPIstuff
module spinalm_tools
use utilities, only: die_alloc
use healpix_types
use healpix_fft, only : real_fft
use pix_tools, ONLY : nside2npix
IMPLICIT none
#ifdef HEALPIXI4B
integer, parameter :: I_NPIX = I4B !I4B in older versions
#else
integer, parameter :: I_NPIX = I8B !I4B in older versions
#endif
Type HealpixInfo
integer :: nside, lmax, Lastlmax
integer(I_NPIX) :: npix
logical pol
REAL(KIND=DP), dimension(:,:), Pointer :: w8ring_TQU => NULL()
INTEGER(I8B), dimension(:), pointer :: istart_south => NULL() , istart_north => NULL()
COMPLEX(DPC),dimension(:), pointer :: trig => NULL()
REAL(DP), dimension(:), Pointer :: recfac => NULL() , Lambda_slm => NULL()
integer MpiId, MPISize, MpiStat, last_nph
integer(I4B), dimension(:), pointer :: ith_start => NULL() , ith_end => NULL()
integer, dimension(:), pointer :: North_Start => NULL() , North_Size => NULL() , &
South_Start => NULL() , South_Size => NULL() !MPI uses integer type, can't use I8B here
end type HealpixInfo
Type HealpixMapArray
REAL(SP), dimension(:,:), pointer :: M => NULL()
end Type HealpixMapArray
type HealpixAllCl
!All (a^i a^j) C_l
!Index 0:lmax, i, j, where i,j are T E B
real(SP), dimension(:,:,:), pointer :: Cl => NULL()
end type HealpixAllCl
type HealpixCrossPowers
!Array of cross-power spectra
integer nmaps, lmax, npol
Type(HealpixAllCl), dimension(:,:), pointer :: Ps => NULL()
end type HealpixCrossPowers
type HealpixPackedScalAlms
COMPLEX(SPC), dimension(:,:), pointer :: alms => NULL()
end type HealpixPackedScalAlms
type HealpixPackedAlms
COMPLEX(SPC), dimension(:,:,:), pointer :: alms => NULL()
end type HealpixPackedAlms
Type LensGradients
COMPLEX(SPC), dimension(:), pointer :: grad_phiN => NULL() , grad_phiS => NULL()
end Type LensGradients
integer, parameter :: interp_edge = 2
!number of high-res pixels to go outside deflected region to get good interpolation
integer, parameter :: EB_sign = -1
!definition: for pol {}_2 a_{lm} = EB_sign*(E + iB)_{lm}
!EB_sign = -1 corresponds to Healpix and CAMB/CMBFAST conventions
logical :: mmax_approx = .true.
!when true, uses that fact that don't need high m near the poles because the legendre
!functions are tiny for m >> l sin(theta)
integer, parameter :: interp_basic=0, interp_cyl = 1
integer, parameter :: division_equalrows=1, division_equalpix=2, division_balanced =3
! keep everything private unless stated otherwise
private
! define large and small numbers used to renormalise the recursion on the Legendre Polynomials
real(KIND=DP), private, PARAMETER :: FL_LARGE = 1.0e30_dp
real(KIND=DP), private, PARAMETER :: FL_SMALL = 1.0e-30_dp
real(KIND=DP), private :: OVFLOW, UNFLOW, ScaleFactors(-10:10)
! make (front end) routines public
public :: spinalm2map, alm2GradientMap, map2spinalm,scalalm2map, mmax_approx, HealpixInfo, &
HealpixInit,HealpixFree, map2scalalm, a_ix, scalalm2LensedMap, &
alm2Lensedmap, map2polalm, polalm2map, alm2LensedQuadContrib, EB_sign, &
alm2LensedmapInterp, scalalm2LensedmapInterp,scalalm2LensedmapInterpCyl, &
alm2LensedmapInterpCyl, interp_basic, interp_cyl , GeteTime, &
division_equalrows, division_equalpix, division_balanced, HealpixMapArray, &
HealpixCrossPowers, HealpixAllCl, maparray2scalcrosspowers,maparray2crosspowers, &
HealpixCrossPowers_Free, healpix_wakeMPI, healpix_sleepMPI, scalalm2bispectrum, &
I_NPIX, HealpixPackedScalAlms, HealpixPackedAlms, Alm2PackAlmFiltered, &
PackAlm2AlmFiltered, lmax2nalms, maparray2packedscalalms, packedscalalms2maparray
contains
function GeteTime()
use MPIStuff
double precision GeteTime
#ifndef MPIPIX
real etime
call cpu_time(etime)
GeteTime = etime
#else
GeteTime = MPI_WTime()
#endif
end function GeteTime
function a_ix(lmax, l, m) result(index)
integer, intent(in) :: lmax, l, m
integer(I_NPIX) :: index
index = (m*(2*lmax-m+1))/2 + l + 1
end function a_ix
subroutine HealpixInit(H, nside, lmax, HasPol, w8dir, method)
USE fitstools, ONLY : getsize_fits, input_map
use MPIStuff
use healpix_types
Type (HealpixInfo) :: H
Integer, optional, intent(in) :: method
logical, intent(in), optional :: HasPol
character(LEN=*), optional, intent(in) :: w8dir
logical use_weights
integer, intent(in) :: nside, lmax
!real(dp) logOVFLOW
integer npol, n_rings
character(LEN=120) :: sstr, filename
REAL(SP), dimension(:,:), allocatable :: w8
integer nph, i, delta, st
! Changed for new division between threads
Real(sp) :: mean_pix !Mean number of pixels in each section of northern hemisphere
real(sp) :: pix_w, mean_weight,time_weights(2*nside)
Integer :: pixels, row
Integer :: division = 1
!Determines whether to give each section
! equal numbers of rows (1), or equal numbers of pixels (2)
! (2) is much faster for 'exact' lensing
#ifdef MPIPIX
integer status, ierror
#endif
CHARACTER(LEN=*), PARAMETER :: code = 'HealpixInit'
#ifndef MPIPIX
call HealpixFree(H)
!If MPI must call healpixFree manually
#endif
nullify(H%recfac,H%Lambda_slm)
call HealpixInitTrig(H,nside,lmax)
npol = 1
if (present(HasPol)) then
npol = 3
end if
H%pol = HasPol
allocate(H%w8ring_TQU(1:2*nside,1:max(1,npol)))
use_weights = present(w8dir)
if (use_weights) then
use_weights = w8dir/=''
end if
if (use_weights) then
allocate(w8(1:2*nside,1:max(1,npol)))
write (sstr,"(I5.5)") nside
filename= trim(w8dir)//"weight_ring_n"//trim(sstr)//".fits"
n_rings = 2 * nside
if (getsize_fits(filename) /= n_rings) then
write (*,*) 'HealpixInit:wrong file'//trim(filename)
stop
endif
if (HasPol) then
call input_map(filename, w8, n_rings, 3, fmissval=0.0_sp)
else
call input_map(filename, w8, n_rings, 1, fmissval=0.0_sp)
endif
H%w8ring_TQU = 1 + w8
deallocate(w8)
else
H%w8ring_TQU=1
endif
!Get factors for making well behaved Ylm
OVFLOW=exp(log(FL_LARGE))
UNFLOW=exp(log(FL_SMALL))
! logOVFLOW=log(FL_LARGE)
ScaleFactors=0
do i=-10,10
ScaleFactors(i) = FL_LARGE**i !exp(i*logOVFLOW)
end do
!Mpi properties
H%MpiId = 0; H%MpiSize = 1
H%MpiStat = 0
#ifdef MPIPIX
if (SP==KIND(1.d0)) then
SP_MPI = MPI_DOUBLE_PRECISION
CSP_MPI = MPI_DOUBLE_COMPLEX
else if (SP == KIND(1.)) then
SP_MPI = MPI_REAL
CSP_MPI= MPI_COMPLEX
else
stop 'Unknown SP KIND for MPI'
end if
call mpi_comm_size(mpi_comm_world,H%MpiSize,ierror)
call mpi_comm_rank(mpi_comm_world,H%MpiId,ierror)
if (ierror/=MPI_SUCCESS) stop 'HealpixInit: MPI rank'
#endif
!Sectioning of the sphere between threads
!Following things to bear in mind:
! * range of l needed smaller near poles
! * healpix has 4*nside pix per ring for i>nside, but linear with i for i<=nside
! * - this means some rings have inefficient FFT at i<nside where i is still large
! * map2alm and alm2map are naively ~ proportional only to number of rings if FFT efficient
! * Lensing interpolation time is roughly proportional to number of pixels
If (present(method)) division = method
#ifdef MPIPIX
if (DebugMsgs >1 .and. H%MpiId==0) print *,'mpi_division = ', division
if (H%MpiSIze==1) division = division_equalrows
#else
division = division_equalrows
#endif
! If (division == division_balanced) Then
! st = (2*nside)/(3*H%MpiSize) !Put more in poles for balanced
! delta = (2*nside - st)/H%MpiSize
! st = 1 + st + mod(2*nside-st,H%MpiSize)
! else
delta = (2*nside)/H%MpiSize
st = 1 + mod(2*nside,H%MpiSize)
! end if
allocate(H%ith_start(0:H%MpiSIze-1), H%ith_end(0:H%MpiSIze-1), H%North_Start(0:H%MpiSIze-1), &
H%North_Size(0:H%MpiSIze-1), H%South_Start(0:H%MpiSIze-1), H%South_Size(0:H%MpiSIze-1))
H%ith_start = 1
H%ith_end = 2*nside
if ( division == division_balanced) then
do i=1, nside*2
!for healpix transform timing grows approximately linearly to nside, then drops and stays ~constant
if (i< nside) then
time_weights(i) = 0.7 + i*24./nside !
else
time_weights(i) = 16 !+ real(2*(i-nside))/nside
end if
! time_weights(i) = 8 + nside*(sin(i*pi/(4*nside))**0.8 + 0.2) + max(0,(i-nside)/10)
!! if (i> nside/2 .and. i< nside) then
! time_weights(i) = time_weights(i)*1.2
! end if
end do
mean_weight = sum(time_weights)/H%MpiSize
pix_w = 0
else if (division == division_equalpix) then
!Giving less to poles usually a good idea
! if (H%MpiSize<3) then
! first_pix = (3*nside*(6*nside+2)/H%MpiSize)/4
! else
! first_pix = (nside*(6*nside+2)/H%MpiSize)/2
! end if
! mean_pix = (nside*(6*nside+2) - first_pix)/(H%MpiSize-1)
pixels = 0
mean_pix = nside*real(6*nside+2)/H%MpiSize
end if
do i= 0, H%MpiSize -1
If (division == division_equalpix) Then
! New version SJS 15/12/2004 for equal pixels per thread
! mean_pix is the average number of pixels given to each thread
! New method - divide into ~equal numbers of pixels
! Should be significantly faster if using 'exact' lensing method
! very marginaly faster if using interpolation method
! ideally need a third method which gives less to the poles
! if doing interpolation
if (i == 0) then
H%ith_start(i) = 1
else
H%ith_start(i) = H%ith_end(i-1) + 1
end if
row = H%ith_start(i)-1
do while (pixels .LT. (i+1.0)*mean_pix)
row = row + 1
nph = 4*nside
if (row .LT. nside) nph = 4*row
pixels = pixels + nph
end do
H%ith_end(i) = row
If (i == (H%MpiSize-1)) H%ith_end(i) = 2*nside
Else If (division == division_equalrows) Then
!divide into equal numbers of rows
if (i == 0) then
!Do more, but poles are faster anyway
H%ith_start(i) = 1
H%ith_end(i) = st + delta-1
else
H%ith_start(i) = st + i*delta
H%ith_end(i) = H%ith_start(i) + delta -1
end if
Else If (division == division_balanced) Then
!New method Oct 07
if (i == 0) then
H%ith_start(i) = 1
else
H%ith_start(i) = H%ith_end(i-1) + 1
end if
row = H%ith_start(i)-1
do while (pix_w < (i+1)*mean_weight .and. row < 2*nside)
row = row + 1
pix_w = pix_w + time_weights(row)
end do
If (i == (H%MpiSize-1)) row= 2*nside
H%ith_end(i) = row
#ifdef MPIPIX
if (DebugMsgs > 1 .and. H%MpiId==0) write(*,*) i, 'row end = ',row
#endif
Else
Stop 'HealpixInit : Unknown method'
End If
if (H%ith_end(i)< nside) then
nph = 4*H%ith_end(i)
else
nph = 4*nside
endif
H%North_start(i) = H%istart_north(H%ith_start(i)-1)
H%North_Size(i) = H%istart_north(H%ith_end(i)-1) + nph &
-H%North_start(i)
if (H%ith_start(i) < nside) then
nph = 4*H%ith_start(i)
else
nph = 4*nside
endif
if (H%ith_end(i) == nside*2) then
H%South_start(i) = H%istart_south(H%ith_end(i)-1)
else
H%South_start(i) = H%istart_south(H%ith_end(i))
end if
H%South_Size(i) = H%istart_south(H%ith_start(i)) + nph &
- H%South_start(i)
end do
#ifdef MPIPIX
if (H%MpiId>0) call MessageLoop(H)
#endif
end subroutine HealpixInit
function lmax2nalms(lmax)
integer, intent(in) :: lmax
integer(I_NPIX) :: lmax2nalms
lmax2nalms = (int(lmax+1,I_NPIX)*(lmax+2))/2
end function lmax2nalms
subroutine HealpixInitTrig(H, nside, lmax, not_healpix)
use MPIStuff
use healpix_types
logical, intent(in), optional :: not_healpix
logical not_heal
Type (HealpixInfo) :: H
integer, intent(in) :: lmax, nside
integer ith, status, nph,test_mpi_int
CHARACTER(LEN=*), PARAMETER :: code = 'HealpixTrig'
nullify(H%trig)
H%last_nph = -1
H%lmax = lmax
H%Lastlmax = 0
H%nside = nside
!Note nside does not have to be 2^n here, as also used for lensing cylindrical grid
H%npix = 12*int(nside,I8B)**2
test_mpi_int = H%npix
if (H%npix /= test_mpi_int) &
stop 'Large npix would need compilation (and MPI library) with long integers'
not_heal = .false.
if (present(not_healpix)) not_heal = not_healpix
if (not_heal) then
nullify(H%istart_north)
nullify(H%istart_south)
else
ALLOCATE(H%istart_north(0:2*nside),stat = status)
if (status /= 0) call die_alloc(code,'istart_north')
ALLOCATE(H%istart_south(0:2*nside),stat = status)
if (status /= 0) call die_alloc(code,'istart_south')
H%istart_north(0)=0
H%istart_south(0)=nside2npix(nside)
do ith=1,2*nside
if (ith.lt.nside) then ! polar cap (north)
nph = 4*ith
else ! tropical band (north) + equator
nph = 4*nside
endif
H%istart_north(ith)=H%istart_north(ith-1)+nph
H%istart_south(ith)=H%istart_south(ith-1)-nph
enddo
end if
end subroutine HealpixInitTrig
subroutine HealpixInfo_GetTrig(H, nph)
Type (HealpixInfo) :: H
integer, intent(in) :: nph
integer status, m
real(dp) phi0
if (H%last_nph /= nph) then
deallocate(H%trig,stat = status)
ALLOCATE(H%trig(0:max(2*H%nside,H%lmax)),stat = status)
H%trig=1
phi0=PI/DBLE((nph/4)*4)
do m=0,max(2*H%nside,H%lmax)
H%trig(m)= CMPLX( DCOS(m*phi0), DSIN(m*phi0), kind=DP)
enddo
H%last_nph = nph
end if
end subroutine HealpixInfo_GetTrig
function NearestFastFFTnum(i)
!returns next number of form 2^n 3^m for low m
integer, intent(in) :: i
integer NearestFastFFTnum
integer j, vals(71)
vals = (/128, 144, 192, 256, 288, 384, 512, 576, 768, 1024, 1152, 1536, 2048, 2304, 3072, &
4096 , 4608, 6144, 8192, 9216, 12288, 16384, 18432, 24576, 32768, 36864, 49152, 65536, 73728, &
98304 , 131072, 147456, 196608, 262144, 294912, 393216, 524288, 589824, 786432, 1048576, &
1179648 , 1572864, 2097152, 2359296, 3145728, 4194304, 4718592, 6291456, 8388608, &
9437184 , 12582912, 16777216, 18874368, 25165824, 33554432, 37748736, 50331648, 67108864, &
75497472 , 100663296, 134217728, 150994944, 201326592, 268435456, 301989888, 402653184, 452984832, &
536870912 , 603979776, 805306368, 905969664/)
do j=1,71
if (i*0.9<=vals(j)) then
NearestFastFFTnum = vals(j)
return
end if
end do
stop 'NearestFastFFTnum: number too large'
end function NearestFastFFTnum
function ScaleFactor(i)
integer, intent(in) :: i
real(dp) :: ScaleFactor
if (i>-10) then
ScaleFactor = ScaleFactors(i)
else
ScaleFactor = 0
end if
end function ScaleFactor
subroutine HealpixFree(H)
Type (HealpixInfo) :: H
integer status
#ifdef MPIPIX
if (H%MpiId == 0) call SendMessages(H, 'EXIT')
#endif
deallocate(H%w8ring_TQU, stat = status)
deallocate(H%istart_north, H%istart_south, stat = status)
deallocate(H%trig, stat = status)
deallocate(H%recfac, stat = status)
deallocate(H%ith_start, H%ith_end,H%North_Start, H%North_Size, &
H%South_Start, H%South_Size, stat = status)
nullify(H%w8ring_TQU)
end subroutine HealpixFree
subroutine HealpixInitRecfac(H,nlmax)
Type (HealpixInfo) :: H
INTEGER(I4B), intent(in):: nlmax
integer(I8B) :: m, l
integer(I_NPIX) :: a_ix
integer status
integer(I8B) l2, m2
if (H%MpiId > 0 .and. associated(H%recfac) .and. nlmax == H%Lastlmax) return
call HealpixFreeRecfac(H)
H%Lastlmax = nlmax
deallocate(H%recfac,stat= status)
ALLOCATE(H%recfac(lmax2nalms(nlmax)),stat = status)
if (status /= 0) call die_alloc('HealpixInitRecfac','recfac')
a_ix = 0
do m = 0, nlmax
m2 = m**2
do l = m, nlmax
a_ix = a_ix + 1
l2 = (l+1)**2
H%recfac(a_ix) = SQRT( real(4 * l2 - 1,dp) / real(l2-m2,dp) )
end do
end do
end subroutine HealpixInitRecfac
subroutine HealpixFreeRecfac(H)
Type (HealpixInfo) :: H
integer status
if (H%MpiId > 0) return !cache it as have loads of memory
deallocate(H%recfac,stat= status)
end subroutine HealpixFreeRecfac
function get_mmax(nlmax,sth)
integer, intent(in) :: nlmax
real(dp), intent(in) :: sth
integer get_mmax
if (mmax_approx) then
get_mmax = min(nlmax,max(40,nint(1.25*nlmax*sth)))
else
get_mmax = nlmax
end if
end function get_mmax
function l_min_ylm(m, sth) result(lmin)
!From heapix 2, roughly consistent with choice of get_mmax above
!================================================================
! returns minimal order l at which to keep Ylm
! |Ylm| < eps * Y00 ==>
! m_cut(theta, l) = theta * l * e / 2 + | ln(eps)| + ln(l)/2
! if eps = 1.e-15 and l < 1.e4
! m_cut(theta, l) = theta * l * 1.35 + 40
! the choice of 1.35 (or larger)
! also insures that the equatorial rings will have all their Ylm's computed
! default parameters are HPX_MXL0 = 40 and HPX_MXL1 = 1.35_DP
!======================================================
! parameters of short-cut: defined in module header
! dummy variables
integer(I4B) :: lmin
integer(I4B), intent(IN) :: m
real(DP), intent(IN) :: sth
integer, parameter :: HPX_MXL0 = 40
real(dp), parameter :: HPX_MXL1 = 1.35_dp
lmin = m ! default
if (mmax_approx) lmin = max(lmin, int((m - HPX_MXL0)/(HPX_MXL1 * sth)))
end function l_min_ylm
subroutine spinring_synthesis(H,nlmax,datain,nph,dataout,kphi0,mmax_ring)
!Don't fully follow the signs here, but the answer is correct
!Note no point using FFTW etc as FFT is a negligible fraction of computation cost
!=======================================================================
! RING_SYNTHESIS
! called by alm2map
! calls real_fft
!
! dataout(j) = sum_m datain(m) * exp(i*m*phi(j))
! with phi(j) = j*2pi/nph + kphi0*pi/nph and kphi0 =0 or 1
!
! as the set of frequencies {m} is larger than nph,
! we wrap frequencies within {0..nph-1}
! ie m = k*nph + m' with m' in {0..nph-1}
! then
! noting bw(m') = exp(i*m'*phi0)
! * sum_k (datain(k*nph+m') exp(i*k*pi*kphi0))
! with bw(nph-m') = CONJ(bw(m')) (if datain(-m) = CONJ(datain(m)))
! dataout(j) = sum_m' [ bw(m') exp (i*j*m'*2pi/nph) ]
! = Fourier Transform of bw
! is real
!
! NB nph is not necessarily a power of 2
!
!=======================================================================
Type (HealpixInfo) :: H
INTEGER(I4B) :: nsmax
INTEGER(I4B), INTENT(IN) :: nlmax
INTEGER(I4B), INTENT(IN) :: mmax_ring
INTEGER(I4B), INTENT(IN) :: nph, kphi0
COMPLEX(DPC), dimension(0:nlmax), INTENT(IN) :: datain
REAL(SP), dimension(0:nph-1), INTENT(OUT) :: dataout
REAL(DP), dimension(0:nph-1) :: data
INTEGER(I4B) :: iw,ksign,m,k,kshift
COMPLEX(DPC), dimension(0:nph-1) :: bw
COMPLEX(DPC) :: dat
#ifdef MPIPIX
integer status
#endif
!=======================================================================
call HealpixInfo_GetTrig(H, nph)
nsmax = H%nside
ksign = + 1
kshift = (-1)**kphi0 ! either 1 or -1
bw(0:nph-1) = CMPLX(0.0_dp, 0.0_dp, KIND=DP)
! all frequencies [-m,m] are wrapped in [0,nph-1]
bw(0)=datain(0)
do m = 1, mmax_ring ! in -nlmax, nlmax
iw = MODULO(m, nph) ! between 0 and nph-1 = m', F90 intrisic
k = (m - iw) / nph ! number of 'turns'
bw(iw) = bw(iw) + datain(m)*(kshift**k) ! complex number
iw = MODULO(-m, nph) ! between 0 and nph-1 = m', F90 intrisic
k = (-m - iw) / nph ! number of 'turns'
bw(iw) = bw(iw) + CONJG(datain(m))*(kshift**k) ! complex number
enddo
! kshift**k = 1 for even turn numbers
! = 1 or -1 for odd turn numbers : results from the shift in space
! applies the shift in position <-> phase factor in Fourier space
data(0)=REAL(bw(0))
!Data is in packed storage
do iw = 1, nph/2 -1
m = ksign*(iw)
if(kphi0==1) then
dat =bw(iw) * H%trig(m)
else
dat =bw(iw)
endif
data(iw*2-1 ) = REAL(dat)
data(iw*2) = AIMAG(dat)
enddo
! nph is always even for Healpix
iw=nph/2
m = ksign*(iw)
if(kphi0==1) then
dat =bw(iw) * H%trig(m)
else
dat =bw(iw)
endif
data(iw*2-1) = REAL(dat)
call real_fft (data, backward=.true.)
! ^^^^^^^^^^^^
dataout=REAL(data(0:nph-1))
end subroutine spinring_synthesis
subroutine alm2GradientMap(H, inlmax, alm, map_QU)
!Get the map of the gradient of alm (know pure E, so quicker than general routine)
!internally use EB_sign=1 convention, though result is independent
use alm_tools
use MPIstuff
Type (HealpixInfo) :: H
INTEGER(I4B), INTENT(IN) :: inlmax
integer nsmax
COMPLEX(SPC), INTENT(IN), dimension(:,:,:) :: alm
COMPLEX(SPC), INTENT(OUT), dimension(0:H%npix-1), target :: map_QU
COMPLEX(SPC), dimension(:), pointer :: map2N,map2S
COMPLEX(SPC), dimension(:), allocatable :: alm2
INTEGER(I4B) :: l, m, ith, scalem, scalel ! alm related
INTEGER(I4B) :: nph, kphi0, nlmax
REAL(DP) :: cth, sth, dth1, dth2, dst1
REAL(DP) :: a_rec, lam_mm, lam_lm, lam_lm1m, lam_0, lam_1, lam_2
REAL(DP) :: fm, f2m, fm2, corfac
REAL(DP) :: c_on_s2, fm_on_s2, one_on_s2
REAL(DP) :: lambda_w, lambda_x, lambda_w_1, lambda_x_1, a_w
REAL(DP) :: a_w_m
COMPLEX(DPC) :: factor_1, factor_2, factor_1_1, factor_2_1
COMPLEX(DPC) :: b_n_Q, b_s_Q, b_n_U, b_s_U
CHARACTER(LEN=*), PARAMETER :: code = 'ALM2GRADIENTMAP'
COMPLEX(DPC) :: b_north_Q(0:H%lmax), b_north_U(0:H%lmax)
COMPLEX(DPC) :: b_south_Q(0:H%lmax), b_south_U(0:H%lmax)
INTEGER(I4B) :: status,par_lm
INTEGER(I_NPIX) :: a_ix, nalms
REAL(DP) , dimension(:), allocatable :: cth_l
REAL(DP), dimension(:), allocatable :: lam_fact
REAL(SP), dimension(:), allocatable :: ringR, ringI
integer mmax_ring, lmin
#ifdef MPIPIX
double precision Initime
#endif
!=======================================================================
nsmax = H%nside
nlmax = inlmax
#ifdef MPIPIX
StartTime = Getetime()
iniTime = StartTime
if (H%MpiId==0) then
print *,code //': Sending to farm '
call SendMessages(H,code)
end if
call SyncInts(nlmax)
#endif
nalms = lmax2nalms(nlmax)
allocate(alm2(nalms),stat = status )
if (status /= 0) call die_alloc(code,'alm2')
if (H%MpiId==0) call Alm2PackAlm(alm,alm2,nlmax)
#ifdef MPIPIX
call MPI_BCAST(alm2,SIze(alm2),CSP_MPI, 0, MPI_COMM_WORLD, ierr)
if(DebugMsgs>1) print *,code//' Got alm ',H%MpiId, GeteTime() - StartTime
allocate(map2N(H%North_Start(H%MpiId):H%North_Start(H%MpiId)+H%North_Size(H%MpiId)-1),stat = status)
if (status /= 0) call die_alloc(code,'map2')
allocate(map2S(H%South_Start(H%MpiId):H%South_Start(H%MpiId)+H%South_Size(H%MpiId)-1),stat = status)
if (status /= 0) call die_alloc(code,'map2')
#else
map2N => map_QU
map2S => map_QU
#endif
ALLOCATE(lam_fact(nalms),stat = status)
if (status /= 0) call die_alloc(code,'lam_fact')
ALLOCATE(cth_l(nlmax))
ALLOCATE(ringR(0:4*nsmax-1),ringI(0:4*nsmax-1),stat = status)
if (status /= 0) call die_alloc(code,'ring')
call HealpixInitRecfac(H,nlmax)
call GetLamfact(lam_fact, nlmax)
dth1 = 1.0_dp / (3.0_dp*DBLE(nsmax)**2)
dth2 = 2.0_dp / (3.0_dp*DBLE(nsmax))
dst1 = 1.0_dp / (SQRT(6.0_dp) * DBLE(nsmax) )
! --------------------------------------------
do ith = H%ith_start(H%MpiId), H%ith_end(H%MpiId) ! 0 <= cos theta < 1
! cos(theta) in the pixelisation scheme
if (ith < nsmax) then ! polar cap (north)
cth = 1.0_dp - DBLE(ith)**2 * dth1 !cos theta
nph = 4*ith
kphi0 = 1
sth = SIN( 2.0_dp * ASIN( ith * dst1 ) ) ! sin(theta)
else ! tropical band (north) + equator
cth = DBLE(2*nsmax-ith) * dth2 !cos theta
nph = 4*nsmax
kphi0 = MOD(ith+1-nsmax,2)
sth = DSQRT((1.0_dp-cth)*(1.0_dp+cth)) ! sin(theta)
endif
one_on_s2 = 1.0_dp / sth**2 ! 1/sin^2
c_on_s2 = cth * one_on_s2
do l=1, nlmax
cth_l(l) = cth*real(l,dp)
end do
mmax_ring = get_mmax(nlmax,sth)
b_north_Q(0:nlmax) = 0
b_north_U(0:nlmax) = 0
b_south_Q(0:nlmax) = 0
b_south_U(0:nlmax) = 0
lam_mm = sq4pi_inv ! lamda_00
scalem=1
a_ix = 0
a_w = -1._dp / sth
do m = 0, mmax_ring
fm = DBLE(m)
f2m = 2.0_dp * fm
fm2 = fm * fm
fm_on_s2 = fm * one_on_s2
! ---------- l = m ----------
par_lm = -1 ! = (-1)^(l+m+s)
if (m >= 1) then ! lambda_0_0 for m>0
lam_mm = -lam_mm*sth*dsqrt((f2m+1.0_dp)/f2m)
endif
if (abs(lam_mm) < UNFLOW) then
lam_mm=lam_mm*OVFLOW
scalem=scalem-1
endif
corfac = ScaleFactor(scalem)*lam_mm/OVFLOW
lam_lm = corfac ! actual lambda_mm
a_ix = a_ix + 1
if (m >=1) then
!normal_l cancels with gradient, sign from gradient
lambda_x = - lam_lm * fm / sth
lambda_w = -lambda_x * cth
b_n_Q = lambda_w * alm2(a_ix)
b_s_Q = par_lm * b_n_Q
b_n_U = (0,-1)* lambda_x * alm2(a_ix)
b_s_U = -par_lm * b_n_U
else
b_n_Q=0
b_s_Q=0
b_n_U=0
b_s_U=0
end if
! ---------- l > m ----------
lam_0 = 0.0_dp
lam_1 = 1.0_dp
scalel=0
a_rec = H%recfac(a_ix)
lam_2 = cth * lam_1 * a_rec
lmin = l_min_ylm(m, sth)
a_w_m = a_w*fm
do l = m+1, nlmax-1, 2
!This is semi-optimized version where we do two at once
!par_lm starts off positive (negative on entry to loop)
!doesn't gain much here
lam_lm1m=lam_lm ! actual lambda_l-1,m
lam_lm = lam_2 * corfac ! actual lambda_lm, OVFLOW factors removed
lam_0 = lam_1 / a_rec
lam_1 = lam_2
a_ix = a_ix + 1
a_rec = H%recfac(a_ix)
lam_2 = (cth * lam_1 - lam_0) * a_rec
if (l >= lmin) then
lambda_w_1 = a_w * (lam_fact(a_ix)*lam_lm1m - cth_l(l)*lam_lm)
lambda_x_1 = a_w_m * lam_lm
lam_lm1m=lam_lm ! actual lambda_l-1,m
lam_lm = lam_2 * corfac ! actual lambda_lm, OVFLOW factors removed
lambda_w = a_w * (lam_fact(a_ix+1)*lam_lm1m - cth_l(l+1)*lam_lm)
lambda_x = a_w_m * lam_lm
factor_1_1 = lambda_w_1 * alm2(a_ix)
factor_1 = lambda_w * alm2(a_ix+1)
b_n_Q = b_n_Q + factor_1 + factor_1_1
b_s_Q = b_s_Q - factor_1 + factor_1_1
factor_2_1 = lambda_x_1*cmplx(-aimag(alm2(a_ix)),real(alm2(a_ix)))
factor_2 = lambda_x*cmplx(-aimag(alm2(a_ix+1)),real(alm2(a_ix+1)))
b_n_U = b_n_U - factor_2 - factor_2_1
b_s_U = b_s_U - factor_2 + factor_2_1
else
lam_lm1m=lam_lm ! actual lambda_l-1,m
lam_lm = lam_2 * corfac ! actual lambda_lm, OVFLOW factors removed
end if
lam_0 = lam_1 / a_rec