Programming With Fortran
Programming With Fortran
AE6382
Introduction to FORTRAN
OBJECTIVES
Program structure
Data types and specification statements
Essential program control
FORTRAN I/O
subfunctions and subroutines
AE6382
FORTRAN History
Version history
Statement Format
73-80
Optional
Line #s
AE6382
Statement Format
statement labels are not required unless the statement is the target
of a goto
labels are numeric values only
any character in column 6, other than space or 0, indicates that
this line is a continuation of the previous line
there is usually a limit of 19 on the number of continuations
AE6382
Statement Format
AE6382
FORTRAN
Compiler
FORTRAN
Program
Source Code
Make Changes
in Source Code
Object Code
Libraries
Executable
File
Executable
Code
Execute
Program
AE6382
AE6382
program
function
subroutine
block data
The program unit contains the main code and the point
where execution starts
AE6382
Fortran Program
The program unit contains the main code and the point
where execution starts
internal functions
internal subroutines
AE6382
Fortran Subroutine
AE6382
Fortran Subroutine
SUBROUTINE MULT(A,B,C)
C = A * B
RETURN
END
CALL MULT(5.0,X,VALUE)
AE6382
Fortran Function
AE6382
Fortran Function
REAL FUNCTION MULT(A,B)
MULT = A * B
RETURN
END
VALUE = MULT(5.0,X)
AE6382
AE6382
AE6382
AE6382
Fortran Variable
AE6382
INTEGER
REAL
DOUBLE PRECISION
COMPLEX
LOGICAL
CHARACTER (77+)
AE6382
AE6382
AE6382
one computer may have a 32 bit integer while another may use
16 bit as its default
REAL*8, INTEGER*4
the number after the * indicates the number of bytes used
most computers have 8 bit bytes
not every architecture will have every combination
not a practical problem in an Intel world
but knowledge of the architecture of the system where a legacy
Fortran program was developed is needed to convert to Intel
AE6382
AE6382
Fortran Arrays
AE6382
Fortran Arrays
AE6382
Fortran Arrays
AE6382
AE6382
each program unit that declares the common block has access
to it
AE6382
COMMON/SET1/A,B(50,5),C
AE6382
Problems
Fortran Equivalence
AE6382
Fortran Parameter
AE6382
Fortran Literals
Other
integers - 1, -34
real - 1.0, 4.3E10, 5.1D-5
complex (5.2,.8)
logical - .true., .false.
character title line
AE6382
Fortran Literals
INTEGER A
A = 34
REAL A(20)
A(1) = 31.4159E-1
ITERM = -10.3
COMPLEX Z
Z = (10,-10.5)
REAL_PART = REAL(Z)
AIMAG_PART = AIMAG(Z)
Z = CMPLX(REAL_PART * 2,AIMAG_PART)
AE6382
Fortran Expressions
numeric
2 * 3.14159 * RADIUS**2
SIN(PI)
logical
IBOOL = .TRUE.
I .EQ. 10 .AND. ISTOP
note: LOGICAL ISTOP
AE6382
**
(exponentiation)
*/
unary + binary + -
AE6382
5/2
FLOAT(5)/2
=>
=>
2 not 2.5
2.5
AE6382
Intrinsic Functions
AE6382
Type Conversion
Conversion to integer
generic
available only in 77 and above
argument specific
INT(any)
the generic version
INT(real)
IFIX(real)
IDINT(double)
Conversion to real
REAL(any)
the generic version
FLOAT(integer)
REAL(integer)
SNGL(double)
AE6382
Type Conversion
Conversion to double
Conversion to complex
DBLE(any)
ICHAR(character)
CHAR(integer)
AE6382
Truncation
AINT(real or double)
AINT(real)
DINT(double)
ANINT(real or double)
ANINT(real)
DNINT(double)
NINT(real or double)
NINT(real)
IDNINT(double)
AE6382
Math Functions
exponential
SIN(real or double)
SIN(real)
DSIN(double)
CSIN(complex)
EXP(real or double)
EXP(real)
DEXP(double)
CEXP(complex)
natural logarithm
LOG(real or double)
ALOG(real)
DLOG(double)
CLOG(complex)
AE6382
Math Functions
tangent (radians)
square root
TAN(real or double)
TAN(real)
DSIN(double)
SQRT(real or double)
SQRT(real)
DSQRT(double)
CSQRT(complex)
hyperbolic sine
SINH(real or double)
SINH(real)
DSINH(double)
AE6382
Math Functions
AE6382
Fortran Statements
assignment (=)
branching (GOTO)
comparison (IF)
looping (DO)
subroutine invocation (CALL)
AE6382
Fortran Assignment
INTEGER A
A = A + 1
AE6382
Fortran Branching
200 C = B * A
AE6382
Fortran Branching
assigned goto
computed goto
GOTO and GO TO are equivalent
AE6382
Fortran Branching
Assigned goto
ASSIGN 100 TO TARGET
GOTO TARGET
100 CONTINUE
AE6382
Fortran Branching
Computed goto
Operates much like a case or switch statement in other
languages
GOTO (100,200,300,400),IGO
100 CONTINUE
GOTO 500
200 CONTINUE
GOTO 500
500 CONTINUE
AE6382
Fortran Continue
AE6382
Fortran IF
AE6382
Fortran 3-way If
GOTO 100
20 CONTINUE
GOTO 100
30 CONTINUE
IF (ABS(RADIUS-EPS)) 10,10,20
10 CONTINUE
GOTO 100
20 CONTINUE
GOTO 100
100 CONTINUE
GOTO 100
100 CONTINUE
AE6382
Fortran Logical If
100 CONTINUE
IF (IMODE .EQ. 2) A = SQRT(CVALUE)
LOGICAL QUICK
QUICK = .TRUE.
IF (QUICK) STEP=0.5
IF (.NOT. QUICK) STEP = 0.01
AE6382
Fortran Logical If
LOGICAL QUICK
QUICK = .TRUE.
IF (QUICK) STEP=0.5
IF (.NOT. QUICK) STEP = 0.01
AE6382
Fortran Modern If
then clause
else clause
else if clause
AE6382
Fortran Modern If
END IF
AE6382
Fortran Modern If
ELSE
END IF
AE6382
Fortran Looping
100 CONTINUE
Fortran Looping
nested loops can share the same label very bad form
DO 100 I=1,10,2
DO 100 J=1,5,1
A(I,J) = VALUE
100 CONTINUE
200 CONTINUE
AE6382
Fortran Looping
100 CONTINUE
AE6382
Fortran Looping
DO 100 I=1,100
GOTO 1000
CALL XYZ
99 CONTINUE
100 CONTINUE
100 CONTINUE
1000 CONTINUE
GOTO 99
AE6382
Fortran Looping
ENDDO
DO I=1,100
DO J=1,50
A(I,J) = I*J
END DO
ENDDO
AE6382
Fortran Looping
ENDDO
END DO
AE6382
Fortran Looping
AE6382
CALL XX(A,B,C)
SUBROUTINE XX(X,Y,Z)
END
END
AE6382
AE6382
Miscellaneous Statements
AE6382
READ
WRITE
OPEN
CLOSE
INQUIRE
REWIND
BACKSPACE
ENDFILE
FORMAT
older programs will just use a unit without any declaration the
linkage to a file was performed by the OS
the following usage is common
PROGRAM MAIN(INPUT,OUTPUT,TAPE5=INPUT,TAPE6=OUTPUT)
AE6382
formatted
unformatted or binary
sequential
random
AE6382
AE6382
AE6382
AE6382
AE6382
9000 FORMAT(1X,4F8.5,2x,E14.6,//)
READ(5,(1X,4F8.5,2x,E14.6,//),IOSTAT=IERR) A,B,C,D,E
IF (IERR) 100, 200, 300
C 100 EOF, 200 OK, 300 an error
AE6382
AE6382
Namelist directed
REAL A(5,5)
READ(5,*) A
--- same as
READ(5,*) ((A(I,J),I=1,5),J=1,5)
AE6382
FORMAT Statement
AE6382
Format Specifiers
X format code
I format code
Syntax: Iw
Specifies format for an integer using a field width of w spaces. If
integer value exceeds this space, output will consist of ****
F format code
Syntax: nX
Specifies n spaces to be included at this point
Syntax: Fw.d
Specifies format for a REAL number using a field width of w
spaces and printing d digits to the right of the decimal point.
A format code
Syntax: A or Aw
Specifies format for a CHARACTER using a field width equal to
the number of characters, or using exactly w spaces (padded
with blanks to the right if characters are less than w.
AE6382
T format code
Syntax: Tn
Skip (tab) to column number n
Syntax: quoted_string
Print the quoted string in the output (not used in input)
L format code
Syntax: Lw
Print value of logical variable as T or F, right-justified in field of
width, w.
AE6382
BN format code
Syntax: BN
Ignore embedded blanks in a numeric field
BZ format code
Syntax: BZ
Treat embedded blanks in a numeric field as zero
AE6382
AE6382
E format code
Syntax: Ew.d
Print value of REAL variable using scientific notation with a
mantissa of d digits and a total field width of w.
Ex:
E14.5 produces for the REAL value -1.23456789e+4:
|----+----o----+----o----+----o----+----|
-0.12345E+05
You must leave room for sign, leading 0,decimal point, E, sign,
and 2 digits for exponent (typically at least 7 spaces)
If specified width is too small, mantissa precision, d, will be
reduced unless d<1 in which case *** will be output.
Using nP prefix will shift mantissa digit right by n and reduce
exponent by n. Ex; 1PE14.5 above yields:
|----+----o----+----o----+----o----+----|
-1.23456E+04
AE6382
G format code
Syntax: Gw.d
Print value of REAL variable using Fw.d format unless value is
too large or too small, in which case use Ew.d format.
Ex:
G14.5 produces for the REAL value -1.23456789e+4:
|----+----o----+----o----+----o----+----|
-12345.67890
When the number gets too big (or too small) for F, it is switched
to an E format. Ex: the value -1.23456789e-18 becomes:
|----+----o----+----o----+----o----+----|
-0.1234567E-19
Forward slash, /
Repeat factor
Carriage control
ACCESS
STATUS
AE6382
AE6382
re-read a line
AE6382
AE6382
AE6382
Sub Programs
the subroutine
the function
AE6382
Sub Programs
SINE =
RETURN
END
AE6382
Sub Programs
AE6382
Sub Programs
supplying only the name will pass the address of the start of the array
with indices supplied, the address of that element is passed
missing indices are assumed to be 1
DIMENSION A(5,5)
SUBROUTINE SUB(X,Y)
CALL SUB(A,A(3,2))
DIMENSION X(5,5)
REAL Y
END
AE6382
Sub Programs
the shape of the array must match in both the caller and the
called units
the shape is the number of dimensions and extent of each
dimension, 5x5
a mis-match will generally result in incorrect calculations and
results
DIMENSION A(5,5)
SUBROUTINE SUB(X)
DIMENSION X(5,5)
CALL SUB(A)
END
AE6382
Sub Programs
SUBROUTINE SUB(X)
DIMENSION X(5,1)
CALL SUB(A)
END
AE6382
Array Storage
For an array dimensioned as A(M,N). The mapping for element A(I,J) to the equivalent onedimensional array is,
INDEX = (I - 1) * (J - 1)*M
AE6382
Array Storage
program order
implicit none
integer i,j
integer a(5,5), b(25)
equivalence (a(1,1),b(1))
do i=1,5
do j=1,5
a(i,j) = i*100 + j
enddo
enddo
write(6,9000) ((a(i,j),j=1,5),i=1,5)
write(6,9010) b
write(6,9020) a
9000 format(/,'a(i,j): ',/,(5(1x,i4.4)))
9010 format(/,'b:',/,(15(1x,i4.4)))
a(i,j):
0101 0102 0103 0104 0105
0201 0202 0203 0204 0205
0301 0302 0303 0304 0305
0401 0402 0403 0404 0405
0501 0502 0503 0504 0505
9020 format(/,'a:',/,(5(1x,i4.4)))
end
b:
0101 0201 0301 0401 0501 0102 0202 0302 0402 0502 0103 0203 0303 0403 0503
0104 0204 0304 0404 0504 0105 0205 0305 0405 0505
a:
0101 0201 0301 0401 0501
0102 0202 0302 0402 0502
0103 0203 0303 0403 0503
0104 0204 0304 0404 0504
0105 0205 0305 0405 0505
AE6382
Sub Programs
CALL SUB(A,5,5)
END
SUBROUTINE SUB(J,IROW,ICOL)
DIMENSION J(IROW,ICOL)
END
AE6382
Sub Programs
program main_add
implicit none
c
c
c
c
do i=1,m
define the largest matrix allowed
read(5,8010) (b(i,j),j=1,n)
integer MAXROW,MAXCOL
write(6,*)i,(b(i,j),j=1,n)
parameter (MAXROW=9,MAXCOL=9)
c
c
enddo
c
c
call the subroutine to add the matrices,
result in c
call add(a,b,c,MAXROW,m,MAXCOL,n)
real b,c
dimension b(MAXROW,MAXCOL),c(MAXROW,MAXCOL)
c
c
c
c
character*80 line
write(6,9000) ((c(i,j),j=1,n),i=1,m)
c
c
write the result (use explicit do loop for
rows)
c
read the first line of the file, the title
read(5,'(a80)') line
c
columns)
c
c
do 20 i=1,m
read(5,*) m,n
write(6,9000) (c(i,j),j=1,n)
local memory
integer i,j,m,n
20 continue
c
8000 format(5f10.2)
read(5,8000) (a(i,j),j=1,n)
8010 format(bz,5f10.2)
write(6,*)i,(a(i,j),j=1,n)
9000 format(10f14.4)
10 continue
end
AE6382
Sub Programs
c
c
define the subroutine to perform the
matrix addition
c
c = a + c
(matrix addition)
c
a, b, and c are declared in the calling
program to have
c
c
dimension (mmax,nmax)
local variables
integer i,j
c
c
enddo
enddo
c
subroutine add(a,b,c,mmax,m,nmax,n)
implicit none
return
end
c
c
c
c
c
c
the right-most dimension can always be
specified as 1
c
see the 1-d conversion formula as to
why
c
AE6382
Sub Programs
AE6382
Sub Programs
PART1.F
program sample
include 'common.f
write(*,*) 'a=',a,', b=',b,', c=',c
COMMON.F
call modify
common/test/a,b,c
integer a,b,c
end
PART2.F
block data
include 'common.f
data a,b,c/7,42,70/
end
PART3.F
subroutine modify
MAKEFILE
include 'common.f
integer total
total = a + b + c
write(*,*) "Total=",total
a = a / 2
b = b / 2
c = c / 2
return
end
AE6382
Statement Functions
F(X,Y) = X**2 + Y
A = 3.245 * F(4.3,B) - C
END
AE6382
Fortran Books
http://www.star.le.ac.uk/~cgp/fortran.html
PDF, HTML, and TeX versions are available
also on tsquare under resources
http://www.kcl.ac.uk/kis/support/cc/fortran/f77book.pdf
can be downloaded from above
AE6382
Not Fortran
#include <stdio.h>
main(t,_,a)
char *a;
{
return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}
AE6382
AE6382