Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Fortran Programming. FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction.

Similar presentations


Presentation on theme: "Introduction to Fortran Programming. FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction."— Presentation transcript:

1 Introduction to Fortran Programming

2 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Home Introduction Program Structure Getting Started Fortran Statements Declarations Opening FilesReading Data The IF StatementDO Loops Output CompilatingRun Program HOME Quiz

3 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Learning Objectives The goal of this Fortran learning module is to give a quick introduction to the most common features of the Fortran 77 programming language This includes  Declarations Declarations  Open external files Open external files  Read input data Read input data  Loops Loops  Format statements Format statements  Compilating Compilating  Run program Run program

4 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Introduction Fortran is a general purpose programming language, mainly intended for mathematical computations in e.g. engineering. Fortran is an acronym for FORmula TRANslation, and was originally capitalized as FORTRAN. Fortran was the first ever high-level programming languages. The work on Fortran started in the 1950's at IBM and there have been many versions since. By convention, a Fortran version is denoted by the last two digits of the year the standard was proposed. We will be using Fortran 77. Why Fortran?

5 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Why Fortran? You may think that using a programming language developed in the early fifties is a waste of time considering how fast things evolve in the computing business. But: Fortran is the dominant programming language used in engineering applications. It is therefore important for engineering graduates to be able to read and modify Fortran code. From time to time, so-called experts predict that Fortran will rapidly fade in popularity and soon become extinct. These predictions have always failed. Fortran is the most enduring computer programming language in history.

6 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Getting started Log on a UNIX server, in our case Petra In the terminal window, start emacs followed by the name you wish to give your program. Fortran files are always denoted by.femacs

7 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Emacs Emacs is a text editor analogous to Notepad in Windows. Emacs is much more advanced than Notepad, and is widely used as a HTML-editor We will be using emacs for our Fortran programs Emacs startup Window

8 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Structure of a Program This is the structure of a main program: program name declarations statements stop end In the name field, it is customary to use the same name as the Fortranfile has A program must always end with end Here’s a Fortran Template that contains the most elementary Arithmetic Operators in Fortran

9 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Arithmetic Operators The arithmetic operators are: + addition -Subtraction /division *Multiplication **exponentiation The priority rule is: ** has highest priority followed by / and * followed by + and - Within any one priority level, evaluation is carried out from left to right In general, ANY EXPRESSION ENCLOSED IN PARENTHESES IS EVALUATED FIRST The use of paranthesis is highly recommended!!

10 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Fortran Statements  Fortran77 programs are typed in lines of up to 72 characters, with the first six columns of each line reserved for special purposes.special purposes ALWAYS BEGIN AT, OR AFTER, COLUMN 7. As a result, Fortran77 statements ALWAYS BEGIN AT, OR AFTER, COLUMN 7.  In emacs, the TAB key will bring you to column 7 firstcolumn  If the first column contains ”c” or a ”*”, the the entire line would be treated as a comment.  A line can only contain 72 characters, but you will often see yourself in need of more space 6thcolumn  A ”*” in the 6th column specifies that this line is a continuation of the previous Column Position Rules

11 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Column Position Rules Fortran 77 is not a free-format language, but has a very strict set of rules for how the source code should be formatted. The most important rules are the column position rules: Col. 1 :Blank, or a "c" or “ * " for comments Col. 2-5 :Statement label (optional) Col. 6 : Continuation of previous line (optional) Col. 7-72 : Statements Col. 73-80: Sequence number (optional, rarely used today)

12 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Declarations A Fortran program always starts with declaring which variables you will be using. A variable consists of 1-6 characters chosen from the letters a-z and the digits 0-9. List of Fortran data types  integer list of variables integer  real list of variables real  character list of variables character  logical list of variables logical  complexlist of variables complex See an example See an example

13 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Declarations Sample Back

14 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Integer An Integer data type is an exact number Often used as a numerator in loops Example:  INTEGER MONTHS  Declares the variable MONTHS as an Integer  In this example we wish to use the variable MONTHS as an exact number ex. 1, 5 or 8 months, not 1.5 or 6.4 months Back

15 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Real The real data type stores numbers using a floating-point representation Handles numbers with a fractional part as well as whole numbers Example:  REAL SALARY, OVTIME  Declares the variables SALARY and OVTIME as floating-point numbers. These may be of the form:  1.75E+5, 12345.678, 2000.0 If you need double precision, you should declare the variable as REAL*8 (REAL is by default REAL*4) This is seldom required, but some numerical calculations need very high precision Back

16 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Character The character data type stores a character or a textstring If you wish to have input from the user of a program f.ex. if he wishes to continue or not, you may use the data type character Example:  CHARACTER ANSWER, INPUT*5  Declares the variables ANSWER and INPUT. ANSWER may contain one or many characters, while INPUT has a maximum of five characters If both variables declared as CHARACTER should have a maximum of five letters, you simply write:  CHARACTER*5 ANSWER, INPUT Back

