Fortran Programming Tools under Linux
Two methods exist for tracking down compiling errors in your Fortran source code: compiler messages and “lints”. Error messages reported by the gcc compiler are helpful, and this may be adequate for relatively simple programs. For example, suppose I made an error in the main program shown in the first example by forgetting to put in the statement:
that was line 11. Furthermore, assume I also messed up the array declaration in the subroutine trig by typing:
DIMENSION X(1), Y(1)
(Hey, it could happen!) Attempting to compile this incorrect program, now named baddemo.f, results in the following error sequence:
$ f77 -o baddemo baddemo.f f2ctmp_baddemo.f: MAIN f77demo: Error on line 17 of f2ctmp_baddemo.f: DO loop or BLOCK IF not closed Error on line 17 of f2ctmp_baddemo.f: missing statement label 10 trig: Error on line 22 of f2ctmp_baddemo.f: statement function y amid executables. Warning on line 25 of f2ctmp_baddemo.f: local variable sin never used Warning on line 25 of f2ctmp_baddemo.f: local variable exp never used
The error messages give information to help isolate the problems, but the line numbers don't always seem to correspond to the the line numbers of the original Fortran source. This makes it a little harder to track down obscure errors, especially in longer programs. Unfortunately, there doesn't seem to be an option in f77 or f2c to generate a program listing with line numbers. (That doesn't mean it can't be done!)
In addition to compiler error messages, there are several source code checkers or “lints” that can be used to help isolate errors in the source code. An easy-to-use checking program is ftnchek. In its simplest usage, ftnchek examines your program for a variety of potential errors, and it can make life easier by generating a program listing. ftnchek has a long list of options and a thorough man page. Remember, Fortran checking programs will not identify all the errors in your program. However, the combination of a checker and f77 error messages should help you combat compilation errors. (Of course, careful programming will help as well!)
The hard-core (and really adventurous) programmer can obtain a package called Toolpack from the usual Linux sites. This large package is a set of programs and C shell scripts that provides rigorous FORTRAN 77 code checking, along with static and dynamic analysis. See the Fortran_FAQ (directory /usr/doc/faq/lang in the Slackware distribution) for a description of Toolpack.
Fortran's main usage has been in the scientific and engineering fields, and because the language has survived for decades, thousands of high-quality programs and subroutine libraries exist, many of which are freely available. These programs include application programs written for specific purposes, mathematical subroutine libraries, general purpose run-time routines, and graphical plotting and display packages.
Describing even a small percentage of available programs is impossible, but you can get a rough idea of what's out there by pointing your Web browser at netlib2.cs.utk.edu. This site is the Netlib Repository at UTK/ORNL, and it archives over 100 packages containing mathematical software, papers, and databases. Fortran programmers will be particularly interested in a “code motherlode” collection called slatec. This is a “...comprehensive library containing over 1400 general purpose mathematical and statistical routines written in FORTRAN 77.” (Source code, folks!) To give an idea of the slatec library's magnitude, its Table of Contents takes up 222,161 bytes on my disk.
One key factor in my decision to use Linux as a Fortran programming platform was the PGPLOT package. This highly versatile Fortran library provides 100 primitive and higher level subroutines for drawing scientific graphs on various graphic display devices. For example, with the PGPLOT library, you can create multiple graphs in multiple X windows, output plots to PostScript and other supported printing devices, or create files that are compatible with HPGL format or Latex picture environment.
In addition to Linux, PGPLOT is available for twelve other flavors of Unix (AIX, Cray, HP, SGI, NeXT, etc.), two versions of OpenVMS, and MS-DOS using Microsoft Power Station 32-bit Fortran. This wide availability is an attractive feature if you want to develop consistent Fortran applications across platforms. I am also informed that PGPLOT capabilities are available in a compiled form (PGPERL) for use with Perl scripts.
A simple demonstration of PGPLOT is provided by the program listed below. This program is the same as the one given previously, with nine (indented) lines of code added to create a simple plot.
C============================== C Simple Program to Illustrate C PGPLOT Graphic Tools C============================== PROGRAM PGDEMO INTEGER PGBEG DIMENSION X(100), Y(100) PI=2.*ACOS(0.) N=100 IER = PGBEG(0,'?',1,1) IF (IER.NE.1) STOP CALL PGSCRN(0, 'AntiqueWhite', IER) CALL PGSCRN(1, 'MidnightBlue', IER) DO 10 I=1,N X(I)=I*(2*PI/N) 10 CONTINUE CALL TRIG(N,X,Y) CALL PGENV(0., 4., 0., .4, 0, 1) CALL PGLAB('X Values', 'Y Values', 'PGPLOT Demo') CALL PGLINE(100, X, Y) CALL PGEND DO 20 J=1,5 PRINT 15, X(J), Y(J) 15 FORMAT(2X,2F8.3) 20 CONTINUE STOP END SUBROUTINE TRIG(N,X,Y) DIMENSION X(1), Y(1) DO 10 I=1,N Y(I) = SIN(X(I))*EXP(-X(I)) 10 CONTINUE RETURN END
Subroutine PGBEN in the pgdemo program performs the plot initialization. Placing the ? character in the PGBEN parameter list causes the program to query for which output device to use. The two calls to PGSCRN simply change the background and foreground colors (and there are plenty colors from which to choose). PGENV establishes the limits of the x- and y-axis, and PGLAB labels the axes and plot. (Math/Greek symbols and subscript/superscript capabilities are available.) The generated curve is plotted by PGLINE, and the plot is completed by PGEND.
This PGPLOT program is compiled by the command:
$ f77 -o pgdemo pgdemo.f -pgplot -lX11
It is necessary to include the X11 library after the PGPLOT library because some of the PGPLOT subroutines create X windows and control their attributes.
Program execution results in the messages shown in Figure 1.
The pgdemo program first queried for which output device to use. I answered with a ? to see the list of available devices (which is configured when the package is installed). From the list I then selected /XWINDOW, and the simple plot shown in Figure 2 was drawn in an X window. PGPLOT supports a long list of output devices, but not all are available for Linux users.
Now the fun can really begin by selecting font sizes, axis types, alphanumeric notations, and a host of other options. If you have had any previous experience with Fortran plotting routines, PGPLOT will be easy to learn and use.