I am reading from a file that contains a value, T, which will be used to initialize several arrays that will take T as the first dimension with allocate later. i.e.
subroutine read_file(T,F,array,A,B,C,...)
...
real, intent(out) :: T,F
real, intent(in) :: A,B,C
real, intent(out) :: array
...
read(1,*) T
...
read(1,*) F
...
read(1,*) array(1), array(5), array(6)
read(1,*) array(2), array(4)
read(1,*) array(3)
...
if (F.eq.1) then
array(1) = A(1)
array(2) = B(2)
array(3) = C(3)
endif
...
program main
...
do I=1,nproc
...
do J=1,nsteps
...
call read_file(T,F,array,A,B,C,...)
...
enddo
...
if (F.eq.1.and.etc.) then
...
allocate(A(T,3))
allocate(B(T,6))
allocate(C(T))
...
endif
...
enddo
The read statement is contained in a read_file subroutine in a module in modules.for. The allocate statements are in main.for where read_file is also called.
Subroutine read_file also reads many other things and it is called many times over the course of 1 execution of the code where some files may have T be zero.
I need to pass in A, B, and C into read_file. Depending on the condition of a flag, F, also being read in read_file, the values in A, B, and C need to be assigned to array(1,6) that would otherwise be read directly from the file.
So I guess my question is: How do I pass in arrays that may not have been allocated in size? I have written checks in the code to make sure A, B, and C will not be actually used unless they went through allocation with a known size given as user input T, but the compiler has been giving me issues.
I tried compile the code and intel compiler first returned the error saying the types for A,B,C are not declared in read_file, so I declared them using T and real :: A(T,3)
in read_file. Then it said since T is a intent(out)
, it cannot be used to give dimension to A, B, and C since they are intent(in)
. So I removed the intent(out)
from T (as in it is just real :: T
now).
And now, the error says:
If the actual argument is scalar, the dummy argument shall be scalar unless the actual argument is of type character or is an element of an array that is not assumed shape, pointer, or polymorphic.
I edited my question to provide more code and clarify my question.
Thanks to the people who answered and commented, I now know that I can declare a variable as allocatable in the subroutine, which should solve my problem.
Thanks!
Jesse
A
,B
, andC
in your subroutine. Change the interface toreal, intent(out), allocatable :: a(:,:), b(:,:), c(:)
, then in your subroutine after readingT
allocate the variables. You get the possible side benefit thatintent(out)
will automatically deallocateA
,B
, andC
. – evetsreal :: A(T,3)
". Where did you declare them? Are you writing a procedure inside read_file that receives the arrays A, B, C as arguments? If so, instead of passing them as explicit-shape (A(T,3)
), can't you just pass them as assumed-shape (A(:,:)
)? – Rodrigo Rodrigues