Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fortran, passing allocatable arrays to a subroutine with right bounds

I need in a program to pass some allocatable arrays to subroutines, and i need to know if the way I do it are in the standard or not.

If you know where I can search for the standard of fortran, Tell me please.

Here is a little code that will better explain than words

program test

use modt99

implicit none

real(pr), dimension(:), allocatable :: vx

allocate(vx(-1:6))
vx=(/666,214,558,332,-521,-999,120,55/)
call test3(vx,vx,vx)
deallocate(vx)

end program test

with the module modt99

module modt99

contains
subroutine test3(v1,v2,v3)
  real(pr), dimension(:), intent(in) :: v1
  real(pr), dimension(0:), intent(in) :: v2
  real(pr), dimension(:), allocatable, intent(in) :: v3

  print*,'================================'
  print*,v1(1:3)
  print*,'================================'
  print*,v2(1:3)
  print*,'================================'
  print*,v3(1:3)
  print*,'================================'

end subroutine test3


end module modt99

on screen, I get

 ================================
   666.000000000000        214.000000000000        558.000000000000     
 ================================
   214.000000000000        558.000000000000        332.000000000000     
 ================================
   558.000000000000        332.000000000000       -521.000000000000     
 ================================

So are the three ways of dummy arguments in subroutine test3 legal (in what version of fortran, 90, 95, 2003?) and are their behavior normal?

like image 851
user2910558 Avatar asked Oct 23 '13 09:10

user2910558


1 Answers

The first version passes the array slice to the subroutine. Note that boundary information are not passed along in this way, arrays are assumed to start at 1 and go to size(array).

The second way is just like the first one, but you manually set the lower boundary to 0, that's why printing v3(1:3) gives you the values with an offset of 1.

The third way passes all array information to the subroutine (including boundaries), hence the "correct" indexing. Passing allocatable arrays was introduced with Fortran 2003.

Apart from the fact that you have an aliasing issue (passing the same variable to three different dummy arguments), all three versions are legal.

You can find all documents of the standards here.

Especially, take a look at the Fortran 2003 Standard, Ch. 5.1.2.5 DIMENSION attribute to see the differences between assumed shape and deferred shape arrays in dummy arguments.

like image 55
Alexander Vogt Avatar answered Oct 19 '22 20:10

Alexander Vogt