SlideShare ist ein Scribd-Unternehmen logo
1 von 11
Downloaden Sie, um offline zu lesen
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
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
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
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
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
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
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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
Fortran - concise review
Fortran - concise reviewFortran - concise review
Fortran - concise review
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Chapter3
Chapter3Chapter3
Chapter3
 
C Token’s
C Token’sC Token’s
C Token’s
 
Python ppt
Python pptPython ppt
Python ppt
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Orthogonal Matrices
Orthogonal MatricesOrthogonal Matrices
Orthogonal Matrices
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
String functions in C
String functions in CString functions in C
String functions in C
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Data types in C
Data types in CData types in C
Data types in C
 
XII Computer Science- Chapter 1-Function
XII  Computer Science- Chapter 1-FunctionXII  Computer Science- Chapter 1-Function
XII Computer Science- Chapter 1-Function
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 

Ähnlich wie Fortran introduction

C programming session 07
C programming session 07C programming session 07
C programming session 07
AjayBahoriya
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
mrecedu
 

Ähnlich wie Fortran introduction (20)

Introduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John LadoIntroduction to C Language - Version 1.0 by Mark John Lado
Introduction to C Language - Version 1.0 by Mark John Lado
 
Introduction to Python Programming
Introduction to Python ProgrammingIntroduction to Python Programming
Introduction to Python Programming
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3
 
Python
PythonPython
Python
 
Introduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIMLIntroduction to Python Programming | InsideAIML
Introduction to Python Programming | InsideAIML
 
Fortran 95
Fortran 95Fortran 95
Fortran 95
 
intro2fortran.pptx
intro2fortran.pptxintro2fortran.pptx
intro2fortran.pptx
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
Learn C
Learn CLearn C
Learn C
 
C notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-semC notes diploma-ee-3rd-sem
C notes diploma-ee-3rd-sem
 
C Language Unit-1
C Language Unit-1C Language Unit-1
C Language Unit-1
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Welcome to python workshop
Welcome to python workshopWelcome to python workshop
Welcome to python workshop
 
Uni texus austin
Uni texus austinUni texus austin
Uni texus austin
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
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