17 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Logical The logical data type is mainly used in conjunction with IF statements which select a course of action according to whether some condition is true or false. A logical variable (or array element) may be used to store such a condition value for future use. Logical variables and arrays are also useful when dealing with two-valued data such as whether a person is male or female, a file open or closed, power on or off, etc. Back

18 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Complex The complex data type stores two real values as a single entity Complex numbers arise naturally when extracting the roots of negative numbers and are used in many branches of mathematics, physics, and engineering. A complex number is often represented as (A + iB), where A and B are the real and imaginary parts respectively, and i 2 =-1. Electrical engineers, having used the letter i to represent current, use the notation (A + jB) instead. Back

19 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Opening External Files Very often you need a large amount of input data and you wish to use your output data to make a chart or a graph etc It is then very useful to have external output and input files. Syntax used can be viewed It is customary to open the output file together with the input file, even though it may not be written to at once Example Here

20 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Open File Syntax OPEN(UNIT=,FILE=” ",STATUS=”SPECIFY") UNIT assigns a unique number to this file which will have to be used everytime this file is referred to. FILE is a characterstring denoting the file. STATUS is by default ”NEW”, ”OLD” or ”UNKNOWN”. For an output file, STATUS should be ”NEW” or ”UNKNOWN” (because it has notbeen created yet), whereas an input file should read either ”OLD” or ”UNKNOWN” (because this file must be created before you run the program). Example

21 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Open File Sample Input file (existing file) has status ”OLD” Output file (non-existing file) has status ”UNKNOWN” (can also have status ”NEW”)

22 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Reading Data Two ways of reading input data: 1)From file 2)From screen 1) Reading input data from file:  First, input file must be open (see opening external files ) opening external files Then, the command is: READ(, ) list of variables Unit number is the same one as was assigned to the file when opening, format is discussed here, but it is common to use here *, which means free format. After the bracket, the variables listed in the input file should be listed, in the proper order. Read Data from Screen Example

23 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Read Data from Screen 2) Read data from screen In order to read data from screen, you must first make the program ask for input data. Such a command may be: PRINT *, ’ ’ The PRINT command prints to screen, * is still free format followed by a statement When this has been done, the program must read the input READ *, The READ command allocates the input data to the pre-declared variable Confused? This example should clear things upexample Example

24 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Example Reads from input file (INN.DAT) Prints ”type your first name:” to screen Reads whatever the user decided to call himself, f.ex. Perry See an example of how the input file INN.DAT of this program may look like INN.DAT

25 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Sample INN.DAT The READ sentence in the program was:  READ(10,*)I, N, YEARS, X, Y, THETA, EPS I, N, and YEARS were declared as integers andintegers are therefore exact numbers. The remaining variables were declared as realreal, and are represented as floating-point numbers. Notice that the order of the variables in the read command must be the same as it is in the input file I N YEARS X Y THETA EPS

26 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz The IF Statement An important part of any programming language are the conditional statements. The most common such statement in Fortran is the if statement, which actually has several forms. The simplest one is the logical if statement:  IF (logical expression) executable statement It says that if something is true, do something If you wish to include several statements, the general form is: IF (logical expression) THEN statements ELSEIF (logical expression) THEN statements : ELSE statements ENDIF What is a logical expression? Click to know more See an example on the use of the if statement

27 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Logical Expressions Fortran Statement Meaning Analogue Symbol.GT.Greater Than>.LT.Less Than<.GE.Greater or Equal .LE.Less or Equal .EQ.Equal=.NE.Not Equal 

28 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz IF Statement Sample PRINT *,’TYPE IN THE TIME, IN MILITARY TIME E.G. 1000:’ READ *, TIME IF (TIME.LT. 1130) THEN PRINT *,’NOT LUNCH YET’ ELSE IF (TIME.EQ. 1130) THEN PRINT*,’GO AND HAVE LUNCH’ ELSE IF (TIME.GT. 1200) THEN PRINT*,’SORRY, YOUR LUNCH HOUR HAS PASSED’ ENDIF This is a simple example that says if it’s not 11.30 it is not lunch, if it is 11.30 you can have lunch and if it’s more than 12.00 it is too late This is not a complete program, declarations have not been made

29 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz DO Loops The repetition of a number of statements a predetermined number of times is so important that Fortran contains a special construction that allows this to be done. In general, a ”DO loop" may contain any Fortran statements, including another do statement, known as a "nested DO loops". The syntax is: DO 100 INDEX= int1,int2[,int3] 'statements' 100 CONTINUE The number 100 is a statement label (More....)(More....) INDEX is a variable, and starts at int1 in steps of int3 and ends at int2 For example, DO 100 MONTHS=1,N,6 would go through all N months in steps of six. However, Steps are normally not included as you most of the time wish to run the loop for every step in the interval DO loop Example

