-
Introduction
Code to Program
Program units
Declaring Variables
Additional specifications
Executable GOTO statements
Other executable statements
Input/output statements
Call statements
FORMAT-letters
Extension in Fortran 90 regarding the FORMAT-letters
Addition regarding input/output
This page is designed to give a quick reference for Fortran users. I typically use Fortran because there are a lot of seismology legacy codes written in Fortran77. Since I have taken the time to build a large library of useful subroutines, it is fast and easy for me to program in Fortran. Fortran is more useful when you use postscript or plotting subroutines like PSPLOT and EZXPLOT
Most modern computational research is done in fortran or C. These languages are fast and efficient. Plus there are a large number of codes already developed for these languages that make application easier. The compilers for these languages are reasonably standardized across the various platforms (Mac, Windows, & Unix). Scripting Languages like Matlab or Mathtype are typically limited or confining in overall computational efficiency/capacity.
A code is the written set of instructions that a compiler interprets. The compiler builds an executable program that performs the tasks laid out in the code.
I like the GNU compilers. There are GNU compilers for nearly every operating system, and are relatively standardized across the various platforms. The GNU Fortran77 compiler is g77. For Mac OS X use fink & fink commander to install the g77.
To compile a code in a shell, terminal or xterm window:
prompt%: g77 program_file.f -o executable_output.exe
Large codes are often easiest to compile with a make file. The following is a generic make file.
| PROGRAM | main program |
| FUNCTION | function, FUNCTION can be preceded by some of the specifications of the variables below, except IMPLICIT |
| SUBROUTINE | subroutine |
| ENTRY | extra entry in subprograms |
| BLOCK DATA | common data, usually given initial values |
The following is an example program, Hello_World.f:
PROGRAM Hello_World
C |One can comment with the C in the first space of the line| C
C | or after a ! in any line. | C
C | | C
C |This program writes to the screen 'Hello World' 5 times. | C
DO 10 I = 1, 5, 1 !Integer goes from 1 to 5
WRITE(6,*) 'Hello World' !Write to screen 6 times.
10 END DO
STOP !Exit the program
END !Stop compiling
| IMPLICIT | default IMPLICIT REAL(A-H, O-Z), INTEGER(I-N) |
| IMPLICIT NONE | not standard, but very useful, it is available in Fortran 90. Gives the "Pascal convention" that all variables have to be specified. Sun and DEC the same effect can be obtained with the switch -u in the compilation command. |
| INTEGER | Discrete values (e.g. 1, 2, 3). |
| REAL | Nearly continuous numbers limited by bit size (e.g. 4.523231085). |
| DOUBLE PRECISION | Nearly continuous numbers limited by 2 X bit size (e.g. 8.5738X10^28). |
| COMPLEX | Number with imaginary (-x)^0.5 portion (0.12323, -23432). |
| LOGICAL | A TRUE or FALSE value. |
| CHARACTER*X | A Character string of X spaces. |
| DIMENSION | can also be given directly in the type specification, as well as in a COMMON |
| COMMON | common storage area for variables that are in several program units |
| EQUIVALENCE | common storage area for several variables in the same program unit |
| PARAMETER | makes a variable into a constant with a certain value |
| EXTERNAL | tells the system that the identifier is an external function or an external subroutine |
| INTRINSIC | tells the system that the identifier is an intrinsic function (or a subroutine, only in Fortran 90) |
| SAVE | Saves the values between exit or return from one subroutine into the new call of the same subroutine or function |
| DATA | puts initial values into variables |
| GOTO sn1 | Ordinary GOTO statement (jumps to the statement with number sn1). |
| GOTO (sn1,sn2,sn3), ie | Conditional GOTO statement. If the integer expression (ie) is 1, 2 or 3, execution jumps to statement number sn1, sn2 or sn3 (an arbitrary number of statement numbers snr are permitted). |
| GOTO snv, (snr1, snr2, snr3) | An assigned GOTO statement, jumps to the statement number that equals the statement number variable(snv). |
| GOTO snv | This is an assigned ordinary GOTO statement, it is a combination of the first one, GOTO snr1, and previous one, GOTO snv without a list of permitted alternatives. |
| ASSIGN sn TO snv | Statement number variables (snv) can not be assigned with an ordinary assignment of the type (integer variable = integer expression), it has to be done with the ASSIGN statement. The statement number variable can then be used for an assigned GOTO statement and in the ordinary GOTO statement and also in connection with FORMAT. |
| IF (LOGICAL) sn1,sn2,sn3 |
LOGICAL IF-statement, jumps to statement number sn1 if the expression is negative, sn2 if the expression is zero, sn3 if the expression is positive. |
| IF (LOGICAL) STATEMENT | Conditional Statement: If the logical expression is true, then the statement is performed, otherwise execution of statement is skipped. |
| IF(LOGICAL) THEN | Conditional Statement: If the logical expression is true, then continue until "ELSE", "ELSE IF", "END IF", or "CONTINUE" are encountered. |
| ELSE IF (LOGICAL) THEN | Alternate Conditional Statement: If the first logical in IF (LOGICAL) THEN was not encountered, then perform this conditional statement until "ELSE", "ELSE IF", "END IF", or "CONTINUE" are encountered. |
| ELSE | Alternate: If previous logical conditions were not satisfied, then preform the following statements until "ELSE", "ELSE IF", "END IF", or "CONTINUE" are encountered. |
| END IF | Exit conditional. |
| CONTINUE | Continuation, does nothing. It is recommended for clean conclusion of a DO-loop. |
| STOP | Concluding statement for program, stops execution. |
| END | Concluding statement for code, stops compilation of the program unit and also execution if it is in the main program. If END is found during execution of a subprogram, an automatic return to the calling program unit is executed (replaces the explicit RETURN statement). |
| PAUSE | Pause statement, stops execution temporarily (implementation dependent). |
| DO sn var = var1, var2, var3 | DO-loop. Floating-point numbers are permitted as variables in the DO-loop, but they are not recommended. It is preferable to use integers. |
| OPEN | Open a file so the program can use it. |
| CLOSE | Close a file. A file that has not been closed can sometimes not be read. |
| READ | Input from file or keyboard |
| WRITE | Output to file or screne. |
| Synonym to WRITE. It works on a standard unit. | |
| INQUIRE | Inquires about file status. |
| REWIND | Rewinds a file to the beginning. |
| BACKSPACE | Rewinds a file one record. |
| ENDFILE | Marks end of file. |
| FORMAT | Formatting statement describing layout of output. |
| CALL sbrtn | call a subroutine sbrtn. |
| fnctn | a function is called by giving the functionname fnctn. |
| RETURN | return from the subprogram (subroutine or function). |
| OUTPUT TYPE: | I.E. | COMMENT | |
| INTEGER | I | I5 | 5 positions reserved |
| FLOATING-POINT | F | F8.3 | A real number with decimals. A total of 8 digits (including decimal position), 3 for the fractional part, The remainder for pre-decimal part. |
| SCIENTIFIC NOTATION | E | E14.6 |
14 positions of which, 6 are used for the decimals, 4 - for the exponent, 1 - for the sign, 1 - for the starting zero, 1 - for the decimal point, 1 - for a blank character. |
| DOUBLE PRECISION | D | D20.12 | Same as E, but for double precision. |
| G | G14.6 | Same as F, if the number can be given within the field, else as E. | |
| LOGICAL | L | L1 | TRUE or FALSE |
| CHARACTER STRING | A | A7 | 7 characters are available in A7 |
| Positioning | T | Tn | n positions from the left |
| TL | TLn | n positions towards left | |
| TR | TRn | n positions towards right | |
| X | nX | n positions towards right | |
| NO NEW LINE | $ | $ | this is used if you wish to do input in direct connection with an output, to stay on the same line. Not standard! Not Fortran 90! |
| Discontinue | : | : | if the list does not contain any more elements the format is also finished here. |
| NEW RECORD/LINE | / | / | normally a new line |
| Binary | B | B | No format in Fortran 77 but B Fortran 90 |
| Octal | O | B | Not in Fortran 77 but Fortran 90 |
| Hexadecimal | Z | Z | not Fortran 77 but Fortran 90 |
| OUTPUT | SP | + is written | |
| SS | + is not written | ||
| S | standard (normal SS) In all alternatives a minus - is written for negative values | ||
| INPUT | BZ | blanks are interpreted as zeroes | |
| BN | blanks are not regarded as anything (blanks are skipped) |
Fortran has many intrinsic mathematical functions that are useful.
| FUNCTION | DESCRIPTION |
| abs(x) | Absolute value of x |
| iabs(i) | Absolute value of integer i (pre-F90) |
| cos(x) | Trigonometric cosine of x |
| sin(x) | Trigonometric sine of x |
| tan(x) | Trigonometric tangent of x |
| acos(x) | Trigonometric inverse cosine (cos-1)of x |
| asin(x) | Trigonometric inverse sine (sin-1)of x |
| atan(x) | Trigonometric inverse tangent (tan-1) of x |
| exp(x) | Calculates e(2.7183....) to the power of x |
| log(x) | Calculates the natural log (loge(x)) |
| log10(x) | Calculates the log base 10 (log10(x)) |
| sqrt(x) | Calculates the square root of x |
| nint(x) | Calculates nearest integer (rounds up 0.5 --> 1 -1.5 --> -1) |
| max(x1,x2,...) | Returns maximum value |
| min(x1,x2,...) | Returns minimum value |
Fortran has several mathematical operations that are useful.
| OPERATION | DESCRIPTION |
| x+y | Addition of x to y |
| x-y | Subtraction of y from x |
| x*y | Multiplcation of x and y |
| x/y | Division of y from x |
| x**y | x to the power of y |
| ((x+y)*y)**z | Parentheses indicate order of operation |
ADDITIONAL FORMAT-LETTERS IN FORTRAN 90:
You can now replace E as the mark for output in exponential form by ES and then you get the Scientific form with output of one digit different from zero before the decimal point. If you instead replace E by EN you get an ENgineering form with one to three digits before the decimal point and the exponent evenly divisible by three. If the output value is zero you get the same output from ES and EN as from E. Another extension is that the FORMATs I, B, O, and Z may be written Iw.m, Bw.m, Ow.m, and Zw.m, where w is the usual field width, and the optional m indicates the minimum number of digits, with leading zeros as necessary.
ADDITIONAL INPUT/OUTPUT STATEMENTS:
Most of these have a large number of parameters in the so-called control list which has been expanded considerably in Fortran 90. They are treated shortly in five pages of NAG (1992) and are ACCESS, ACTION, ADVANCE, APPEND, APOSTROPHE, ASIS, BLANK, DELETE, DELIM, DIRECT, END, EOR, ERR, EXIST, FILE, FMT, FORM, FORMATTED, IOLENGTH, IOSTAT, KEEP, NAME, NAMED, NEXTREC, NEW, NML, NO, NULL, NUMBER, OLD, OPENED, PAD, POSITION, QUOTE, READ, READWRITE, REC, RECL, REPLACE, REWIND, SCRATCH, SEQUENTIAL, SIZE, STATUS, UNDEFINED, UNFORMATTED, UNIT, UNKNOWN, WRITE, YES and ZERO.
