Basic Fortran Overview, Examples & Information

Basic F95 Program with Intrinsic Data

Circle:

MODULE Circle_Operations
IMPLICIT NONE
CONTAINS
FUNCTION Area(radius)
REAL :: Area
REAL, INTENT(IN) :: Radius
Area = Radius**2 * 3.14159
END FUNCTION Area
FUNCTION Circumference(radius)
REAL :: Circumference
REAL, INTENT(IN) :: Radius
Circumference = Radius * 2 * 3.14159
END FUNCTION Circumference
END MODULE Circle_Operations

PROGRAM Circle
USE Circle_Operations
IMPLICIT NONE
REAL :: r=5.0
PRINT *,”Area=”,Area(r)
PRINT *,”Circumference=”,Circumference(r)
END PROGRAM Circle

Non-Optimized vs. Optimized Compiler Output

Un-Optimized Output:

# 2 A = A*B+C
movq -16(%rbp),%rdx # C.base
movss 0(%rdx),%xmm1 #
movq -32(%rbp),%rsi # A.base
movss 0(%rsi),%xmm2 #
movq -24(%rbp),%rdi # B.base
movss 0(%rdi),%xmm0 #
mulss %xmm2,%xmm0 #
addss %xmm1,%xmm0 #
movq -32(%rbp),%rax # A.base
movss %xmm0,0(%rax) #

Optimized Output:

# 2 a = a*b+c
movss 0(%rdi),%xmm0 # [0] id:8 A+0x0
mulss 0(%rsi),%xmm0 # [3]
addss 0(%rdx),%xmm0 # [9]
movss %xmm0,0(%rdi) # [15] id:8 A+0x0


 

KIND

The Fortran 90 concept of kind provides the means for selecting and specifying the numeric model of integer and real data; both variables and constants. Language features are provided for inquiring about the numeric model and specifying the kind of the data entity.

KIND Functions

KIND(x) x is a numeric
expression
SELECTED_INT_KIND(x) x is an integer expression
SELECTED_REAL_KIND(p,r) p and r are integer
expressions

INTEGER KIND Examples

INTEGER, PARAMETER :: I1=SELECTED_INT_KIND(2)
INTEGER, PARAMETER :: I2=SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: I4=SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: I8=SELECTED_INT_KIND(18)
INTEGER(KIND=I1) :: I, J, K
INTEGER(KIND=I2) :: M(10), N
INTEGER(I4) :: Int4
INTEGER(I8) :: Large
INTEGER(4) :: iflag
INTEGER(8) :: n_count

REAL KIND Examples

INTEGER, PARAMETER :: SP=SELECTED_REAL_KIND(6,37)
INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(15,307)
INTEGER, PARAMETER :: QP=SELECTED_REAL_KIND(31,307)
REAL(KIND=SP) :: A, B, C
REAL(KIND=SP) :: U, V, W
REAL(KIND=QP) :: O, P, Q
REAL(KIND=KIND(1.0e0)) :: SP_var
REAL(KIND=KIND(1.0d0)) :: DP_var
REAL(SP) :: D, E, F
REAL(SP) :: X, Y, Z
REAL(SP) :: R, S, T
REAL(4) :: Single
REAL(8) :: Double
REAL(16) :: Quad

KIND With Constants

127_I1 127_1
32767_I2 32767_2
2147483647_I4 2147483647_4
9223372036854775807_I8 9223372036854775807_8

1.234_SP
56.789_DP
3.402823E+38_SP
1.797693134862315E+308_DP
1.79769313486231570814527423731730E+308_16


 

DATA Statements

Variables, substrings, arrays, and array elements are given initial values with DATA statements. DATA statements may appear only after the declaration statements in the program unit in which they appear.
DATA vlist /clist/[[,] vlist /clist/]…
vlist contains the symbolic names of variables, arrays, array elements, substrings, and implied DO lists clist contains the constants which will be used to provide the initial values for the items in vlist.

DATA Statements

DATA A,B,C,D,E/1.0,1.0,1.0,1.0,1.0/
DATA A,B,C,D,E/5*1.0/

INTEGER M(5), DAYS(7)
DATA M, DAYS/5*0, 1, 2, 3, 4, 5, 6, 7/

CHARACTER*10 Color
DATA Color /”Yellow“/

