Fortran Fortran 77 subroutine for calculating spherical harmonics

AI Thread Summary
The discussion revolves around understanding a Fortran 77 subroutine that calculates spherical harmonics using the CERN library RASLGF for Legendre functions. Key points include confusion about the line "leg(k)=((-1)**nn)*leg(k)," which is intended to handle negative values of n by flipping the sign of leg(k) when nn is odd. This approach is permissible as the RASLGF library allows for negative n. Another point of confusion is the multiplier "((-1)**(nn)/sqrt(2*pi))" used for the outputs Yr(k,n) and Yi(k,n). This multiplier is not commonly referenced in standard definitions of spherical harmonics, leading to uncertainty about its purpose. The discussion also touches on formatting code for clarity, emphasizing the use of proper code tags for better readability.
nepapak
Messages
4
Reaction score
0
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.
 
Technology news on Phys.org
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.
nepapak said:
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
Code:
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.
 
Kk thanks 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
]
 
It seems to me that those extra slashes are a lot more work than the "code" tags. :confused:
 
They definitely are, but to tell you the truth I couldn't figure out what I was supposed to do with the
Code:
 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.
 
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.
 
Yay! tiz working now, thx! Now maybe somebody will be able to understand it ;)
Code:
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
 
nepapak said:
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

...

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.

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.
 

Similar threads

Replies
8
Views
4K
Replies
2
Views
2K
Replies
8
Views
2K
Replies
18
Views
6K
Replies
2
Views
3K
Replies
54
Views
5K
Back
Top