Physics 270: Introduction to Computational Physics

Problem 10-Introduction to FORTRAN programming techniques


  1. Introduction and Goals

  2. To learn how to run FORTRAN programs on the mainframe computer, and optionally on a desktop.
  3. Simple FORTRAN-77 program

  4. What appears below is a simple FORTRAN program. Note that the column numbers matter.
    Program instructions begin in column 7, and cannot exceed column 72. Labels (statement numbers)
    are in columns 1-6. To continue a statement on the next line, put a dot (.) in column 6.

    C  Some variables to work with are declared first.
           REAL VAR1, VAR2
    C
    C Here is some old-fashioned FORTRAN type code.
    C I put it here to show you how things used to be done.
    C It should not necessarily be used in your programs!
    C The program loops until you input a "zero".
    C
    100    CONTINUE
    C This is a good way to work with keyboard input/output.
           WRITE(*,*) 'Input a number, please...'
           READ(*,*) VAR1
           VAR2=VAR1*VAR1
           WRITE(*,*) 'The square of your number is...'
           WRITE(*,*) VAR2
    C Now test for 'zero'
           IF (VAR1.NE.0) GO TO 100
           STOP 'It worked!'
           END

  5. How to run F77 programs on the Alpha's

  6. You need to first create a 'text' file using an editor. A very simple one that I recommend is called 'pico'. It is the same editor that is used by the e-mail program 'pine', and is very user friendly.
    Make a file containing your program, and store it with a short name and the extension .f or .f77, that is, something like 'testfort.f' .
    Then, at the prompt, type '>f77 testfort.f'. Careful! Capitals count in Unix.
    If you have no errors, the computer will create a file called 'a.out', which is the default. You then 'run' this program by typing, at the prompt, '>a.out'. You can move the file to another name if you want, like this: '>mv a.out testfort' or '>mv a.out testfort.exec', or whatever you like.
  7. How to run FORTRAN-90 on a desktop PC.

  8. You should go to the Web page of Lahey company, at www.lahey.com . Follow the instructions to download and install the Essential Lahey Fortran-90 package, called "ELF90". You will need to run this in an MS-DOS window.
  9. Tasks
    1. Log onto your mainframe account, and type '>man f77', and read the instructions on how to use the FORTRAN-77 compiler.
    2. Use an editor to create the sample program listed above. Compile and run it, and verify that it works. It should give you the square of any number you input, until you enter zero, when it will quit.
    3. Write a FORTRAN program to calculate the product of two COMPLEX numbers, and output the magnitude, and real and imaginary parts of the product. Use 'console' input/output as shown in the above program. The imaginary part of a COMPLEX number is returned by the function AIMAG(Z), the real part by AREAL(Z). Double precision versions of these functions are DIMAG and DREAL.
    4. Write a FORTRAN program to calculate the first 100 prime integers (that is, all primes from 3 to 100). Output the prime numbers to a file. Use the example given below to do your file output. The logic of your program could be something like the following:
      1. C  All even numbers are not prime. Start with N=3, and increment by 2 each time.
        INTEGER NEW, NTOT, PRIMES(100), REMAINDER
        C  NEW is the number you are testing; NTOT is how many integers have
        C  been found that are prime.
          Loop from NEW=3,100,2
           C Test to see if it's a prime
             REMAINDER = NEW/PRIMES(ILoop)
             IF REMAINDER*PRIMES(ILoop) = NEW Then NEW is not prime.
             End Loop over all primes so far
             IF No PRIMES divided into NEW, write it out to your file
                 as a new prime
        End Loop over NEW

Here is a program showing how to do file input/output: fileio.f