DATA Implied-DO

INTEGER M(10,10),N(10),L(4)
CHARACTER*3 S(5)

DATA (N(I),I=1,10),((M(I,J),J=3,8),I=3,8)/5*1,5*2,36*99/
DATA (L(I),I=1,4) /’ABCD’,’EFGH’,’IJKL’,’MNOP’/
DATA (S(I),I=1,5) /’ABC’,’DEF’,’GHI’,’JKL’,’MNO’/


 

Control statements

Control statements provide the mechanism for altering or controlling the logical flow of a program’s execution. The general form of a control statement is a block construct. It begins with an initial keyword, may have intermediate keywords, and ends with a termination statement.

GOTO Statements

GOTO s unconditional GOTO
GOTO (s, [,s]…]) e computed GOTO
GOTO i [[,] (s [,s]…)] assigned GOTO

Where s is the label of a statement, e is an integer expression, and i is the symbolic name of an integer variable that has been assigned the label of a statement with the ASSIGN statement.

GOTO 100

Block IF Construct

[name:] IF (e) THEN
block
ELSE [name]
block
END IF [name]

If the logical expression is .TRUE., the first block of
statements is executed, otherwise the second block of
statements is executed.

DO Example

Outer: DO
FLAG = .FALSE.
Inner: DO I=1, N-1
IF (A(I) < A(I+1)) CYCLE Inner
TEMP = A(I)
A(I) = A(I+1)
A(I+1) = TEMP
FLAG = .TRUE.
END DO Inner
IF (.NOT. FLAG) EXIT Outer
END DO Outer

CASE Example

CHARACTER :: Ch
LOGICAL :: Numeric, Alpha, Punctuation

SELECT CASE (Ch)
CASE (‘A’:’Z’, ‘a’:’z’)
Alpha = .TRUE.
CASE (‘0′:’9’)
Numeric = .TRUE.
CASE DEFAULT
Punctuation = .TRUE.
END SELECT

End & Stop

END Statement

Every program unit must have an END statement which
terminates the range of an individual program unit within a
source file. A source file itself may contain more than one
program unit. An END statement is executable. If
encountered in a main program it terminates execution; if
encountered in a subroutine or function subprogram it has the
effect of a RETURN statement.

END

STOP Statement:
Normally, program executions ends when the END statement
of the main program is reached. Another way to terminate
program execution is with the STOP statement.

STOP [s]

The optional string s may be a CHARACTER constant or string
of five or fewer digits and is output to standard out.


 

Input/Output Statements

Input and output statements or data transfer statements provide the means through which Fortran programs communicate data to external devices. Input is reading; output is called writing. Data transfer can be either formatted or unformatted. There are also auxiliary input/output for manipulating external files such as opening and closing files, rewinding and backspacing files, and inquiring about files.

PRINT

IMPLICIT NONE
INTEGER :: I
REAL :: Tire, Mile, Revs, RPM, t

Tire = 22.0 * 3.14159 ! inches per 1 revolution
Mile = 12.0 * 5280.0 ! inches in 1 mile
Revs = Mile/Tire ! revolutions per mile

DO I = 10, 100, 10
t = I/60.0 ! miles per minute
RPM = Revs * t ! revolutions per minute
print *, i, RPM
END DO

END

Your Output Will Be:

10 152.789
20 305.578
30 458.367
40 611.155
50 763.944
60 916.733
70 1069.52
80 1222.31
90 1375.10
100 1527.89

READ

The general form of the READ statement can take two forms:

READ f, [iolist]
READ (cilist) [iolist]

Where cilist is the control information list. The cilist
contains a unit specifier and optionally specifiers for format,
error, end-of-file, end-of-record, name list, record, advance,
and size.

WRITE

The WRITE statement provides the Fortran mechanism for
transferring output to external sources. The general form of
the WRITE statement is:

WRITE (cilist) [iolist]

Where cilist is the control information list.

OPEN

The OPEN statement is be used to connect to an existing file,
create a file, or modify an existing file connection.

OPEN ([UNIT=] u [,olist])

In addition to ERR and IOSTAT, olist may contain specifiers
to indicate the name of the file, the type and form of the
connection, and the position at open.

CLOSE

The CLOSE statement is used to disconnect a unit from an
external file:

CLOSE ([UNIT=] u [,clist])

