c......Example 3(a): Modified Cartesian to spherical polar coordinates
c.....               convertor: Part 1
c23456
      program ex3a
c
      implicit none
c
      double precision cart(3),polar(3)
      double precision pi
      integer i,ichoice
c
      pi = 3.14159265358979d0
c
c..... Read in whether to convert Cartesian to spherical polar
c..... or to convert spherical polar to Cartesian
c
      print *,'If you want to convert Cartesian to spherical polar enter
     & 1'
      print *,' or'
      print *,'if you want to convert spherical polar to Cartesian enter
     & 2'
      read *,ichoice
c
c..... Beginning of if loop
c
c..... Perform Cartesian to spherical polar conversion
c
       if(ichoice.eq.1) then
c
c..... Print off instructions
c
       print *,'You have chosen to convert Cartesian to spherical polar'
c
c..... Read in the three Cartesian coordinates from the screen
c
         print *, 'Enter (x,y,z) on one line separated by commas'
         read *,cart(1),cart(2),cart(3)
c
c.....NOTE: There are a variety of ways of converting from Cartesian to
c.....      spherical polar coordinates. The method illustratd below is
c.....      just one.
c
c..... Calculate the r vector
c..... Assign the r vector to polar(1)
c
          polar(1) = sqrt(cart(1)**2+cart(2)**2+cart(3)**2)
c
c..... Calculate the value of theta using the fact that z = r*cos(theta)
c..... Assign theta to polar(2)
c
          polar(2) = acos(cart(3)/polar(1))
c
c..... Calculate the value of phi using the fact that
c..... x = r*sin(theta)*cos(phi)
c
          polar(3) = acos(cart(1)/(polar(1)*sin(polar(2))))
c
c..... Remember that the trigonometric units are by default radians
c..... To convert to degrees you need a value for pi,
c..... Recall: 2*pi radians = 360 degrees
c
          polar(1) = polar(1)
          polar(2) = polar(2)/pi*180.0d0
          polar(3) = polar(3)/pi*180.0d0
c
c..... Output the results to the keyboard
c..... The angular results are output in degrees
c
          print *,'Spherical polar coordinates are (angles in degrees):'
          print *, ' r = ',polar(1)
          print *, ' theta = ',polar(2)
          print *, ' phi = ',polar(3)
c
c..... Spherical polar to cartesian conversion
c
       else if(ichoice.eq.2) then
c
c..... Print off instructions
c
         print *,'You have chosen to convert spherical polar to
     &Cartesian'
         print *, 'Enter (r,theta,phi) on one line separated by commas'
         print *,'Enter theta and phi in degrees.'
         read *,polar(1),polar(2),polar(3)
c
c..... Convert angles in degrees to angles in radians
c
         polar(2) = polar(2)/180.0d0*pi
         polar(3) = polar(3)/180.0d0*pi
c
c..... Perform the simple conversion using the well known formulae
c.....   x = r*sin(theta)*cos(phi)
c.....   y = r*sin(theta)*sin(phi)
c.....   z = r*cos(theta)
c
         cart(1) = polar(1)*sin(polar(2))*cos(polar(3))
         cart(2) = polar(1)*sin(polar(2))*sin(polar(3))
         cart(3) = polar(1)*cos(polar(2))
c
c..... Output the results
c
         print *,'Cartesian coordiantes are:'
         print *,'x = ',cart(1)
         print *,'y = ',cart(2)
         print *,'z = ',cart(3)
c
c..... If 1 or 2 is not entered print a warning message
       else
         print *,'You MUST enter 1 or 2!'
c
c..... End of the if-else block
c
       end if
c
       end


Return to Fortran Workshop Main Page


This page maintained by alex.brown@ualberta.ca of the Department of Chemistry, University of Alberta

Last updated August 8, 2003.