Introduction to FORTRAN 77/90

Introduction to FORTRAN 77/90

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 Intro to Fortran 2 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 Intro to Fortran 3 Fortran Brief (continued) • Advantages: – Tried and true (more or less) – Low-level language…which means it’s 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 Intro to Fortran 4 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 Intro to Fortran 5 The best way to learn is to do… 11/30/2011 Intro to Fortran 6 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 Intro to Fortran 7 Getting started • Every program needs a name implicit none – requires you, the • programmer, to initialize all variables…it 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 Intro to Fortran 8 Making the code user-friendly • Unless you plan to wrap your code in another code, it’s a good idea of give yourself reminders of how to run the code • This example code takes in only one command line input…anything else causes the code to bomb and to output simple … usage instructions 11/30/2011 Intro to Fortran 9 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 Intro to Fortran 10 Allocating memory and opening files • Allocation can be done anywhere in the code, so long as it’s done before the respective array is used • Allocation only needs to be done once • Deallocation is also an option…should 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 Intro to Fortran 11 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 Intro to Fortran 12 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 Intro to Fortran 13 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 Intro to Fortran 14 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 group…Fortran 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 Intro to Fortran 15 … Format commands … • Format commands can appear anywhere (I like to put them at the end) • The line number must be unique (i.e. can’t 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 Intro to Fortran 16 17 Intro to to Fortran Intro Endings … … … … … … … In Fortran: if it begins,InFortran: it must end. … 11/30/2011 • 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 Intro to Fortran 18 Intrinsic Functions • Generally easier to use than external functions because they don’t require building libraries, additional compiler options, etc. • Limited in number and applicability • Complete list: http://gcc.gnu.org/onlinedocs/gfortran/Intrins ic-Procedures.html 11/30/2011 Intro to Fortran 19 That’s 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 Intro to Fortran 20 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us