pointer, structure ,union and intro to file handling
Fortran introduction
1. 1/27/2010
Introduction to Fortran
FORTRAN
Formula translation
First high level programming language
Has been in continual use for over half a century in
computationally intensive areas such as climate
modeling, CFD, computational physics and
computational chemistry
FORTRAN 77, Fortran 90, Fortran 95, Fortran 2003,
Fortran 2008
1
2. 1/27/2010
Why learn Fortran?
Fortran is the dominant programming
language used in engineering applications
Thousands of codes are available free of
cost
It is important for engineering graduates to
be able to read and modify Fortran code
Text editor Compiler
Program organization
Program name
Declarations
Statements
end
2
3. 1/27/2010
Program name
Every Fortran program must begin with
a program line, giving the name of the
program.
Examples:
program newtonmethod
program a123
program newton_method
PROGRAM, program, PROgram all are same i.e.
Fortran is case insensitive.
Declarations
The variable types in Fortran are:
• Integer
• Real (single precision real variable)
• Double precision
• Character
• Complex
• Logical
3
4. 1/27/2010
Examples:
integer i, j, k
real temperature, concentration
Or
integer:: i, j, k
real:: temperature, concentration
If we want to initialize the variable in the type
declaration statement
real:: inlet_concentration=1.0
Implicit none
Integer if the name starts with one of the
letters (i-n)
Single precision real variable otherwise
God is real, unless declared as integer
It is highly recommended to use implicit
none statement at the beginning of the
program
program newtonmethod
implicit none
real:: concentration, temperature
..
end program newtonmethod
4
5. 1/27/2010
Parameter
integer, parameter:: n=200
real, parameter:: pi=3.14159
Comments
You can write comments using !
Example:
program newtonmethod
! This program solves the non-linear equations by newton
! method
Fixed form vs Free form
Fortran 77 standard was dominant for many years
until Fortran 90 standard was released
Fortran 77 codes were written in fixed form in punch
card days
column 1 to 5 form the label field
column 6 forms the continuation marker
column 7 to 72 form the statement field
In free form these rules do not apply. Always use
free form.
5
6. 1/27/2010
Basic I/O statements
Print*
Instructs the compiler to print items to the
screen
print*, x
Read*
Reads the user input
print*,Éenter the initial concentrationÉ
read*, C0
Advanced I/O statements
Open
open(unit=2, file= initial.txt , status=old)
open(unit=3,file= final.txt , status=new)
Read
read(2,*) c0
Write
write(3,*) cf
or
write(3,10) cf
10 format(f10.4)
close(2,3)
6
7. 1/27/2010
If…Then….Else constructions
If(error<epsilon) then
print*,ÉConvergence criteria satisfiedÉ
exit
else
print*,ÉConvergence criteria not satisfiedÉ
end if
If there is only one statement
If(error<epsilon) print*,Éconvergence attainedÉ
Do loops
do i=1,n
read(2,*)c0(i)
end do
do
statements
If(condition satisfied) exit
end do
7
8. 1/27/2010
Functions
Intrinsic functions
E.g. abs(x), cos(x), sqrt(x), exp(x)
User defined functions
program annulusarea
implicit none
real r, area, a, b
area(r)=3.142*r*r
print*,Éenter the inner and outer radii of the annulusÉ
read*,a, b
print*,ÉThe area of the annulus isÉ, area(b)-area(a)
end program annulusarea
Function subprogram
function fact(n)
integer fact, n, p
p=1
do i=1,n
p=p*i
end do
fact=p
end program factorial
integer::n=10, fact
print*, fact(n)
end program factorial
8
9. 1/27/2010
Subroutines
Subroutines help to break large program into
small sub programs
Independent testing of subtasks
Reusable code
Isolation from unintended side effects
We get Fortran codes from the internet in
the form of subroutines!. We need to call
subroutine from the main program.
Solving non-linear equations by Newton method
F ( X , P) = 0
J ( X n , P ).∆ X n = − F ( X n , P)
We can write separate subroutines for:
• Input data (data.f)
• Evaluating function values (fval.f)
• Evaluating Jacobian(Jacobian.f)
• Solving linear equations (leqnsolver.f)
Call data(p)
Call fval(X,F)
Call jacobian(X, jac)
Call leqnsolver(f,jac)
9
10. 1/27/2010
Fortran compilers
Windows
Silverfrost f95 (free)
Intel Fortran compiler (commercial)
Linux
G95
Gfortran
References
• Stephen J. Chapman, Fortran 90/95 for
scientists and engineers
• Stephen J. Chapman, Introduction Fortran
90/95 for scientists and engineers
• Fortran compiler manual
• Internet
10
11. 1/27/2010
Fortran codes
Netlib.org (free)
IMSL (commercial)
NAG Fortran library (commercial)
Numerical Recipes in Fortran 90 (Vol 1
and Vol 2), by W. H. Press, S. A.
Teukolsky, et. al ,
11