Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.

Similar presentations


Presentation on theme: "Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing."— Presentation transcript:

1 Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing

2 What is Fortran? One of the oldest programming languages still in use Fortran77, Fortran90 Used primarily by geophysical sciences: – Good at math and manipulation of large data sets – Consistent with earliest NWP models

3 I program in Fortran…

4 … …

5 Why Learn It? If you are pursuing a graduate degree in AOS, you will eventually run into Fortran: – An assignment – A research problem – Someone else’s code* *THIS is why commenting code is so important (later on that)

6 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE

7 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE The first seven columns of the program code are reserved for special functions – most lines of code begin on the seventh column

8 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE This line identifies the code as a program (instead of, say, a subroutine) and gives it the name MYPROGRAM.

9 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE All variables used in the code have to be declared at the top, before any of the main code is run.

10 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE Here is where the magic happens.

11 Your Typical Program c234567 PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE This identifies the end of the program

12 A More Complicated Program PROGRAM MYPROGRAM Options Declaration of Variables MAIN CODE CALL MYSUBROUTINE MAIN CODE STOP END SUBROUTINE MYSUBROUTINE Options Declaration of Variables MAIN CODE CALL MYSUBROUTINE MAIN CODE RETURN END

13 A More Complicated Program PROGRAM MYPROGRAM Options Declaration of Variables MAIN CODE CALL MYSUBROUTINE MAIN CODE STOP END SUBROUTINE MYSUBROUTINE Options Declaration of Variables MAIN CODE RETURN END Being able to trace the flow of information is by far the most important thing to learn about programming, in Fortran or in any other language. You will eventually have to write code that is not only able to perform complicated functions, but is readable to someone else who wishes to use it.

14 Declaring Variables All variables in the program must be of a specific type: – Numbers – Letters, Words, Phrases, Names, etc. – Logic Variables – 1-D, 2-D, 3-D, etc. blocks of these

15 Numbers Numbers can be: – Integers – This is any integer value (e.g. -5, 0, 1) – “Real”/”Floating-Point”/etc. – This is any number (e.g. 5.6, 2.11245) In general, “integer” variables are good for keeping counts of things, while “real” variables are used for data.

16 Character Strings Any variable that is a set of letters is a string of CHARACTERs. – Name – Messages (UH_OH = ‘Program failed!’) – File names

17 Logical Variables LOGICAL variables are either ‘true’ or ‘false’, and are usually used to determine if some set of criteria has occurred or not. IF (Some variable is greater than 5) THEN its_greater_than_five = TRUE ENDIF

18 Arrays Usually you are going to need a whole block of numbers representing your data set (e.g. a block of numbers representing vorticity at every point in some domain) You can specify an array of any of these variable types

19 Arrays The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions: VALUES(55,90)

20 Arrays The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions: VALUES(4,3) Variable array called VALUES

21 Arrays The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions: VALUES(4,3) Array set with 4 elements in one dimension, And 3 in the other. This array has a total of (4*3) = 12 elements.

22 Arrays The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions: VALUES (1,1) VALUES (1,1) VALUES (2,1) VALUES (2,1) VALUES (3,1) VALUES (3,1) VALUES (4,1) VALUES (4,1) VALUES (1,2) VALUES (1,2) VALUES (1,3) VALUES (1,3) VALUES (1,2) VALUES (1,2) VALUES (2,2) VALUES (2,2) VALUES (3,2) VALUES (3,2) VALUES (2,3) VALUES (2,3) VALUES (3,3) VALUES (3,3) VALUES (4,3) VALUES (4,3) Each element of VALUES is identifiable by its index.

23 Arrays The array has a type (INTEGER, REAL, etc.), a single name, and a set of dimensions: VALUES(3,2) VALUES (1,1) VALUES (1,1) VALUES (2,1) VALUES (2,1) VALUES (3,1) VALUES (3,1) VALUES (4,1) VALUES (4,1) VALUES (1,2) VALUES (1,2) VALUES (1,3) VALUES (1,3) VALUES (1,2) VALUES (1,2) VALUES (2,2) VALUES (2,2) VALUES (3,2) VALUES (3,2) VALUES (2,3) VALUES (2,3) VALUES (3,3) VALUES (3,3) VALUES (4,3) VALUES (4,3) Each element of VALUES is identifiable by its index.

24 Arrays

25 Loops Values in arrays have indices that correspond to their location in the array Loops provide the ability to go through each member of the array individually: For i = 1, 3 For j = 1, 2 VALUES(i,j) End

26 Scientific Computing Scientific computing generally involves dealing with very large sets of data over many dimensions, and mathematically manipulating the data in a desirable way Arrays, loops, logical traps, etc. Poor coding technique will come back to bite you!

27 Comments All coding languages allow you to sneak in messages in the code itself to allow a reader to understand what is going on – these are called “comments” For i = 1, 4 c c I’m looping over the first index of VALUES here. c

28 Comments All coding languages allow you to sneak in messages in the code itself to allow a reader to understand what is going on – these are called “comments” For i = 1, 4 c c I’m looping over the first index of VALUES here. c Code sees this identifier as “comment” and Skips it.

29 In The Next Few Weeks Fortran – Basics Fortran – “Advanced” Matlab 1 Matlab 2 Matlab 3 IDL 1 IDL 2


Download ppt "Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing."

Similar presentations


Ads by Google