1 / 15

Fortran

Fortran. Background . Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language. FORTRAN 66 First HLL Standard FORTRAN 77 Character data types Improved Do Loops and If statements Fortran 90

Download Presentation

Fortran

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fortran

  2. Background • Fortran or FORmulaTRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language. • FORTRAN 66 • First HLL Standard • FORTRAN 77 • Character data types • Improved Do Loops and If statements • Fortran 90 • Free format source code • Modern control structures • Derived Data Types • Powerful array notation • Dynamic Memory allocation • Operator overloading • Keyword argument passing • The INTENT attribute • Control of numeric precision • Support for Modules • Fortran 95 • minor improvements • Fortran 2003 • Object oriented Fortran • Fortran 2008 • Concurrent Programming • Fortran 2015 • Minor improvements Programming Languages Family Tree

  3. Development Environment • You will want to be comfortable using an text-editor like vi or emacs– that supports syntax highlighting and auto-indentation etc... There is an ide called Photran that I have never used. • I prefer emacs but both vi and emacs have similar functionality. • I usually run emacs in the terminal (fast!) • emacs program.f90 –nw • Important emacs commands to remember • ctrl-x ctrl-f (find a file) • ctrl-x ctrl-s (save current file) • ctrl-x ctrl-c (close emacs) • ctrl-s (search) • ctrl-x 2 (split window horizontally) • ctrl-x 3 (split window vertically) • ctrl-x 0 (close current window) • ctrl-x o (switch to next window) • ctrl-x k (kill current file) • ctrl-x b (switch to next file • alt-% (search and replace) • ctrl-a (start of line) • ctrl-e (end of line) • Also good to remember 'ctrl-z' to suspend emacs and 'fg' to resume

  4. Connecting to Bluehive • Open a terminal in mac or linux • Putty or MobaxTerm in windows • ssh <netid>@bluehive.circ.rochester.edu • mkdirfortran_class • cd fortran_class • emacs hello_world.f90

  5. Compiling • I will use intel compiler although gfortran compiler will also work. • module load intel • ifort hello_world.f90 • ./a.out

  6. Fortran Basics • /public/jcarrol5/fortran/ex1.f90 • Program structure • Comments (! This is a comment) • Continuation & lines • Variable Declarations • Data Types • INTEGER • REAL • CHARACTER • LOGICAL • Operators • Arithmetic: +, -, /, *, ** • CHARACTER concatenation: // • Relational: >, <, >=, <=, /=, == • Logical: .AND., .OR., .XOR., .NOT., .EQV., .NEQV. • Use () to group expressions • Intrinsic numerical functions • MOD, FLOOR, CEILING, SIGN, MAX, MIN • Intrinsic math functions • SQRT, LOG, LOG10, EXP, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN • Basic I/O • READ, WRITE

  7. Exercise 1 • Exercise 1: Write a program that uses Heron's formula to calculate the area of a triangle given the length of its three sides.

  8. Format Specifiers • /public/jcarrol5/fortran/ex1b.f90 • Format specifiers. • write(*,'(A10,2F12.5,I3,L1)') string1, float1, float2, int1, logical1 • I – integer • A – string • F – float • E – exponential • EN – scientific notation • ES – engineering notation • All format specifiers can be modified with a repeat count and width. In addition, the precision can be specified for Format specifiers for real numbers. • 4F10.5 means use a floating point format of width 10 and 5 decimal points 4 times.

  9. Subroutines and Functions • /public/jcarrol5/fortran/ex2.f90 • Fortran programs can contain subroutines and functions. • Functions return a value • Subroutines do not • Fortran normally passes variables by reference – so be careful when modifying function or subroutine arguments. Best to declare the intent to avoid common mistakes. • If a constant or an expression is passed as an argument, a temporary variable is created to store the result of the expression before being passed to the subroutine or function.

  10. Exercise 2 • Modify previous program to use a function to calculate the area of the triangle. • Use format specifiers for the output.

  11. Control Structures • /public/jcarrol5/fortran/ex3.f90 • Fortran has 3 basic control structures • DO loops • With a counter (an optional increment) • DO counter=start, end[, increment] • ... • END DO • With a while statement • DO WHILE (a < b) • ... • END DO • With an exit statement • DO • ... • IF (condition) EXIT • END DO • IF blocks • IF (condition) THEN • ... • ELSE IF (other condition) THEN • ... • ELSE • ... • END IF • SELECT CASE statements • SELECT CASE (var) • CASE(constant1) • execution statements • CASE(constant2) • execution statements • CASE DEFAULT • execution statements • END SELECT

  12. Exercise 3 • Write a Fortran program to calculate the number of uniq combinations of size r taken from a set of size n – ie 'n choose r'. • Validate that n and r are both non-negative and that n is greater than or equal to r. • Write a factorial function • Write a combination function that calls the factorial function • Format the output

  13. Recursive Functions and Subroutines • /public/jcarrol5/fortran/ex4.f90 • Often a subroutine or function may want to call itself. This is called recursion. • Recursive subroutines and functions require the recursive keyword. • Need to have some termination condition

  14. Exercise 4 • Modify the factorial function in your combination program to be recursive.

  15. Tomorrow • Modules • Derived Data Types • Public, Private Entities • Arrays • Namelists • Reading/Writing to files • Pointers

More Related