Fortran
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Fortran PowerPoint PPT Presentation


  • 129 Views
  • Uploaded on
  • Presentation posted in: General

Fortran. Background. Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language. FORTRAN 66 First HLL Standard FORTRAN 77 Character data types Improved Do Loops and If statements Fortran 90

Download Presentation

Fortran

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Fortran

Fortran


Background

Background

  • Fortran or FORmulaTRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.

  • FORTRAN 66

    • First HLL Standard

  • FORTRAN 77

    • Character data types

    • Improved Do Loops and If statements

  • Fortran 90

    • Free format source code

    • Modern control structures

    • Derived Data Types

    • Powerful array notation

    • Dynamic Memory allocation

    • Operator overloading

    • Keyword argument passing

    • The INTENT attribute

    • Control of numeric precision

    • Support for Modules

  • Fortran 95

    • minor improvements

  • Fortran 2003

    • Object oriented Fortran

  • Fortran 2008

    • Concurrent Programming

  • Fortran 2015

    • Minor improvements

Programming Languages Family Tree


Development environment

Development Environment

  • You will want to be comfortable using an text-editor like vi or emacs– that supports syntax highlighting and auto-indentation etc... There is an ide called Photran that I have never used.

  • I prefer emacs but both vi and emacs have similar functionality.

  • I usually run emacs in the terminal (fast!)

    • emacs program.f90 –nw

  • Important emacs commands to remember

    • ctrl-x ctrl-f (find a file)

    • ctrl-x ctrl-s (save current file)

    • ctrl-x ctrl-c (close emacs)

    • ctrl-s (search)

    • ctrl-x 2 (split window horizontally)

    • ctrl-x 3 (split window vertically)

    • ctrl-x 0 (close current window)

    • ctrl-x o (switch to next window)

    • ctrl-x k (kill current file)

    • ctrl-x b (switch to next file

    • alt-% (search and replace)

    • ctrl-a (start of line)

    • ctrl-e (end of line)

  • Also good to remember 'ctrl-z' to suspend emacs and 'fg' to resume


Connecting to bluehive

Connecting to Bluehive

  • Open a terminal in mac or linux

  • Putty or MobaxTerm in windows

  • ssh <netid>@bluehive.circ.rochester.edu

  • mkdirfortran_class

  • cd fortran_class

  • emacs hello_world.f90


Compiling

Compiling

  • I will use intel compiler although gfortran compiler will also work.

    • module load intel

    • ifort hello_world.f90

    • ./a.out


Fortran basics

Fortran Basics

  • /public/jcarrol5/fortran/ex1.f90

  • Program structure

  • Comments (! This is a comment)

  • Continuation &

    lines

  • Variable Declarations

  • Data Types

    • INTEGER

    • REAL

    • CHARACTER

    • LOGICAL

  • Operators

    • Arithmetic: +, -, /, *, **

    • CHARACTER concatenation: //

    • Relational: >, <, >=, <=, /=, ==

    • Logical: .AND., .OR., .XOR., .NOT., .EQV., .NEQV.

    • Use () to group expressions

  • Intrinsic numerical functions

    • MOD, FLOOR, CEILING, SIGN, MAX, MIN

  • Intrinsic math functions

    • SQRT, LOG, LOG10, EXP, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN

  • Basic I/O

    • READ, WRITE


Exercise 1

Exercise 1

  • Exercise 1: Write a program that uses Heron's formula to calculate the area of a triangle given the length of its three sides.


Format specifiers

Format Specifiers

  • /public/jcarrol5/fortran/ex1b.f90

  • Format specifiers.

    • write(*,'(A10,2F12.5,I3,L1)') string1, float1, float2, int1, logical1

    • I – integer

    • A – string

    • F – float

    • E – exponential

    • EN – scientific notation

    • ES – engineering notation

  • All format specifiers can be modified with a repeat count and width. In addition, the precision can be specified for Format specifiers for real numbers.

    • 4F10.5 means use a floating point format of width 10 and 5 decimal points 4 times.


Subroutines and functions

Subroutines and Functions

  • /public/jcarrol5/fortran/ex2.f90

  • Fortran programs can contain subroutines and functions.

    • Functions return a value

    • Subroutines do not

  • Fortran normally passes variables by reference – so be careful when modifying function or subroutine arguments. Best to declare the intent to avoid common mistakes.

  • If a constant or an expression is passed as an argument, a temporary variable is created to store the result of the expression before being passed to the subroutine or function.


Exercise 2

Exercise 2

  • Modify previous program to use a function to calculate the area of the triangle.

  • Use format specifiers for the output.


Control structures

Control Structures

  • /public/jcarrol5/fortran/ex3.f90

  • Fortran has 3 basic control structures

    • DO loops

      • With a counter (an optional increment)

        • DO counter=start, end[, increment]

          • ...

        • END DO

      • With a while statement

        • DO WHILE (a < b)

          • ...

        • END DO

      • With an exit statement

        • DO

          • ...

          • IF (condition) EXIT

        • END DO

    • IF blocks

      • IF (condition) THEN

        • ...

      • ELSE IF (other condition) THEN

        • ...

      • ELSE

        • ...

      • END IF

    • SELECT CASE statements

      • SELECT CASE (var)

      • CASE(constant1)

        • execution statements

      • CASE(constant2)

        • execution statements

      • CASE DEFAULT

        • execution statements

      • END SELECT


Exercise 3

Exercise 3

  • Write a Fortran program to calculate the number of uniq combinations of size r taken from a set of size n – ie 'n choose r'.

    • Validate that n and r are both non-negative and that n is greater than or equal to r.

    • Write a factorial function

    • Write a combination function that calls the factorial function

    • Format the output


Recursive functions and subroutines

Recursive Functions and Subroutines

  • /public/jcarrol5/fortran/ex4.f90

  • Often a subroutine or function may want to call itself. This is called recursion.

  • Recursive subroutines and functions require the recursive keyword.

  • Need to have some termination condition


Exercise 4

Exercise 4

  • Modify the factorial function in your combination program to be recursive.


Tomorrow

Tomorrow

  • Modules

  • Derived Data Types

  • Public, Private Entities

  • Arrays

  • Namelists

  • Reading/Writing to files

  • Pointers


  • Login