Introduction to FORTRAN 77/90 - ?· Fortran Brief (continued) • Differences between versions – Fortran…

Download Introduction to FORTRAN 77/90 - ?· Fortran Brief (continued) • Differences between versions – Fortran…

Post on 06-Sep-2018

216 views

Category:

Documents

4 download

TRANSCRIPT

  • Introduction to FORTRAN 77/90

    Brent Minchew November 30, 2011

  • FORTRAN Brief Originally an acronym (FORmula TRANslation)

    that evolved into a name: Fortran First written in 1957 at IBM for use with punch

    cards Latest standard version is Fortran 2003

    (though the latest version on many compilers is Fortran 90 or, possibly, Fortran 95)

    In general, newer versions are backward compatible, at least to Fortran 77

    11/30/2011 2 Intro to Fortran

  • Fortran Brief (continued)

    Differences between versions Fortran 77 is the foundation of most legacy code Fortran 90 added array operations, structures, and

    dynamic memory allocation, among other features

    Fortran 95 was a minor upgrade to Fortran 90 Fortran 2003 adds other useful features such as

    dynamic character allocation and some intrinsic functions

    11/30/2011 3 Intro to Fortran

  • Fortran Brief (continued) Advantages:

    Tried and true (more or less) Low-level languagewhich means its fast Large libraries (BLAS, LAPACK, HSL, etc.) Lots of legacy code (particularly Fortran 77)

    Disadvantages: All variables must be initialized Few intrinsic functions and libraries can be a

    headache to implement Debugging can be relatively painful

    11/30/2011 4 Intro to Fortran

  • Things to keep in mind about Fortran Write the code in any text editor (e.g. vim, emacs, Notepad++, etc.) Compile on your favorite compiler (gfortran, Intel, etc.) Not case sensitive Most compilers limit lines to 72 columns; expandable to 132

    columns with the proper compiler options This includes spaces but not comments Lines can be appended using & to end one line and begin the next

    Comments begin with ! (some compilers still support beginning comments with C as well) There is no terminating command as in C/C++ Everything that follows ! on a given line is commented

    Array indices begin with 1 (as in Matlab) By default, operations on arrays are element by element (unlike

    Matlab) Spaces and tabs have no meaning in free form (recommended)

    11/30/2011 5 Intro to Fortran

  • The best way to learn is to do

    11/30/2011 6 Intro to Fortran

  • Example Code Outline Takes in a command file from a command line

    argument Reads the command file which:

    Specifies an array of complex data (name and dimensions)

    Assigns an output Parses the command file Calculates the amplitude and phase of the

    complex data Writes amplitude and phase to file Calculates mean and standard deviation of phase prints to the screen

    11/30/2011 7 Intro to Fortran

  • Getting started Every program needs a name implicit none requires you, the

    programmer, to initialize all variablesit is best to start every program with implicit none

    Everything else is initialization which must be done at the beginning of the program

    Variable types are: Character Integer (4 or 8 byte) Floating Point (4 or 8 byte) Complex (8 or 16 byte)

    Structures are also available with Fortran 90 and newer (note the use of % instead of . for structure variables)

    11/30/2011 8 Intro to Fortran

  • Making the code user-friendly

    Unless you plan to wrap your code in another code, its a good idea of give yourself reminders of how to run the code

    This example code takes in only one command line inputanything else causes the code to bomb and to output simple usage instructions

    11/30/2011 9 Intro to Fortran

  • Parsing the command file Parsing code: Command file:

    Code searches for a divider (in this case ::) If divider is found, the line is split into an inquiry (left of the divider) and a user response (right of the divider) The inquiry (label) is read into case select and variables are assigned accordingly Read statement converts string input to requisite variable (number) format

    11/30/2011 10 Intro to Fortran

  • Allocating memory and opening files Allocation can be done anywhere in the

    code, so long as its done before the respective array is used

    Allocation only needs to be done once Deallocation is also an optionshould be

    used sparingly unless memory is a limiting factor

    Files only need to be opened once (I prefer to do this all together towards the beginning of the program)

    By default, all files are closed when the program exits but you can use the close command (e.g. close(11))

    Open command always begins with unit number (any integer except 6), then file name; the rest is optional

    Access = data input mode Form = file format (unformatted is for binary

    files) Status = file status when opened Recl = record length (specified here as

    bytes*number_of_entries) // tie up string fragments

    11/30/2011 11 Intro to Fortran

  • Do loops and if statements Do loops (same concept as for loops in Matlab)

    are initilized in order: do loop_variable=begin,end,step The default step = 1 (no need to specify) Important note: when the do loop exits,

    loop_variable = end+step Read and write statement can contain implicit

    do loops (in this example these are mm=1,cols) If statements must have the form:

    If (condition) command (or then for block statements)

    When elseif or else statements are needed, if statements must be in block form (if then else)

    Elseif must have the form elseif (condition) then

    Note that end statements go with opening statements (enddo or end do for do loops, etc.)

    11/30/2011 12 Intro to Fortran

  • Input/Output

    Read command: Read(unit,rec) for unformatted files (if

    files are formatted, rec is replaced by a format command)

    Unit = integer defined in open statement Rec = record number; record length was

    specified in open statement Read(unit,*,options) is a wildcard format

    not suitable for unformatted files, but is good for input commands (see parsing section)

    Write command: Same form as read for unformatted and

    formatted data Write(*,format,options) =

    write(6,format,options) = write to screen in given format with options

    \b\b\b is a crude carriage return command (\b = backspace one space)

    Advance=no does not advance one line after writing

    11/30/2011 13 Intro to Fortran

  • Functions Functions must be

    initialized at the beginning of the program and defined after the end of the program (or in a separate file that is compiled with the driver)

    They can appear in an algebraic statement

    Variables in parenthesis are inputs

    Function name is output Functions must have end

    statement Return statement tells the

    program to return to the calling program

    11/30/2011 14 Intro to Fortran

  • Subroutines Must be called Do not need to be initialized

    within the program Are defined after the end of

    the program (or in a separate file that is compiled with the driver)

    Important! Outputs and inputs are passed in the same groupFortran does not distinguish by default Can use intent command in

    subroutine initialization to specify input and output variables

    In some cases, it may be a good idea to pass copies of variables

    11/30/2011 15 Intro to Fortran

  • Format commands

    Format commands can appear anywhere (I like to put them at the end) The line number must be unique (i.e. cant have two 100 format calls) Different formatting cells are separated by a comma Cells are defined by a single letter denoting type (a for text, i for integer, f

    for fixed point, e for exponential form, etc.) and a number specifying the cell width (in spaces). Fixed point and exponential form require a decimal point followed by the number of decimal places

    A more thorough description of format commands is here: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

    11/30/2011 16 Intro to Fortran

  • Endings In Fortran: if it begins, it must end.

    11/30/2011 17 Intro to Fortran

  • Compilers Once the code is written it needs to be compiled Many compilers are available, each with various options GNU is open source and is probably installed on all GPS

    computers (Getting started wiki: http://gcc.gnu.org/wiki/GFortranGettingStarted)

    GNU compile command for the example program (in Linux) is: gfortran example.f ffixed-line-length-0 ffree-form O3 o fortex Where: ffixed-line-length-0 specifies that the line length in unlimited (in

    practice this means that the max line length is 132) ffree-form removes significance of character placement in the line O3 is an optimization routine (optional) o fortex is the executable

    11/30/2011 18 Intro to Fortran

    http://gcc.gnu.org/wiki/GFortranGettingStarted

  • Intrinsic Functions

    Generally easier to use than external functions because they dont require building libraries, additional compiler options, etc.

    Limited in number and applicability Complete list:

    http://gcc.gnu.org/onlinedocs/gfortran/Intrinsic-Procedures.html

    11/30/2011 19 Intro to Fortran

  • Thats it

    Feel free to ask me any questions about Fortran

    I would appreciate feedback on the class (organization, content, etc.)

    Feel free to use/modify/distribute the example code

    11/30/2011 20 Intro to Fortran

    Introduction to FORTRAN 77/90FORTRAN BriefFortran Brief (continued)Fortran Brief (continued)Things to keep in mind about FortranThe best way to learn is to doExample Code OutlineGetting started Making the code user-friendlyParsing the command fileAllocating memory and opening filesDo loops and if statementsInput/OutputFunctionsSubroutinesFormat commandsEndingsCompilersIntrinsic FunctionsThats it

Recommended

View more >