Wednesday, April 9, 2014

Learn f90 in a day

1. Main Features of Fortran 90

Fortran 90 is a new programming language intended for use in scientific and engineering applications. It is a language that has developed by the introduction of features that are new to Fortran, but are based on experience of other languages (like C and Matlab for instance). Fortran 90 is very different from earlier versions of Fortran, yet it is completely backwards compatible with Fortran 77.

The features of Fortran 90 are far too numerous to mention in entirety here, but some of the key features are outlined below:

a) Free format on source code.
    In Fortran 90, you can use either the Fortran 77 input format or free format. If you use free format, the file extension .f90 should be used for the file name.  
b) Dynamic allocation and pointers.
    It is now possible to allocate storage dynamically. This enables us to finally get rid of all the "work" arrays!
c) User defined data types.   (data structure)
    You can now define your own composite data types, similar to struct in C or record in Pascal.
d) Modules. ( oo programming)
    Modules enables you to program in an object oriented style, similar to C++. Modules can also be used to hide global variables, thereby making the Fortran 77 common construct outdated.
e) Recursive functions. 
    Now a part of the language.
f) Built-in array operations.   (similar to IDL)
    Statements like A=0 and C=A+B are now valid when A and B are arrays. There are also built-in functions for matrix operations, e.g., matmul for performing matrix multiplication.
g) Operator overloading.
    You can define your own meaning of operators like + and = for your own data types (objects). 


2. Compile f90 

f90 main.f90  ! create an executable file called a.out. 
f90 main.f90 -o main.out   ! create an executable file called main.out.
f90 main.f90 -L/usr/class/me390/lib -lmy_lib90   ! link library

3. Basic program structure 
Key features
    Free-form source code is allowed. Statements may begin in any column.
    Lines may extend to 132 characters.
    More than one statement may be placed on a line ( ; )
    In-line comments are allowed ( ! )
    Continuation ( & )
    Variable names may consist of up to 31 characters (letters, digits, and  _ )
    The first character of a variable name must be a letter.

Declarations

    a) The command IMPLICIT NONE is allowed. This feature cancels the default naming
        convention.
   b)  The separator :: is required in a type specification statement whenever it is used to
        initialize a variable or to declare a special attribute (such as PARAMETER).

       REAL :: Area = 0.
       INTEGER :: Num_Months = 12              ! 
       REAL, PARAMETER :: Pi = 3.1415927       ! define a parameter

       REAL Area_circle     =     REAL :: Area_circle           ! same

  c)  The precision of a real, integer, complex, or logical variable may be specified using a kind type
       parameter.
    Eg:  to declare a variable A to be a real number with at least 10 decimal places of precision with a range of at least -10^34 to 10^34, do the following:

       REAL(KIND = SELECTED_REAL_KIND(10,34)) :: A          ! KIND is used specify precision

    If the processor you are using is unable to support a variable with this type specification, a compile-time error will result.

   d)  To declare a real variable to have the equivalent of Fortran 77 accuracy DOUBLE PRECISION, simply do this:

       INTEGER, PARAMETER :: DP = SELECTED_REAL_KIND(14)      ! Define a parameter DP
       REAL(KIND = DP) :: A                                   ! Use the parameter DP  

    This declares the variable A to be of type real and have at least 14 decimal places of accuracy.

Program composition
The basic form of a Fortran 90 program is as follows:
PROGRAM name
   declarations
   executable statements
END PROGRAM


4. Logical expressions
    Symbolic forms of the relational operators are allowed:
                Old Operator    New Symbol
                ------------      ------
               .LT.                <
               .GT.               >
               .EQ.               ==
               .LE.               <=
              .GE.               >=
               .NE.               /=

    IF-ELSE constructs may be named by attaching a label at the beginning of the block:

name:    IF (logical argument) THEN
                          statements
            ELSE IF (logical argument) THEN name      ! name is optional
                          statements
                       .
                       .
            ENDIF name


         if ( jj> 10 ) exit       ! simple version


      if ( jj> 10 ) then
         exit
      endif


    The CASE construct may be used to execute a set of multi-alternative selection criteria:

       SELECT CASE (selector)   ! selector may be an integer, character, or logical expression.
           CASE (list #1)                   ! a list of one or more possible values of the selector index
              statements
           CASE (list #2)             statements
          .
          .
          .

           CASE DEFAULT           ! may be omitted
              statements
      END SELECT


5. Loops

 a)   DO loops may have either of the two forms:

       DO index_variable = start, end, step           statements
       ENDDO                 ( or END DO)
    or,

       DO nnn index_variable = start, end, step           statements
       nnn CONTINUE
    In both cases, the step variable is optional and is assumed to have the value 1 if omitted. As with   IF-ELSE and CASE constructs, DO loops may also have names.

 b)   WHILE loops are allowed. They have the general form:
       DO WHILE (logical argument)
          statements
       END DO

   c)  An "infinite" loop may be performed as follows:

       DO
          conditional statement EXIT
       END DO

     CYCLE  : go to next iteration of the loop.
     EXIT  : exit current loop 
      continue: The continue statement is an odd statement in FORTRAN. It is an executable statement but it takes no action in the program. It's primarily used as a place holder to span gaps created between branches or loops and the rest of the program.

