Chapter P10. FORTRAN

Goals for this chapter:
  • g77 (g77)
 

FORTRAN ... an ethernal scientific programming language!
Dr. Giovanni A. Orlando.

 
 
 

FORTRAN ... an ethernal scientific programming language


FORTRAN is an acronym for FORmula TRANslation. The FORTRAN, was and is the standard language to write programs for scientific purpose.

I learn FORTRAN when I enter in the Department of Mathematics, in Science Faculty in UCV, in 1980. In those days the faculty system was a Burroghs B6700 and the normal mode to introduce data in the system was using cards.

A card contains a simple line of code.

FORTRAN is used for engineers on any field, also to calculate material resistance and others. Without to speak about minor or elementary examples, like we will cover here, FORTRAN was used also in NASA and other governative institutions worldwide. For example, one of our most eminent professors in UCV (Venezuela), Prof. Pereira develop in those days a package called PASVA, that is a numerical implementation in FORTRAN of the finite difference method to solve differential equations. It was used by NASA in some days.

Also the latest GNU GCC 3.1, covered in this training and dated May 2002, includes a FORTRAN compiler: g77.

One of the reason, FORTRAN is still (and will be) alive is that all the scientific routines written in FORTRAN, may used or linked to modern C/C++ source without any problem. In the same sense COBOL was the language invented by US Army to catalog its warehouse, FORTRAN is the "standard" language for scientific programming.

Fortran Generations

Before to start to work with the FORTRAN, is important to know its generations. While, in 1980 was used the FORTRAN 77, and actually is also the most frequent implementation, FORTRAN 90 a object oriented version start to used as a standard fortran compiler.

Therefore, we have:

Fortran 90 features.

We list here the FORTRAN 90 features.

Discovering FORTRAN

FORTRAN is more near to BASIC as a labeled programming language than C, C++ or Java. Its design is dated 1950 and start at IBM.

We will start with a single program.

      program circle
      real r, area

c This program reads a real number r and prints
c the area of a circle with radius r.

      write (*,*) 'Give radius r:'
      read  (*,*) r
      area = 3.14159*r*r
      write (*,*) 'Area = ', area

      stop
      end
 

To compile the program we simply use the following command:

[root@ftosx1 Chap10]# g77 circle.f
[root@ftosx1 Chap10]# ls -al
total 34
drwxr-xr-x    2 root     root          176 Jul 22 17:09 .
drwxr-xr-x   38 root     root         1088 Jul 22 17:07 ..
-rwxr-xr-x    1 root     root        13313 Jul 22 17:09 a.out
[root@ftosx1 Chap10]#

Note that the "a.out" binary default is completely equivalent to the "cc" or "gcc" C compiler.

Running we will have:

[root@ftosx1 Chap10]# ./a.out
 Give radius r:
1
 Area =   3.14159012
[root@ftosx1 Chap10]#
 

The previous is not quite complicated and seems "human readable". We have:

C for comments
program to define a program
real to define real numbers
write equivalent to PRINT (BASIC) or printf (C)
read to read from I/O.
AREA is a variable.
STOP to stop the program and
END to finish.

However, the mode used here are the free format modes. Free because read from any channel available.

In FORTRAN the "read" and "write" needs to use a channel communication. In the years where when was designed, the system, basically a room-system, have a fixed number for each device.

So, the "5" was the reader device (actually the keyboard, in the modern PC), while the printer was the device number "6", the normal printer, but also the "tty" where we runs the program.

In those days the mode to insert program, was using the "card reader", and the mode to get OUTPUT, was using the printer ... Then, was invented the teletype.

Following this explanation, we will introduce the normal I/O chanels.
 

The READ and WRITE instructions.

