# A tutorial Introduction to Fortran - PowerPoint PPT Presentation

A tutorial Introduction to Fortran

1 / 23
A tutorial Introduction to Fortran

## A tutorial Introduction to Fortran

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. A tutorial Introduction to Fortran Siddhartha Ghosh sghosh@ucar.edu Consulting Services Group

2. 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

3. 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

4. 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

5. 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

6. 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

7. .LT. for < .LE. for <= .GT. for > .GE. for >= .EQ. for = Arithmetic relational operators

8. Typical structure of conditional statements are if ( x .ge. y ) statements If (z .lt. p) then statements else statements endif Conditional Statements

9. 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

10. 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

11. 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

12. 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

13. 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

14. 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

15. 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

16. 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

17. 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

18. 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

19. 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

20. 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

21. 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

22. 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.