| Goals for this chapter: |
|
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.
Therefore, we have:
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.
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]#
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.
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)
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
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.
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.
Check the Interactive Exam Cram Programming:
Internet Resources for this Chapter.