a tutorial introduction to fortran

23
cientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh [email protected] Consulting Services Group

Upload: oro

Post on 14-Jan-2016

99 views

Category:

Documents


3 download

DESCRIPTION

A tutorial Introduction to Fortran. Siddhartha Ghosh [email protected] Consulting Services Group. Basic datatypes Operators for arithmetic and logical variables Condition statements Loops Formatted and Unformatted I/O The netcdf interface Brief description of a toy Model and output. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: A tutorial Introduction  to  Fortran

Scientific Computing Division

A tutorial Introduction to

Fortran

Siddhartha Ghosh

[email protected]

Consulting Services Group

Page 2: A tutorial Introduction  to  Fortran

Scientific Computing Division

Contents

• Basic datatypes

• Operators for arithmetic and logical variables

• Condition statements

• Loops

• Formatted and Unformatted I/O

• The netcdf interface

• Brief description of a toy Model and output

Page 3: A tutorial Introduction  to  Fortran

Scientific Computing Division

References

• On-line book: Metcalf and Reid http://library.books24x7.com/book/id_2142/toc.asp

• Fortran90 Handbook – Jeanne Adams et. al. • Online man pages and documentation• G95 project page: http//www.g95.org• Fortran news group

http://news-reader.org/comp.lang.fortran• Numerous news groups, tutorials etc from Google!

Page 4: A tutorial Introduction  to  Fortran

Scientific Computing Division

Any computer program in its most basic form:•Loads data from memory•does some operations using those data and•stores data again in same or different locations

In Fortran, these memory locations havean associated type i.e. a predefined way to interpret the sequence of bits

A simple example: c = a + b

Data types

Page 5: A tutorial Introduction  to  Fortran

Scientific Computing Division

Fortran Intrinsic Data types

• integer (by default 4-bytes or 32 bits)

• real (by default 4-bytes or 32 bits)

• complex (two reals, by default each 4-bytes)

• logical (by default 4-byte)

• character etc. (by default 1-byte)

Page 6: A tutorial Introduction  to  Fortran

Scientific Computing Division

Arithmetic Operators

• Common operators +, -, *, / and ** (for power)

• These can be overloaded or modified to do something different than defaultRange and precision of basic datatypes

•integer : range +/- 2^31-1•real : range +/- 10^38 with 7 digits•real(8) : range +/- 10^308 with 15 digits

Page 7: A tutorial Introduction  to  Fortran

Scientific Computing Division

Arithmetic relational operators

• .LT. for <

• .LE. for <=

• .GT. for >

• .GE. for >=

• .EQ. for =

Page 8: A tutorial Introduction  to  Fortran

Scientific Computing Division

Conditional Statements

Typical structure of conditional statements are

• if ( x .ge. y ) statements

• If (z .lt. p) then

statements

• else

statements

• endif

Page 9: A tutorial Introduction  to  Fortran

Scientific Computing Division

Logical operators

• Logical locations can have values .TRUE. And .FALSE. only

• The operations are AND, OR, NOT, EQV and NEQV

• Compound conditions may be created using logical operators

Page 10: A tutorial Introduction  to  Fortran

Scientific Computing Division

Loops

• Most common is do loop where you have a counter to track the count

• Loop till some condition is met i.e. while loop

• There are other loops e.g. forall which we will not cover here.

Page 11: A tutorial Introduction  to  Fortran

Scientific Computing Division

Arrays

• Array is one, two, three .. till seven dimensional rectangular distribution of data

• defined (or declared) as a(100) e.g. or

• a(100,10,93) a three dimensional array

• The elements are accessed through indices

• Fortran supports very rich array semantics which we will not get into the detail here

Page 12: A tutorial Introduction  to  Fortran

Scientific Computing Division

Arrays, smart operations

Fortran90/95 allows smart operations over

arrays:

• A + B will sum-up corresponding array

elements, sizes of A and B will have to be equal

• Note that this is element by element operations, So often you may not get what you would expect

Page 13: A tutorial Introduction  to  Fortran

Scientific Computing Division

Functions and Subroutines

• Function is a block of statements which executes and return a value against a call.

• Subroutine is exactly the same entity except it does not return a value.

Note: In Fortran in function or subroutine call statement the address locations of arguments are passes, so any modifications of argument variables are available to caller after it returns.

Page 14: A tutorial Introduction  to  Fortran

Scientific Computing Division

Modules

• The data-structure that are defined within function or subroutine and are not passed through arguments

are called automatic variables. These are created during each invocation i.e. it doesn’t remember values of previous invocation.

• Modules allow sharing of data between different

subprogram unit. Also,• It allows protection of data, exposing only that is

needed.

Page 15: A tutorial Introduction  to  Fortran

Scientific Computing Division

User defined data-types

• We have seen intrinsic data types and collection of those in terms of array

• It may often be convenient to group few data items together

• The code composite.f90 illustrates such structure.

Page 16: A tutorial Introduction  to  Fortran

Scientific Computing Division

Formatted I/O

• We have already used free-formatted write statements.

• Usually it is convenient to read in free-format i.e. system defined format

• The syntax for output is:

write(unit-no,format-st)variables

Page 17: A tutorial Introduction  to  Fortran

Scientific Computing Division

Formatted I/O to files

• Files are associated with unit numbers through a call to open

• The unit numbers are used in read and write statement to input or output data into files.

• Often input from a file may be directed to stdin using < sign in Unix.

• Similarly > filename may be used to dump stdout to a file.

Page 18: A tutorial Introduction  to  Fortran

Scientific Computing Division

Namelist

• Namelists are for grouping I/O operations

• Most often parameter values as well as the name of the startup and output files are read using namelist

• Often parametric output are dumped to namelist files for subsequent runs.

Page 19: A tutorial Introduction  to  Fortran

Scientific Computing Division

Binary I/O

• Most often model statefile containing all the values of physical variables in 3-d grid are rather large

• It is also required to save the intermediate states for further analysis as well as continuing the integration

• The above requirement is conveniently met by binary I/O

Page 20: A tutorial Introduction  to  Fortran

Scientific Computing Division

I/O using netcdf files

• This is not part of Fortran but in NCAR this exposure would be useful

• The binary I/O though very efficient has several shortcomings e.g. it is somewhat platform dependent, it does not store sufficient header or metadata information for enquiring the content etc.

• All these shortcomings are addressed in netcdf and also they added few more goodies

Page 21: A tutorial Introduction  to  Fortran

Scientific Computing Division

netcdf• You may enquire structure of a netcdf file using

ncdump• You will notice in the example program the typical

sequence of writing a netcdf file is: (a) define a netcdf dataset (b) define dimensions needed for variables (c) define variables and finally (d) write the variables.• While reading you may enquire and find all the

dimension (or metadata) information unlike in case of binary I/O.

• Also notice the filesizes which is closer to corresponding binary file sizes.

Page 22: A tutorial Introduction  to  Fortran

Scientific Computing Division

1d Shallow Water Eqn. (A toy problem)• The equations:

= 0

= 0

The rigid boundary at both ends. Refer to the appendix ofThis presentation for detail.

Page 23: A tutorial Introduction  to  Fortran

Scientific Computing Division