[Santa Clara University]
Department of Mathematics
and Computer Science

CSCI 169

Fortran-90 Information

D. C. Smolarski, S.J.

[Return to Math 169 Homepage | Return to Supplement Listing Page]

Contents


Fortran-90 on the HP Unix Server

  1. Create a source code (program) file using an editor of your choice (e.g., Emacs or VI). The extension should be .f90 This assumes the file is in "free source format" as opposed to the older "fixed source format" (with certain components limited to certain "columns").

    If one uses the extension .f or .for the contents are assumed to be in "fixed source format" even though the code may, in fact, contain Fortran-90 structures/features.

  2. SPECIAL CHARACTERISTICS
    1. (Using Emacs) Proper indentation can be achieved by using the <tab>. (In "fixed source format," if the first character in a line is a number, this number is assumed to be in "column 6" and is interpreted as a continuation card symbol.) If the character after the <tab> is not a number, it is interpreted as a standard initial statement and indented appropriately.

    2. File i/o can be performed using the default files ftnxx where xx is an appropriate number (other than 5 or 6). E.g., one uses WRITE(21,102) or READ(53,105) to write to or read from files ftn21 or ftn53. This will work for all numbers between 1 and 99 except for 5 and 6 which automatically write to the terminal screen.

  3. After the source code file is created, execution of the code is via the standard Unix sequence of steps assuming the compiler name is f90. E.g.,
    			f90 mpfor.f90
    			a.out
    
    Of course, standard Unix practice is to redirect the executable into a file other than a.out and this is done, e.g., by
    			f90 -o mpfor mpfor.f90
    
    In this case, typing mpfor will run the program. To redirect input and output (assuming 5 and 6 were used in READ and WRITE statements), one uses the standard Unix file redirection, namely:
                           mpfor < forinp > forout
    

Sample Fortran Source Code (Fixed Source Form)

     
c234567890...
c FILE mp1a.f
      PROGRAM MP1A
C
C     SAMPLE FORTRAN PROGRAM
C     BY DENNIS C. SMOLARSKI, S.J.
C     COMPUTE POWERS OF A COMPLEX NUMBER
C     TO SHOW THE EXPONENTIATION OPERATOR IN
C     FORTRAN AND TO SHOW THE VARIABLE
C     TYPE 'COMPLEX'
C
      COMPLEX X,OUT(10)
      INTEGER I
      X = (0.0,2.0)
      CALL POWERS(X,OUT)
      CALL PRNTOT(X,OUT)
      STOP
      END
C
      SUBROUTINE POWERS(A,B)
      COMPLEX A,B(10)
      INTEGER I
      DO 10 I=1,10
        B(I) = A**I
10    CONTINUE
      RETURN
      END
C
      SUBROUTINE PRNTOT(X,OUT)
      COMPLEX X,OUT(10)
      INTEGER J
      WRITE(21,100)
100   FORMAT(1X,' THE OUTPUT IS AS FOLLOWS:')
      WRITE(21,101) X
101   FORMAT(1X,' THE POWERS OF ',F9.3,' +',F9.3,' I ARE:')
      WRITE(21,102)
102   FORMAT('   POWER     VALUE ')
      DO 10 I=1,10
        WRITE(21,103) I,OUT(I)
103     FORMAT(1X,I5,3X,F9.3,' +',F9.3,' I')
10    CONTINUE
      RETURN
      END

Sample Fortran Source Code (Free Source Form)

    ! file mp1b.f90
    program mp1b
      !  Sample program for Fortran-90
      !**************************************
      !***   Declaration of Variables  ******
      !
      integer :: i
      !  next two lines compute the "kind" value for extended precision
      integer, parameter :: dpkind = kind(0.0d0) 
      integer, parameter :: cmpxdpkind = kind((0.0d0,0.0d0))
      complex (kind=cmpxdpkind) :: a, root
      real (kind=dpkind) :: cost, sint
      real (kind=dpkind), parameter :: pi = 3.141592653589793238462d0
      !
      !**************************************
      !  main part of the program -- output header
      !**************************************
      !
      write(6,*) " Practice Machine Problem"
      write(6,*) "   Root Number     Value"
      !
      ! *** the following two lines compute the basic 16th root of 1
      ! *** See Appendix H, p A51 of Stewart's Calculus book
      !
      cost = cos(pi/8.0d0)
      sint = sin(pi/8.0d0)
      a = cmplx(cost,sint,dpkind)
      !
      ! **** loop from 1 to 16 by increments of 1 ****
      ! the loop uses the value of  a  to compute all other 16th roots of 1
      ! complex values are printed as two real values in parentheses
      !
      root = a
      do i=1,16
         write(6,*) i, root
         root = root*a
      end do
      !
      ! **** final section ***
      ! the following shows "formatted" output
      !
      write(6,101) pi, pi
    101 format (1x, "pi = ",f5.2," or ",f20.15)
      !
    end program mp1

Sample Execution

            math: f90 -o mp1a mp1a.f
	    math: mp1a
	    math: more ftn21
              THE OUTPUT IS AS FOLLOWS:
	      THE POWERS OF     0.000 +    2.000 I ARE:
	       POWER      VALUE 
	          1       0.000 +    2.000 I
		  2      -4.000 +    0.000 I
		  3       0.000 +   -8.000 I
		  4      16.000 +    0.000 I
		  5       0.000 +   32.000 I
		  6     -64.000 +    0.000 I
		  7       0.000 + -128.000 I
		  8     256.000 +    0.000 I
		  9       0.000 +  512.000 I
		 10   -1024.000 +    0.000 I
            math: f90 -o mp1b mp1b.f90
	    math: mp1b > mp1bout.out
	    math: more mp1bout.out
              Practice Machine Problem
                Root Number     Value
             1 (0.923879532511287,0.38268343236509)
             2 (0.707106781186548,0.707106781186548)
	     3 (0.38268343236509,0.923879532511287)
	     4 (-1.110223024625157E-16,1.0)
	     5 (-0.38268343236509,0.923879532511287)
	     6 (-0.70710678118655,0.707106781186548)
	     7 (-0.92387953251129,0.38268343236509)
	     8 (-1.0,-1.665334536937735E-16)
	     9 (-0.92387953251129,-0.38268343236509)
	    10 (-0.70710678118655,-0.70710678118655)
	    11 (-0.38268343236509,-0.92387953251129)
	    12 (2.775557561562891E-16,-1.0)
	    13 (0.38268343236509,-0.92387953251129)
	    14 (0.707106781186548,-0.70710678118655)
	    15 (0.923879532511287,-0.38268343236509)
	    16 (1.0,3.885780586188048E-16)
	    math:


My email address is dsmolarski "at" scu.edu

This page last updated 2 April 2002. Updated: 28 February 2011.