Title: Fortran
1Fortran
- General
- Mathematical computations (engineering,
computational biology) - FORmula TRANslation ? FORTRAN
- Started in 1950's at IBM
- Fortran 66
- Fortran 77 ? this class
- Fortran 90 (95)
- Why
- dominant programming language used in engineering
applications - ( gt 50 engineering applications)
2Layout
12345678901234567890123456789012345678901234567890
12 ... 34567890
Column 1 marked C for comment line
73-80 Sequence number
1-5 statement number
6 continuation
7-72 main text
3- Fortran 77 is not a free-format language,
- strict set of rules for source code
- column position rules
- Col. 1 Blank, or a "c" or "" for comments
- Col. 2-5 Statement label (optional)
- Col. 6 Continuation of previous line
(optional) - Col. 7-72 Statements
- Col. 73-80 Sequence number (optional, rarely
used today)
4Variable names
- No difference between upper or lower cases
- No reserved keyword
- Integer
- Real
- Complex
- (1.0,2.2), (1.0e0,0.22e1)
- Logical .true. , .false.
- Character a, 123
- Double precision 3.14159265d0
5- Variable names generally consist of 1-6
characters - chosen from the letters a-z and the digits
0-9 - The first character must be a letter
- Case insensitive (upper case, lower case)
-
6integer sum, id, SSN integer2 short real
temp, height double precision temp, height,
long complex curr,volt logical done,
error character20 name, names(20)
Extra caution I-N rule
7Default integer variable names
- Any names start with
- I,J,K,L,M,N (i,j,k,l,m,n)
- To overrule
- IMPLICIT NONE
- To add additional implicit variable type
- IMPLICIT COMPLEX C,X
8- A general Fortran program
- a main program (or driver) and
- several subprograms (or procedures or
subroutines). - program name
- declarations
- statements
- stop
- end
- subprograms ..
9--------------------------------------------------
------- program circle real r, area c This
program reads a real number r and prints c the
area of a circle with radius r. write (,)
'Give radius r' read (,) r area
3.14159rr write (,) 'Area ', area stop
end
10- Constant
- parameter (name constant, ... , name
constant) - parameter (pi3.14159,c3.e8)
- The "variable" defined in the parameter
statement is not a - variable but rather a constant whose
value can never change -
- A "variable" can appear in at most one parameter
statement - The parameter statement(s) must come before the
first - executable statement
11data
- DATA x,y,z/1.0,2.2,3.e0/
- placed before executable statements
12Arithmetic Expressions operand operator operand
exponentiation ,/ multiplication,
division ,- addition, subtraction caution
for division integer/integer ? perform integer
division 5/3 ? 1 5.0/3.0 ?
1.66667 5.0/3 ? 1.66667
13Logical expressions Logical expressions can only
have the value .TRUE. or .FALSE.. A logical
expression can be formed by comparing arithmetic
expressions using the following relational
operators meaning .LT. lt .LE. lt
.GT. gt .GE. gt .EQ. .NE. !
14Assignment The assignment has the form
variable_name expression Evaluate the right
hand side and assign the resulting value to the
variable on the left. For example, area pi
r2
15Type conversion different data types occur in
the same expression ? type conversion implicitly
real x x x 1 explicitly int
(variable) int(2.3) ? 2 real real(2)
?2.0 dble ichar char
16key-board input / screen output
read (,) list-of-variables write(,)
list-of-variables
integer m, n real x, y read(,) m, n ! -1
100 read(,) x, y ! -1.0 1e2 write(,)
m,n write(,) x,y
17Format statement write(, label)
list-of-variables label format format-code
Eg. format(1x,i3,1x,f5.2)
A - text string D - double precision numbers E -
real numbers, exponent notation F - real
numbers, fixed point format I - integer X -
horizontal skip (space) / - vertical skip
(newline)
18 x 0.025 write(,100) 'x', x 100 format
(A,F) write(,110) 'x', x 110 format
(A,F5.3) write(,120) 'x', x 120 format
(A,E) write(,130) 'x', x 130 format
(A,E8.1) write (,999) x 999 format ('The
answer is x ', F8.3) Output x
0.0250000 x0.025 x 0.2500000E-01 x
0.3E-01 The answer is x 0.250
19- FILE I/O
- open statement (no need to specific read/write
at open) -
- open(unit15,fileinput1.dat,statusold)
- open(unit16,fileoutput.dat,statusnew)
- open(11,filenewinput.dat)
- open(12,filenewoutput.dat)
20- Write statement
- write(unit number,label) variable list
- open(11,fileoutput.dat,statusnew)
- write(11,130) 'x', x
- 130 format (A,E8.1)
- output.dat
- x 0.3E-01
21- Read statement
- read (unit number, label) variable list
- open(10,fileinput.dat,statusold)
- read(10,) x,y
- input.dat
- 10.0 20.0
22- Source code format
- Data type
- Simple I/O
- Control statement
- if
- block if
- if-else
- if-else if
- do
- go to
23- if (logical expression) executable statement
- logical expression .true. executes statement
- logical expression .false. do not execute
statement
true
false
24 if (x .LT. 0) x -x ! Find the absolute
value of x if (weight.gt.20) countcount1
program checknumber write(,) input
number read(,) n If (n.lt.0) write(,)
negative number If(n.eq.0) write(,) value
equal to 0 If(n.gt.0) write(,) positive
output stop end
25if (logical expression) then statements endif
if ( grade.gt.1.67) then sumhsumhhour sumgsu
mggrade coursecourse1 endif gpasumg/sumh
statements
26If (logical expression ) then statement
1 statement 2 statement m else statement
a statement b statement c endif
statement group a
statement group b
27 logical expression
true
false
statement group A
statement group B
28numpos0 numneg0 assign x value If (x.gt.0)
then ysqrt(x)sin(x) sumsumx numposnumpos1 el
se ylog(abs(x)) sumsumabs(x) numnegnumneg1 e
ndif
29if (logical expression) then statements else if
(logical expression) then statements else if
(logical expression) then statements else
statements endif
block 1
block 2
block 3
30T
condition 1
block 1
T
block 2
condition 2
T
condition 3
block 3
T
block n
condition n
else block
F
31Multiprocessor parallel program computer node
selection
C multiway selection if(myrank.eq.0)
then write(,) start job on node 0 else
if(myrank.eq.1) then write(,) start job on
node 1 else if(myrank.eq.2) then write(,)
start job on node 2 else if(myrank.eq.n-2)
then write(,) start job on node
n-2 else write(,) start job on node
n-1 endif
32Nested if if (x .GT. 0) then if (x .GE. y)
then write(,) 'x is positive and x gt y'
else write(,) 'x is positive but x lt y'
endif elseif (x .LT. 0) then
write(,) 'x is negative' else
write(,) 'x is zero' endif
33 do label var expr1, expr2, expr3 statements
label continue var integer exp1
initial value of var exp2 terminating
bound exp3 increment step do var expr1,
expr2, expr3 statements enddo
popular
exp3 default is 1
34 integer i, n, sum sum 0 do 10 i 1,
n !do i1,n sum sum i write(,) 'i ',
i write(,) 'sum ', sum 10 continue
!enddo
integer i do 20 i 10, 1, -2 write(,) 'i
', i 20 continue
35Nested loop do index11,nmax do
index21,mmax statements enddo !enddo for
index2 enddo !enddo for index1
Typically used in the multiple dimension array
manipulation
36while (logical expr) do statements enddo
i 1 sum0 while (i.lt.100) do sum sum i
i i2 enddo
37go to n
unconditional branch
Better to use in do/while loop
do i1,200 sumsumi if(sum.gt.100) goto
20 enddo 20 write(,) sum
38CONTINUE
- n CONTINUE
- Used to serve as the end of a DO loop
- n is the statement
- Example
- do 9 I1,100,2
- do 8 j1,4
-
- 8 continue
- ...
- 9 continue
39- Control statement
- if
- block if
- if-else
- if-else if
- do
- go to
- Arrays
- Subprograms
- Function
- Subroutine
40One dimensional Array
real a1,a2,a3,a20 real a(20)
default, index starts from 1 to 20
a(1)
a(2)
a(17)
a(18)
a(19)
a(20)
41- real var(lowerboundupperbound)
- lowerbound lt upperbound
- bounds must be integer numbers
real b(019), weird(-162237)
Number of element 20 400
real a(-510) a(-5),a(-4),a(0),a(1),a(10)
a(-5)
a(-4)
a(7)
a(8)
a(9)
a(10)
42100
real temp(-100100) open(1,filetemp.dat,status
old) do i-100,100 read(1,)
temp(i) enddo stop end
temp.dat 20 22 . . . 29
Read (1,)(temp(j), j-100,100)
-100
Temp.dat 20 22 29
43Two dimensional array --gt no pointer
real image(200,300),coord(-20020,-40200) Fortra
n stores array by column!
data statement data list of variable names
/list of values/ data a,b,c/2.3,2.1,1.5/ real
a(2,3) data a/1.0,2.0,3.0,4.0,5.0,6.0/
1 3 5 2 4 6
443.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.
8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2
5.5 6.70
real y(4,6) read(,) y write(,) y stop end
Input line 3.4 10.9 7.1 32.7 23.1 2.1 8.8
21.6 6.7 Output 3.4 10.9 7.1 32.7 23.1
3.1 43.2 75.2 4.0 1.7 12.1 43.2 11.2 1.5 23.0
3.2 3.4 12.1 12.1 5.5 2.1 8.8 21.6 6.7
45real y(4,6) do i1,4 read(,) ( y(i,j) ,
j1,6) enddo do i1,4 write(,)
(y(i,j),j1,6) enddo stop end
inputline
3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.
8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2
5.5 6.70
Output same as inputline
46real y(4,6) read(,) (( y(i,j) ,
j1,6),i1,4) write(,2) (( y(i,j) ,
j1,6),i1,4) format(1x,6f7.1) stop end
2
Intputline
3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1
8.8 7.1 43.2 12.1 23.0 12.1 21.6
32.7 75.2 43.2 3.2 5.5 6.70
3.4 23.1 4.0 11.2 3.4 2.1 10.9 3.1 1.7 1.5 12.1 8.
8 7.1 43.2 12.1 23.0 12.1 21.6 32.7 75.2 43.2 3.2
5.5 6.70
Outputline
47three dimensional array and multiple dimensional
array
real a(10,20,30), ab(10,20,30,40)
A(j,k,m) j varies most rapidly, k next, m most
slowly A(1,1,1), A(2,1,1),,A(10,1,1) A(1,2,1),
A(2,2,1),...
real temp(10,10,10) temp(5,5,5)
21.4 temp(1,2,3) 20.0
1 2 3 4 5 6 7 8 9 10
48- Subprogram
- Function
- One return value
- Subroutine
- May have multiple return values
49type function name (list-of-variables)
declarations statements name return end
- Functions have a type. This type must also be
declared in the calling program. - The return value should be stored in a variable
with the same name as the function. - Functions are terminated by the return statement
instead of stop - type integer, complex, real,
50C This is the function function sum(w) real
w(3,4) sum0 do j1,3 do i1,4
sumsumw(i,j) enddo enddo return end
C This is the main program dimension
w(3,4) read(,) w write(,) sum is ,
sum(w) stop end
Inputline 3.2 1.2 5.4 3.2 9.0 6.5 2.1 12.2 32.1
45.3 1.2 1.2 Output sum is 122.50
51Function with adjustable dimension
real function sum(a,nrow,ncol) dimension
a(nrow,ncol) sum0.0 do i1,nrow do j1,ncol
sumsuma(i,j) enddo enddo return end
parameter(nrow4,ncol5) real f(nrow,ncol) do
i1,nrow read(,) (f(i,j),j1,ncol) enddo write(
,) sum(f,nrow,ncol) stop end
52function within function
complex function fun1(y) complex
ci cicmplx(0.0,1.0) fun1fun_sub1(y)cifun_sub2(
y) return end real function fun_sub1(y) fun_s
ub1sqrt(y) return end real function
fun_sub2(y) fun_sub2yy return end
53Function ? essentially return only one
variable Subroutine ? able to return multiple
variables
Syntax
subroutine name (input/output variables)
declarations statements return end
Subroutine has no type, should not be declared in
the calling program
54Subroutine
subroutine iswap (a, b) integer a, b c
Local variables integer tmp tmp a b
a1 return end
- declare the input/output parameters
- declare the local variables
- use same variable names in different subprograms
55- Fortran 77 uses the call-by-reference
- the memory address of the arguments (pointers)
are passed - the input variable may change its value
- integer m, n
- m 1
- n 2
- call iswap(m, n)
- write(,) m, n
- stop
- end
call matrixvector(B,c,d,m) c mm20
now subroutine matrixvector(A,x,b,n) real
a(n,n),x(n),b(n) nn20 return end
56Common Block? data share among subprograms/program
program main some declarations real alpha,
beta common /coeff/ alpha, beta
statements call sub1() stop end
subroutine sub1 (some arguments) declarations
of arguments real alpha1, beta1 common /coeff/
alpha1, beta1 statements return end
subroutine sub2 (some arguments) declarations of
arguments real alpha, beta common /coeff/
alpha, beta statements return end
57Syntax common / name / list-of-variables
- The common statement should appear together with
the variable - declarations, before the executable
statements. - Different common blocks must have different
names (just like variables). - A variable can belong to more than one common
block. - The variables in a common block do not need to
have the same names - each place they occur (although it is a good
idea to do so), but they - must be listed in the same order and have the
same type.
58C main program common/coef/alpha,beta . . .
alpha
beta
C function common/coef/alpha1,beta1 . . .
C subroutine common/coef/alpha2,beta2 . . .
59Common block with array elements
program main integer nmax parameter (nmax20)
integer n real A(nmax, nmax) common /matrix/
A, n, nmax subroutine sub1 (...) integer
nmax parameter (nmax20) integer n real A(nmax,
nmax) common /matrix/ A, n, nmax
60Arrays with variable dimensions cannot appear in
common blocks
program main integer nmax parameter (nmax20)
integer n real A(nmax, nmax) common /matrix/
A, n, nmax subroutine sub1 (ndim) integer
ndim integer n real A(ndim,ndim) common
/matrix/ A, n, nmax
61With common block, dont need to pass variables
subroutine sub1 declaration common/all/list of
variables statements return end subroutine
sub2 declaration common/all/list of
variables statements return end
Program main declaration common/all/list of
variables call sub1 call sub2 stop end
62Using include statement
subroutine sub1 Include var.dim sub1_program
local declaration statements return end subroutin
e sub2 include var.dim sub2_program local
declaration statements return end
var.dim shared declaration common/com1/list of
variables common/com2/list of variables
Program main include var.dim main_program local
declaration call sub1 call sub2 stop end
63program main real origo(3), x(3) real d, dist
common /silly/ origo read(,) origo(1),
origo(2), origo(3) read(,) x(1), x(2), x(3) d
dist(x) write(,) 'The distance is ', d stop
end real function dist (x) real x(3) real
x0, y0, z0 common /silly/ x0, y0, z0 dist
sqrt((x(1)-x0)2 (x(2)-y0)2 (x(3)-z0)2)
return end