Introduction to FORTRAN 90 University of Liverpool course

  • Slides: 13
Download presentation
Introduction to FORTRAN 90 University of Liverpool course

Introduction to FORTRAN 90 University of Liverpool course

Telling a Computer What To Do To get a computer to perform a specific

Telling a Computer What To Do To get a computer to perform a specific task it must be given a sequence of unambiguous instructions or a program. An everyday example is instructions on how to assemble a bedside cabinet. The instructions must be followed precisely and in the correct order: 1. insert the spigot into hole `A', 2. apply glue along the edge of side panel, 3. press together side and top panels 4. attach toggle pin `B' to gromit `C' 5. . and so on

Programming Languages Programming languages must be: • totally unambiguous (unlike natural languages, for example,

Programming Languages Programming languages must be: • totally unambiguous (unlike natural languages, for example, English), • expressive it must be fairly easy to program common tasks • practical it must be an easy language for the compiler to translate • simple to use. All programming languages have a very precise syntax (or grammar). This ensures all syntactically correct programs have a single meaning.

High level Programming Languages Assembler code is a Low Level Language. Fortran 90, Fortran

High level Programming Languages Assembler code is a Low Level Language. Fortran 90, Fortran 77, ADA, C and Java are High Level Languages. • a program is a series of instructions to the CPU, • could write all programs in assembler code but this is a slow, complex and error prone process • high level languages are more expressive, more secure and quicker to use • the high level program is compiled (translated) into assembler code by a compiler.

An Example Problem To convert from o. F (Fahrenheit) to o. C (Centigrade) we

An Example Problem To convert from o. F (Fahrenheit) to o. C (Centigrade) we can use the following formula: C = 5 (F - 32) / 9 To convert from o. C to K (Kelvin) we add 273. The program would accept a Fahrenheit temperature as input and produce the Centigrade and Kelvin equivalent as output.

An Example Program PROGRAM Temp_Conversion IMPLICIT NONE INTEGER : : Deg_F, Deg_C, K PRINT*,

An Example Program PROGRAM Temp_Conversion IMPLICIT NONE INTEGER : : Deg_F, Deg_C, K PRINT*, ''Please type in the temp in F'' READ*, Deg_F Deg_C = 5*(Deg_F 32)/9 PRINT*, ''This is equal to'', Deg_C, ''C'' K = Deg_C + 273 PRINT*, ''and'', K, ''K'' END PROGRAM Temp_Conversion This program, called Temp. f 90, can be compiled: f 90 Temp. f 90 and run: a. out

Analysis of Program The code is delimited by PROGRAM. . . END PROGRAM statements.

Analysis of Program The code is delimited by PROGRAM. . . END PROGRAM statements. Between these there are two distinct areas. Specification Part • specifies named memory locations (variables) for use • specifies the type of the variable, Execution Part • reads in data • calculates the temp in o C and K and • prints out results.

Specification Part • IMPLICIT NONE this should always be present. Means all variables must

Specification Part • IMPLICIT NONE this should always be present. Means all variables must be declared. • INTEGER : : Deg_F, Deg_C, K declares three INTEGER (whole number) variables. Other variable types: REAL real numbers, e. g. , 3: 1459, 0: 31459, 1017 LOGICAL take values. TRUE. or. FALSE. , CHARACTER contains single alphanumeric character, e. g. , 'a', CHARACTER(LEN=12) contains 12 alphanumeric characters, a string, Fortran 90 is not case sensitive. K is the same as k and INTEGER is the same as integer.

Execution Part This is the part of the program that does the actual `work'.

Execution Part This is the part of the program that does the actual `work'. PRINT*, ''Please type in the temp in F'' writes string to screen, READ*, Deg_F reads a value from the keyboard and assigns it to the INTEGER variable Deg F, Deg_C = 5*(Deg_F 32)/9 the expression on the RHS is evaluated and assigned to the INTEGER variable Deg C * is the multiplication operator, is the subtraction operator, / is the division operator, (takes longer than *) = is the assignment operator. PRINT*, ''This is equal to'', Deg_C, ''C'' displays a string on the screen followed by the value of a variable (Deg_C) followed by a second string (''C''). By default, input is from the keyboard and output to the screen.

How to Write a Computer Program There are 4 main steps: 1. specify the

How to Write a Computer Program There are 4 main steps: 1. specify the problem, 2. analyse and break down into a series of steps towards solution, 3. write the Fortran 90 code, 4. compile and run (i. e. , test the program). It may be necessary to iterate between steps 3 and 4 in order to remove any mistakes. The testing phase is very important.

Compiling and running 1. Create a program using notepad with file extension. f 90

Compiling and running 1. Create a program using notepad with file extension. f 90 – e. g. prog. f 90 2. Compile by typing f 90 prog. f 90 3. Run by typing a. out (f 90 by default creates an executable called a. out) 4. To give the executable a different name, use f 90 –o name prog. f 90. Then type name to run the program.

Free and Fixed format The original Fortran language used a so called fixed format,

Free and Fixed format The original Fortran language used a so called fixed format, where the first 5 columns were used for labels, column 6 for a continuation character and columns 7 72 for code. Such programs can be written with Fortran 90, but must have an extension. f. The default for Fortran 90 is free format, since there is far less need for labels in this language. Free format programs must have extension. f 90 (more about this later)

PROGRAM QES IMPLICIT NONE INTEGER : : a, b, c, D REAL : :

PROGRAM QES IMPLICIT NONE INTEGER : : a, b, c, D REAL : : Real_Part, Imag_Part PRINT*, ''Type in values for a, b and c'' READ*, a, b, c IF (a /= 0) THEN ! Calculate discriminant D = b*b 4*a*c IF (D == 0) THEN ! one root PRINT*, ''Root is '', b/(2. 0*a) ELSE IF (D > 0) THEN ! real roots PRINT*, ''Roots are'', ( b+SQRT(REAL(D)))/(2. 0*a), & ''and'', ( b SQRT(REAL(D)))/(2. 0*a) ELSE ! complex roots Real_Part = b/(2. 0*a) ! D < 0 so must take SQRT of D Imag_Part = (SQRT(REAL( D))/(2. 0*a)) PRINT*, ''1 st Root'', Real_Part, ''+'', Imag_Part, ''i'' PRINT*, ''2 nd Root'', Real_Part, '' '', Imag_Part, ''i'' END IF ELSE !a=0 PRINT*, ''Not a quadratic equation'' END IF END PROGRAM QES