# Fortran 77 subroutine for calculating spherical harmonics

1. Oct 21, 2011

### nepapak

Hey guys I am trying to understand a code for a Fortran 77 subroutine which calculates spherical harmonics using the CERN library RASLGF for legendre functions. The code looks like this

subroutine harmonics(max,theta,phi,Yr,Yi)

implicit none

integer max,k,nn,n,grens
double precision x,theta,phi,ct,nphi,pi
double precision leg(0:max)
double precision Yr(0:max,0:(2*max)),Yi(0:max,0:(2*max))
parameter (pi=3.141592654)

do k=0,max
do n=0,2*max
Yr(k,n)=0.
Yi(k,n)=0.
enddo
enddo

ct=cos(theta)
do nn=-max,max
nphi=nn*phi
grens=abs(nn)
call DASLGF(2,ct,nn,max,leg) ! calculates the associated Legendre
! functions
do k=grens,max
n=nn+k
if (nn.lt.0) then !different definition for leg(k) with n neg
leg(k)=((-1)**nn)*leg(k)
endif
Yr(k,n)=((-1)**(nn)/sqrt(2*pi))*cos(nphi)*leg(k) !p(k)=P_k^n
Yi(k,n)=((-1)**(nn)/sqrt(2*pi))*sin(nphi)*leg(k)
enddo
enddo
return
end

Now there are two parts I don't understand. Why is this line written leg(k)=((-1)**nn)*leg(k) when it says in the description of the RASLGF library that it is permisable for n to be negative?
Second problem is why is there a ((-1)**(nn)/sqrt(2*pi)) multiplier for Yr(k,n) and Yi(k,n)? I have looked at a lot of definitions for spherical harmonics but I just can't seem to find any reference for such a multiplier.

2. Oct 21, 2011

### Staff: Mentor

When you post code, put [ code ] and [ /code ] tags around it, without the extra spaces. This makes the code easier to read by preserving the original indentation.

3. Oct 24, 2011

### nepapak

Kk thx for the heads up, I didn't even notice that the indentation had completely disappeared. I hope it looks better like this:
[
/////subroutine harmonics(max,theta,phi,Yr,Yi)

/////implicit none

/////integer max,k,nn,n,grens
/////double precision x,theta,phi,ct,nphi,pi
/////double precision leg(0:max)
/////double precision Yr(0:max,0:(2*max)),Yi(0:max,0:(2*max))
/////parameter (pi=3.141592654)

/////do k=0,max
////////do n=0,2*max
//////////Yr(k,n)=0.
//////////Yi(k,n)=0.
////////enddo
/////enddo

/////ct=cos(theta)
/////do nn=-max,max
////////nphi=nn*phi
////////grens=abs(nn)
////////call DASLGF(2,ct,nn,max,leg) ! calculates the associated Legendre
/////////////////////////////////////////! functions
////////do k=grens,max
//////////n=nn+k
//////////if (nn.lt.0) then !different definition for leg(k) with n neg
/////////////leg(k)=((-1)**nn)*leg(k)
//////////endif
//////////Yr(k,n)=((-1)**(nn)/sqrt(2*pi))*cos(nphi)*leg(k) !p(k)=P_k^n
//////////Yi(k,n)=((-1)**(nn)/sqrt(2*pi))*sin(nphi)*leg(k)
////////enddo
/////enddo
/////return
/////end
]

4. Oct 24, 2011

### Staff: Mentor

It seems to me that those extra slashes are a lot more work than the "code" tags.

5. Oct 24, 2011

### nepapak

They definitely are, but to tell you the truth I couldn't figure out what I was supposed to do with the
Code (Text):
tags :confused:. I mean, when I  copied the code in the middle of ''['' and '']'' the code still lost its original indentation when I looked at the preview of the post, so I couldn't think of any other way of preserving the indentation other than resorting to the slashes.

6. Oct 24, 2011

### Staff: Mentor

The code tags include the square brackets (but no spaces).

If you're still confused, look at the collection of icons above the text-entry box on the message-composition page. One of them looks like a document icon with "<>" symbols on it. Select your code, click that icon, and you'll get proper code tags at the beginning and end.

Or simply click on the icon, get a pair of code tags without anything between them, and then insert your code between them.

7. Oct 25, 2011

### nepapak

Yay! tiz working now, thx! Now maybe somebody will be able to understand it ;)
Code (Text):
c****************************************************************************
c Subroutine to calculate spherical harmonics. It uses the CERN-library
c (RASLGF) to calculate the associated Legendre functions (normalised!).
c Gives as output all Y_k till k=max.
c
c Nele Vermeulen. April 2004.
c****************************************************************************

subroutine harmonics(max,theta,phi,Yr,Yi)

implicit none

integer max,k,nn,n,grens
double precision x,theta,phi,ct,nphi,pi
double precision leg(0:max)
double precision Yr(0:max,0:(2*max)),Yi(0:max,0:(2*max))
parameter (pi=3.141592654)

do k=0,max
do n=0,2*max
Yr(k,n)=0.
Yi(k,n)=0.
enddo
enddo

ct=cos(theta)
do nn=-max,max
nphi=nn*phi
grens=abs(nn)
call DASLGF(2,ct,nn,max,leg) ! calculates the associated Legendre
! functions
do k=grens,max
n=nn+k
if (nn.lt.0) then   !different definition for leg(k) with n neg
leg(k)=((-1)**nn)*leg(k)
endif
Yr(k,n)=((-1)**(nn)/sqrt(2*pi))*cos(nphi)*leg(k) !p(k)=P_k^n
Yi(k,n)=((-1)**(nn)/sqrt(2*pi))*sin(nphi)*leg(k)
enddo
enddo
return
end

8. Oct 25, 2011

### swartzism

leg(k)=((-1)**nn)*leg(k) still allows for negative values. Since nn is the loop index, when it is an odd number, this is simply flipping the sign on leg(k).

I do not know much about spherical harmonics, so I'm not sure the reasoning for doing this, nor do I know the answer to your second question.