Date Modified Category Fortran Tags Fortran

Fortran variable types are character, integer, real, complex, logical. We will use them with following examples while practicing what kind of information actually they could contain.

Example program 1: character definition and usage.

program calc_chrctr
  implicit none   !forces the programmer to declare all variables
  integer :: a,b
  character(len=5) :: myName

  myName = 'Hello World'     !Define the value of 'name'

  !> It should print only first 5 character, not the whole word
  print*, myName             !Print on the terminal

end program calc_chrctr

Example program 2: integer definition and usage.

program calc_integer
  implicit none   !forces the programmer to declare all variables
  integer :: w,l
  integer :: prmtr, area, diag1, diag2 

  w = 3                    !Define a
  l = 4                    !Define b
  prmtr = 2*(w + l)        !Calculate the perimeter of rectangle
  area  = w*l              !Calculate the area of rectangle

  !> Should give error on compilation
  diag1  = sqrt(w**2+l**2) !Calculate diagonal
  diag2 =(w**2+l**2)**0.5  !Calculate diagonal

  print*, 'Perimeter = ',prmtr
  print*, 'Area = ' ,area
  print*, 'diag1 = ',diag1
  print*, 'diag2 = ',diag2

end program calc_integer

Example program 3: real definition and usage.

program calc_real
  implicit none   !forces the programmer to declare all variables
  real(8) :: w,l 
  real(8) :: prmtr, area, diag1, diag2

  w = 3.0                    !Define a
  l = 4.0                    !Define b
  prmtr = 2.0*(w + l)        !Calculate the perimeter of rectangle
  area  = w*l              !Calculate the area of rectangle
  diag1  = sqrt(w**2.0+l**2.0) !Calculate diagonal
  diag2 =(w**2.0+l**2.0)**0.5  !Calculate diagonal

  print*, 'Perimeter = ',prmtr
  print*, 'Area = ' ,area
  print*, 'diag1 = ',diag1
  print*, 'diag2 = ',diag2

end program calc_real

Discussion

Is there any difference for output of `program calc_integer` and `program calc_real`?

Bonuses

implicit none forces the programmer to declare all variables that is considered as a good style.

type type is a Fortran structure that allow uses create data objects which can consist different data types.

type myType
   integer:: i
   real*8 :: a(3)
end type myType

And to use a derived data type, define your variables like in the following line.

type(myType) :: data1, data2

Set Variable Precision

Great deal in Fortran. Following example is the one, I always use in my code, please also see new possibility for Fortran 2008 here.

module precision
  implicit none
  ! explicit visibility declaration
  private
  ! self-documentation
  public :: int1,int2,int4,int8
  public :: real4,real8
  public :: ik, rk
  ! integer kinds
  integer, parameter :: int1 = selected_int_kind(1)
  integer, parameter :: int2 = selected_int_kind(2)
  integer, parameter :: int4 = selected_int_kind(8)
  integer, parameter :: int8 = selected_int_kind(10)
  ! floating point kinds
  integer, parameter :: real4 = selected_real_kind(6)
  integer, parameter :: real8 = selected_real_kind(15)
  integer, parameter :: real16 = selected_real_kind(32)
  ! generic kinds
  integer, parameter :: ik = int4   ! generic integer kind
  integer, parameter :: rk = real8 ! generic real kind
end module precision

Usage of the module above is demonstrated as an example in subprograms part.

Arrays

Only simple definition of arrays are given below for a brief introduction. Detailed use of arrays will be discovered later (like usage of the extent, how to use them with subroutines and functions, etc). Arrays requires;

rank which is the number of “indices”

shape which indicates number of elements in each dimension

extent array elements with start and end

Info

Maximum rank is 7 for Fortran90.
integer, parameter :: small = 4, big = 10
real, dimension(small, small) :: array1
integer, parameter :: start = -4, stop = 5
integer, dimension(start:stop) :: array2, array3

See Fortran90 best practices page again to see efficient to access Fortran arrays.

In many cases programmers does not know about the size of required arrays, allocatable, allocate and deallocate are here to help in these situations.

program allocate_arrays
implicit none
complex, pointer :: fft (:, :)   ! Complex array pointer

read*, m, n
allocate (fft(m, n)) 
   . . .
deallocate (fft)
end program allocate_arrays

Several intrinsic array-type functions are available for processing multi-dimensional arrays, like all, any, count, maxval, maxloc, sum, product, matmul and transpose. Many more can be seen here with examples.

Memory

Small reminder for memory usage. You can always make accurate estimations of your memory usage.

1 byte = 8 binary digits
1 Kb 1024 bytes  2^10 bytes
1 Mb 1024 Kb     2^20 bytes
1 Gb 1024 Mb     2^30 bytes