Arrays in Fortran


There are many type of declarations in Fortran referring to arrays that we should be aware of, before attempting to use any of them.

  1. Explicit-shape Arrays with constant bounds
    Explicit-shape arrays with constant bounds are non-dummy arrays whose shape is explicitly specified. They may be declared in the main program or inside the procedure body.
    They do not appear in the argument list of a procedure and they may be initialized in their type declaration statements. Here are two examples:

    integer, parameter :: n = 20
    !non-initialized two dimensional array
    real, dimension(n,n) :: arr
    !initialized with lower bound -3
    real, dimension(-3:5) :: arr2 = 8.0

  2. Dummy Arrays
    Dummy arrays are arrays that appear in the argument list of procedures. No memory is allocated, because they should be allocated externally prior to be used.
    There are three types:
    1. Explicit-shape dummy arrays' dimensions are explicitly declared by other arguments in the procedure's argument list.
      All the advanced features of Fortran arrays can be used with those type of arrays, including whole array operations, array sections and array intrinsic functions. An example of such an array follows:

      subroutine test ( arr, n, m1, m2 )
      integer, intent(in) :: n, m1, m2
      real, dimension(n,m1:m2) :: arr

    2. Assumed-shape dummy arrays are arrays that appear in the argument list of a procedure, whose dimensions are declared by colons. The only thing that must be declared is the shape (the number of dimensions).
      All the advanced features of Fortran can be used with these arrays. Here is an example of a two-dimensional array:

      subroutine test ( array )
      real, dimension(:,:) :: array
      !whole array operation
      array = 9.0

    3. Assumed-size dummy arrays also appear in the argument list of procedures. The size of all dimensions except for the last must be explicitly specified. An assumed-size array CANNOT be used with whole array operations or with many of the intrinsic functions, because the shape of the actual array is unknown. Assumed-size arrays are remnants of older versions of Fortran and that's why they SHOULD NEVER be used in any new programs. Here is an example:

      subroutine test ( array )
      real, dimension(10,*) :: array

  3. Automatic Arrays
    Automatic arrays are explicit-shape arrays with non-constant bounds that appear inside procedure only. They do not appear in the procedure's argument list, however, the bounds of those arrays are declared either inside the procedure's argument list or inside a module.
    When the procedure is invoked the arrays is automatically created. At the end of the procedure the array is automatically destroyed.
    Each time the procedure is invoked, the array will be created from scratch, that is data is not preserved after the end of the procedure (the SAVE attribute may not be specified and default initialization is forbidden). Here is an example of this kind of array:

    subroutine test ( n, m )
    integer, intent(in) :: n, m
    real, dimension(n,m) :: array

  4. Deferred-shape Arrays
    Deferred-shape arrays are allocatable arrays or pointer arrays. A deferred-shape array is declared with an ALLOCATABLE (or POINTER) attribute and with the dimensions specified by colons.
    The array may be used after its explicit memory allocation by using an ALLOCATE statement. It is important to release the memory at the end by calling the DEALLOCATE statement or else memory leaks occur.
    A deferred-shape array may not be initialized in its type declaration statement.
    If an allocatable array is declared and allocated inside a procedure and it is desired to keep the array between invocations, the array must be declared with the SAVE attribute. Here is an example of a deferred-shape array:

    !allocatable two-dimension array
    integer, allocatable :: array(:,:)
    allocate ( array(50,50) )
    ...
    deallocate ( array )

Comments

Popular posts from this blog

Write Unicode text using VBA

Calling Fortran intrinsic functions from Visual Basic

Dictionary class extensions (CopyTo, Sort) (C#)