6. Array
1) One-dimensional Arrays
       REAL, DIMENSION(10) :: A, B       ! 1-based  default
       INTEGER, DIMENSION(0:9) :: C    ! 0-based     

Array initialization

       A = (/ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 /)
    or,
       A = (/ (I, I = 1, 10) /)

Array assignment ( must have the same physical dimension )
       B = A

Array operation ( must have the same physical dimension )
       A = A + B
       C = 2*C
    For example, if A is assigned the values
       A = (/ (I, I = 1,10) /)

       WHERE (A > 5)
          B = 1.
       ELSEWHERE
          B = 0.
       END WHERE         !B has the values 0, 0, 0, 0, 0, 1, 1, 1, 1, 1.

       DOT_PRODUCT(A, B):    ! returns the dot product of A and B
       MAXVAL(A):        ! returns the maximum value in array A
       MAXLOC(A):        ! returns a one-element 1D array whose value is
                          ! the location of the first occurrence of the
                        ! maximum value in A
       PRODUCT(A):        ! returns the product of the elements of A
       SUM(A):                ! returns the sum of the elements of A

Dynamic Array allocation
  a) To declare a real allocatable array A, do
       REAL, DIMENSION(:), ALLOCATABLE :: A  ! : represent one dimension

   b) To allocate memory at run time
       ALLOCATE ( A(N) )  
       ALLOCATE ( A(N), STAT = AllocateStatus)   ! AllocateStatus takes the value 0 if allocation is successful
       IF (AllocateStatus /= 0) STOP "*** Not enough memory ***"

    c) To release memory allocatedly
       DEALLOCATE (A, STAT = DeAllocateStatus)   ! DeAllocateStatus : 0 if the deallocation was successful.


2) Multi-dimensional arrays
Array delcaration:
    Multi-dimensional arrays can be dimensioned with statements like
       REAL, DIMENSION(2,3) :: A               !  1-based by default
       REAL, DIMENSION(0:1,0:2) :: B         !  0-based
       INTEGER, DIMENSION(10,20,3) :: I

Array initialization:
    The maximum limit on the rank (the number of dimensions) of an array is 7.
     For instance, the values 1, 2, 3, 4, 5, 6 may be assigned to the two-dimensional array A by

       A = (/ 1, 2, 3, 4, 5, 6 /)  !  assign the values to A in column order similar to the rules of Fortran 77.

Array assignment: (both arrays in question have the same physical dimension)
       B = A

       WHERE (logical argument)
          sequence of array assignments
       ELSEWHERE
          sequence of array assignments
       END WHERE

       MAXVAL(A, D):    ! returns an array of one less dimension than A
                ! containing the maximum values of A along
                ! dimension D (if D is omitted, returns the
                ! maximum value in the entire array)
       MAXLOC(A):    ! returns a one-element 1D array whose value is
                ! the location of the first occurrence of the
                ! maximum value in A
       SUM(A, D):    ! returns an array of one less dimension than A
                ! containing the sums of the elements of A along
                ! dimension D (if D is omitted, returns sum of the
                ! elements in the entire array)
       MATMUL(A, B):    ! returns the matrix product of A and B
       TRANSPOSE(A):    ! returns the transpose of the 2D array A

Dynamic memory allocation:
  a)  To declare a real allocatable array A, do
       REAL, DIMENSION(:, :), ALLOCATABLE :: A  ! : represent two dimensions
                   OR
       REAL, ALLOCATABLE :: A(:, :)  ! : represent two dimensions

   b) To allocate memory at run time
       ALLOCATE ( A(N, N), STAT = AllocateStatus)
       IF (AllocateStatus /= 0) STOP "*** Not enough memory ***"

    c) To release momory allocated dynamically
       DEALLOCATE (A, STAT = DeAllocateStatus)


Internal subroutine / function. (local defined functions)
eg:
PROGRAM COLOR_GUIDE
...
 CONTAINS
  FUNCTION HUE(BLUE)   ! An internal procedure
  ...
  END FUNCTION HUE
END PROGRAM
 

No comments:

Post a Comment