160 likes | 300 Views
Introduction to FORTRAN-90. University of Liverpool course. 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.
E N D
Introduction to FORTRAN-90 University of Liverpool course
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, 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 syntacticallycorrect programs have a single meaning.
Highlevel Programming Languages • Assembler code is a LowLevel 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 errorprone process • highlevel languages are more expressive, more secure and quicker to use • the highlevel program is compiled (translated) into assembler code by a compiler.
An Example Problem To convert from oF (Fahrenheit) to oC (Centigrade) we can use the following formula: C = 5 (F - 32) / 9 To convert from oC 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*, ''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.f90, can be compiled: f90 Temp.f90 and run: a.out
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 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'. 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_F32)/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 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 • Create a program using notepad with file extension .f90 – e.g. prog.f90 • Compile by typing f90 prog.f90 • Run by typing a.out (f90 by default creates an executable called a.out) • To give the executable a different name, use f90 –o name prog.f90. Then type name to run the program.
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 .f90 (more about this later)
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'', (bSQRT(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*, ''1st Root'', Real_Part, ''+'', Imag_Part, ''i'' PRINT*, ''2nd Root'', Real_Part, '''', Imag_Part, ''i'' END IF ELSE ! a = 0 PRINT*, ''Not a quadratic equation'' END IF END PROGRAM QES