Fortran 77 subroutine for calculating spherical harmonics

In summary, the conversation discusses a Fortran 77 subroutine that calculates spherical harmonics using the CERN library RASLGF for legendre functions. The subroutine includes a line that raises the function leg(k) to the power of (-1)**nn, but it is stated in the description of the library that n can be negative. The second issue discussed is the presence of a ((-1)**(nn)/sqrt(2*pi)) multiplier for the variables Yr(k,n) and Yi(k,n), which is not found in other definitions of spherical harmonics. The conversation ends with a suggestion to use [code] tags for code formatting instead of manually adding slashes.
  • #1
nepapak
4
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
  • #2
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.
 
  • #3
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
]
 
  • #4
It seems to me that those extra slashes are a lot more work than the "code" tags. :confused:
 
  • #5
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.
 
  • #6
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
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
 
  • #8
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.
 

What is Fortran 77 subroutine for calculating spherical harmonics?

Fortran 77 is a programming language commonly used in scientific and engineering applications. A subroutine is a section of code that performs a specific task and can be called upon by other parts of a program. In this case, the subroutine is designed to calculate spherical harmonics, which are mathematical functions used in physics and engineering.

Why is a subroutine specifically needed for calculating spherical harmonics?

Spherical harmonics involve complex mathematical equations that can be tedious and time-consuming to calculate by hand. A subroutine allows for a more efficient and accurate way of obtaining these values, especially when dealing with large sets of data.

How does the Fortran 77 subroutine for calculating spherical harmonics work?

The subroutine utilizes a set of pre-defined equations and algorithms to calculate the spherical harmonics. It takes in input parameters, such as the degree and order of the harmonic, and returns the corresponding value. The subroutine can be customized and adapted for different purposes.

What are the benefits of using Fortran 77 subroutine for calculating spherical harmonics?

Fortran 77 is a high-level language that is optimized for scientific and mathematical computations. This makes it a suitable choice for calculating spherical harmonics, which involve complex mathematical operations. The subroutine also allows for faster and more accurate calculations compared to manual methods.

Are there any limitations to using Fortran 77 subroutine for calculating spherical harmonics?

While Fortran 77 is a powerful language, it does have some limitations. For example, it may not be as user-friendly as more modern programming languages and may require a certain level of expertise to use effectively. Additionally, the subroutine may not be suitable for highly specialized or advanced calculations.

Similar threads

  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Linear and Abstract Algebra
Replies
6
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top