/
MakeMagGradGridDH.F95
1347 lines (1060 loc) · 47.9 KB
/
MakeMagGradGridDH.F95
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
subroutine MakeMagGradGridDH(cilm, lmax, r0, a, f, vxx, vyy, vzz, vxy, &
vxz, vyz, n, sampling, lmax_calc, exitstatus)
!------------------------------------------------------------------------------
!
! Given the magnetic potential spherical harmonic coefficients CILM, this
! subroutine will compute 2D Driscol and Healy sampled grids of the six
! components of the magnetic field tensor in a local north-oriented
! reference frame:
!
! (Vxx, Vxy, Vxz)
! (Vyx, Vyy, Vyz)
! (Vzx, Vzy, Vzz)
!
! where X points NORTH, Y points WEST, and Z points UPWARD. The magnetic
! potential is defined as
!
! V = R0 Sum_{l=0}^LMAX (R0/r)^(l+1) Sum_{m=-l}^l C_{lm} Y_{lm},
!
! where the Gauss coefficients are in units of nT, and the spherical
! harmonic functions are Schmidt semi-normalized.
!
! Laplaces equation implies that Vxx + Vyy + Vzz = 0, and the tensor
! is symmetric. The components are calculated according to eq. 1 in
! Petrovskaya and Vershkov (2006, J. Geod, 80, 117-127), which is based on the
! eq. 3.28 in Reed (1973, Ohio State Univ., Dept. Geod. Sci., Rep. 201,
! Columbus, OH). Note that Reed's equations are in terms of latitude, and
! that the Y axis points East.
!
! Vzz = Vrr
! Vxx = 1/r Vr + 1/r^2 Vtt
! Vyy = 1/r Vr + 1/r^2 /tan(t) Vt + 1/r^2 /sin(t)^2 Vpp
! Vxy = 1/r^2 /sin(t) Vtp - cos(t)/sin(t)^2 /r^2 Vp
! Vxz = 1/r^2 Vt - 1/r Vrt
! Vyz = 1/r^2 /sin(t) Vp - 1/r /sin(t) Vrp
!
! where r, t, p stand for radius, theta, and phi, and subscripts on V denote
! partial derivatives.
!
! The output grids are in units of nT / m and are cacluated on a flattened
! ellipsoid with semi-major axis A and flattening F. The output grids contain
! N samples in latitude and longitude by default, but if the optional
! parameter SAMPLING is set to 2, the grids will contain N samples in
! latitude and 2N samples in longitude. The first latitudinal band of the
! grid corresponds to 90 N, the latitudinal band for 90 S is not calculated,
! and the latitudinal sampling interval is 180/N degrees. The first
! longitudinal band is 0 E, the longitudinal band for 360 E is not
! calculated, and the longitudinal sampling interval is 360/N for equally
! sampled and 180/N for equally spaced grids, respectively.
!
! Calling Parameters
!
! IN
! cilm Schmidth seminormalized magnetic potential spherical
! harmonic coefficients.
! lmax The maximum spherical harmonic degree of the function,
! used to determine the number of samples N.
! r0 Reference radius of potential coefficients.
! a The semimajor axis of the flattened ellipsoid.
! f Flattening of the planet.
!
! IN, OPTIONAL
! sampling (1) Grid is N latitudes by N longitudes (default).
! (2) Grid is N by 2N. The higher frequencies resulting
! from this oversampling in longitude are discarded, and
! hence not aliased into lower frequencies.
! lmax_calc The maximum spherical harmonic degree to evaluate
! the coefficients up to.
!
! OUT
! Vxx x-x component of the magnetic field tensor.
! Vyy y-y component of the magnetic field tensor.
! Vzz z-z component of the magnetic field tensor.
! Vxy x-y component of the magnetic field tensor.
! Vxz x-z component of the magnetic field tensor.
! Vyz y-z component of the magnetic field tensor.
! N Number of samples in latitude. Number of samples in
! longitude is N when sampling is 1 (default), and is
! 2N when sampling is 2.
!
! OPTIONAL (OUT)
! exitstatus If present, instead of executing a STOP when an error
! is encountered, the variable exitstatus will be
! returned describing the error.
! 0 = No errors;
! 1 = Improper dimensions of input array;
! 2 = Improper bounds for input variable;
! 3 = Error allocating memory;
! 4 = File IO error.
!
! Notes:
! 1. If lmax is greater than the the maximum spherical harmonic
! degree of the input file, Cilm will be ZERO PADDED!
! (i.e., those degrees after lmax are assumed to be zero).
! 2. Latitude is geocentric latitude.
!
! Dependencies: FFTW3
!
! Copyright (c) 2016, SHTOOLS
! All rights reserved.
!
!------------------------------------------------------------------------------
use FFTW3
#ifdef FFTW3_UNDERSCORE
#define dfftw_plan_dft_c2r_1d dfftw_plan_dft_c2r_1d_
#define dfftw_execute dfftw_execute_
#define dfftw_destroy_plan dfftw_destroy_plan_
#endif
implicit none
real*8, intent(in) :: cilm(:,:,:), r0, a, f
real*8, intent(out) :: vxx(:,:), vyy(:,:), vzz(:,:), vxy(:,:), vxz(:,:), &
vyz(:,:)
integer, intent(in) :: lmax
integer, intent(out) :: n
integer, intent(in), optional :: sampling, lmax_calc
integer, intent(out), optional :: exitstatus
integer :: l, m, i, l1, m1, lmax_comp, i_eq, i_s, astat(4), nlong
real*8 :: grid(4*lmax+4), pi, theta, scalef, rescalem, u, p, dp, dp2, &
dp2s, pmm, sint, pm1, pm2, z, tempr, r_ex, lat, &
prefactor(lmax), coefr0, coefrs0, coeft0, coefts0, coefp0, &
coefps0, coefrr0, coefrrs0, coefrt0, coefrts0, coefrp0, &
coefrps0, coeftp0, coeftps0, coefpp0, coefpps0, coeftt0, &
coeftts0
complex*16 :: coef(2*lmax+3), coefr(2*lmax+3), coefrs(2*lmax+3), &
coeft(2*lmax+3), coefts(2*lmax+3), coefp(2*lmax+3), &
coefps(2*lmax+3), tempc, coefrr(2*lmax+3), coefrrs(2*lmax+3), &
coefrt(2*lmax+3), coefrts(2*lmax+3), coefrp(2*lmax+3), &
coefrps(2*lmax+3), coeftp(2*lmax+3), coeftps(2*lmax+3), &
coefpp(2*lmax+3), coefpps(2*lmax+3), coeftt(2*lmax+3), &
coeftts(2*lmax+3)
integer*8 :: plan
real*8, save, allocatable :: ff1(:,:), ff2(:,:), sqr(:)
integer*1, save, allocatable :: fsymsign(:,:)
integer, save :: lmax_old = 0
external :: dfftw_plan_dft_c2r_1d, dfftw_execute, dfftw_destroy_plan
!$OMP threadprivate(ff1, ff2, sqr, fsymsign, lmax_old)
if (present(exitstatus)) exitstatus = 0
n = 2 * lmax + 2
if (present(sampling)) then
if (sampling /= 1 .and. sampling /=2) then
print*, "Error --- MakeMagGradGridDH"
print*, "Optional parameter SAMPLING must be 1 (N by N) " // &
"or 2 (N by 2N)."
print*, "Input value is ", sampling
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
end if
end if
if (size(cilm(:,1,1)) < 2) then
print*, "Error --- MakeMagGradGridDH"
print*, "CILM must be dimensioned as (2, *, *)."
print*, "Input dimension is ", size(cilm(:,1,1)), size(cilm(1,:,1)), &
size(cilm(1,1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
if (present(sampling)) then
if (sampling == 1) then
nlong = n
else
nlong = 2 * n
end if
else
nlong = n
end if
if (size(vxx(:,1)) < n .or. size(vxx(1,:)) < nlong .or. size(vyy(:,1)) < n &
.or. size(vyy(1,:)) < nlong .or. size(vzz(:,1)) < n .or. &
size(vzz(1,:)) < nlong .or. size(vxy(:,1)) < n .or. &
size(vxy(1,:)) < nlong .or. size(vxz(:,1)) < n .or. &
size(vxz(1,:)) < nlong .or. size(vyz(:,1)) < n .or. &
size(vyz(1,:)) < nlong) then
print*, "Error --- MakeMagGradGridDH"
if (present(sampling)) then
if (sampling == 1) then
print*, "VXX, VYY, VZZ, VXY, VXZ, and VYZ must be " // &
"dimensioned as (N, N) where N is ", n
else if (sampling == 2) then
print*, "VXX, VYY, VZZ, VXY, VXZ, and VYZ must be " // &
"dimensioned as (N, 2N) where N is ", n
end if
else
print*, "VXX, VYY, VZZ, VXY, VXZ, and VYZ must be dimensioned " // &
"as (N, N) where N is ", n
end if
print*, "Input dimensions are ", size(vxx(:,1)), size(vxx(1,:)), &
size(vyy(:,1)), size(vyy(1,:)), size(vzz(:,1)), size(vzz(1,:)), &
size(vxy(:,1)), size(vxy(1,:)), size(vxz(:,1)), size(vxz(1,:)), &
size(vyz(:,1)), size(vyz(1,:))
if (present(exitstatus)) then
exitstatus = 1
return
else
stop
end if
end if
pi = acos(-1.0d0)
scalef = 1.0d-280
if (present(lmax_calc)) then
if (lmax_calc > lmax) then
print*, "Error --- MakeMagGradGridDH"
print*, "LMAX_CALC must be less than or equal to LMAX."
print*, "LMAX = ", lmax
print*, "LMAX_CALC = ", lmax_calc
if (present(exitstatus)) then
exitstatus = 2
return
else
stop
end if
else
lmax_comp = min(lmax, size(cilm(1,1,:))-1, size(cilm(1,:,1))-1, &
lmax_calc)
end if
else
lmax_comp = min(lmax, size(cilm(1,1,:))-1, size(cilm(1,:,1))-1)
end if
!--------------------------------------------------------------------------
!
! Calculate recursion constants used in computing Legendre polynomials
!
!--------------------------------------------------------------------------
if (lmax_comp /= lmax_old) then
if (allocated (sqr)) deallocate (sqr)
if (allocated (ff1)) deallocate (ff1)
if (allocated (ff2)) deallocate (ff2)
if (allocated (fsymsign)) deallocate (fsymsign)
allocate (sqr(2 * lmax_comp + 1), stat=astat(1))
allocate (ff1(lmax_comp+1,lmax_comp+1), stat=astat(2))
allocate (ff2(lmax_comp+1,lmax_comp+1), stat=astat(3))
allocate (fsymsign(lmax_comp+1,lmax_comp+1), stat=astat(4))
if (sum(astat(1:4)) /= 0) then
print*, "Error --- MakeMagGradGridDH"
print*, "Problem allocating arrays SQR, FF1, FF2, or FSYMSIGN", &
astat(1), astat(2), astat(3), astat(4)
if (present(exitstatus)) then
exitstatus = 3
return
else
stop
end if
end if
!----------------------------------------------------------------------
!
! Calculate signs used for symmetry of Legendre functions about
! equator. For the first derivative in theta, these signs are
! reversed.
!
!----------------------------------------------------------------------
do l = 0, lmax_comp, 1
do m = 0, l, 1
if (mod(l-m,2) == 0) then
fsymsign(l+1,m+1) = 1
else
fsymsign(l+1,m+1) = -1
end if
end do
end do
!----------------------------------------------------------------------
!
! Precompute square roots of integers that are used several times.
!
!----------------------------------------------------------------------
do l=1, 2 * lmax_comp + 1
sqr(l) = sqrt(dble(l))
end do
!----------------------------------------------------------------------
!
! Precompute multiplicative factors used in recursion relationships
! P(l,m) = x*f1(l,m)*P(l-1,m) - P(l-2,m)*f2(l,m)
! k = l*(l+1)/2 + m + 1
! Note that prefactors are not used for the case when m=l as a
! different recursion is used. Furthermore, for m=l-1, Plmbar(l-2,m)
! is assumed to be zero.
!
!----------------------------------------------------------------------
if (lmax_comp /= 0) then
ff1(2,1) = 1.0d0
ff2(2,1) = 0.0d0
endif
do l = 2, lmax_comp, 1
ff1(l+1,1) = dble(2*l-1) / dble(l)
ff2(l+1,1) = dble(l-1) / dble(l)
do m = 1, l-2, 1
ff1(l+1,m+1) = dble(2*l-1) / sqr(l+m) / sqr(l-m)
ff2(l+1,m+1) = sqr(l-m-1) * sqr(l+m-1) / sqr(l+m) / sqr(l-m)
end do
ff1(l+1,l)= dble(2*l-1) / sqr(l+m) / sqr(l-m)
ff2(l+1,l) = 0.0d0
end do
lmax_old = lmax_comp
end if
!--------------------------------------------------------------------------
!
! Determine Clms one l at a time by intergrating over latitude.
!
!--------------------------------------------------------------------------
call dfftw_plan_dft_c2r_1d(plan, nlong, coef(1:nlong/2+1), grid(1:nlong), &
FFTW_MEASURE)
i_eq = n / 2 + 1 ! Index correspondong to zero latitude
do i = 1, i_eq - 1, 1
i_s = 2 * i_eq - i
theta = pi * dble(i-1) / dble(n)
z = cos(theta)
u = sqrt( (1.0d0-z) * (1.0d0+z) )
sint = sin(theta)
lat = pi / 2.0d0 - theta
if (i == 1) then ! Reference ellipsoid radius
r_ex = a * (1.0d0 - f)
else
r_ex = (1.0d0 + tan(lat)**2) / &
(1.0d0 + tan(lat)**2 / (1.0d0 - f)**2 )
r_ex = a * sqrt(r_ex)
end if
coefr(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefr0 = 0.0d0
coefrs(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrs0 = 0.0d0
coefrr(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrr0 = 0.0d0
coefrrs(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrrs0 = 0.0d0
coeft(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coeft0 = 0.0d0
coefts(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefts0 = 0.0d0
coeftp(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coeftp0 = 0.0d0
coeftps(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coeftps0 = 0.0d0
coefp(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefp0 = 0.0d0
coefps(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefps0 = 0.0d0
coefrt(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrt0 = 0.0d0
coefrts(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrts0 = 0.0d0
coefrp(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrp0 = 0.0d0
coefrps(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefrps0 = 0.0d0
coefpp(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefpp0 = 0.0d0
coefpps(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coefpps0 = 0.0d0
coeftt(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coeftt0 = 0.0d0
coeftts(1:lmax+2) = dcmplx(0.0d0,0.0d0)
coeftts0 = 0.0d0
pm2 = 1.0d0
! l = 0 terms are zero
! derivative in theta and phi of l=0 term is 0, so no need to
! calculate this
if (lmax_comp /= 0) then ! l = 1
prefactor(1) = (r0 / r_ex)**2
do l = 2, lmax_comp, 1
prefactor(l) = prefactor(l-1) * r0 / r_ex
end do
pm1 = ff1(2,1) * z
! -2 = (l+1) prefactor
tempr = cilm(1,2,1) * pm1 * (-2) * prefactor(1)
coefr0 = coefr0 + tempr
coefrs0 = coefrs0 - tempr ! fsymsign = -1
! 6 = (l+1)*(l+2) prefactor
tempr = cilm(1,2,1) * pm1 * (6) * prefactor(1)
coefrr0 = coefrr0 + tempr
coefrrs0 = coefrrs0 - tempr ! fsymsign = -1
! dp is the first derivative with respect to Z
dp = ff1(2,1)
tempr = cilm(1,2,1) * dp * prefactor(1)
coeft0 = coeft0 + tempr
coefts0 = coefts0 + tempr ! reverse fsymsign
! -2 = (l+1) prefactor
tempr = cilm(1,2,1) * dp * (-2) * prefactor(1)
coefrt0 = coefrt0 + tempr
coefrts0 = coefrts0 + tempr ! reverse fsymsign
! dp2 is the second derivative with respect to THETA. Must
! multiply dp by -sin(theta) in the recurrence relation
! note that the first term is symmetric about the equator according
! the fsymsign, whereas the second term differs
! by -1.
dp2 = -2 * pm1 + z * dp
dp2s = 2 * pm1 + z * dp
tempr = cilm(1,2,1) * prefactor(1)
coeftt0 = coeftt0 + tempr * dp2
coeftts0 = coeftts0 + tempr * dp2s
end if
do l = 2, lmax_comp, 1
l1 = l + 1
p = ff1(l1,1) * z * pm1 - ff2(l1,1) * pm2
tempr = cilm(1,l1,1) * p * (-l1) * prefactor(l)
coefr0 = coefr0 + tempr
coefrs0 = coefrs0 + tempr * fsymsign(l1,1)
tempr = cilm(1,l1,1) * p * (l1) * (l1+1) * prefactor(l)
coefrr0 = coefrr0 + tempr
coefrrs0 = coefrrs0 + tempr * fsymsign(l1,1)
dp = l * (pm1 - z * p) / u**2
tempr = cilm(1,l1,1) * dp * prefactor(l)
coeft0 = coeft0 + tempr
coefts0 = coefts0 - tempr * fsymsign(l1,1) ! reverse fsymsign
tempr = cilm(1,l1,1) * dp * (-l1) * prefactor(l)
coefrt0 = coefrt0 + tempr
coefrts0 = coefrts0 - tempr * fsymsign(l1,1)
dp2 = -l * l1 * p + z * dp
dp2s = -l * l1 * p * fsymsign(l1,1) - z * dp * fsymsign(l1,1)
tempr = cilm(1,l1,1) * prefactor(l)
coeftt0 = coeftt0 + tempr * dp2
coeftts0 = coeftts0 + tempr * dp2s
pm2 = pm1
pm1 = p
end do
pmm = sqr(2) * scalef
rescalem = 1.0d0 / scalef
do m = 1, lmax_comp-1, 1
m1 = m + 1
rescalem = rescalem * u
pmm = pmm * sqr(2*m+1) / sqr(2*m)
pm2 = pmm / sqr(2*m+1)
tempc = dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) * pm2 * (-m-1) &
* prefactor(m) ! (m,m)
coefr(m1) = coefr(m1) + tempc
coefrs(m1) = coefrs(m1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) * pm2 * (m+1) &
* (m+2) * prefactor(m) ! (m,m)
coefrr(m1) = coefrr(m1) + tempc
coefrrs(m1) = coefrrs(m1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(2,m1,m1), cilm(1,m1,m1)) * pm2 &
* prefactor(m) * m ! (m,m)
coefp(m1) = coefp(m1) + tempc
coefps(m1) = coefps(m1) + tempc ! fsymsign = 1
tempc = -dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) * pm2 &
* prefactor(m) * m**2 ! (m,m)
coefpp(m1) = coefpp(m1) + tempc
coefpps(m1) = coefpps(m1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(2,m1,m1), cilm(1,m1,m1)) * pm2 *(-m-1) &
* prefactor(m) * m ! (m,m)
coefrp(m1) = coefrp(m1) + tempc
coefrps(m1) = coefrps(m1) + tempc ! fsymsign = 1
dp = -m * z * pm2 / u**2
tempc = dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) * dp &
* prefactor(m) ! (m,m)
coeft(m1) = coeft(m1) + tempc
coefts(m1) = coefts(m1) - tempc ! reverse fsymsign
tempc = dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) * dp * (-m-1) &
* prefactor(m) ! (m,m)
coefrt(m1) = coefrt(m1) + tempc
coefrts(m1) = coefrts(m1) - tempc ! reverse fsymsign
tempc = dcmplx(cilm(2,m1,m1), cilm(1,m1,m1)) * dp &
* prefactor(m) * m ! (m,m)
coeftp(m1) = coeftp(m1) + tempc
coeftps(m1) = coeftps(m1) - tempc ! reverse fsymsign
dp2 = -(m*m1 - (m**2)/u**2) * pm2 + z * dp
dp2s = -(m*m1 - (m**2)/u**2) * pm2 - z * dp
tempc = dcmplx(cilm(1,m1,m1), - cilm(2,m1,m1)) &
* prefactor(m) ! (m,m)
coeftt(m1) = coeftt(m1) + tempc * dp2
coeftts(m1) = coeftts(m1) + tempc * dp2s
pm1 = z * ff1(m1+1,m1) * pm2
tempc = dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) * pm1 * (-m-2) &
* prefactor(m+1) ! (m+1,m)
coefr(m1) = coefr(m1) + tempc
coefrs(m1) = coefrs(m1) - tempc ! fsymsign = -1
tempc = dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) * pm1 * (m+2) &
* (m+3) * prefactor(m+1) ! (m+1,m)
coefrr(m1) = coefrr(m1) + tempc
coefrrs(m1) = coefrrs(m1) - tempc ! fsymsign = -1
tempc = dcmplx(cilm(2,m1+1,m1), cilm(1,m1+1,m1)) * pm1 &
* prefactor(m+1) * m ! (m+1,m)
coefp(m1) = coefp(m1) + tempc
coefps(m1) = coefps(m1) - tempc ! fsymsign = -1
tempc = -dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) * pm1 &
* prefactor(m+1) * m**2 ! (m+1,m)
coefpp(m1) = coefpp(m1) + tempc
coefpps(m1) = coefpps(m1) - tempc ! fsymsign = -1
tempc = dcmplx(cilm(2,m1+1,m1), cilm(1,m1+1,m1)) * pm1 * (-m-2) &
* prefactor(m+1) * m ! (m+1,m)
coefrp(m1) = coefrp(m1) + tempc
coefrps(m1) = coefrps(m1) - tempc ! fsymsign = -1
dp = (pm2 * sqr(2*m+1) - z * (m+1) * pm1) / u**2
tempc = dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) * dp &
* prefactor(m+1) ! (m+1,m)
coeft(m1) = coeft(m1) + tempc
coefts(m1) = coefts(m1) + tempc ! reverse fsymsign
tempc = dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) * dp &
* (-m-2) * prefactor(m+1) ! (m+1,m)
coefrt(m1) = coefrt(m1) + tempc
coefrts(m1) = coefrts(m1) + tempc ! reverse fsymsign
tempc = dcmplx(cilm(2,m1+1,m1), cilm(1,m1+1,m1)) * dp &
* prefactor(m+1) * m ! (m+1,m)
coeftp(m1) = coeftp(m1) + tempc
coeftps(m1) = coeftps(m1) + tempc ! reverse fsymsign
dp2 = -(m1*(m1+1) - (m**2)/u**2) * pm1 + z * dp
dp2s = (m1*(m1+1) - (m**2)/u**2) * pm1 + z * dp
tempc = dcmplx(cilm(1,m1+1,m1), - cilm(2,m1+1,m1)) &
* prefactor(m+1) ! (m+1,m)
coeftt(m1) = coeftt(m1) + tempc * dp2
coeftts(m1) = coeftts(m1) + tempc * dp2s
do l = m + 2, lmax_comp, 1
l1 = l + 1
p = z * ff1(l1,m1) * pm1 - ff2(l1,m1) * pm2
tempc = dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * p * (-l1) &
* prefactor(l)
coefr(m1) = coefr(m1) + tempc
coefrs(m1) = coefrs(m1) + tempc * fsymsign(l1,m1)
tempc = dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * p * (l1) &
* (l1+1) * prefactor(l)
coefrr(m1) = coefrr(m1) + tempc
coefrrs(m1) = coefrrs(m1) + tempc * fsymsign(l1,m1)
tempc = dcmplx(cilm(2,l1,m1), cilm(1,l1,m1)) * p &
* prefactor(l) * m
coefp(m1) = coefp(m1) + tempc
coefps(m1) = coefps(m1) + tempc * fsymsign(l1,m1)
tempc = -dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * p &
* prefactor(l) * m**2
coefpp(m1) = coefpp(m1) + tempc
coefpps(m1) = coefpps(m1) + tempc * fsymsign(l1,m1)
tempc = dcmplx(cilm(2,l1,m1), cilm(1,l1,m1)) * p * (-l1) &
* prefactor(l) * m
coefrp(m1) = coefrp(m1) + tempc
coefrps(m1) = coefrps(m1) + tempc * fsymsign(l1,m1)
dp = ( sqr(l+m) * sqr(l-m) * pm1 - l * z * p ) / u**2
tempc = dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * dp &
* prefactor(l)
coeft(m1) = coeft(m1) + tempc
! reverse fsymsign
coefts(m1) = coefts(m1) - tempc * fsymsign(l1,m1)
tempc = dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * dp * (-l1) &
* prefactor(l)
coefrt(m1) = coefrt(m1) + tempc
! reverse fsymsign
coefrts(m1) = coefrts(m1) - tempc * fsymsign(l1,m1)
tempc = dcmplx(cilm(2,l1,m1), cilm(1,l1,m1)) * dp &
* prefactor(l) * m
coeftp(m1) = coeftp(m1) + tempc
! reverse fsymsign
coeftps(m1) = coeftps(m1) - tempc * fsymsign(l1,m1)
dp2 = -(l * l1 -(m**2)/u**2) * p + z * dp
dp2s = -(l * l1 -(m**2)/u**2) * p * fsymsign(l1,m1) &
- z * dp * fsymsign(l1,m1)
tempc = dcmplx(cilm(1,l1,m1), - cilm(2,l1,m1)) * prefactor(l)
coeftt(m1) = coeftt(m1) + tempc * dp2
coeftts(m1) = coeftts(m1) + tempc * dp2s
pm2 = pm1
pm1 = p
end do
coefr(m1) = coefr(m1) * rescalem
coefrs(m1) = coefrs(m1) * rescalem
coefrr(m1) = coefrr(m1) * rescalem
coefrrs(m1) = coefrrs(m1) * rescalem
coeft(m1) = coeft(m1) * rescalem
coefts(m1) = coefts(m1) * rescalem
coeftt(m1) = coeftt(m1) * rescalem
coeftts(m1) = coeftts(m1) * rescalem
coefrt(m1) = coefrt(m1) * rescalem
coefrts(m1) = coefrts(m1) * rescalem
coefp(m1) = coefp(m1) * rescalem
coefps(m1) = coefps(m1) * rescalem
coefpp(m1) = coefpp(m1) * rescalem
coefpps(m1) = coefpps(m1) * rescalem
coeftp(m1) = coeftp(m1) * rescalem
coeftps(m1) = coeftps(m1) * rescalem
coefrp(m1) = coefrp(m1) * rescalem
coefrps(m1) = coefrps(m1) * rescalem
end do
if (lmax_comp /= 0) then
rescalem = rescalem * u
pmm = pmm / sqr(2*lmax_comp) * rescalem
tempc = dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) * pmm &
* (-lmax_comp-1) * prefactor(lmax_comp)
coefr(lmax_comp+1) = coefr(lmax_comp+1) + tempc
coefrs(lmax_comp+1) = coefrs(lmax_comp+1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) * pmm &
* (lmax_comp+1) * (lmax_comp+2) &
* prefactor(lmax_comp)
coefrr(lmax_comp+1) = coefrr(lmax_comp+1) + tempc
coefrrs(lmax_comp+1) = coefrrs(lmax_comp+1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(2,lmax_comp+1,lmax_comp+1), &
cilm(1,lmax_comp+1,lmax_comp+1)) * pmm &
* prefactor(lmax_comp) * lmax_comp
coefp(lmax_comp+1) = coefp(lmax_comp+1) + tempc
coefps(lmax_comp+1) = coefps(lmax_comp+1) + tempc ! fsymsign = 1
tempc = -dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) * pmm &
* prefactor(lmax_comp) * lmax_comp**2
coefpp(lmax_comp+1) = coefpp(lmax_comp+1) + tempc
coefpps(lmax_comp+1) = coefpps(lmax_comp+1) + tempc ! fsymsign = 1
tempc = dcmplx(cilm(2,lmax_comp+1,lmax_comp+1), &
cilm(1,lmax_comp+1,lmax_comp+1)) * pmm &
* (-lmax_comp-1) * prefactor(lmax_comp) * lmax_comp
coefrp(lmax_comp+1) = coefrp(lmax_comp+1) + tempc
coefrps(lmax_comp+1) = coefrps(lmax_comp+1) + tempc ! fsymsign = 1
dp = -lmax_comp * z * pmm / u**2
tempc = dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) * dp &
* prefactor(lmax_comp)
coeft(lmax_comp+1) = coeft(lmax_comp+1) + tempc
! reverse fsymsign
coefts(lmax_comp+1) = coefts(lmax_comp+1) - tempc
tempc = dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) * dp &
* (-lmax_comp-1) * prefactor(lmax_comp)
coefrt(lmax_comp+1) = coefrt(lmax_comp+1) + tempc
! reverse fsymsign
coefrts(lmax_comp+1) = coefrts(lmax_comp+1) - tempc
tempc = dcmplx(cilm(2,lmax_comp+1,lmax_comp+1), &
cilm(1,lmax_comp+1,lmax_comp+1)) * dp &
* prefactor(lmax_comp) * lmax_comp
coeftp(lmax_comp+1) = coeftp(lmax_comp+1) + tempc
coeftps(lmax_comp+1) = coeftps(lmax_comp+1) - tempc
dp2 = -(lmax_comp*(lmax_comp+1)-(lmax_comp**2)/u**2) * pmm + z * dp
dp2s = -(lmax_comp*(lmax_comp+1)-(lmax_comp**2)/u**2) * pmm - z * dp
tempc = dcmplx(cilm(1,lmax_comp+1,lmax_comp+1), &
- cilm(2,lmax_comp+1,lmax_comp+1)) &
* prefactor(lmax_comp)
coeftt(lmax_comp+1) = coeftt(lmax_comp+1) + tempc * dp2
coeftts(lmax_comp+1) = coeftts(lmax_comp+1) + tempc * dp2s
end if
! Note that the first angular derivatives are with repsect to z,
! but that the second is with respect to theta.
coefr0 = coefr0 * r0 / r_ex
coefr(2:lmax+1) = coefr(2:lmax+1) * r0 / r_ex
coefrs0 = coefrs0 * r0 / r_ex
coefrs(2:lmax+1) = coefrs(2:lmax+1) * r0 / r_ex
coefrt0 = -sint * coefrt0 * r0 / r_ex
coefrt(2:lmax+1) = -sint * coefrt(2:lmax+1) * r0 / r_ex
coefrts0 = -sint * coefrts0 * r0 / r_ex
coefrts(2:lmax+1) = -sint * coefrts(2:lmax+1) * r0 / r_ex
coefrr0 = coefrr0 * r0 / r_ex**2
coefrr(2:lmax+1) = coefrr(2:lmax+1) * r0 / r_ex**2
coefrrs0 = coefrrs0 * r0 / r_ex**2
coefrrs(2:lmax+1) = coefrrs(2:lmax+1) * r0 / r_ex**2
coeft0 = -sint * coeft0 * r0
coeft(2:lmax+1) = -sint * coeft(2:lmax+1) * r0
coefts0 = -sint * coefts0 * r0
coefts(2:lmax+1) = -sint * coefts(2:lmax+1) * r0
coeftt0 = coeftt0 * r0
coeftt(2:lmax+1) = coeftt(2:lmax+1) * r0
coeftts0 = coeftts0 * r0
coeftts(2:lmax+1) = coeftts(2:lmax+1) * r0
coeftp0 = -sint * coeftp0 * r0
coeftp(2:lmax+1) = -sint * coeftp(2:lmax+1) * r0
coeftps0 = -sint * coeftps0 * r0
coeftps(2:lmax+1) = -sint * coeftps(2:lmax+1) * r0
coefp0 = coefp0 * r0
coefp(2:lmax+1) = coefp(2:lmax+1) * r0
coefps0 = coefps0 * r0
coefps(2:lmax+1) = coefps(2:lmax+1) * r0
coefpp0 = coefpp0 * r0
coefpp(2:lmax+1) = coefpp(2:lmax+1) * r0
coefpps0 = coefpps0 * r0
coefpps(2:lmax+1) = coefpps(2:lmax+1) * r0
coefrp0 = coefrp0 * r0 / r_ex
coefrp(2:lmax+1) = coefrp(2:lmax+1) * r0 / r_ex
coefrps0 = coefrps0 * r0 / r_ex
coefrps(2:lmax+1) = coefrps(2:lmax+1) * r0 / r_ex
! Vzz = Vrr
coef(1) = dcmplx(coefrr0,0.0d0)
coef(2:lmax+1) = coefrr(2:lmax+1) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vzz(i,1:nlong) = grid(1:nlong)
if (i == 1) then
vyy(1,1:nlong) = 0.0d0 ! These derivatives are
vzz(1,1:nlong) = 0.0d0 ! undefined at the pole
vxy(1,1:nlong) = 0.0d0
vxz(1,1:nlong) = 0.0d0
vyz(1,1:nlong) = 0.0d0
else
! Vxx = 1/r Vr + 1/r^2 Vtt
coef(1) = dcmplx(coefr0/r_ex + coeftt0/r_ex**2,0.0d0)
coef(2:lmax+1) = (coefr(2:lmax+1)/r_ex &
+ coeftt(2:lmax+1)/r_ex**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxx(i,1:nlong) = grid(1:nlong)
! Vyy = 1/r Vr + 1/r^2 /tan(t) Vt + 1/r^2 /sin(t)^2 Vpp
coef(1) = dcmplx(coefr0/r_ex + coeft0/(r_ex**2)/tan(theta) &
+ coefpp0/(r_ex**2)/u**2 ,0.0d0)
coef(2:lmax+1) = (coefr(2:lmax+1)/r_ex &
+ coeft(2:lmax+1)/(r_ex**2)/tan(theta) + &
coefpp(2:lmax+1)/(r_ex**2)/u**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vyy(i,1:nlong) = grid(1:nlong)
! Vxy = 1/r^2 /sin(t) Vtp - cos(t)/sin(t)^2 /r^2 Vp
coef(1) = dcmplx(coeftp0/sint/r_ex**2 &
- coefp0/(r_ex**2)*z/u**2, 0.0d0)
coef(2:lmax+1) = (coeftp(2:lmax+1)/sint/r_ex**2 &
- coefp(2:lmax+1)/(r_ex**2)*z/u**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxy(i,1:nlong) = grid(1:nlong)
! Vxz = 1/r^2 Vt - 1/r Vrt
coef(1) = dcmplx(coeft0/r_ex**2 - coefrt0/r_ex, 0.0d0)
coef(2:lmax+1) = (coeft(2:lmax+1)/r_ex**2 &
- coefrt(2:lmax+1)/r_ex ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxz(i,1:nlong) = grid(1:nlong)
! Vyz = 1/r^2 /sin(t) Vp - 1/r /sin(t) Vrp
coef(1) = dcmplx(coefp0/sint/r_ex**2 - coefrp0/sint/r_ex, 0.0d0)
coef(2:lmax+1) = (coefp(2:lmax+1)/sint/r_ex**2 &
- coefrp(2:lmax+1)/sint/r_ex ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vyz(i,1:nlong) = grid(1:nlong)
end if
if (i /= 1) then ! don't compute value for south pole.
! Vzz = Vrr
coef(1) = dcmplx(coefrrs0,0.0d0)
coef(2:lmax+1) = coefrrs(2:lmax+1) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vzz(i_s,1:nlong) = grid(1:nlong)
! Vxx = 1/r Vr + 1/r^2 Vtt
coef(1) = dcmplx(coefrs0/r_ex + coeftts0/r_ex**2,0.0d0)
coef(2:lmax+1) = (coefrs(2:lmax+1)/r_ex &
+ coeftts(2:lmax+1)/r_ex**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxx(i_s,1:nlong) = grid(1:nlong)
! Vyy = 1/r Vr + 1/r^2 /tan(t) Vt + 1/r^2 /sin(t)^2 Vpp
coef(1) = dcmplx(coefrs0/r_ex + coefts0/(r_ex**2)/tan(theta) &
+ coefpps0/(r_ex**2)/u**2 ,0.0d0)
coef(2:lmax+1) = (coefrs(2:lmax+1)/r_ex &
+ coefts(2:lmax+1)/(r_ex**2)/tan(theta) + &
coefpps(2:lmax+1)/(r_ex**2)/u**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vyy(i_s,1:nlong) = grid(1:nlong)
! Vxy = 1/r^2 /sin(t) Vtp - cos(t)/sin(t)^2 /r^2 Vp
coef(1) = dcmplx(coeftps0/sint/r_ex**2 &
- coefps0/(r_ex**2)*z/u**2, 0.0d0)
coef(2:lmax+1) = (coeftps(2:lmax+1)/sint/r_ex**2 &
- coefps(2:lmax+1)/(r_ex**2)*z/u**2 ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxy(i_s,1:nlong) = grid(1:nlong)
! Vxz = 1/r^2 Vt - 1/r Vrt
coef(1) = dcmplx(coefts0/r_ex**2 - coefrts0/r_ex, 0.0d0)
coef(2:lmax+1) = (coefts(2:lmax+1)/r_ex**2 &
- coefrts(2:lmax+1)/r_ex ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform
vxz(i_s,1:nlong) = grid(1:nlong)
! Vyz = 1/r^2 /sin(t) Vp - 1/r /sin(t) Vrp
coef(1) = dcmplx(coefps0/sint/r_ex**2 - coefrps0/sint/r_ex, 0.0d0)
coef(2:lmax+1) = (coefps(2:lmax+1)/sint/r_ex**2 &
- coefrps(2:lmax+1)/sint/r_ex ) / 2.0d0
if (present(sampling)) then
if (sampling == 2) then
coef(lmax+2:2*lmax+3) = dcmplx(0.0d0,0.0d0)
end if
end if
call dfftw_execute(plan) ! take fourier transform