C MANAGER:

      MODULE MANAGER
      IMPLICIT NONE

      integer, public :: NUMITEMSALLOCATED

      TYPE a_itemdata
         double precision, public, allocatable :: P(:,:);  
         integer,          public, allocatable :: T(:,:);
         double precision, public, allocatable :: TN(:,:);
         integer,          public, allocatable :: V(:,:);     
         integer,          public, allocatable :: F(:,:);     
         integer,          public, allocatable :: H(:,:);      
         integer, public :: NUMP;                                
         integer, public :: NUMT;                               
         double precision, public, allocatable :: EL(:);   
         double precision, public, allocatable :: EU(:,:);     
      END TYPE a_itemdata

      type ( a_itemdata ), dimension(:), public, allocatable :: ITEMLIST
      type ( a_itemdata ), dimension(:), public, allocatable :: ITEMLIST_TMP

      END MODULE MANAGER



C NEWITEM:

      integer function NEWITEM ( NUMP, NUMT )
      USE MANAGER
      IMPLICIT NONE

      INTEGER NUMP, NUMT
#include <common.blk>
      integer errStat
      type ( a_itemdata ), pointer :: newitemdata

      NEWITEM = 0

C     Create a new item object.

      allocate ( newitemdata, stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif

C     Initialize data for this contact object.

      newitemdata%NUMP  = NUMP
      newitemdata%NUMT = NUMT

C     Allocate arrays for this item which can be sized at this time.

      allocate ( newitemdata%P(NPP,NUMP), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%P(1:NPP,1:NUMP) = 0D0

      allocate ( newitemdata%T(MPT,NUMT), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%T(1:MPT,1:NUMT) = 0

      allocate ( newitemdata%TN(NPT,NUMT), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%TN(1:NPT,1:NUMT) = 0D0

      allocate ( newitemdata%V(MPV,NUMP), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%V(1:MPV,1:NUMP) = 0

      allocate ( newitemdata%F(MPF,NUMT), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%F(1:MPF,1:NUMT) = 0

      allocate ( newitemdata%H(MPH,3*NUMT), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%H(1:MPH,1:3*NUMT) = 0

      allocate ( newitemdata%EU(NPEU,3*NUMT), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif
      newitemdata%EU(1:NPEU,1:3*NUMT) = 0D0

C     The EL array is not allocated at this time.

C     T list needs to be resized.

      IF ( NUMITEMSALLOCATED .GT. 0 ) THEN

C        Create a temporary holder for the pointers to the previously allocated data.

         ALLOCATE ( ITEMLIST_TMP(NUMITEMSALLOCATED), stat=errStat )
         if ( errStat .NE. 0 ) then
            RETURN
         endif

C        Copy the current pointers into the temporary.

         ITEMLIST_TMP(1:NUMITEMSALLOCATED) = ITEMLIST(1:NUMITEMSALLOCATED)

      ENDIF

C     Destroy the main T list.

      if ( allocated(ITEMLIST) ) then
         deallocate(ITEMLIST)
      endif

C     Create new ITEMLIST pointer array.

      ALLOCATE ( ITEMLIST(NUMITEMSALLOCATED+1), stat=errStat )
      if ( errStat .NE. 0 ) then
         RETURN
      endif

C     Assign null pointers to the new elements of the array.

      IF ( NUMITEMSALLOCATED .GT. 0 ) THEN
         ITEMLIST(1:NUMITEMSALLOCATED) = ITEMLIST_TMP(1:NUMITEMSALLOCATED)
         if ( allocated(ITEMLIST_TMP) ) then
            deallocate(ITEMLIST_TMP)
         endif
      ENDIF

      ITEMLIST(NUMITEMSALLOCATED+1) = newitemdata

      NUMITEMSALLOCATED = NUMITEMSALLOCATED + 1

      NEWITEM = NUMITEMSALLOCATED ! return value is integer index

      RETURN
      END function NEWITEM




C FREEITEMS:  

      SUBROUTINE FREEITEMS ( )
      USE MANAGER
      IMPLICIT NONE
      INTEGER I, J, ERRSTAT

      DO I = 1, NUMITEMSALLOCATED

         IF ( ALLOCATED(ITEMLIST(I)%P) ) THEN
            DEALLOCATE ( ITEMLIST(I)%P)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%T) ) THEN
            DEALLOCATE ( ITEMLIST(I)%T)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%TN) ) THEN
            DEALLOCATE ( ITEMLIST(I)%TN)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%V) ) THEN
            DEALLOCATE ( ITEMLIST(I)%V)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%F) ) THEN
            DEALLOCATE ( ITEMLIST(I)%F)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%H) ) THEN
            DEALLOCATE ( ITEMLIST(I)%H)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%EU) ) THEN
            DEALLOCATE ( ITEMLIST(I)%EU)
         endif

         IF ( ALLOCATED(ITEMLIST(I)%EL) ) THEN
            DEALLOCATE ( ITEMLIST(I)%EL)
         endif

      ENDDO

      IF ( ALLOCATED ( ITEMLIST ) ) THEN
         deallocate ( ITEMLIST )
      ENDIF

      RETURN
      END SUBROUTINE FREEITEMS



      BLOCK DATA BDATA ! Specify leading dimensions for item arrays.
#include <common.blk>

      DATA MPT  /  3 / ! T  integer array.
      DATA NPT  /  7 / ! T  real    array.
      DATA MPV  /  5 / ! V  integer array.
      DATA MPF  /  5 / ! F  integer array.
      DATA MPH  /  9 / ! H  integer array.
      DATA NPP  / 18 / ! P  real    array.
      DATA NPEU /  3 / ! EU real    array.

      END BLOCK DATA BDATA
