Beruflich Dokumente
Kultur Dokumente
Ravi Chella
Chemical Engineering
Outline I
1 INTRODUCTION
Program Structure
Compiling, linking, and running a Fortran code
2 VARIABLES
Data Types
Declaration
Mathematical operators
Intrinsic functions
Relational Logical Operators
Combinational logic operators
3 CONTROL STRUCTURES
Block IF/ELSE
Example 1
DO loops
Iterative DO loops
Ravi Chella (CBE) Modern Fortran (90/95/2003) May 10, 2016 2 / 58
INTRODUCTION Program Structure
Outline II
Exercise 1
FORALL block
4 ARRAYS
Array types and Specifications
Static and Dynamic Arrays
Array operations
Multiplication and Division
Arrays and Intrinsics
Mask Operations
5 PROCEDURES
Internal Procedures
Interface Blocks
Automatic Arrays
INTENT attribute
Keyword Arguments
Ravi Chella (CBE) Modern Fortran (90/95/2003) May 10, 2016 3 / 58
INTRODUCTION Program Structure
Outline III
Optional Arguments
6 MODULES
Usage
Selective Inclusion
Implicit and Explicit Interfaces
SAVE attribute
PUBLIC and PRIVATE attributes
Modules Cascade
7 DERIVED TYPES AND OPERATORS
More Complex Derived Types
Exercise 4
Operators
Overloading
8 Input Output
OPEN Statement
Ravi Chella (CBE) Modern Fortran (90/95/2003) May 10, 2016 4 / 58
INTRODUCTION Program Structure
3 termination: Stop
Statement may start in any column including the first column (0-132
characters)
Statement continuation with an ampersand (&) as last character
Multiple statements can be on one line separated by semicolons (;)
Exclamation point (!) starts a comment (except in literal strings);
Everything following ! on the line is considered a comment
Blanks are not allowed in keywords or variables
Fortran is not case-sensitive: dens and DEns refer to the same
variable
exponentiation is ** not as in MATLAB
By default, array indices start at 1, not 0 as in C-type languages
Recommended Practices
Properly indent code.
Comment code sections and individual statements.
Use reasonably descriptive variable names.
Always initialize variables before using.
1 Follow the style guide for your research group.
Hierarchy of operations:
1 Contents of expressions in parantheses, starting from innermost, to
outermost.
2 exponentials evaluated from right to left,
3 * and / evaluated from left to right,
4 + and - evaluated from left to right.
BLOCK IF
1 I F ( l o g i c a l e x p r 1 ) THEN
Statement 1
3 Statement 2
...
5 ELSE I F ( l o g i c a l e x p r 2 ) ! o p t i o n a l ( can h a v e more t h a n one )
Statement 3
7 ...
ELSE ! optional
9 Statement 4
...
11 END I F
1 PROGRAM r o o t s
! Purpose :
3 ! T h i s program s o l v e s f o r t h e r o o t s o f a q u a d r a t i c e q u a t i o n
! ax 2+ bx+c=0
5 ! Record o f r e v i s i o n s :
! Date Programmer D e s c r i p t i o n o f Change
7 ! ==== ========== =====================
! 04/07/2016 RC O r i g i n a l code
9 IMPLICIT NONE
REAL : : a ! C o e f f i c i e n t o f x 2 term o f e q u a t i o n
11 REAL : : b ! C o e f f i c i e n t o f x term o f e q u a t i o n
REAL : : c ! C o n s t a n t term o f e q u a t i o n
13 REAL : : d ! discriminant
REAL : : r e a l p a r t ! Real p a r t ( f o r complex r o o t s )
15 REAL : : i m a g p a r t ! I m a g i n a r y p a r t ( f o r complex r o o t s )
REAL r1 , r 2 ! Solutions for real roots
17 WRITE ( , ) Q u a d r a t i c e q u a t i o n , a x 2 + b x + c = 0 , r o o t
solver
WRITE ( , ) E n t e r t h e c o e f f i c i e n t s a , b , c ( v a l u e s s e p a r a t e d
by s p a c e s )
19 READ ( , ) a , b , c
WRITE ( , ) The c o e f f i c i e n t s a , b , and c a r e : , a , b , c
Ravi Chella (CBE) Modern Fortran (90/95/2003) May 10, 2016 16 / 58
CONTROL STRUCTURES Example 1
d = b 2 4 . 0 a c ! c a l c u l a t e d i s c r i m i n a n t
2 I F ( d > 0 . 0 ) THEN ! Two r e a l r o o t s
r 1 = ( b + SQRT( d ) ) / ( 2 . 0 a )
4 r 2 = ( b SQRT( d ) ) / ( 2 . 0 a )
WRITE ( , ) Ths e q u a t i o n h a s two r e a l r o o t s : , r1 , r 2
6 ELSE I F ( d < 0 . 0 ) THEN ! c o m p l e x c o n j u g a t e r o o t s
r e a l p a r t = b / ( 2 . 0 a )
8 i m a g p a r t = SQRT( ABS( d ) ) / ( 2 . 0 a )
WRITE ( , ) T h i s e q u a t i o n h a s c o m p l e x c o n j u g a t e r o o t s :
10 WRITE ( , ) r e a l p a r t : , r e a l p a r t , i m a g i n a r y p a r t : ,
imag part
ELSE ! repeated root
12 r 1 = b / ( 2 . 0 a )
WRITE ( , ) Ths e q u a t i o n h a s r e p e a t e d r e a l r o o t s : , r 1
14 END I F
END PROGRAM r o o t s
index starts with the value istart, with its value incremented by incr
each time through the loop, until index > iend.
CYCLE can be used to skip a section of code in the loop.
EXIT can be used to exit the loop
1 OUTER : DO i =1 ,5
INNER : DO j =1 ,7
3 x=r a n d ( )
I F ( x <0) CYCLE INNER ! s k i p t o end o f l o o p
5 I F ( x >1) EXIT OUTER ! e x i t l o o p
END DO INNER
7 END DO OUTER
replaces
1 DO i = 1 , n
DO j =1,m
3 I F ( work ( i , j ) /= 0 . 0 ) THEN
work ( i , j ) = 1 . / work ( i , j )
5 END I F
END DO
7 END DO
Explicit Shape Arrays Explicit shape arrays are defined with a specified
rank, each dimension must have an upper bound specified, and a lower
bound may be specified. Each bound is explicitly defined with a
specification of the form:
[lower-bound:] upper-bound
An array has a maximum of seven dimensions.
The following are valid explicit array declarations:
INTEGER NUM1(1,2,3) !Three dimensions
INTEGER NUM2(-12:6,10:100) !Two dimensions w lower & upper bounds
INTEGER NUM3(M:N,P:Q,L,99) !Array with 4 dimensions
There are two special cases of explicit arrays:
in a procedure, explicit arrays whose bounds are passed in from the calling
program are called automatic-arrays.
As a second type, in a procedure, where an array is a dummy array and the
bounds are passed from the calling program, is called an adjustable-array.
Matrix multiplication
To do matrix multiplication, you can use the intrinsic matmul
c = matmul(a,b) ! a and b have to be be compatible (No.
cols of A=No. rows of B)
For large matrices it may be more efficient to use a BLAS or Intel MKL
library function
Element by element division (a and b have the same size)
c = a/b
Since all array operations happen element wise, you can apply intrinsics to
an array
REAL , DIMENSION ( 1 0 , 1 0 ) : : a , b
2 REAL : : c
1 INTEGER , DIMENSION ( 4 , 3 ) : : a
a ( 1 , : ) =[1 , 4 , 2 ]
3 a ( 2 , : ) =[4 , 6 , 0 ]
a ( 3 , : ) =[1 , 7 , 3 ]
5 a ( 4 , : ) =[6 , 5 , 2 ]
Program units can contain internal procedures, which may NOT, however,
contain further internal procedures. The internal procedures are collected
together at the end of the program unit and are preceded by a CONTAINS
statement.
PROGRAM main
2 IMPLICIT NONE
REAL : : a , b , c , d
4 ...
d=add ( )
6 ...
CONTAINS
8
FUNCTION add ( )
10 IMPLICIT NONE
REAL : : add ! a , b , c , d e f i n e d i n main
12 add=a+b+c
END FUNCTION add
14
END PROGRAM main
15 END SUBROUTINE a r i t h m e t i c
If the intent is IN, the argument value may not be changed within the
subprogram. If the intent is OUT, the argument may only be used to
return information from the procedure to the calling program. If the intent
is INOUT, then the argument may be used to transfer information in both
directions between the procedure and calling program.
1 SUBROUTINE s w a p r e a l ( a , b )
IMPLICIT NONE
3 REAL , INTENT (INOUT) : : a , b
REAL : : temp
5 temp = a
a = b
7 b = temp
END SUBROUTINE s w a p r e a l
1 MODULE msimp
REAL : : a
3 INTEGER : : b
CONTAINS
5 SUBROUTINE sub ( c , d )
INTEGER : : c
7 REAL : : d
d = d a c + b
9 END SUBROUTINE sub
END MODULE msimp
Benefits:
Allows consistency check by the compiler
Assumed-shape arrays, optional parameters, etc.
The values of all the local variables and arrays in a procedure become
undefined whenever the procedure is exited.
The next time that the procedure is invoked, the values of the local
variables may or may not be the same as the last time the procedure was
exited.
The SAVE attribute must be used to guarantee that local variables and
arrays are saved unchanged between calls to a procedure.
1 REAL , SAVE : : a
REAL : : b =1.0 ! any v a r i a b l e t h a t i s i n i t i a l i z e d i s
a u t o m a t i c a l l y saved
3 SAVE : : v a r 1 , v a r 2 ! s a v e v a r 1 and v a r 2
SAVE ! a l l o f t h e l o c a l v a r i a b l e s w i l l be s a v e d
All PUBLIC content can be used from outside of the module, i.e. by
subprograms that use the module.
PRIVATE items are only accessible from within the module.
Keywords PRIVATE or PUBLIC can stand alone, or be an attribute.
The default attribute for all data and procedures in a module is PUBLIC.
Protected variables can be accessed read-only from program units outside
the module and may be modified inside but not outside the module.
If a module contains a PRIVATE statement without a list of private items,
then by default every dtat item and procedure in the module is private.
Any items that should be public must be explicitly listed in a separate
PUBLIC statement. This is the recommended approach to design modules.
5 MODULE bbb
USE MODULE aaa
7 ...
END MODULE bbb
9
MODULE c c c
11 USE MODULE bbb
...
13 END MODULE c c c
Module ccc now has access to all public objects within module bbb and
module aaa
DERIVED TYPES Ofter more complex data types are required. Fortran90
allows this through derived types.
1 TYPE p a r t i c l e
REAL , DIMENSION ( 3 ) : : pos , v e l , a c c
3 REAL : : mass
INTEGER : : n
5 END TYPE p a r t i c l e
7 TYPE( p a r t i c l e ) : : p
9 p%mass = 1 . 0
p%n = 1
11 p%p o s = [ 1 . 0 , 2 . 0 , 4 . 0 ]
p%v e l = 0 . 0
13 p%a c c = 0 . 0
Write a Fortran90 program which uses a derived type to store the details
of box of particles
Length, width and height real
Number of particles integer
Mass of each particle real
Charge of each particle being -1.6*10**-19
This program should take input from the keyboard to setup the box of
particles.
19 END MODULE a
IOSTAT=ios
where ios is an integer variable which is set to zero if the statement is
executed successfully or to an implementation dependent constant
otherwise.
FORM=fm
where fm may be FORMATTED or UNFORMATTED, the default is
FORMATTED for sequential files and UNFORMATTED for direct
access files.
ACCESS=acc
where acc may be SEQUENTIAL or DIRECT
RECL=rl
where rl is the maximum record length (positive integer) for a direct
access file. For formatted files this is the number of characters and for
unformatted it is usually the number of bytes or words (system
dependent). BLANK=bl
where bl is either NULL or ZERO and determines how blanks in a
numeric field are interpreted.
Ravi Chella (CBE) Modern Fortran (90/95/2003) May 10, 2016 57 / 58
Input Output OPEN Statement
POSITION=pos
where pos may be ASIS, REWIND or APPEND which are interpreted
as positioning the file at the position it was previously accessed,
positioning the file at the start; and positioning the file after the previously
end of the file. Defaults to ASIS.
PAD=pad
where pad may be YES or NO. If YES formatted input is padded out
with blank characters; if NO the length of the input record should not be
exceeded.
DELIM=del
where del may be APOSTROPHE or QUOTE or NONE indicating
which character used when delimiting character expressions in list-directed
or NAMELIST output. Defaults to NONE.
ACTION=act
where act may be READ, WRITE or READWRITE specifying the
permitted modes of operation on the file. Default is processor dependent.