Search code examples
fortran90subroutinederived-types

Fortran 90, how to use array defined in derived type in a subroutine


I defined a derived type as follows:

  TYPE CLST_MEAN
     REAL(8), ALLOCATABLE :: OMX(:,:), OMZ(:,:)
     REAL(8), ALLOCATABLE :: U(:,:), W(:,:)
     REAL(8), ALLOCATABLE :: YO(:,:), ZO(:,:)
     REAL(8), ALLOCATABLE :: XU(:,:), ZU(:,:)
     INTEGER :: NUM
  END TYPE Clst_Mean

In the main code, I defined an array and input it in a subroutine as

   TYPE(CLST_MEAN), ALLOCATABLE :: MEAN(:)
   ALLOCATE(MEAN(NCL))
   DO I = 1, NCL
      ALLOCATE(MEAN(I)%OMX(NY,NZ))
      ALLOCATE(MEAN(I)%OMZ(NY,NZ))
      ALLOCATE(MEAN(I)%YO(NY,NZ))
      ALLOCATE(MEAN(I)%ZO(NY,NZ))
      ALLOCATE(MEAN(I)%U(NX,NZ))
      ALLOCATE(MEAN(I)%W(NX,NZ))
      ALLOCATE(MEAN(I)%XU(NX,NZ))
      ALLOCATE(MEAN(I)%ZU(NX,NZ))
   END DO
   CALL K_MEAN(MEAN,SMP)

In the subroutine,

SUBROUTINE K_MEAN(CL_MEAN,SMP)
  USE DATATYPE, ONLY : CLST_MEAN, SAMPLE
  IMPLICIT NONE
  TYPE(CLST_MEAN), DIMENSION(:), INTENT(OUT) :: CL_MEAN
  ....
  write(*,*) size(cl_mean), SIZE(CL_MEAN(1)%OMX)

The output for size(cl_mean) is correct. But the output of size(cl_mean(1)%omx) is 1. That means that the compiler considers cl_mean(1)%omx is a variable not an array.

How can I access the array? Thank you.


Solution

  • I think using intent(out) causes the allocatable array in the derived datatype to be deallocated. Using intent(inout) fixed the problem for me using gfortran 4.7.