Date Modified Category Fortran Tags Fortran

Fortran program

Already showed that, but here once more to remember.

Example program 1:

program myProgram
  use myModule
  implicit none
  integer :: a,b
  character(len=*) :: name
end program myProgram

Make an example of interpolation use bash, after go to the modules

Compile your first Fortran PROGRAM Again

gfortran myfile.f90

gfortran myfile.f90 -o program.exe

If you need to run your on a machine which gfortran is not installed

gfortran -static myfile.f -o program.exe

Compiling programs in more than one file

gfortran -c aux.f

gfortran -c diag.f

gfortran flow.f aux.o diag.o -o flow.exe

Compiler Command Options

Compilers have variety of options dedicated for various purposes like dialect control, preprocessing, error/warning handling and debugging. GNU Fortran compiler options can be found here, for Intel compiler please click here.

Fortran module

Modules are one of the structural levels avaliable in Fortran program design:

Program >> Modules >> Procedures >> Statements

Program and statements already discussed in earlier parts.

module myModule
  ! Explicit is better than implicit, mostly
  use mySubModule                 ! makes you think
  use myIO only: write, read      ! nicer way
  implitict none

! Makes all subprograms private
private 

! Turn necessary subprograms public explicitly
public mySubroutine

contains

  subroutine mySubroutine
    implicit none
    . 
    .
  end subroutine mySubroutine

  function myFunction
    implicit none
    .
    .
  end function myFunction

end module

Fortran subroutine

subroutine mySubroutine
  implicit none
  .
  .
end subroutine mySubroutine

Fortran function

function myFunction
  implicit none
  .
  .
end function myFunction

How to pass arrays to subroutines/functions?

It is generally recommend to use assumed-shape as your default way to pass arrays. It will not copy the array.

Static Storage Assumption

save statements makes the local variables persistent, or using -fno-automatic option druing compilation treats each program unit (except those marked as recursive) as if the save statement were specified for every local variable and array referenced in it.

subroutine mySubroutine(array1, array2)
  implicit none
  real(8), intent(in)  : array1(:)
  real(8), intent(out) : array2(:,:)

Using explicit-shape of the arrays might be required or more handy in some cases.

For example;

! Subroutine using assumed-shape
subroutine mySubroutine1(x)
  implicit none
  write(*,*) lbound(x)

! Subroutine using explicit-shapee
subroutine mySubroutine2(x, n)
  implicit none
  real(8), intent(in) :: x(n(1):n(2))
  write(*,*) lbound(x)

Passing Un-allocated Arrays

An allocatable array cannot be passed to a before its allocation. But this can be done with a pointer array.