0

I'd like to know if a loop can be created, inside which I can call a subroutine in which there are arrays to be defined whose size varies as a function of loop variable. I tried as following, but got error "array bound is not scalar integer". How to solve this issue?

.
.
.
iloop: do i=5,24,0.5
jloop: do j=5,20 
call check(i,j,divlen,Machexit,final)
if (final.eq.1) exit iloop
enddo jloop
enddo iloop
.
.
end program

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Subroutine check(i,j,divlen,Machexit,final)
INTEGER, PARAMETER :: IVLpts=10 
real :: i,divlen,Machexit
integer :: final,j
integer :: exppts,intstrtpts,contourstrtpts,totalpts,P1,P2,P3,P4,P5,P6,P7
exppts=((j*IVLpts)+((j-1)*(IVLpts-1)))
P2=(exppts-IVLpts)
P3=(IVLpts-1)
P6=(exppts-P2)

call check2(IVLpts,i,j,divlen,Machexit,final,exppts,P2,P3,P6)

End subroutine check

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Subroutine check2(IVLpts,i,j,divlen,Machexit,final,exppts,P2,P3,P6)
Real, PARAMETER :: gamma=1.4d0,Mini=1.02d0
integer,allocatable :: expcontourpts(:),M(:),x(:),y(:)
real,allocatable :: AoverAstar(:),Mvariance(:)
allocate(expcontourpts(exppts))
allocate(M(exppts))
allocate(x(exppts))
allocate(y(exppts))
allocate(AoverAstar(P6))
allocate(Mvariance(P6))
.
.
.
End subroutine check2

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Sathish
  • 1
  • 2
  • 1
    Are these procedures in a host scope that has implicit none? If not there are a whole heap of things that will default to being REAL, some of which you are using as array indices... – IanH Feb 06 '14 at 08:59
  • thanks for the help...but, the host scope has an implicit none prescribed in it....dint include it in the snippet given here.... – Sathish Feb 06 '14 at 09:44
  • Sorry but, like IanH, I don't see the declaration of arguments of check2, in particular exppts and p6 : they are real values by default and cannot be used as array dimension. I also suspect that you did not put your subroutines within a module starting by IMPLICIT NONE. If they are outside any host, then they are independent and should have IMPLICIT NONE inside. – Francois Jacq Feb 06 '14 at 10:45
  • Which line generates the error message? – M. S. B. Feb 06 '14 at 14:22
  • it was basically the array definitions....since i missed to deallocate the allocated arrays...!!! so after adding the 'deallocation' part and some minor changes, the code is now running, though there are some glitches which it shows.... Thankyou all for all the discussions and help.... – Sathish Feb 07 '14 at 10:20
  • aside, but you should never use a real variable as aloop index. (doesnt that throw a warning?) – agentp Feb 07 '14 at 13:07

1 Answers1

1

Here is what I'm answering:

I'd like to know if a loop can be created, inside which I can call a subroutine in which there are arrays to be defined whose size varies as a function of loop variable.

The answer to this is "yes", but allocate() isn't the way I would do it. Just use one of the arguments as the array dimension. Here is an example that does a summation in a very inefficient way, but uses the method I'm describing:

program main
  implicit none
  integer :: i, j

  do i = 1,5
    call sum_values(i,j)
    write(*,*) j
  end do

end program main

subroutine sum_values(i,j)
  implicit none
  integer, intent(in) :: i
  integer, intent(out) :: j
  integer, dimension(i) :: an_array

  an_array(1:i) = 1
  j = sum(an_array(1:i))

end subroutine sum_values

Critical line is:

  integer, dimension(i) :: an_array

The dimension of the array was given in the argument list when it was called.

I see that you're using a different approach. That approach should still work, but I think the way I'm describing is a lot less error-prone and appropriate for the complexity of what you've asked for. Make sure to use "implicit none" statements, and declare in/out for all the arguments.

AlanSE
  • 2,597
  • 2
  • 29
  • 22
  • giving as dimension(i) gives error since i changes it's values everytime....thats why i had to go for allocation-deallocation of the arrays....which in now working, though there are some other minor glitches associated.... – Sathish Feb 07 '14 at 10:22
  • @Sathish The code I posted compiles and runs, so why would it not work in yours? The variable i should be declared before you use it in the array dimension, and it should have the intent(in) parameter. Perhaps your code lacked that, and that's why it didn't work. Being mixed in with other errors could make it more difficult, but both methods do work and are commonly used by other people. – AlanSE Feb 07 '14 at 13:03
  • @AlanSE I'm quite surprised this works. What compiler ?? – agentp Feb 07 '14 at 13:17
  • @george gfortran by default. I also test things on ifort and g95, but I haven't checked this. I know it will work on those compilers. This approach is very standard fortran. Let me know if the code I posted doesn't work on yours, but I'm quite confident it will. – AlanSE Feb 07 '14 at 13:19
  • ok, see here http://stackoverflow.com/questions/6617015/variable-size-arrays-in-fortran-without-allocate, and +1... Anybody know which fortran version introduced this sort of implicit allocation? – agentp Feb 07 '14 at 13:42
  • @george I've seen this sort of behaviour in `ifort` as far back as version 10, and perhaps even in Compaq Visual Fortran before that. – bdforbes Feb 07 '14 at 21:06
  • yeah, it wil work introducing in a subroutine with intent(in) condition, i think....i tried on it after removing the subroutine and calling directly inside the loop, which might be the reason for an error there... between, I'd like to know one thing...if am downloading, say a recent ubuntu 13.10 or so, will it have the compilers F90,gfortran and ifort by default or atleast any one of them? I am pretty new to linux based coding and would love to try them, since i happened to hear great words on their better performance.... – Sathish Feb 08 '14 at 06:06
  • @Sathish gfortran is part of the gcc package. I believe this comes bundled with Ubuntu. – AlanSE Feb 08 '14 at 13:25
  • @AlanSE thankyou, and i think other compilers like ifort can be manually installed without any clash with existing compilers, right? – Sathish Feb 10 '14 at 03:51