es440+911fortlab1

Upload: getsweet

Post on 14-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 ES440+911FortLab1

    1/12

    School of Engineering

    University of Warwick

    ES440/911 Computational Fluid Dynamics

    Lab session Week 13: Fortran I

    Novak Elliott ([email protected])

    A. Introduction

    The goal of this lab session is to give a quick introduction to the most useful features

    of the Fortran 77 programming language for scientific applications.

    Fortran, an acronym for Formula Translation, is a general-purpose, procedural,

    programming language that is especially suited to numeric computation and scientific

    computing. Originally developed by IBM in the 1950s, Fortran came to dominate this

    area of programming early on and has been in continual use in computationallyintensive areas such as climate modelling, computational chemistry and

    computational fluid dynamics (CFD) for half a century.

    There have been many revisions of the Fortran language, each being designated by the

    year the ANSI standard was proposed. Thus we have Fortran 66, Fortran 77, Fortran

    90, Fortran 95, Fortran 2003, and the latest revision in progress is tentatively entitled

    Fortran 2008.

    The most common Fortran version today is still Fortran 77, although Fortran 90 is

    growing in popularity. There are also several versions of Fortran aimed at parallel

    computers. The most important one is High Performance Fortran (HPF), which is a

    de-facto standard and indeed many features have made there way into official ANSI

    revisions. Users should be aware that most Fortran 77 compilers allow a superset of

    Fortran 77, i.e. they allow non-standard extensions. In this lab session we will

    emphasizestandard ANSI Fortran 77which will give your programs greater

    portability to other platforms (such as linux, for example).

    Finally, given that Fortran is half a century old and many other languages have been

    developed since its creation, one may reasonably ask: Why bother learning Fortran?

    or Shouldnt I learn C++ instead? There are many arguments for and against one

    language over another but what should always be remembered is that everyprogramming language was designed with a specific purpose in mind. Fortran was

    designed to be very good at number crunching but is not very good for generating

    graphics. Similarly, Javas strength is web applications, C is great for direct access to

    hardware and C++ is good for object-oriented tasks. Admittedly, with a deep

    understanding of the subtleties of the C++ language one can write a C++ program

    with the same computational efficiency as a Fortran program and arguably with

    prettier syntax. However, writing a fast number crunching program in Fortran is

    much simpler and achieves the same result. Moreover, some of the leading

    commercial CFD packages on the market (e.g., Star CCM+, CFX) allow users to

    write their own code in Fortran to extend the capabilities of the software to solve

    highly customised problems, a service often offered by CFD consultant engineers.

    mailto:[email protected]:[email protected]
  • 7/30/2019 ES440+911FortLab1

    2/12

    B. Fortran Programming Tutorial

    1. Fortran Basics

    1.1 Getting up and running

    A Fortran program is simply a sequence of computer instructions written as lines of

    text. The text has to follow certain syntax to be a valid program. We will start bylooking at a simple example:

    program circle

    implicit none

    c This program simply calculates the area of a circle

    c Declarations (variables and constants)

    double precision radius, area

    c Statements (where the work is done)

    radius = 10.0D0area = 3.141592D0 * radius * radius

    write (*,*) 'The area = ', area

    stop

    end

    Open the program Notepad (start->Run->type notepad and hit enter), copy and

    paste the above text and then save the file as circle.fin your home directory (File->Save as->choose All Files and type circle.f). Next open a command prompt

    (start->Run>type cmd then press enter), change to your home directory (type

    i:\), and type the following command:

    g77 o circle circle.f

    You should now have a program called circle.exe. Run this program. You shouldsee the following output:

    The area = 314.1592

    Congratulations, you have just written your first Fortran program! Now lets have a

    look at what weve just done.

    1.2 Compiler and linker

    We began with a text file and ended up with a computer program how did we do it?

    The answer is by using the compiler and linker, g77.exe, which is a program forreading source code (circle.f) and compiling it into object code and then linking all ofthe object code together into a single executable program (circle.exe). Sometimes the

    compiling and linking is done in separate stages but in our case its done all in one go

    so from here on we will simply refer to the process as compiling and g77 as thecompiler.

  • 7/30/2019 ES440+911FortLab1

    3/12

    1.3 Comments

    The lines that begin with with a "c" are comments and while they are ignored by the

    computer they make the program more readable for humans. Well-written comments

    are crucial to program readibility. Commercial Fortran codes often contain about

    50% comments. Comments may also begin with an asterisk * instead of a c.

    1.4 Program Organisation

    The structure of a Fortran program is:

    program nameimplicit none

    declarations

    executable statements

    stop

    end

    Note: Courier Font is used for Fortran commands,Italic Roman Fontis used for

    generic descriptons andArial Font is used for input and output from the commandline. The implicit none statement ensures that all variables are explicitly

    declared and makes programs easier to debug and understand do NOT omit this

    statement! The stop statement is optional and may seem superfluous since the

    program will stop when it reaches the end anyways, but it is recommended to always

    terminate a program with thestop

    statement to emphasize that the execution flow

    stops there.

    1.5 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

    Most lines in a Fortran 77 program start with 6 blanks and end before column 72, i.e.

    only the statement field is used. Note that Fortran 90 allows free format.

    1.6 Continuation

    Occasionally, a statement does not fit into one single line. One can then break the

    statement into two or more lines, and use the continuation mark in column 6.

    Example:

    c23456789 (This demonstrates column position!)

    c The next statement goes over two physical linesarea = 3.14159265358979D0

  • 7/30/2019 ES440+911FortLab1

    4/12

    & * radius * radius

    Any character can be used instead of the ampersand sign as a continuation character.

    It is considered good programming style to use either the ampersand (&), plus sign (+)

    or numbers (2 for the second line, 3 for the third, and so on).

    1.7 Blank spacesBlank spaces are ignored in Fortran 77. So if you remove all blanks in a Fortran 77

    program, the program is still syntactically correct but almost unreadable for humans.

    1.8 Exercises

    1(a) Identify at least 3 errors in the following Fortran 77 program:

    c23456789 (This demonstrates column position!)

    test programmeimplicit none

    ccinteger intint = 12write(*,*) 'The value of int is ',

    + intendstop

    1(b) Correct these errors, compile the program as test.exe and run it. The output

    should be:

    The value of int is 12

    Hint: if the compiler generates error messages then these will help identify the

    problems in the code dont be afraid of these, this is what debugging is all about and

    accounts for the majority of program development time!

    2. Variables, declarations and types

    2.1. Variable names

    Variable names in Fortran consist of 1-6 characters chosen from the letters a-z and the

    digits 0-9. The first character must be a letter! (Note: Fortran 90 allows variable

    names of arbitrary length). Fortran 77 does not distinguish between upper and lowercase, in fact, it assumes all input is upper case. However, nearly all Fortran 77

    compilers will accept lower case. If you should ever encounter a Fortran 77 compiler

    that insists on upper case it is usually easy to convert the source code to all upper

    case.

    2.2 Types and declarations

    Every variable must be defined in a declaration. This establishes the type of the

    variable. The most common declarations are:

    integer list of variables

    real list of variablesdouble precision list of variables

  • 7/30/2019 ES440+911FortLab1

    5/12

    complex list of variables

    logical list of variables

    character list of variables

    The list of variables should consist of variable names separated by commas. We will

    be using the integer type for general counting and indexing operations, thedouble precision type for high accuracy numerical computations and the

    character type for reporting.

    2.3 The parameter statement

    Some constants appear many times in a program. It is then often desirable to define

    them only once, in the beginning of the program. This is what the parameter

    statement is for. It also makes programs more readable. For example, the test area

    program should have been written like this:

    double precision radius, area, piparameter (pi = 3.141592D0)

    area = pi * radius * radius

    The syntax of the parameter statement is

    parameter (name=constant, ... ,name=constant)

    Note the following:

    Once a variable is assigned a value in a parameter statement it becomes a

    constant whose value cannot change for the rest of the program A variable can appear in at most one parameter statement

    The parameter statement(s) must come before the first executable

    statement

    Common uses of the parameter statement in CFD programs are to define fluid

    properties (e.g. density), geometry dimensions (pipe diameter), initial conditions

    (ambient temperature) and boundary conditions (inlet velocity).

    2.4 Exercises

    2(a) Which of the following variable names are invalid, and why?

    A5, 5a, variable, XY3Z4Q, AT&T, number1, NO1, NO 1, NO_1,stop

    2(b) Write a program to check your answer to part (a).

    3. Expressions and assignment

    3.1 Constants

    The simplest form of an expression is a constant. Here are some examples for the

    integer type:

  • 7/30/2019 ES440+911FortLab1

    6/12

    10-10032767

    +15

    and the double precision type:

    1.0D0-0.25D02.0D63.333D-1

    The D-notation means that you should multiply the constant by 10 raised to the power

    following the "D". Hence, 2.0D6 is two million, while 3.333D-1 is approximately

    one third.

    Note: do not leave off the D! 1.0 is implicitly a real constant whereas 1.0D0 is a

    double precision constant. In the circle program try changing the value of pi

    from a double precision constant to a real constant and see what happens to the area!

    The reasoning behind this is found below in section 3.3.

    The last type we will deal with are character constants. These are most often used as

    an array of characters, called a string. These consist of an arbitrary sequence of

    characters enclosed in apostrophes (single quotes):

    'ABC''Anything goes!''It is a nice day'

    Strings and character constants are case sensitive. A problem arises if you want to

    have an apostrophe in the string itself. In this case, you should double the apostrophe:

    'It''s a nice day'

    3.2 Expressions

    The simplest expressions are of the form

    operandoperatoroperand

    and an example is

    x + y

    The result of an expression is itself an operand, hence we can nest expressions

    together like

    x + 2 * y

  • 7/30/2019 ES440+911FortLab1

    7/12

    This raises the question of precedence: Does the last expression mean x + (2*y)

    or(x+2)*y? The precedence of arithmetic operators in Fortran 77 are (from highest

    to lowest):

    () {Brackets}

    ** {Indicial exponentiation}*,/ {Multiplication, Division}

    +,- {Addition, Subtraction}

    i.e. the old BIMDAS rule from primary school. All these operators are calculated

    left-to-right, except the exponentiation operator **, which has right-to-left

    precedence. Its best to use brackets to ensure that each expression is evaluated in the

    order intended.

    Note: Extreme caution must be taken when using the division operator, which has a

    quite different meaning for integers and double precision. If the operands are bothintegers, an integer division is performed, otherwise a floating point arithmetic

    division is performed. E.g., 3/2 equals 1, while 3.0/2.0 equals 1.5.

    3.3 Assignment

    The assignment has the form

    variable_name = expression

    The interpretation is as follows: Evaluate the right hand side and assign the resulting

    value to the variable on the left. The expression on the right may contain other

    variables, but these never change value! For example,

    area = pi * radius**2

    does not change the value ofpi orradius, only area.

    3.4 Exercises

    3(a) Calculate the value of the following Fortran 77 expressions (by hand):

    2+1-10/3/42**3/3*2-5

    -(3*4-2)**(3-2**1+1)/-2

    3(b) Write a Fortran 77 program that prints these constant expressions using the

    write(*,*) statement (as used in circle program in section 1.1) and check your

    hand calculations.

    3(c) Use spaces and brackets to make the order of evaluation more clear.

    4. Conditional statements and decision making

    4.1 Logical expressions

    Logical expressions can only have the value .TRUE. or.FALSE. A logical

    expression can be formed by comparing arithmetic expressions using the followingrelational operators:

  • 7/30/2019 ES440+911FortLab1

    8/12

    .LT. meaning =

    .EQ. =

    .NE. /=

    So you cannot use symbols like < or = for comparison in Fortran 77, instead you have

    to use the correct two-letter abbreviation enclosed by dots! (Such symbols are allowed

    in Fortran 90, though.)

    Logical expressions can be combined by the logical operators .AND..OR..NOT.

    which have the expected meaning.

    4.2 If statements

    The ability to make decisions is what makes a program useful and the simplest way to

    do this is with the logical if statement:

    if (logical expression) executable statement

    This has to be written on one line. This example finds the absolute value of x:

    if (x .LT. 0) x = -x

    If more than one statement should be executed inside the if, then the following

    syntax should be used:

    if (logical expression) then

    statementsendif

    The most general form of the if statement has the following form:

    if (logical expression) then

    statements

    elseif (logical expression) then

    statements

    : :

    else

    statementsendif

    The execution flow is from top to bottom. The conditional expressions are evaluated

    in sequence until one is found to be true. Then the associated code is executed and the

    control jumps to the next statement after the endif.

    if statements can be nested in several levels. To ensure readability, it is important to

    use proper indentation. Here is an example:

  • 7/30/2019 ES440+911FortLab1

    9/12

    if (x .GT. 0) thenif (x .GE. y) then

    write(*,*) 'x is positive and x >= y'else

    write(*,*) 'x is positive but x < y'

    endifelseif (x .LT. 0) then

    write(*,*) 'x is negative'else

    write(*,*) 'x is zero'endif

    You should avoid nesting too many levels ofif statements as the logic becomes

    difficult to follow..

    4.3 Exercises

    4(a) Calculate the value of these logical expressions (by hand):

    (.true. .and. .false.) .or. .true.(2.lt.2) .or. (5 .eq. 11/2)

    and then write a Fortran program to check your answers. What happens if you

    remove the brackets?

    4(b) Write a Fortran 77 program that reads in two integers from the user and then

    assigns the integer variable t the following value:

    x+y if x and y are both positive

    x-y if x is positive and y negative

    y if x is negative

    0 if x or y is zero

    Print the value oft at the end of the program.

    Hint: to read in a number from the user the read(*,*) function can be used:

    write(*,*)'Input first number and press enter: '

    read(*,*) x

    5. Loops and arrays

    5.1 Loops

    For performing repeated tasks, such as calculating the pressure in every cell of a CFD

    model, for example, loops are used. Fortran has the do-loop which corresponds to

    what is known as afor-loop in other languages.

    Here is a simple example that prints the cumulative sums of the integers from 1

    through n (assume n has been assigned a value elsewhere):

  • 7/30/2019 ES440+911FortLab1

    10/12

    integer i, n, sum

    sum = 0do 10 i = 1, n

    sum = sum + i

    write(*,*) 'i = ', iwrite(*,*) 'sum = ', sum

    10 continue

    The number10 is a statement label. 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.

    The variable defined in the do-statement is incremented by 1 by default. However,you can define any other integer to be the step. This program segment prints the even

    numbers between 1 and 10 in decreasing order:

    integer i

    do 20 i = 10, 1, -2write(*,*) 'i =', i

    20 continue

    The general form of the do loop is as follows:

    do label var= expr1, expr2, expr3

    statementslabel continue

    varis the loop variable (often called the loop index) which must be integer. expr1

    specifies the initial value ofvar, expr2 is the terminating bound, and expr3 is the

    increment (step).

    Note: The do-loop variable must never be changed by other statements within the

    loop! This will cause great confusion.

    5.2 Arrays

    Many scientific computations use vectors and matrices. The data construct Fortran

    uses for representing such objects is the array. A one-dimensional array corresponds

    to a vector, while a two-dimensional array corresponds to a matrix. Arrays are

    usually manipulated using do-loops.

    One-dimensional arrays

    The simplest array is the one-dimensional array, which is just a linear sequence of

    elements stored consecutively in memory. For example, the declaration

    double precision a(20)

  • 7/30/2019 ES440+911FortLab1

    11/12

    declares a as a double precision array of length 20. That is, a consists of 20

    double precision numbers stored contiguously in memory. By convention,

    Fortran arrays are indexed from 1 and up. Thus the first number in the array is

    denoted by a(1) and the last by a(20)

    The type of an array element can be any of the basic data types. Examples:

    integer i(10)double precision x(100)

    Each element of an array can be thought of as a separate variable. You reference the

    i'th element of array a by a(i). Here is a code segment that stores the 10 first square

    numbers in the array sq:

    integer i, sq(10)

    do 100 i = 1, 10sq(i) = i**2

    100 continue

    Note: A common bug in Fortran is that the program tries to access array elements that

    are out of bounds or undefined. This is the responsibility of the programmer, and the

    Fortran compiler will not detect any such bugs!

    Two-dimensional arrays

    Matrices are very important in linear algebra. Matrices are usually represented by

    two-dimensional arrays. For example, the declaration

    double precision A(3,5)

    defines a two-dimensional array of 3*5=15 double precision numbers. It is

    useful to think of the first index as the row index, and the second as the column index.

    Hence we get the graphical picture:

    (1,1) (1,2) (1,3) (1,4) (1,5) (2,1) (2,2) (2,3) (2,4) (2,5) (3,1) (3,2) (3,3) (3,4) (3,5)It is quite common in Fortran to declare arrays that are larger than the matrix we want

    to store. (This is because Fortran does not have dynamic storage allocation and its

    usual to make the array big enough to handle the worst case scenario.) This is

    perfectly legal. Example:

    double precision A(3,5)integer r,c

    cc We will only use the upper 3 by 3c part of this array.

    c

  • 7/30/2019 ES440+911FortLab1

    12/12

    c Loop through each column cdo 20 c = 1, 3

    c Loop through each row rdo 10 r = 1, 3

    a(r,c) = dble(r)/dble(c)

    10 continue20 continue

    Note: The elements in the submatrix A(1:3,4:5) are undefined. Do not assume

    these elements are initialized to zero by the compiler (some compilers will do this, but

    not all).

    The dimension statement

    There is an alternate way to declare arrays in Fortran 77. The statements

    integer A, x

    dimension x(50)dimension A(10,20)

    are equivalent to

    integer A(10,20), x(50)

    This dimension statement is considered old-fashioned style today but sometimes when

    a program contains many arrays it is convenient to have their dimensions grouped

    separately to the variable declarations for quick reference.

    5.3 Exercises

    5(a) Write a program that calculates and prints out the first 100 Fibonacci numbers.

    The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

    5(b) Write a program that prints out a 10 x 10 checker board using 1s and 0s.

    5(c) Write a program that solves the following three simultaneous equations:

    x + 2y + 3z = 6

    4x + 5y + 6z = 15

    7x + 8y = 15

    Hint: recall from linear algebraAX = B, X = A-1 B