250 likes | 437 Views
A tutorial Introduction to Fortran. Siddhartha Ghosh sghosh@ucar.edu 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.
E N D
A tutorial Introduction to Fortran Siddhartha Ghosh sghosh@ucar.edu 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 Contents
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! References
Data types • 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 A simple example: c = a + b In Fortran, these memory locations have an associated type i.e. a predefined way to interpret the sequence of bits
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) Fortran Intrinsic Data types
Common operators +, -, *, / and ** (for power) These can be overloaded or modified to do something different than default Arithmetic Operators • Range 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
.LT. for < .LE. for <= .GT. for > .GE. for >= .EQ. for = Arithmetic relational operators
Typical structure of conditional statements are if ( x .ge. y ) statements If (z .lt. p) then statements else statements endif Conditional Statements
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 Logical operators
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. Loops
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 Arrays
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 Arrays, smart operations
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. Functions and Subroutines
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. Modules
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. User defined data-types
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 Formatted I/O
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. Formatted I/O to files
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. Namelist
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 Binary I/O
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 I/O using netcdf files
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. netcdf
1d Shallow Water Eqn. (A toy problem) • The equations: = 0 = 0 The rigid boundary at both ends. Refer to the appendix of This presentation for detail.