clist may contain IOSTAT, ERR, and STATUS specifiers.
The STATUS specifier is a character expression that must be
equal to either “KEEP” or “DELETE”.

INQUIRE

The INQUIRE is used to obtain information about the
properties of files or units. The form of the INQUIRE
statement is:

INQUIRE ([UNIT=] u, ilist)
INQUIRE (FILE= f, ilist)
INQUIRE (IOLENGTH= i) output-list

In addition to ERR and IOSTAT, ilist may contain specifiers
to indicate the name of the file, the type and form of the
connection.

INQUIRE Statement Specifiers:

EXIST= logical variable
OPENED= logical variable
NUMBER= integer variable
NAMED= logical variable
NAME= character variable
ACCESS= character variable
SEQUENTIAL= character variable
DIRECT= character variable
FORM= character variable
FORMATTED= character variable
UNFORMATTED = character variable
RECL= integer variable
NEXTREC= integer variable
BLANK= character variable
POSITION= character variable
ACTION= character variable
READ= character variable
WRITE= character variable
READWRITE= character variable
DELIM= character variable
PAD= character variable

Other I/O Statements

Additional I/O statements include:

REWIND ([UNIT=] u [,alist])
BACKSPACE ([UNIT=] u [,alist])
ENDFILE ([UNIT=] u [,alist])

a list may contain IOSTAT and ERR specifiers.

Namelist Directed Input Example

INTEGER I; REAL X (8); CHARACTER (11) P; COMPLEX Z; LOGICAL G
NAMELIST / TODAY / G, I, P, Z, X
READ (*, NML = TODAY)

The input data records are:
&TODAY I = 12345, X(1) = 12345, X(3:4) = 2*1.5, I=6, ! comment
P = “ISN’T_BOB’S”, Z = (123,0)/

I 6
X(1) 12345.0
X(2) unchanged
X(3) 1.5
X(4) 1.5
X(5)–X(8) unchanged
P ISN’T_BOB’S
Z (123.0,0.0)
G unchanged


 

An array is a compound data object that consists of a sequence of data elements all of the same type and type parameters. An array is referenced by a single symbolic name. When an array name is used alone it refers to the entire sequence starting with the first element. When an array name is qualified by a subscript it refers to an individual element of the sequence.

Array Declaration Statements

REAL, DIMENSION(10) :: A, B(10,10)
REAL A(10), B(10,10)

REAL A, B
DIMENSION A(10), B(10,10)

The elements of A are A(1), A(2), A(3),…, A(10).
The elements of B are B(1,1), B(2,1), B(3,1), B(4,1), B(5,1), B(1,2), B(2,2), B(3,2), B(4,2),… B(10,10).

The number of elements of an array is called its size. The size of A is 10 and the size of B is 100 (10 x 10).

Automatic Array

An automatic array is an explicit shape array in a subprogram
that takes its dimension bounds and hence its shape from
non-constant expressions. Such an array is local to the
subprogram and is allocated on entry:

SUBROUTINE SWAP(A, B)
REAL, DIMENSION(:) :: A, B
REAL, DIMENSION(SIZE(A)) :: WORK
WORK = A
A = B
B = WORK
END SUBROUTINE SWAP

KIND Functions

KIND(x) x is a numeric
expression
SELECTED_INT_KIND(x) x is an integer expression
SELECTED_REAL_KIND(p,r) p and r are integer
expressions

Vector Subscript Example

REAL, DIMENSION(5,2) :: A
A(:,1) = (/1.,2.,3.,4.,5./)
A(:,2) = (/6.,7.,8.,9.,10./)
PRINT *,A((/1,3,5/),1)
PRINT *,A((/1,3,5/),2)
END
1.00000 3.00000 5.00000
6.00000 8.00000 10.0000

Modules Example

A typical application of a module is to define numeric kinds:

MODULE NUMERIC_KINDS
INTEGER, PARAMETER :: I1=SELECTED_INT_KIND(2)
INTEGER, PARAMETER :: I2=SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: I4=SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: I8=SELECTED_INT_KIND(18)
INTEGER, PARAMETER :: SP=SELECTED_REAL_KIND(6,37)
INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(15,307)
INTEGER, PARAMETER :: QP=SELECTED_REAL_KIND(31,307)
END MODULE NUMERIC_KINDS