FORTRAN 90

advertisement
FORTRAN 90
Lecture : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc
Mechanical Engineering Dep.
INTRODUCTION TO FORTRAN 90
1. History
FORTRAN (mathematical FORmula TRANslation system) was originally
developed in 1954 by IBM. Fortran was one of the first languages to allow the
programmer to use higher level (i.e. architecture independent) statements rather
than a particular machine’s assembly language. This resulted in programs being
easier to read, understand and debug and saved the programmer from having to
work with the details of the underlying computer architecture.
In 1958 the second version was released with a number of additions
(subroutines, functions, and common blocks). A number of other companies
then started developing their own versions of compilers (programs which
translate the high level commands to machine code) to deal with the problem of
portability and machine dependency.
In 1962 Fortran IV was released. This attempted to standardize the
language in order to work independent of the computer (as long as the Fortran
IV compiler was available!)
In 1966 the first ANSI (American National Standards Institute) standard
(Fortran 66) was released which defined a solid base for further development of
the language.
In 1978 the second ANSI standard (Fortran 77) was released which
standardized extensions, allowed structured programming, and introduced new
features for the IF construct and the character data type.
The third ANSI standard (Fortran 90) was released in 1991, with a new
revision expected within 10 years. This model will study in this grade.
ϭ
2. Programming basics
To create a program that works, and works efficiently you will need to do
the following before start writing the program.
Make sure that you understand the aim of the program.
Decide on the information that the program will needs as input and that it
should produce as output.
Make sure that you understand how to computations will be done and the
order in which they should be done.
Create a program that will solve the specified problem with the
algorithm(s) and input/output requirements that you have identified.
Test the program thoroughly.
3. The main parts of a Fortran 90 program
 Program name
The first line of the program gives the programs name, e.g.
Program start1
 Initialization section: declaration of variables.
In particular, it is used to declare all the variables that will be used
to store number, etc, within your program such as integer, real and
character.
 Main program body.
The main program body contains all the executable Fortran
statements that will carry out the task you wish to perform. The last
statement must be an end statement.
Ϯ
4. Program layout
1. The source line may contain up to 132 characters (including space).
2. The character includes both upper and lower case letters and the
underscore.
3. Identifier names can consist of between 1 and 31 alphanumeric characters
(including the underscore).
4. The exclamation mark (!) introduces a comment. A comment can start
anywhere on a source line and thus can be placed alongside the relevant
code. The rest of the line after the (!) is ignored by the compiler.
REAL :: length1
! Length1 at start test in mm (room temperature)
REAL :: length2
! Length2 at end test in mm (after cooling)
5. The ampersand character (&) means "continued on the next line"
z = f + (y-0.5)*log(y) - y + 0.91893853320 + &
(((-0.00059523810*z + 0.00079365079)*z - &
0.00277777778)*z + 0.08333333333)/y
WRITE(*,*) ’UNIVERSITY OF BABYLON / COLLEGE &
& OF ENGINEERING’
6. The semicolon is used as a statement separator, allowing multiple
statements to appear on one line.
a = 2; b = 7; c = 3
ϯ
Example:
A simple program in Fortran 90 to divided two integers, and then display
the integers and their divide.
program divide1
integer :: i,j
real :: x
!
! program to demonstrate integer division
!
write (*,*) 'Enter two integers'
read (*,*) i,j
x=i/j
write (*,*) i,"divided by",j,"is",x
stop
end program divide1
5. Arithmetic Expressions
Variables, parameters and numeric constants may be combined using the
following operators:
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
For example:
cost * number
cost * number + postage
10 + 3
4 * pi
ϰ
1 / pressure
pi * radius * radius
The arithmetic expression may also include brackets which should be
used to clarify the required sequence of operations in an expression.
For example:
pi*radius**2
might be interpreted as
(pi*radius)**2
The following table lists the priority or order of execution of the various
operators.
Precedence
Operator
1
()
2
**
3
*/
4
+-
The operators are evaluated in order of ascending precedence, that is,
brackets first, then ** followed by * / and finally + -. Operators of equal
precedence are evaluated working from left to right across the expression.
ϱ
Download