30 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz The Label Statement Typically, there will be many loops and other statements in a single program that require a statement label. The programmer is responsible for assigning a unique number to each label in each program (or subprogram). Recall that column positions 2-5 are reserved for statement labels. The numerical value of statement labels have no significance, so any integer numbers can be used. Typically, most programmers increment labels by 10 at a time.

31 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Sample DO Loop Your task is to compute the sum of the series:  The DO loop should then look something like this: SUM=0 K=0 DO 100 K=1,N SUM=SUM+(1/SQRT(K)) 100 CONTINUE where N and K have been assigned to a value earlier in the program

32 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Formatted Output When the program has calculated whatever it was supposed to calculate, it would be of no use if you dont get the results in a readable and understandable manner, and preferably to an external file so you can use your calculations in a graphical presentation etc For this we will use the WRITE and FORMAT statements Syntax : WRITE(*, label) list-of-variables label FORMAT( format-code) Explanations and Example

33 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz WRITE(*, label) list-of-variables label FORMAT( format-code) Remember the statement label?label The wildcard * writes the result to screen, whereas a unit number would write to an external file assigned to this number. A wide variety of format combinations exists. Formatted Output The Most Common

34 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Format Codes A - text string D - double precision numbers, exponent notation E - real numbers, exponent notation F - real numbers, fixed point format I - integer X - horizontal skip (space) / - vertical skip (newline) The format code F (and similarly D, E ) has the general form Fw.d where w is an integer constant denoting the field width and d is an integer constant denoting the number of significant digits For integers only the field width is specified, so the syntax is Iw. Similarly, character strings can be specified as Aw but the field width is often dropped. Examples

35 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Format Examples The format code F11.3 would make 1 million look like: 1000000.000 (a total of 11 spaces where 3 has been assigned to the decimal part. Notice that period, pluss and minus will take up one space each) For large numbers it is more fit to use the exponent notation: E7.2 would produce 1.00E+6 out of 1 million Example: WRITE(*,100)P,T,Z 100FORMAT(2F8.4, F7.6) should look something like this: PTZ 303.4058451.62510.98654

36 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Compilating When your done writing your program, it’s time to compile your program. What is a compilator? We will use the following compilator xlf -o prog fort.f Simply write this in the UNIX terminal window. Fort.f is the name of the fortran file, if your file is called simple.f, you should write xlf –o prog simple.f Hopefully nothing will happen, but most likely a list of errors is going to show Errors in the Compilation Running your Program

37 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz What is a Compilator The compiler is an independent program which translates the entire source code into machine code at once The machine code is usually saved on a file, often called an executable image, which can then be run whenever it is needed This compilates and links the fortranprogram fort.f and creates the executable module prog

38 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Errors No programmer gets everything right the first time The compiler will, when it detects an error, let you know in which line the program the error occured The most frequent errors are the simplest ones; the programmer forgot a paranthesis, a comma, used too many columns (remember that you are only allowed to use 72 coulmns) or just wrote the same word in two different ways Debug your errors and compilate over again If you’re only getting error messages, don’t do this (push picture to view)

39 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Run Program When the compilation is complete it is time to run your program Type ”prog” in your terminal window, and the program should run If you made your program write the results to an output file, you may open the file and view the results

40 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz This section includes a quiz on the topics covered by this module. The quiz is meant as a control to see if you have learned some of the most important features Hit object to start quiz (Depending on your connection, this may take a few seconds...) Quiz

41 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz General information Title:Introduction to Fortran Teacher(s):Professor Jon Kleppe Assistant(s):Per Jørgen Dahl Svendsen Abstract:Provide a good background for solving problems within petroleum related topics using numerical methods 4 keywords:Fortran, loops, input/output, statments Topic discipline:Petroleum Engineering Level:1 Prerequisites:None Learning goals:Introduce the user to Fortran Programming Size in megabytes:0.9 MB Software requirements:MS Power Point 2002 or later, Flash Player 6.0 Estimated time to complete: Copyright information:The author has copyright to the module and use of the content must be in agreement with the responsible author or in agreement with http://www.learningjournals.net. About the author

42 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz FAQ For Questions, try these Computer encyclopedias:  Dataleksikon Dataleksikon  Webopedia Webopedia

43 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz References Fortran tutorial Professional Programmer's Guide to Fortran77 Introduction to Fortran Programming

44 FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction Learning Objectives Getting started Program Structure Fortran Statements Declarations Opening files Quiz Summary Subsequent to this module you should...  Have a basic understanding of how fortran is built up and how a FORTRAN program is constructed


Download ppt "Introduction to Fortran Programming. FAQReferencesSummaryInfo Reading data The IF Statement Do Loops Formatted output Compilating Run Program Introduction."

Similar presentations


Ads by Google