The Elk Code
genylmv.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2002-2005 J. K. Dewhurst, S. Sharma and C. Ambrosch-Draxl.
3 ! This file is distributed under the terms of the GNU Lesser General Public
4 ! License. See the file COPYING for license details.
5 
6 !BOP
7 ! !ROUTINE: genylmv
8 ! !INTERFACE:
9 pure subroutine genylmv(t4pil,lmax,v,ylm)
10 ! !INPUT/OUTPUT PARAMETERS:
11 ! t4pil : .true. if the plane wave prefactor should be included (in,logical)
12 ! lmax : maximum angular momentum (in,integer)
13 ! v : input vector (in,real(3))
14 ! ylm : array of spherical harmonics (out,complex((lmax+1)**2))
15 ! !DESCRIPTION:
16 ! Generates a sequence of spherical harmonics, including the Condon-Shortley
17 ! phase, evaluated at angles $(\theta,\phi)$ for $0<l<l_{\rm max}$. The values
18 ! are returned in a packed array {\tt ylm} indexed with $j=l(l+1)+m+1$. If
19 ! {\tt t4pil} is set to {\tt .true.} then a prefactor of $4\pi(-i)^l$ is
20 ! included. This is required if the spherical harmonics are to be used in a
21 ! plane wave expansion:
22 ! $$ e^{-i{\bf G}\cdot{\bf r}}=4\pi\sum_{l=0}^{\infty}(-i)^l j_l(Gr)
23 ! \sum_{m=-l}^l Y_{lm}(\hat{\bf G})Y_{lm}^*(\hat{\bf r}), $$
24 ! where $j_l$ are spherical Bessel functions of the first kind.
25 !
26 ! !REVISION HISTORY:
27 ! Created March 2004 (JKD)
28 ! Improved stability, December 2005 (JKD)
29 ! Changed algorithm, June 2019 (JKD)
30 ! Included prefactor option, May 2024 (JKD)
31 !EOP
32 !BOC
33 implicit none
34 ! arguments
35 logical, intent(in) :: t4pil
36 integer, intent(in) :: lmax
37 real(8), intent(in) :: v(3)
38 complex(8), intent(out) :: ylm(*)
39 ! local variables
40 integer l,m,lm1,lm2,lm3,lm4
41 real(8), parameter :: eps=1.d-14
42 real(8), parameter :: fourpi=12.566370614359172954d0
43 real(8) r,st,ct,sp,cp
44 real(8) t1,t2,t3,t4
45 complex(8), parameter :: zi=(0.d0,1.d0)
46 complex(8) z1
47 ylm(1)=0.28209479177387814347d0
48 if (lmax == 0) return
49 r=sqrt(v(1)**2+v(2)**2+v(3)**2)
50 if (r > eps) then
51  t1=v(3)/r
52  if (t1 >= 1.d0) then
53  st=0.d0
54  ct=1.d0
55  else if (t1 <= -1.d0) then
56  st=0.d0
57  ct=-1.d0
58  else
59  st=sqrt(1.d0-t1**2)
60  ct=t1
61  end if
62  if ((abs(v(1)) > eps).or.(abs(v(2)) > eps)) then
63  t1=1.d0/sqrt(v(1)**2+v(2)**2)
64  sp=t1*v(2)
65  cp=t1*v(1)
66  else
67  sp=0.d0
68  cp=1.d0
69  end if
70 else
71  st=0.d0
72  ct=1.d0
73  sp=0.d0
74  cp=1.d0
75 end if
76 z1=cmplx(cp,sp,8)
77 ylm(3)=0.48860251190291992159d0*ct
78 ylm(4)=-0.34549414947133547927d0*st*z1
79 ylm(2)=-conjg(ylm(4))
80 do l=2,lmax
81  lm1=(l+1)**2
82  lm2=l**2
83  lm3=(l-1)**2+1
84  lm4=lm2+1
85  ylm(lm1)=-st*sqrt(dble(2*l+1)/dble(2*l))*z1*ylm(lm2)
86  if (mod(l,2) == 0) then
87  ylm(lm4)=conjg(ylm(lm1))
88  else
89  ylm(lm4)=-conjg(ylm(lm1))
90  end if
91  lm1=lm1-1
92  ylm(lm1)=ct*sqrt(dble(2*l+1))*ylm(lm2)
93  lm4=lm4+1
94  if (mod(l-1,2) == 0) then
95  ylm(lm4)=conjg(ylm(lm1))
96  else
97  ylm(lm4)=-conjg(ylm(lm1))
98  end if
99  t1=ct*sqrt(dble((2*l-1)*(2*l+1)))
100  t2=sqrt(dble((2*l+1))/dble(2*l-3))
101  do m=l-2,1,-1
102  lm1=lm1-1; lm2=lm2-1; lm3=lm3-1; lm4=lm4+1
103  t3=1.d0/sqrt(dble((l-m)*(l+m)))
104  t4=t2*sqrt(dble((l-m-1)*(l+m-1)))
105  ylm(lm1)=t3*(t1*ylm(lm2)-t4*ylm(lm3))
106  if (mod(m,2) == 0) then
107  ylm(lm4)=conjg(ylm(lm1))
108  else
109  ylm(lm4)=-conjg(ylm(lm1))
110  end if
111  end do
112  lm1=lm1-1; lm2=lm2-1; lm3=lm3-1
113  t3=1.d0/dble(l)
114  t4=t2*dble(l-1)
115  ylm(lm1)=t3*(t1*ylm(lm2)-t4*ylm(lm3))
116 end do
117 ! multiply by 4π (-i)ˡ if required
118 if (t4pil) then
119  ylm(1)=fourpi*ylm(1)
120  do l=1,lmax
121  lm1=l**2+1; lm2=lm1+2*l
122  select case(mod(l,4))
123  case(0)
124  ylm(lm1:lm2)=fourpi*ylm(lm1:lm2)
125  case(1)
126  ylm(lm1:lm2)=-fourpi*zi*ylm(lm1:lm2)
127  case(2)
128  ylm(lm1:lm2)=-fourpi*ylm(lm1:lm2)
129  case default
130  ylm(lm1:lm2)=fourpi*zi*ylm(lm1:lm2)
131  end select
132  end do
133 end if
134 end subroutine
135 !EOC
136 
pure subroutine genylmv(t4pil, lmax, v, ylm)
Definition: genylmv.f90:10