Fortran 77 subroutine for calculating spherical harmonics

Click For Summary

Discussion Overview

The discussion revolves around understanding a Fortran 77 subroutine designed to calculate spherical harmonics using the CERN library RASLGF for associated Legendre functions. Participants seek clarification on specific lines of code and their implications within the context of spherical harmonics.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions the line leg(k)=((-1)**nn)*leg(k), asking why it is necessary when the RASLGF library allows for negative values of n.
  • Another participant notes that the line still accommodates negative values since nn is the loop index, suggesting that it flips the sign of leg(k) when nn is odd.
  • There is a query regarding the multiplier ((-1)**(nn)/sqrt(2*pi)) for Yr(k,n) and Yi(k,n), with participants expressing uncertainty about its origin and relevance in definitions of spherical harmonics.
  • One participant acknowledges their limited knowledge of spherical harmonics, indicating they cannot provide reasoning for the multiplier.

Areas of Agreement / Disagreement

Participants express uncertainty regarding the specific code lines and their implications, indicating that there is no consensus on the reasoning behind the use of the sign-flipping and the multiplier in the spherical harmonics calculations.

Contextual Notes

Participants have not resolved the questions about the code, and the discussion reflects varying levels of familiarity with spherical harmonics and the associated mathematical definitions.

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 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
6
Views
4K
  • · Replies 18 ·
Replies
18
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K