Fortran 77
Introduction to f77
Arithmetic Operators & Expressions
+ Addition or Sign
- Subtraction or Sign
* Multiplication
/ Division
** Exponentiation
Expressions may involve numeric constants and variables.
Parentheses may be used as needed to preserve the
intended logic. New variables may be assigned the value of
expressions.
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.1/14
Fortran 77
Introduction to f77
Arithmetic Operators & Expressions cont
Examples:
3.14159 (REAL) if other: 3.14159D0 or DBLE(3.14159)
K
(A+B)*(C+D)
-1.0/X + Y/Z**2 better: ( DBLE(-1)/X ) + ( Y/(Z**2) )
2.0 * 3.14159*RADIUS
DELTA = ( DBLE(-1)/X ) + ( Y/(Z**2) )
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.2/14
Fortran 77
Introduction to f77
Creating Output
WRITE(*,*) ’HAHA’, X Writes format free
(second *) to the standard output unit (xterm/shell
defined by first *) the string ’HAHA’ and the content of
variable X.
WRITE(6,*) ’HAHA’, X Similar to the above
except that the standard output unit (6) is explicitly
stated.
WRITE(6,’(6A,I9)’) ’HAHA’, X Similar to the
previous except that now we use a format specifier
’(6A,I9)’ that actually tells WRITE to print a string
consisting of 6 characters followed by an integer with 9
digits right centered.
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.3/14
Fortran 77
Introduction to f77
Creating Output cont
WRITE(6,100) ’HAHA’, X
100 FORMAT(6A,I9) The format specification
may be done outside the actual WRITE statement with a
FORMAT command which has to have a corresponding
label (100 in this present example).
Format Descriptors Example Handled Data Types
I4 1234 Integer numbers
F12.6 12345.789012 Floating point numbers
E12.4 0.1235E+09 Exponential form
A5 TH8&% String of characters
3 I2 12 34 56 Repeating a number of times
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.4/14
Fortran 77
Introduction to f77
First Program
C23456789012345678921234567893123456789412345678951234567896123456789712
PROGRAM HELLO
IMPLICIT NONE
WRITE(6,*) ’HELLO WORLD’
END
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.5/14
Fortran 77
Introduction to f77
First Program cont
1. Writing the program:
At first the program has to be written (i.e. text has to be
produced) which is a perfect task for vi. In a Linux/UNIX
terminal type:
vi hello.f
and write up the program.
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.6/14
Fortran 77
Introduction to f77
First Program cont
2. Compiling the program:
Next we have to make this program an executable (i.e.
convert the list of commands to a machine-executable
binary) which is done with the help of a compiler. In a
Linux/UNIX terminal type:
f77 ./hello.f
If you prefer having the executable named something
other than a.out you’d use the -o flag:
f77 -o ./hello ./hello.f
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.7/14
Fortran 77
Introduction to f77
First Program cont
3. Running the program:
If compilation was successful, we may run the program
with the specified executable name (a.out by default or
what has been defined with the -o flag above), thus in a
Linux/UNIX terminal type:
./hello
→ http://www.strath.ac.uk/CC/Courses/fortran.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.8/14
Fortran 77
Introduction to f77
Creating Output in Files
Instead of writing to the standard output unit (6) we may
define whatever other unit number, which will then refer to
some file, either to be created afresh or else to be
overwritten. We may use the same format descriptors
shown for the WRITE command. However, we have to
manage the file handling with OPEN and CLOSE commands.
WRITE(44, ’(6A,I9)’) ’HAHA’, X Writes into the
file corresponding to the unit number 44 that had to be
opened before via an OPEN command.
→ http://www.fortran.com/F77_std/f77_std.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.9/14
Fortran 77
Introduction to f77
Creating Output in Files cont
OPEN (UNIT=44, STATUS=’NEW’,
NAME=’TOMAHAWK’, FORM=’FORMATTED’)
Opens a new file with the name TOMAHAWK, which
will be accessible for writing/reading via the unit number
44. Writing/reading will be done in formatted mode.
Further options are available.
CLOSE (UNIT=44, STATUS=’KEEP’) Closes
the file associated with unit number 44 and keeps it
available for further usage (does not delete it).
→ http://www.fortran.com/F77_std/f77_std.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.10/14
Fortran 77
Introduction to f77
Reading Something In
Similar to creating output to the standard output unit (6) or
to some file we may read in data from the standard input
unit (5) or from some file.
READ(5, ’(6A,I9)’) OLGA, X Reads from the
standard input unit (5) a 6-character long string and
assigns it to variable OLGA, and a 9 digit integer
number that is assigned to variable X.
READ(44, ’(6A,I9)’) OLGA, X Same as above,
except that the data is read from some file associated
with unit number 44 that had to be opened before.
→ http://www.fortran.com/F77_std/f77_std.html
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.11/14
Fortran 77
Introduction to f77
Example: Compute Repayment
Consider a Fixed Term Loan:
Suppose a certain amount of money has been taken in the
form of a loan. The money should be payed back within a
certain number of years. In addition the interest rate needs
to be payed back. The following formula describes these
yearly repayments:
amount×rate
repayment = [1−(1+rate)−nr_years ]
Rate in that formula is a fraction, but more conveniently we
want to work with percentages.
→ “Professional Programmer’s Guide to Fortran77”, CG Page, Leicester, UK 1995
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.12/14
Fortran 77
Introduction to f77
Example: Compute Repayment cont
C23456789012345678921234567893123456789412345678951234567896123456789712
PROGRAM LOAN
IMPLICIT NONE
INTEGER NYEARS
DOUBLE PRECISION AMOUNT, PCRATE, RATE, REPAY
WRITE(6,*) ’Enter amount, % rate (APR), # years:’
READ(5,*) AMOUNT, PCRATE, NYEARS
RATE = PCRATE/100.0D0
REPAY = ( AMOUNT * RATE ) / ( 1.0D0 - (1.0D0 + RATE)**(-NYEARS) )
WRITE(6,*) ’Annual repayments are:’, REPAY
END
→ “Professional Programmer’s Guide to Fortran77”, CG Page, Leicester, UK 1995
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.13/14
Fortran 77
Introduction to f77
Example: Compute Repayment cont
Two Variations:
Instead of typing in the input data manually on the keyboard
one could use file redirections.
1. Create file ./loan inp and read it in via redirected stndin,
i.e.
./loan < ./loan_inp
2. Modify program ./loan.f so that it reads in from any
non-stndin unit the file ./loan inp, i.e.
./loan
→ “Professional Programmer’s Guide to Fortran77”, CG Page, Leicester, UK 1995
PH4390: Computational Methods in Physics, MTU Fall 2006 – p.14/14