Fortran

Report
FORTRAN
BACKGROUND
 Fo r t r a n o r FO R m u l a T R A N s l a t i o n wa s d e v e l o p e d i n t h e 1 9 5 0 ' s by I B M a s a n a l te r n a t i v e to
Assembly Language. First successfull high level language.
 FO R T R A N 6 6
 First HLL Standard
 FO R T R A N 77
 Character data types
 Improved Do Loops and If statements
 Fo r t r a n 9 0










Free format source code
Modern control structures
Derived Data Types
Powerful array notation
Dynamic Memory allocation
Operator overloading
Keyword argument passing
The INTENT attribute
Control of numeric precision
Support for Modules
 Fo r t r a n 9 5
 minor improvements
 Fo r t r a n 2 0 0 3
 Object oriented Fortran
 Fo r t r a n 2 0 0 8
 Concurrent Programming
 Fo r t r a n 2 01 5
 Minor improvements
Programming Languages Family Tree
DEVELOPMENT ENVIRONMENT
 Yo u w i l l w a n t to b e c o m fo r t a b l e u s i n g a n tex t - e d i to r l i ke v i o r e m a c s – t h a t s u p p o r t s
s y n t a x h i g h l i g h t i n g a n d a u to - i n d e n t a t i o n et c . . . T h e r e i s a n i d e c a l l e d P h o t r a n t h a t I
h av e n ev e r u s e d .
 I p r e f e r e m a c s b u t b o t h v i a n d e m a c s h av e s i m i la r f u n c t io n a l i t y.
 I u s u a l l y r u n e m a c s i n t h e te r m i n a l ( f a s t ! )
 emacs program.f90 –nw
 I m p o r t a n t e m a c s c o m m a n d s to r e m e m b er













ctrl-x
ctrl-x
ctrl-x
ctrl-s
ctrl-x
ctrl-x
ctrl-x
ctrl-x
ctrl-x
ctrl-x
alt-%
ctrl-a
ctrl-e
ctrl-f (find a file)
ctrl-s (save current file)
ctrl-c (close emacs)
(search)
2 (split window horizontally)
3 (split window vertically)
0 (close current window)
o (switch to next window)
k (kill current file)
b (switch to next file
(search and replace)
(start of line)
(end of line)
 A l s o g o o d to r e m e m b e r ' c t r l - z ' to s u s p e n d e m a c s a n d ' f g ' to r e s u m e
CONNECTING TO BLUEHIVE






Open a terminal in mac or linux
Putty or MobaxTerm in windows
ssh <netid>@bluehive.circ.rochester.edu
mkdir fortran_class
cd fortran_class
emacs hello_world.f90
COMPILING
 I will use intel compiler although gfortran compiler will also
work.
 module load intel
 ifort hello_world.f90
 ./a.out
FORTRAN BASICS




/ p u b l i c / j c a r ro l 5 / fo r t r a n / ex 1 . f 9 0
P r o g r am s t r uc t ur e
Comments (! This is a comment)
C o n t i n ua t i on &
lines
 Va r i a b l e D e c l a r a t i o n s
 D a t a Ty p e s




INTEGER
REAL
CHARACTER
LOGICAL
 O p e r a to r s





Arithmetic: +, -, /, *, **
CHARACTER concatenation: //
Relational: >, <, >=, <=, /=, = =
Logical: .AND., .OR., .XOR., .NOT., .EQV., .NEQV.
Use () to group expressions
 Intrinsic numerical functions
 MOD, FLOOR, CEILING, SIGN, MAX, MIN
 Intrinsic math functions
 SQRT, LOG, LOG10, EXP, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN
 Basic I/O
 READ, WRITE
EXERCISE 1
 Exercise 1: Write a program that uses Heron's formula to
calculate the area of a triangle given the length of its three
sides.
a+b+c
A = p(p - a)(p - b)(p - c) where p =
2
FORMAT SPECIFIERS
 /public/jcarrol5/fortran/ex1b.f90
 Format specifiers.







write(*,'(A10,2F12.5,I3,L1)') string1, float1, float2, int1, logical1
I – integer
A – string
F – float
E – exponential
EN – scientific notation
ES – engineering notation
 All format specifiers can be modified with a repeat count and
width. In addition, the precision can be specified for Format
specifiers for real numbers.
 4F10.5 means use a floating point format of width 10 and 5 decimal
points 4 times.
SUBROUTINES AND FUNCTIONS
 /public/jcarrol5/fortran/ex2.f90
 Fortran programs can contain subroutines and functions.
 Functions return a value
 Subroutines do not
 Fortran normally passes variables by reference – so be careful
when modifying function or subroutine arguments. Best to
declare the intent to avoid common mistakes.
 If a constant or an expression is passed as an argument, a
temporary variable is created to store the result of the
expression before being passed to the subroutine or function.
EXERCISE 2
 Modify previous program to use a function to calculate the
area of the triangle.
 Use format specifiers for the output.
CONTROL STRUCTURES
 /public/jcarrol5/ for tran/ex3.f90
 For tran has 3 basic control
structures
 DO loops
 With a counter (an optional
increment)
 DO counter=start, end[, increment]
 ...
 IF blocks
 IF (condition) THEN
 ...
 ELSE IF (other condition) THEN
 ...
 ELSE
 ...
 END IF
 END DO
 With a while statement
 DO WHILE (a < b)
 ...
 END DO
 With an exit statement
 DO
 ...
 IF (condition) EXIT
 END DO
 SELECT CASE statements
 SELECT CASE (var)
 CASE(constant1)
 execution statements
 CASE(constant2)
 execution statements
 CASE DEFAULT
 execution statements
 END SELECT
EXERCISE 3
 Write a Fortran program to calculate the number of uniq
combinations of size r taken from a set of size n – ie 'n
choose r'.
æ n ö
n!
ç
÷=
è r ø r!(n - r)!
 Validate that n and r are both non-negative and that n is greater than
or equal to r.
 Write a factorial function
 Write a combination function that calls the factorial function
 Format the output
RECURSIVE FUNCTIONS AND
SUBROUTINES
 /public/jcarrol5/fortran/ex4.f90
 Often a subroutine or function may want to call itself. This is
called recursion.
 Recursive subroutines and functions require the recursive
keyword.
 Need to have some termination condition
EXERCISE 4
 Modify the factorial function in your combination program to
be recursive.
TOMORROW







Modules
Derived Data Types
Public, Private Entities
Arrays
Namelists
Reading/Writing to files
Pointers

similar documents