Formerly speaking, the READ and WRITE instruction in FORTRAN are the following:

      read ([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s)
      write([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s)

Therefore, the previous program is more correct if we use the "standard" units for READ and WRITE.

Actually, is not important use "lowercase" or "UPPERCASES", but FORTRAN works with UPPER CASE letters.

So, our "circle2" will be:
 

[root@ftosx1 Chap10]# more circle2.f

      PROGRAM circle
      REAL r, area

c This program reads a real number r and prints
c the area of a circle with radius r.

      write (6,*) 'Give radius r:'
      read  (5,*) r
      area = 3.14159*r*r
      write (*,*) 'Area = ', area

      stop
      end
 

[root@ftosx1 Chap10]#

Now, we will compile to generate a "circle2" binary.

[root@ftosx1 Chap10]# g77 circle2.f -o circle2
[root@ftosx1 Chap10]# ./circle2
 Give radius r:
1
 Area =   3.14159012
[root@ftosx1 Chap10]#
 

Data Declaration

For data we have:

     integer   list of variables
    real      list of variables
    double precision  list of variables
    complex   list of variables
    logical   list of variables
    character list of variables

Arrays are a simple and normal mode to implement vector and matrices:

For vectors we have:

      integer i(10)
      logical aa(0:1)
      double precision x(100)

... and for matrices

      real A(3,5)
 

The expression and assignment are completely equivalent to any other programming language.
 

Loops


FORTRAN uses the labeled mode to control a cycle.

      integer i, n, sum

      sum = 0
      do 10 i = 1, n
         sum = sum + i
         write(*,*) 'i =', i
         write(*,*) 'sum =', sum
  10  continue
 

The line labeled "10" is where the program execution will back to the cycle, like a "}" in C/C++. The GOTO of course is still valid here.

We have also "while" and "until" statement.

The while is:

      do while (logical expr)          statements
      enddo

and the until will be:

      do
         statements
      until (logical expr)
 

FORMAT

FORTRAN suppor a special instruction called: FORMAT. It is equivalent to C printf

The fields will have the following meaning:

   A - text string
   D - double precision numbers, exponent notation
   E - real numbers, exponent notation
   F - real numbers, fixed point format
   I - integer
   X - horizontal skip (space)
   / - vertical skip (newline)

For example, we will have:

This piece of Fortran code

      x = 0.025
      write(*,100) 'x=', x
  100 format (A,F)
      write(*,110) 'x=', x
  110 format (A,F5.3)
      write(*,120) 'x=', x
  120 format (A,E)
      write(*,130) 'x=', x
  130 format (A,E8.1)

produces the following output when we run it:

x=      0.0250000
x=0.025
x=  0.2500000E-01
x= 0.3E-01
 

Compiling FORTRAN with libs

A Fortran package of subprograms may contain hundreds of files. It is very slow and inconvenient to recompile these files every time you want to use any of the subroutines. Under the Unix operating system you can avoid this by making a library file. The library file is an object file, so you only have to compile your additional main (driver) program and then link it with library. (Linking is much faster than compiling.)

Libraries have file names starting with lib and ending in .a. Some libraries have already been installed by your system administrator, usually in the directories /usr/lib and /usr/local/lib. For example, the BLAS library may be stored in the file /usr/local/lib/libblas.a. You use the -l option to link it together with your main program, e.g.

      g77 main.f -lblas

You can link several files with several libraries at the same time if you wish:

  g77 main.f mysub.f -llapack -lblas

The order you list the libraries is significant. In the example above -llapack should be listed before -lblas since LAPACK calls BLAS routines.

If you want to create your own library, you can do so by compiling the source code to object code and then collecting all the object files into one library file. This example generates a library called my_lib:

   g77 -c *.f
   ar rcv libmy_lib.a *.o
   ranlib libmy_lib.a
   rm *.o

Check the manual pages or a Unix book for more information on the commands ar and ranlib. If you have the library file in the current directory, you can link with it as follows:

    g77 main.f -L. -lmy_lib

One advantage of libraries is that you only compile them once but you can use them many times.
 

Some Examples

We will complete this chapter introducing some examples:

We start with the a FORTRAN program to solve the quadratic in real numbers.

! ---------------------------------------------------
!   Solve  Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0
! ---------------------------------------------------

PROGRAM  QuadraticEquation
   IMPLICIT  NONE

   REAL  :: a, b, c
   REAL  :: d
   REAL  :: root1, root2

!  read in the coefficients a, b and c

   WRITE(*,*) 'A, B, C Please : '
   READ(*,*)  a, b, c

!  compute the square root of discriminant d

   d  = SQRT(b*b - 4.0*a*c)

!  solve the equation

   root1 = (-b + d)/(2.0*a)   ! first root
   root2 = (-b - d)/(2.0*a)   ! second root

!  display the results

   WRITE(*,*)
   WRITE(*,*)  'Roots are ', root1, ' and ', root2

END PROGRAM  QuadraticEquation

Running we will have:

[root@ftosx1 FORTRAN]# ./quadratic
 A, B, C Please :
1 0 -1

 Roots are   1. and  -1.
[root@ftosx1 FORTRAN]#
 

If we try to print complex results we will get:

[root@ftosx1 FORTRAN]# ./quadratic
 A, B, C Please :
1 1 1

 Roots are   NAN and   NAN
[root@ftosx1 FORTRAN]#
 

Author's comment.

When I learn FORTRAN, I was sixteen years old, just a "smart and educated" boy. Today, at 39 after all my experience in computer science is an incredible peacefully sensation, teachs FORTRAN in a single chapter.

Exercises

  1. Extend the quadratic example to cover also equations of degree "3" and "4".


Test

  1. What means FORTRAN ?
  2. What is FORTRAN ?
  3. What is the name of GNU Fortran compiler ?
  4. What is the normal FORTRAN extension on UNIX system ?
  5.  What means "WRITE (*,*)" ?
  6. Are present in FORTRAN labels for instructions, like in old BASIC ?
  7. Would be available a GOTO instruction in FORTRAN ?
  8. What means "5" in the FORTRAN instruction "READ (5,10) N"
  9. Would be possible to mix C++ and Fortran libraries ?
  10. List a reason because FORTRAN continues to be used today.
Consult the answers

Check the Interactive Exam Cram Programming: Try the interactive cram ...

Internet Resources for this Chapter.