Fortran Programming Tools under Linux

Steve Hughes proves that the Fortran Programming language's variety of high-quality programming tools and libraries provide a capability that, when coupled with all the features of Linux, makes for a potent programming platform for engineers and scientists.
Finding Fortran Program Errors

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)

instead of

      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
   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
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 Tools and Libraries

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 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  Simple Program to Illustrate
C  PGPLOT Graphic Tools
      DIMENSION X(100), Y(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
      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)
      DIMENSION X(1), Y(1)
      DO 10 I=1,N
      Y(I) = SIN(X(I))*EXP(-X(I))

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.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Fortran on Linux

Anonymous's picture

Take look at this web site:

Also, the Netbeans IDE

Shige's picture

Also, the Netbeans IDE provides good FORTRAN support.

Fortran F90/95

Anonymous's picture

For those interested in FORTRAN under Linux OS, look for Fortran F90/95 which is a high level programming language, successor to the Fortran F77. It includes pointers, dinamic allocation, collon notation (when working with arrays), operator overloading, modules, and many more! It is equivalent to C programming language in all of it's features, plus everything from standard Fortran F77 (complex numbers native support). Fortran F90/95 is THE choice for scientists and engineers, due to the fact that it can be easily mixed with F77 source codes (calling F77 subroutines from F90 source code)! Thus, all subroutines once written in F77 are accesible through F90 sintax (including all repository subroutines found on Netlib (such as Slatec)! Also, there is an complete IDE for Fortran F90 on Linux OS, which is called Photran (based on Eclipse). KDevelop Multilanguage can be used as Fortran F77 IDE on any Linux OS (with KDE desktop)! Fortran F90/95 compilers (free versions) for Linux include: Intel Fortran Compiler for Linux 9.0, G95 fortran compiler, and GCC 4.0 part called gfortran. They all can be integrated with Photran IDE.