Beruflich Dokumente
Kultur Dokumente
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/27/2010
Text editor
Compiler
Program organization
Program name Declarations Statements end
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
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
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
1/27/2010
print*, x
Read* Reads the user input
read(2,*) c0
Write
1/27/2010
IfThen.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
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 fact(n) integer fact, n, p p=1 do i=1,n p=p*i end do fact=p end
Function subprogram
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.
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)
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
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