/
swav.f90
1907 lines (1891 loc) · 72 KB
/
swav.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
module swav
!
! ==============================================================
! This module contains routines for calculating and transforming
! scalar (SSWs) and vector spherical waves (VSWs). It depends on
! Amos' toms644.f to calculate the Bessel and Hankel functions
! using recurrence. Last modified: 18/03/2019.
!
! References:
! ----------
! [1] Stout, Auger and Lfait, J. Mod. Optics 49, 2129-2152 (2002).
! [2] Mishchenko, Travis and Lacis, "Scattering, absorption, and
! emission of light by small particles" (CUP, 2002).
! [3] Chew, J. Electron Waves Applic. 6, 133 (1992).
!
! List of routines:
! ----------------
!
! 1-calcVTACs> Evaluates the vector translation-addition
! coefficients.
!
! 2-calcSTACs> Calculates the scalar translation-addition
! coefficients.
!
! 3-calcVTACsAxial> Calculates the simpler VTACs along the z-axis.
!
! 4-calcSTACsAxial> Calculates the simpler STACs along the z-axis.
!
! 5-calcVSWs> Evaluates the vector spherical waves.
!
! 6-calcSSWs> Evaluates the scalar spherical waves.
!
! 7-calcJCoeffsPW> Evaluates coefficients of scatterer-centred
! VSW expansions for an incident planewave.
!
! 8-offsetCoeffsPW> Translates the VSW coefficients of a plane-
! wave to another origin.
!
! 9-calcWignerBigD> Evaluates the Wigner D-funcctions.
!
! 10-calcWignerLittled> Evaluates the Wigner d-funcctions.
!
! 11-calcWignerd0andMore> Evaluates the Wigner d-functions for n=0,
! and the derivative functions pi and tau.
!
! 12-calcRiccatiBessels> Evaluates the Riccati-Bessel functions psi
! or xi, and their derivatives.
!
! 13-calcSphBessels> Evaluates the spherical Bessel/Hankel funs.
!
! 14-xyz2rtp> (x,y,z) -> (r,theta,phi) for a point.
!
! 15-rtp2xyz> (r,theta,phi) -> (x,y,z) for a point.
!
! 16-calcVTrtp2xyz> Compute the transformation matrix for
! (E_r, E_t, E_p) -> (E_x, E_y, E_z)
!
! 17-calcVTxyz2rtp> Compute the transformation matrix for
! (E_x, E_y, E_z) -> (E_r, E_t, E_p)
! 18-nm2p> Compute a generalised index l=n(n+1)+m, for a unique (n,m)
!
! 19-p2nm> Compute unique (n,m) from a given composite index p
!
! 20-nm2pv2> Some recurrences are defined only for m >= 0, in which case
! we shall use a second version of the composite index
! p_v2:=n*(n+1)/2+m. This subroutins calculates p for (n,m)
!
! 21-testPmax> Tests pmax for commensurability, i.e. is pmax=nnmax*(nmax+2) and nmax=mmax? If not, then stop program
! ==============================================================
!
implicit none
!
private
!
public :: pi, tpi, xyz2rtp, calcVSWs, calcVTACs, calcVTACsAxial, &
calcRiccatiBessels, calcSphBessels, calcWignerBigD, &
calcCoeffsPW, offsetCoeffsPW, calcJCoeffsPW, calcVTxyz2rtp, &
calcWignerd0andMore, nm2p, calcVTrtp2xyz, testPmax, calcAbsMat, calcLamMat
!, &
!calcScatMat, calcStokesPhaseMat, calcStokesIncVec
!
real(8), parameter :: pi = acos(-1.0d0) ! pi
real(8), parameter :: hpi = 0.5d0*pi ! pi/2
real(8), parameter :: tpi = 2.0d0*pi ! 2*pi
real(8), parameter :: fpi = 4.0d0*pi ! 4*pi
complex(8), parameter :: imu = (0.0d0, 1.0d0) ! the imaginary unit i
!real(8), parameter :: eps0=8.8541878128d-12
!real(8), parameter :: mu0= 4.0*pi*1.0d-7
contains
!
subroutine calcVTACs(r0, k, regt, vtacs)
!
! ============================================================
! Compute the irregular (if regt=FALSE) normalised vector
! translation-addition coefficients alpha_{nu,mu;n,m} or
! the regular (if regt=TRUE) beta_{nu,mu;n,m}, as defined
! in Appendix B of Stout02, for a given k*r0 and
!
! 1 <= nu <= nmax, -nu <= mu <= nu;
! 1 <= n <= nmax, -n <= m <= n.
!
! Note that beta=Reg(alpha), where Reg() corresponds to
! replacing the spherical Hankel functions h_n with the
! spherical Bessel functions j_n. For convenience,
! we use a composite index l(n,m) = n*(n+1)+m
!
! 1 <= l:=n*(n+1)+m <= pmax:=nmax*(nmax+2),
!
! which is spanned twice to accounting for both the M_nm
! and the N_nm wave components.
!
! INPUT:
! ------
! r0(3) - relative position vector [REAL]
! k - wave number [COMPLEX]
! regt - regular or not [LOGICAL]
!
! IN/OUTPUT:
! ------
! vtacs(1:2*pmax,1:2*pmax) - the coefficients [COMPLEX]
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
real(8), dimension(3), intent(in) :: r0
complex(8), intent(in) :: k
logical, intent(in) :: regt
complex(8), intent(inout) :: vtacs(:, :)
! Local variables
integer :: nmax, pmax, n, m, l, nu, mu, lambda, i
real(8) :: numuterm1, numuterm2, numuterm3, numuterm4, numuterm5
real(8) :: nmterm1, nmterm2, nnuterm1, nnuterm2
complex(8) :: z
complex(8) :: scoeff(0:size(vtacs, 1)/2, 0:size(vtacs, 2)/2)
character(*), parameter :: myname = 'calcVTACs'
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
!
! Check the passed matrix and infer pmax with nmax
if (size(vtacs, 1) /= size(vtacs, 2)) then
write (*, '(A,A)') myname, '> ERROR: Passed matrix not square'
STOP
else
pmax = size(vtacs, 1)/2
nmax = int(sqrt(dble(pmax)))
i = 2*nmax*(nmax + 2)
if (i /= size(vtacs, 1)) then
if (i < size(vtacs, 1)) then
write (*, '(A,A)') myname, '> WARNING: 2*nmax*(nmax+2) < size(vtacs,1)'
else
write (*, '(A,A)') myname, '> ERROR: 2*nmax*(nmax+2) > size(vtacs,1)'
STOP
end if
end if
end if
!
vtacs = 0 ! Just in case
!
call calcSTACs(r0, k, pmax, regt, scoeff)
!
lambda = 0
nuloop: do nu = 1, nmax
muloop: do mu = -nu, nu
lambda = lambda + 1
numuterm1 = sqrt(dble((nu - mu)*(nu + mu + 1)))
numuterm2 = sqrt(dble((nu + mu)*(nu - mu + 1)))
numuterm3 = sqrt(dble((nu - mu)*(nu + mu)))
numuterm4 = sqrt(dble((nu - mu)*(nu - mu - 1)))
numuterm5 = sqrt(dble((nu + mu)*(nu + mu - 1)))
l = 0
nloop: do n = 1, nmax
nnuterm1 = 0.5d0/sqrt(dble(nu*(nu + 1)*n*(n + 1)))
nnuterm2 = nnuterm1*sqrt(dble(2*nu + 1)/dble(2*nu - 1))
mloop: do m = -n, n
l = l + 1
nmterm1 = sqrt(dble((n - m)*(n + m + 1)))
nmterm2 = sqrt(dble((n + m)*(n - m + 1)))
!
! A_{nu,mu;n,m} block
z = 2*mu*m*scoeff(lambda, l)
if (abs(mu + 1) <= nu .and. abs(m + 1) <= n) then
z = z + nmterm1*numuterm1*scoeff(lambda + 1, l + 1)
end if
if (abs(mu - 1) <= nu .and. abs(m - 1) <= n) then
z = z + nmterm2*numuterm2*scoeff(lambda - 1, l - 1)
end if
z = nnuterm1*z
vtacs(lambda, l) = z
vtacs(lambda + pmax, l + pmax) = z
!
! B_{nu,mu;n,m} block
z = 0
if (abs(mu) < nu) then
call nm2p(nu - 1, mu, i)
z = z + 2*m*numuterm3*scoeff(i, l)
end if
if (abs(mu + 1) < nu .and. abs(m + 1) <= n) then
call nm2p(nu - 1, mu + 1, i)
z = z + nmterm1*numuterm4*scoeff(i, l + 1)
end if
if (abs(mu - 1) < nu .and. abs(m - 1) <= n) then
call nm2p(nu - 1, mu - 1, i)
z = z - nmterm2*numuterm5*scoeff(i, l - 1)
end if
z = -imu*nnuterm2*z
vtacs(lambda + pmax, l) = z
vtacs(lambda, l + pmax) = z
!
end do mloop
end do nloop
!
end do muloop
end do nuloop
!
end subroutine calcVTACs
!
subroutine calcSTACs(r0, k, pmax, regt, scoeff)
!
! ============================================================
! Compute the normalised scalar translation-addition
! coefficients alphas_{nu,mu;n,m} or betas_{nu,mu;n,m} for
!
! 0 <= nu <= nmax, -nu <= mu <= nu;
! 0 <= n <= nmax, -n <= m <= n;
!
! using the recurrence described in Appendix C of Stout02
! [J. Mod. Optics 49, 2129 (2002)] for a given k*r0.
!
! Note that betas=Reg(alphas), where Reg() corresponds to
! replacing the spherical Hankel functions h_n with the
! spherical Bessel functions j_n. Also, the paper Chew92
! [J. Electron Waves Applic. 6, 133 (1992)] is very helpful
! for understanding the implementation.
!
! For convenience, we use a composite index l(n,m) when m
! can be +ve and -ve, and l_{v2}(n,m) for non-negative m:
!
! 0 <= l=n*(n+1)+m <= pmax=nmax*(nmax+2);
! 0 <= l_{v2}=n*(n+1)/2+m <= l_{v2}^{max} = nmax*(nmax+3)/2.
!
! INPUT:
! ------
! r0(3) - relative position vector [REAL]
! k - wave-vector amplitude [COMPLEX]
! pmax - maximal composite index [INTEGER]
! regt - 'take regular part' or not [LOGICAL]
!
! OUTPUT:
! ------
! scoeff(0:pmax,0:pmax) - coefficients [COMPLEX]
!
! Output correponds to the scalar translation-addition
! coefficients alphas (irregular, for regt=FALSE) or betas
! (regular, for regt=TRUE) as defined in Appendix C of
! Stout02. Recall: pmax=nmax*(nmax+2).
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
real(8), dimension(3), intent(in) :: r0
complex(8), intent(in) :: k
integer, intent(in) :: pmax
logical, intent(in) :: regt
complex(8), dimension(0:pmax, 0:pmax), intent(out) :: scoeff
! Local variables
character(*), parameter :: myName = 'calcSTACs'
integer :: n, m, l, nmax, pmax2, nu, mu, numax, lambda, lambdamax
integer :: nu2, ndum1, ndum2, npm, nmm, ipass
integer :: l1, l1v2, l2v2, lv2, lam
real(8) :: r, theta, phi, ctheta, rtp(3), ddum
complex(8) :: z
complex(8), dimension(:), allocatable :: expo, ang, bes
complex(8), dimension(:, :), allocatable :: c
real(8), dimension(:), allocatable :: ap, am, bp, bm, d
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
!
! Check the maximal value of the composite index and get nmax
call testPmax(myname, pmax, nmax)
!
scoeff(:, :) = cmplx(0, 0, 8) ! just in case
!
! The recurrence will be applied (twice) to non-negative m, so
! the composite index of some arrays is bounded above by pmax2:
pmax2 = nmax*(nmax + 3)/2
!
! The recurrence involves extraneous elements (see Chew92),
! so the maximum value of nu needs to be doubled.
numax = nmax + nmax + 1
lambdamax = numax*(numax + 2)
!
! Allocate all internal arrays
allocate (expo(-numax:numax), d(0:lambdamax), ang(0:lambdamax))
allocate (bes(0:numax), c(0:lambdamax, 0:pmax2))
allocate (am(0:lambdamax), ap(0:lambdamax))
allocate (bm(0:lambdamax), bp(0:lambdamax))
!
! Convert r0 from cartesians to spherical polars
call xyz2rtp(r0, rtp, ctheta)
r = rtp(1)
z = k*r ! complex argument for spherical Bessel functions
theta = rtp(2) ! between 0 and Pi
phi = rtp(3) ! between 0 and 2*Pi
!
! Compute angular quantities required for eqn. C3 of Stout02.
do mu = -numax, numax
expo(mu) = exp(imu*mu*phi)
end do
call calcWignerd0andMore(ctheta, lambdamax, d)
!
! Compute the auxiliary coefficients a_nm^+/- and b_nm^+/-
! defined in eqn. C2, as well as the radially-INdependent
! part of the quantities in C3 of Stout02.
do nu = 0, numax
nu2 = 2*nu
ndum1 = nu2 + 1
ddum = sqrt(dble(ndum1))
ndum2 = ndum1*(nu2 - 1)
ndum1 = ndum1*(nu2 + 3)
do mu = -nu, nu
call nm2p(nu, mu, lambda)
npm = nu + mu
nmm = nu - mu
ap(lambda) = -sqrt(dble((npm + 1)*(nmm + 1))/dble(ndum1))
am(lambda) = sqrt(dble((npm)*(nmm))/dble(ndum2))
bp(lambda) = sqrt(dble((npm + 2)*(npm + 1))/dble(ndum1))
bm(lambda) = sqrt(dble((nmm)*(nmm - 1))/dble(ndum2))
! Get alpha0/bhn=beta0/bjn defined in equation C.3
ang(lambda) = (-1)**(nu)*ddum*d(lambda)*expo(-mu)
end do
end do
!
! Apply the recurrence on n to build up c_{nu,mu;n,m} in two
! passes: 1st for m >= 0; 2nd for m < 0. See eqn. C1 of Stout02
passes: do ipass = 1, 2
!
call calcSphBessels(z, numax, regt, bes)
!
! Now apply recursion in eqn. C1 of Stout02
mloop: do m = 0, nmax
!
! Referring to Fig. 2 of Chew92, we first step along the
! diagonal and compute c_{nu,mu; m,m} for all nu and mu
!
if (m == 0) then ! initialise using eqn. C3 of Stout02.
do nu = 0, numax
do mu = -nu, nu
call nm2p(nu, mu, lambda)
c(lambda, 0) = ang(lambda)*bes(nu)
end do
end do
else ! n = m > 0 => use bottom eqn. C1
call nm2pv2(m, m, lv2)
call nm2p(m - 1, m - 1, l1)
call nm2pv2(m - 1, m - 1, l1v2)
do nu = 0, numax - m ! see Fig.3 in Chew92
do mu = -nu, nu
call nm2p(nu, mu, lambda)
c(lambda, lv2) = 0 ! initialise
if (nu > 0 .and. abs(mu - 1) < nu) then
call nm2p(nu - 1, mu - 1, lam)
c(lambda, lv2) = c(lambda, lv2) + bp(lam)*c(lam, l1v2)
end if
call nm2p(nu + 1, mu - 1, lam)
c(lambda, lv2) = c(lambda, lv2) + bm(lam)*c(lam, l1v2)
c(lambda, lv2) = c(lambda, lv2)/bp(l1)
end do
end do
end if
!
! Now compute c_{nu,mu;n,m} for n > m (and all nu and mu).
!
nloop: do n = m + 1, nmax
call nm2pv2(n, m, lv2)
call nm2p(n - 1, m, l1)
call nm2pv2(n - 1, m, l1v2)
call nm2pv2(n - 2, m, l2v2)
do nu = 0, numax - n ! see Fig.3 in Chew92
do mu = -nu, nu
call nm2p(nu, mu, lambda)
c(lambda, lv2) = 0 ! initialise
if (n - 2 >= m) then
c(lambda, lv2) = c(lambda, lv2) - am(l1)*c(lambda, l2v2)
end if
if (nu - 1 >= abs(mu)) then
call nm2p(nu - 1, mu, lam)
c(lambda, lv2) = c(lambda, lv2) + ap(lam)*c(lam, l1v2)
end if
call nm2p(nu + 1, mu, lam)
c(lambda, lv2) = c(lambda, lv2) + am(lam)*c(lam, l1v2)
c(lambda, lv2) = c(lambda, lv2)/ap(l1)
end do
end do
end do nloop
!
end do mloop
!
! Assign final values to the output scoeff. Could do this
! inside mloop and nloop, but post-recurrence asignment
! seems cleaner for debugging purposes.
mloop2: do m = 0, nmax
nloop2: do n = m, nmax
call nm2pv2(n, m, lv2)
if (ipass == 1) then
call nm2p(n, m, l)
do nu = 0, nmax
do mu = -nu, nu
call nm2p(nu, mu, lambda)
scoeff(lambda, l) = c(lambda, lv2)
end do
end do
elseif (ipass == 2 .and. m > 0) then ! use eqn. C4 from Stout02
call nm2p(n, -m, l)
do nu = 0, nmax
do mu = -nu, nu
call nm2p(nu, mu, lambda)
call nm2p(nu, -mu, lam)
if (regt) then
ndum1 = (-1)**(mu - m)
else
!ndum1 = -(-1)**(n+nu+mu-m)
ndum1 = (-1)**(n + nu + mu - m)
end if
scoeff(lambda, l) = ndum1*dconjg(c(lam, lv2))
end do
end do
end if
end do nloop2
end do mloop2
!
! For the second pass, change the radial argument
! as prescribed in eqn. C4 of Stout02.
if (regt) then
z = dconjg(z)
else
z = -dconjg(z)
end if
!
end do passes
!
! Dellocate internal arrays.
deallocate (d, expo, ang, bes)
deallocate (c, am, ap, bm, bp)
!
end subroutine calcSTACs
!
!-------------------------------------------------------------------
! ======= SIMPLER ROUTINES FOR AXIAL TRANSLATION START HERE =======
!-------------------------------------------------------------------
!
subroutine calcVTACsAxial(r0, k, pmax, regt, flip, vtacs, mqn_)
!
! ============================================================
! Compute the irregular (if regt=FALSE) normalised vector
! translation-addition coefficients alpha_{nu,mu;n,m} or
! the regular (if regt=TRUE) beta_{nu,mu;n,m}, as defined
! in Appendix B of Stout02, for a given k*r0 and
!
! 1 <= nu <= nmax, -nu <= mu <= nu;
! 1 <= n <= nmax, m = mu.
!
! Note that beta=Reg(alpha), where Reg() corresponds to
! replacing the spherical Hankel functions h_n with the
! spherical Bessel functions j_n. For convenience,
! we use a composite index l(n,m):
!
! 0 <= l:=n*(n+1)+m <= 2*pmax:=2*nmax*(nmax+2),
!
! where the prefactor of 2 is due to accounting for both
! the M_nm and the N_nm wave components.
!
! INPUT:
! ------
! r0 - the z-axial displacelement distance [+ve REAL]
! k - wave-vector amplitude [COMPLEX]
! pmax - maximal composite index [INTEGER]
! regt - 'take regular part' or not [LOGICAL]
! flip -
! mqn_ - change from qnm to mqn indexing [LOGICAL]
!
! OUTPUT:
! ------
! vtacs(1:2*pmax,1:2*pmax) - the coefficients [COMPLEX]
! with (m,n,q) indexing to produce
! block-diagonal form.
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
real(8), intent(in) :: r0
complex(8), intent(in) :: k
integer, intent(in) :: pmax
logical, intent(in) :: regt, flip
logical, intent(in), optional :: mqn_
complex(8), dimension(2*pmax, 2*pmax), intent(out) :: vtacs
! Local variables
integer :: n, m, l, nmax, nu, mu, lambda, lam1, lo, ln, lnu
real(8) :: numuterm1, numuterm2, numuterm3, &
numuterm4, numuterm5
real(8) :: nmterm1, nmterm2
real(8) :: nnuterm1, nnuterm2
complex(8) :: z
complex(8), dimension(0:pmax, 0:pmax) :: scoeff
character(*), parameter :: myname = 'calcVTACs'
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
!
! Check the maximal value of the composite index and nmax
call testPmax(myname, pmax, nmax)
!
vtacs = 0 ! Just in case
!
call calcSTACsAxial(r0, k, pmax, regt, flip, scoeff)
!
! Can reorder loops and accumulate (m,q,n) index while
! using the nm2p function to compute the (q,n,m) index
lo = 1
muloop: do mu = -nmax, nmax
lnu = lo
nuloop: do nu = max(1, abs(mu)), nmax
call nm2p(nu, mu, lambda)
numuterm1 = sqrt(dble((nu - mu)*(nu + mu + 1)))
numuterm2 = sqrt(dble((nu + mu)*(nu - mu + 1)))
numuterm3 = sqrt(dble((nu - mu)*(nu + mu)))
numuterm4 = sqrt(dble((nu - mu)*(nu - mu - 1)))
numuterm5 = sqrt(dble((nu + mu)*(nu + mu - 1)))
!
ln = lo
nloop: do n = max(1, abs(mu)), nmax
nnuterm1 = 0.5d0/sqrt(dble(nu*(nu + 1)*n*(n + 1)))
nnuterm2 = nnuterm1*sqrt(dble(2*nu + 1)/dble(2*nu - 1))
!
m = mu
!
call nm2p(n, m, l)
nmterm1 = sqrt(dble((n - m)*(n + m + 1)))
nmterm2 = sqrt(dble((n + m)*(n - m + 1)))
!
! A_{nu,mu;n,m} block
z = 2*mu*m*scoeff(lambda, l)
if (abs(mu + 1) <= nu .and. abs(m + 1) <= n) then
z = z + nmterm1*numuterm1*scoeff(lambda + 1, l + 1)
end if
if (abs(mu - 1) <= nu .and. abs(m - 1) <= n) then
z = z + nmterm2*numuterm2*scoeff(lambda - 1, l - 1)
end if
z = nnuterm1*z
vtacs(lnu, ln) = z
vtacs(lnu + 1, ln + 1) = z
!
! B_{nu,mu;n,m} block
z = 0
if (abs(mu) < nu) then
call nm2p(nu - 1, mu, lam1)
z = z + 2*m*numuterm3*scoeff(lam1, l)
end if
if (abs(mu + 1) < nu .and. abs(m + 1) <= n) then
call nm2p(nu - 1, mu + 1, lam1)
z = z + nmterm1*numuterm4*scoeff(lam1, l + 1)
end if
if (abs(mu - 1) < nu .and. abs(m - 1) <= n) then
call nm2p(nu - 1, mu - 1, lam1)
z = z - nmterm2*numuterm5*scoeff(lam1, l - 1)
end if
z = -imu*nnuterm2*z
vtacs(lnu + 1, ln) = z
vtacs(lnu, ln + 1) = z
!
ln = ln + 2
end do nloop
!
lnu = lnu + 2
end do nuloop
lo = lo + 2*(nmax - max(1, abs(m)) + 1)
end do muloop
!
end subroutine calcVTACsAxial
!
subroutine calcSTACsAxial(r0, k, pmax, regt, flip, stacs)
!
! ============================================================
! Compute the normalised scalar translation-addition
! coefficients alphas_{nu,mu;n,m} or betas_{nu,mu;n,m} for
!
! 0 <= nu <= nmax, -nu <= mu <= nu;
! 0 <= n <= nmax, m = mu
!
! using the recurrence described in Appendix C of Stout02
! [J. Mod. Optics 49, 2129 (2002)] for a given k*r0, where
! r0 is a non-negative displacement along the z-axis.
!
! Note that betas=Reg(alphas), where Reg() corresponds to
! replacing the spherical Hankel functions h_n with the
! spherical Bessel functions j_n. Also, the paper Chew92
! [J. Electron Waves Applic. 6, 133 (1992)] is very helpful
! for understanding the implementation.
!
! For convenience, we use a composite index l(n,m) when m
! can be +ve and -ve, and l_{v2}(n,m) for non-negative m:
!
! 0 <= l=n*(n+1)+m <= pmax=nmax*(nmax+2);
! 0 <= l_{v2}=n*(n+1)/2+m <= l_{v2}^{max} = nmax*(nmax+3)/2.
!
! INPUT:
! ------
! r0 - displacement distance [REAL]
! k - wave-vector amplitude [COMPLEX]
! pmax - maximal composite index [INTEGER]
! regt - 'take regular part' or not [LOGICAL]
!
! OUTPUT:
! ------
! stacs(0:pmax,0:pmax) - coefficients [COMPLEX]
!
! Output correponds to the scalar translation-addition
! coefficients alphas (irregular, for regt=FALSE) or betas
! (regular, for regt=TRUE) as defined in Appendix C of
! Stout02. Recall: pmax=nmax*(nmax+2).
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
real(8), intent(in) :: r0
complex(8), intent(in) :: k
integer, intent(in) :: pmax
logical, intent(in) :: regt
logical, intent(in) :: flip
complex(8), dimension(0:pmax, 0:pmax), intent(out) :: stacs
! Local variables
character(*), parameter :: myName = 'calcSTACsAxial'
integer :: n, m, l, nmax, pmax2, nu, mu, numax, lambda, lambdamax
integer :: nu2, ndum1, ndum2, npm, nmm, ipass
integer :: l1, l1v2, l2v2, lv2, lam
real(8) :: r, ddum
complex(8) :: z
complex(8), dimension(:), allocatable :: bes
complex(8), dimension(:, :), allocatable :: c
real(8), dimension(:), allocatable :: ap, am, bp, bm, ang
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
!
! Check the maximal value of the composite index and get nmax
call testPmax(myname, pmax, nmax)
!
stacs(:, :) = cmplx(0, 0, 8) ! just in case
!
! The recurrence will be applied (twice) to non-negative m, so
! the composite index of some arrays is bounded above by pmax2:
pmax2 = nmax*(nmax + 3)/2
!
! The recurrence involves extraneous elements (see Chew92),
! so the maximum value of nu needs to be doubled.
numax = nmax + nmax + 1
lambdamax = numax*(numax + 2)
!
! Allocate all internal arrays
allocate (ang(0:numax))
allocate (bes(0:numax), c(0:lambdamax, 0:pmax2))
allocate (am(0:lambdamax), ap(0:lambdamax))
allocate (bm(0:lambdamax), bp(0:lambdamax))
!
r = r0
z = k*r ! complex argument for spherical Bessel functions
!
! Compute angular quantities required for eqn. C3 of Stout02.
! >>> This can be simplified for axial translation !!! <<<
!call calcWignerd0andMore(1.0d0, lambdamax, d)
!
! Compute the auxiliary coefficients a_nm^+/- and b_nm^+/-
! defined in eqn. C2, as well as the radially-INdependent
! part of the quantities in C3 of Stout02.
do nu = 0, numax
nu2 = 2*nu
ndum1 = nu2 + 1
ddum = sqrt(dble(ndum1))
ndum2 = ndum1*(nu2 - 1)
ndum1 = ndum1*(nu2 + 3)
do mu = -nu, nu
call nm2p(nu, mu, lambda)
npm = nu + mu
nmm = nu - mu
ap(lambda) = -sqrt(dble((npm + 1)*(nmm + 1))/dble(ndum1))
am(lambda) = sqrt(dble((npm)*(nmm))/dble(ndum2))
bp(lambda) = sqrt(dble((npm + 2)*(npm + 1))/dble(ndum1))
bm(lambda) = sqrt(dble((nmm)*(nmm - 1))/dble(ndum2))
end do
! Get alpha0/bhn=beta0/bjn defined in equation C.3,
! which are non-zero only when mu=0 for axial translation
ang(nu) = (-1)**(nu)*ddum
if (flip) ang(nu) = ang(nu)*(-1)**nu
end do
!
!stop
c(:, :) = 0
!
! Apply the recurrence on n to build up c_{nu,mu;n,m} in two
! passes: 1st for m >= 0; 2nd for m < 0. See eqn. C1 of Stout02
passes: do ipass = 1, 2
!
call calcSphBessels(z, numax, regt, bes)
!
! Now apply recursion in eqn. C1 of Stout02
mloop: do m = 0, nmax
!
! Referring to Fig. 2 of Chew92, we first step along the
! diagonal and compute c_{nu,mu; m,m} for all nu and mu
!
if (m == 0) then ! initialise using eqn. C3 of Stout02.
mu = m
do nu = abs(mu), numax
call nm2p(nu, mu, lambda)
c(lambda, 0) = ang(nu)*bes(nu)
end do
else ! n = m > 0 => use bottom eqn. C1
call nm2pv2(m, m, lv2)
call nm2p(m - 1, m - 1, l1)
call nm2pv2(m - 1, m - 1, l1v2)
mu = m
do nu = abs(mu), numax - m ! see Fig.3 in Chew92
call nm2p(nu, mu, lambda)
c(lambda, lv2) = 0 ! initialise
if (nu > 0 .and. abs(mu - 1) < nu) then
call nm2p(nu - 1, mu - 1, lam)
c(lambda, lv2) = c(lambda, lv2) + bp(lam)*c(lam, l1v2)
end if
call nm2p(nu + 1, mu - 1, lam)
c(lambda, lv2) = c(lambda, lv2) + bm(lam)*c(lam, l1v2)
c(lambda, lv2) = c(lambda, lv2)/bp(l1)
end do
end if
!
! Now compute c_{nu,mu;n,m} for n > m (and all nu and mu).
!
nloop: do n = m + 1, nmax
call nm2pv2(n, m, lv2)
call nm2p(n - 1, m, l1)
call nm2pv2(n - 1, m, l1v2)
call nm2pv2(n - 2, m, l2v2)
mu = m
do nu = max(0, abs(mu)), numax - n ! see Fig.3 in Chew92
call nm2p(nu, mu, lambda)
c(lambda, lv2) = 0 ! initialise
if (n - 2 >= m) then
c(lambda, lv2) = c(lambda, lv2) - am(l1)*c(lambda, l2v2)
end if
if (nu - 1 >= abs(mu)) then
call nm2p(nu - 1, mu, lam)
c(lambda, lv2) = c(lambda, lv2) + ap(lam)*c(lam, l1v2)
end if
call nm2p(nu + 1, mu, lam)
c(lambda, lv2) = c(lambda, lv2) + am(lam)*c(lam, l1v2)
c(lambda, lv2) = c(lambda, lv2)/ap(l1)
end do
end do nloop
!
end do mloop
!
! Assign final values to the output stacs. Could do this
! inside mloop and nloop, but post-recurrence asignment
! seems cleaner for debugging purposes.
mloop2: do m = 0, nmax
nloop2: do n = m, nmax
call nm2pv2(n, m, lv2)
if (ipass == 1) then
call nm2p(n, m, l)
mu = m
do nu = abs(mu), nmax
call nm2p(nu, mu, lambda)
stacs(lambda, l) = c(lambda, lv2)
end do
elseif (ipass == 2 .and. m > 0) then ! use eqn. C4 from Stout02
call nm2p(n, -m, l)
mu = -m ! Minus sign should really be here!!!
do nu = abs(mu), nmax
call nm2p(nu, mu, lambda)
call nm2p(nu, -mu, lam)
if (regt) then
ndum1 = (-1)**(mu - m)
else
ndum1 = (-1)**(n + nu + mu - m)
end if
stacs(lambda, l) = ndum1*dconjg(c(lam, lv2))
end do
end if
end do nloop2
end do mloop2
!
! For the second pass, change the radial argument
! as prescribed in eqn. C4 of Stout02.
if (regt) then
z = dconjg(z)
else
z = -dconjg(z)
end if
!
end do passes
!
! Dellocate internal arrays.
deallocate (ang, bes)
deallocate (c, am, ap, bm, bp)
!
end subroutine calcSTACsAxial
!
subroutine calcVSWs(r, k, pmax, regt, cart, waves, wavesB)
!
! ============================================================
! Evaluate (at r) the normalised vector spherical waves, M_nm
! and N_nm for l=n(n+1)+m <= pmax, as defined by equation A4
! in Appendix A of Stout02 [J. Mod. Optics 49, 2129 (2002)].
!
! INPUT:
! ------
! r(3) - Cartiesian coordinate of a point in 3D [REAL]
! k - wave-vector amplitude [COMPLEX]
! pmax - maximal composite index [INTEGER]
! regt - 'take regular part' or not [LOGICAL]
! cart - convert to cartesian coordinates or not [LOGICAL]
!
! OUTPUT:
! ------
! waves(2*pmax,3) - [COMPLEX] elements of the abstract column
! vector defined in eqn.B1 of Stout02, here
! taking the form of a (2*pmax by 3) matrix.
!wavesB(2*pmax,3) - is the same as waves, only by changing the
! position of the elements for magnetic field multiplied by -ik
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
real(8), dimension(3), intent(in) :: r
complex(8), intent(in) :: k
integer, intent(in) :: pmax
logical, intent(in) :: regt, cart
complex(8), dimension(3, 2*pmax), intent(out) :: waves
complex(8), dimension(3, 2*pmax), intent(out), optional :: wavesB
! Local variables
character(*), parameter :: myname = 'calcVSWs'
integer :: n, m, l, nmax
real(8) :: rtp(3), cth, npref1, npref2, s_nm, u_nm
real(8) :: transform(3, 3)
real(8), dimension(0:pmax) :: d, pi, tau
complex(8) :: z, y_nm(3), x_nm(3), z_nm(3)
complex(8), allocatable :: expo(:), bes(:), dbes(:)
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
! write(*,*)'waves',waves
!waves=cmplx(0.0d0,0.0d0)
!wavesB=cmplx(0.0d0,0.0d0)
! Check the maximal value of the composite index
call testPmax(myname, pmax, nmax)
!
allocate (expo(-nmax:nmax), bes(nmax), dbes(nmax))
!
call xyz2rtp(r, rtp, cth) ! only need the cos(theta)
call calcWignerd0andMore(cth, pmax, d, pi, tau)
! Compute the azimuth-angle exponentials
do m = -nmax, nmax
expo(m) = exp(imu*m*rtp(3))
end do
! Compute the derivative of xi_n(kr) w.r.t. kr
z = k*rtp(1) ! z=kr !!! PROBLEMS when z=0 !!!
if (CDABS(z) < 1.0e-30) then
z = z + 1.0e-30 !! avoid division by 0 below by shifting z slightly away from origin
end if
call calcRiccatiBessels(z, nmax, regt, bes, dbes)
bes(1:nmax) = bes(1:nmax)/z ! Get spherical Bess/Hank
if (regt .eqv. .false.) then
!write(*,*)'bes',bes
end if
nloop: do n = 1, nmax
npref1 = sqrt(dble((2*n + 1)*n*(n + 1))/fpi)
npref2 = npref1/dble(n*(n + 1))
mloop: do m = -n, n
call nm2p(n, m, l)
y_nm(1) = npref1*d(l)*expo(m) !note: ynm here is sqrt(n(n+1))*ynm in eq. A1
y_nm(2:3) = 0
s_nm = (-1)**m*npref2*tau(l)
u_nm = (-1)**m*npref2*pi(l)
!
x_nm(1) = y_nm(2) !note: x_nm here is = -xnm in eq. A1
x_nm(2) = u_nm*imu*expo(m)
x_nm(3) = -s_nm*expo(m)
!
z_nm(1) = x_nm(1)
z_nm(2) = -x_nm(3)
z_nm(3) = x_nm(2)
!
! First compute M_nm and then N_nm
waves(1:3, l) = bes(n)*x_nm(1:3) ! M_nm
waves(1:3, l + pmax) = (bes(n)*y_nm(1:3) + dbes(n)*z_nm(1:3))/z ! N_nm
if (present(wavesB)) then
wavesB(1:3, l) = -imu*k*(bes(n)*y_nm(1:3) + dbes(n)*z_nm(1:3))/z ! N_nm, Note:for vector harmonics of B, -ik is multiplied
wavesB(1:3, l + pmax) = -imu*k*bes(n)*x_nm(1:3) ! M_nm, Note:for vector harmonics of B, -ik is multiplied
end if
end do mloop
end do nloop
if (cart) then
transform(1, 1:3) = (/sin(rtp(2))*cos(rtp(3)), &
cos(rtp(2))*cos(rtp(3)), &
-sin(rtp(3))/)
transform(2, 1:3) = (/sin(rtp(2))*sin(rtp(3)), &
cos(rtp(2))*sin(rtp(3)), &
cos(rtp(3))/)
transform(3, 1:3) = (/cos(rtp(2)), -sin(rtp(2)), 0.0d0/)
waves = matmul(transform, waves)
if (present(wavesB)) wavesB = matmul(transform, wavesB)
end if
!
deallocate (expo, bes, dbes)
end subroutine calcVSWs
!
!subroutine calcSSWs(xyz,k,pmax,regt,psi)
!
! ============================================================
! Evaluate (at xyz) the spherical waves psi_nm as defined by
! equation 13a in Chew92 [J. Electron Waves Applic. 6, 133
! (1992)]. Note: l:=n(n+1)+m <= pmax. Also, Chew includes the
! Condon-Shortley phase (-1)^m in the definition of the
! spherical harmonics in eqn. 3b, implying that the Legendre
! polynomials in that equation do not contain the (-1)^m.
! In this routine, the (-1)^m is included in the Legendre
! polynomials obtained from the Wigner d-function.
!
! INPUT:
! ------
! xyz(3) - Cartiesian coordinate of a point in 3D [REAL]
! k - wave-vector amplitude [COMPLEX]
! pmax - maximal composite index [INTEGER]
! regt - 'take regular part' or not [LOGICAL]
!
! OUTPUT:
! ------
! psi(0:pmax) - [COMPLEX] elements of the the spherical waves psi_nm as defined by
! equation 13a in Chew92
! ============================================================
!
!---------------------------------------------------
! Start of variable declarations.
!---------------------------------------------------
! Passed variables
! real(8), dimension(3), intent(in) :: xyz
! complex(8), intent(in) :: k
! integer, intent(in) :: pmax
! logical, intent(in) :: regt
! complex(8), dimension(0:pmax), intent(out) :: psi
! ! Local variables
! character(*), parameter :: myname='calc'
! integer :: nmax,n,m,l
! real(8) :: rtp(3), dum, d(0:pmax)
! complex(8), allocatable :: bess(:), expo(:)
!---------------------------------------------------
! End of variable declarations. Directives start now
!---------------------------------------------------
!
! call testPmax(myname,pmax,nmax)
!allocate(bess(0:nmax), expo(-nmax:nmax))
!
! call xyz2rtp(xyz,rtp,dum) ! dum = cos(theta)
!call calcWignerd0andMore(dum,pmax,d)
!call calcSphBessels(k*rtp(1),nmax,regt,bess)
! do m=-nmax,nmax
! expo(m) = exp(imu*m*rtp(3))
! enddo
!
! do n=0,nmax
! dum=sqrt(dble(2*n+1)/fpi)
! do m=-n,n
! call nm2p(n,m,l)
! psi(l) = bess(n) * dum * d(l) * expo(m) !(-1)^m in d(l)
! !write(*,'(A,2(1x,i2),2(1x,es17.10e2))') 'psi>',n,m,psi(l)
! enddo
! enddo
!
! deallocate(bess,expo)
!
!end subroutine calcSSWs
!call calcJCoeffsPW( &
! ipwE0 = ipwE0(:,i), &
! kVec = hostK_G*ipwDirn(:,i), &
! ipwCoeffsJ = cJ_(:,1,i), &