Fundamentals of Fortran 90

Download Fundamentals of Fortran 90

Post on 06-Feb-2016

29 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Fundamentals of Fortran 90. Basic Building Blocks. Identifiers. An identifier is a name that a programmer creates for items within a program including: variables, constants, and subprograms. Rules for Identifiers. The first character must be a letter. - PowerPoint PPT Presentation

TRANSCRIPT

  • Fundamentals of Fortran 90Basic Building Blocks

  • IdentifiersAn identifier is a name that a programmer creates for items within a program including: variables, constants, and subprograms.

  • Rules for IdentifiersThe first character must be a letter.The remaining characters may be letters, digits, or the underscore.There may be no more than 31 characters in an identifier.

  • Guidelines for IdentifiersCapitalize the first letterIf the identifier is composed of two words which have been concatenated (run together)Capitalize the first letter of each wordOptionally, place an underscore between the wordsSelect identifiers that have some significance to the problem being solved

  • IdentifiersValid: TaxRate Last_Page RadiusNot Valid Time-secs 3rdYear

  • VariablesEach variable can store a single valueSince computers use several different internal representations for various types of data, a specific data type must be associated with each variableThe value can be changed while the program is running, but the data type cannot be changedA variable is a memory location in the primary memory of a computer.

  • Data TypesFortran 90 supports five basic data typesFortran 90 allows a programmer to define additionaldata types to fit a specific application. INTEGER REAL CHARACTER LOGICAL COMPLEX

  • DeclarationsA declaration reserves variables (memory locations) for each of the identifiers and associates a data type with each variableA declaration DOES NOT provide an initial value for any variable

  • Examples of DeclarationsREAL :: A, BINTEGER :: KINTEGER :: NLOGICAL :: IsThereCHARACTER (6) :: W, T, H*17 W, T and H are character variables W and T will hold exactly 6 characters H will hold exactly 17 characters

  • Default TypesFortran does not require that variables be declaredIf a variable is referenced without being declared, Fortran will give that variable a data type based on the first letter of its identifierI through N ==> INTEGERotherwise ==> REAL

  • The IMPLICIT StatementThe IMPLICIT statement can be used to override Fortrans rules for assigning data types to undeclared variablesGood programming style is to declare all variables in a programIf the statement IMPLICIT NONE precedes the first declaration, the Fortran 90 compiler will ensure that every variable which is used in the program has been explicitly declared

  • Writing an INTEGER ValueString of digitsNo commasOptional + or - signExamples: 21856 -89 0

  • Integers: Valid & InvalidValid234+236-2250Not Valid5---517.014,555

  • Writing a REAL Value (1) Fixed Point Form ( xxx.xx )String of digits with decimal pointNo commasOptional + or - signValid Real Values: -54.873 0.0 6.5 18.0 0.56NOT Valid Reals:12,345.67 12

  • Writing a REAL Value (2) Floating Point Form Analogous to scientific notationBasic form: fEnFixed point real ( f ) Mantissa letter Einteger exponent ( n )Interpretation f*10nExamples: 6.57E-3 for 0.00657 -45.6E4 for -456000.0 0.7206E+3 for 720.6

  • Writing a Character ValueTwo formsstringstringEither form can be usedExamplesThis is easyWhat is your name?Thats all folks

  • Declaring a ConstantThis variant on the basic declaration is used to give a symbolic name to a constantThe use of declared constants is considered to be a better programming practice than writing the value of the constant in the code itself unless the constant is an integral part of a formula and will never change value or precision

  • Examples Using PARAMETERINTEGER, PARAMETER :: NumStates = 50

    REAL, PARAMETER :: PI = 3.14159

    CHARACTER (*), PARAMETER :: Prompt = next>

  • Assignment Statement SyntaxThe value represented by the expression on the right hand side of the equal sign is stored in the target variable, replacing its previous value

  • Assignment Statement UsesStore a specific value in a variable N = 32 ; X = -3.5 ; W = catCopy a value from one variable to another T = N Store a computed value in a variable X = 3.5 + W

  • Numeric Conversions on AssignmentINTEGER :: K, N, MREAL :: W, C, Y

    N = 6.8

    X = 53

    K = -6W = K

    Y = 7.8M =NInteger value 6 will be stored in N (truncation rather than rounding)Real value 53.0 will be stored in XInteger value -6 will be stored in KReal value -6.0 will be stored in WReal value 7.8 will be stored in YInteger value 6 will be stored in M

  • Character Padding and Truncation on AssignmentCHARACTER (5) :: A, BCHARACTER (8) :: C

    A = frog

    B = elephant

    C = Bfrog will be stored in A (padded out to five characters with blank at end)Note: The symbol represents a blank spaceeleph will be stored in B (extra characters are truncated from the right end)eleph will be stored in C (B contained eleph which must be padded to 8 characters)

  • Numeric ExpressionsA numeric expression is any mathematically valid sequence of operators, operands, and parentheses.An operand is a constant, variable, or value returned by a function.

  • Arithmetic Operators+ add- subtract* multiply/ divide** exponentiation A**B means AB

  • Result TypesThe result type is determined for each mathematical operationThe type is based on the type of the operandsIf both operands are INTEGER, the result is an integerIf at least one operand is REAL, the result is a real number

  • Precedence RulesEvaluate subexpressions in parenthesesDetermine the value returned by any function callEvaluate ** (right to left) 2**3**5 is the same as 2**(3**5)Evaluate * and / from left to rightEvaluate + and - from left to right

  • Precedence RulesAll exponentiations are performed first; consecutive exponentiations are performed from right to left 2 ** 3 ** 2 = 2 ** 9 = 512 All multiplications and divisions are performed next, in the order in which they appear from left to right. 10 / 5 * 2 = 2 * 2 = 4

  • Precedence RulesThe additions and subtractions are performed last, in the order in which they appear from left to right 10 + 8 2 + 3 = 19 Another example 2 + 4 ** 2 / 2 2 + 16 / 2 2 + 8 10

  • Characteristics of the / OperatorIf numerator and denominator are integers, the quotient is truncated to an integer23/5 is 4 rather than 4.6If either the numerator or denominator is a real number, the quotient is a real number12/9.0 is 1.33333.6/1.2 is 3.08.6/2 is 4.323.0/5.0 is 4.6

  • Characteristics of the ** OperatorIf B is a nonnegative integer, Fortran uses repeated multiplication to evaluate A**BIf B is a real number, Fortran uses the expression eB*ln(A) to approximate A**B. Ex. (-4.0) ** 2.0 is undefined, because the logarithms of negative values are not defined.

    Consider A**B

  • Type ConversionsUsing both integers and reals in a computation is a poor programming practice.Fortran provides some type conversion functions to convert a value to a different data type.Type conversions should be used to avoid mixed data types in an expression.The original value is not changed, but the converted value is placed in a temporary (unnamed) variable.

  • Type Conversion ExamplesINTEGER ==> REALREAL ==> INTEGERINTEGER NREAL XN = 15X = 3.7 * REAL(N)X = 3.7 * 15.0N is unchangedINTEGER KREAL YY = 2.8K = INT(Y) + 5K = 2 + 5Y is unchanged

  • A Few FunctionsA good programmer makes use of functions that are providedas part of the programming language. Fortran 90 has an extensive collection of intrinsic functions. (See Appendix D)SQRT(X) Preferred to X**(0.5)ABS(X) Absolute valueSIN(X), COS(X), TAN(X) Angles must be in radiansEXP(X) eXLOG(X) ln XLOG10(X) log10XMOD(A,B) A mod B

  • Form of a Program ------------ Header portion ------------- Initial comment block PROGRAM statement --------- Specification portion --------- IMPLICIT NONE Type specification statements -------- Execution portion ------------ READ, PRINT, and assignments -------- Ending portion --------------- END PROGRAM statement

  • Structure of a Line of Source CodeMaximum of 132 characters.Can begin anywhere on the line.Two or more statements can appear on a single line if they are separated by semicolons.Blanks can be inserted to improve comprehension.A comment can appear at the end of the line.If a statement label is used, it must appear first. A label is an unsigned integer with at most 5 digits.If a statement is too long, place an ampersand, &, at the end and continue on the next line.

  • CommentsEverything from the exclamation mark (!), to the end of the line is a commentA comment can appear on a line by itself or at the end of some other lineComments are included in a program to provide a person with information about the programComments are ignored by the compiler

  • The PROGRAM StatementProvides a name (identifier) for the programOptional but strongly recommendedRequired by most programming standardsMust be the first statement, but can be preceded by comments

  • The END PROGRAM StatementThis is the only required statementMust be the last line in the source code fileprogram_name must match that on the PROGRAM statement Omit the program-name if there is no program statement

  • The STOP StatementCauses the program to stop its execution at that pointMore than one STOP statement can be includedThis statement is not required, but is recommended by some coding standards

  • Simple Input (list-directed)input_list is a list of variables which will receive the values that are readEach READ statement begins reading a new line of dataA READ statement can read more than one line of data

  • Simple Output (list-directed)output_list is a list of items to be printedcharacter string literals will be printed exactlyvariables will have their values printedEach PRINT statement starts a new line of outputFortran will use its internal rules to determine the location and appearance of values on each line

    Basics of Fortran 90 -- Dr. Sanders