#include "cfx5ext.h"
dllexport(user_output)
      SUBROUTINE USER_OUTPUT (CZ, DZ, IZ, LZ, RZ)
C
C Routine called at end of run
C
C ======================================================================
C Preprocessor includes
C ======================================================================
C
#include "MMS.h"
#include "cfd_constants.h"
C
C ======================================================================
C Arguments
C ======================================================================
C      
      CHARACTER*(1) CZ(*)
      DOUBLE PRECISION DZ(*)
      INTEGER IZ(*)
      LOGICAL LZ(*)
      REAL RZ(*)
C
C ------------------------------
C        External routines
C ------------------------------
C 
      CHARACTER*15 CFROMR
      INTEGER LENACT
      EXTERNAL CFROMR, LENACT
C
C ======================================================================
C Local parameters
C ======================================================================
C
      CHARACTER*(*) ROUTIN
      PARAMETER (ROUTIN = 'USER_OUTPUT')
C
C ======================================================================
C Local variables
C ======================================================================
C
      CHARACTER*4 CRESLT
      CHARACTER*(MXDNAM) CDIR_GLOB, CDIR_ZONE, CDIR_PHYS, 
     &                   CZONE, CBCP, CPHASE
      CHARACTER*120 User_Domain_Name, User_BCP_Name, 
     &              User_Phase_Name, User_Var_Name
      INTEGER NZN,IZN, NBCP,IBCP
      REAL VAR
C
C
C ======================================================================
C Stack pointers
C ======================================================================
C
C ======================================================================
C Executable statements      
C ======================================================================
C
      CALL MESAGE( 'WRITE', ' ')
      CALL MESAGE( 'WRITE', '-----------------')
      CALL MESAGE( 'WRITE', 'Start USER_OUTPUT')
      CALL MESAGE( 'WRITE', '-----------------')
      CALL MESAGE( 'WRITE', ' ')
C
C---- Obtain global meshdata
C
      CDIR_GLOB = ' '
      CALL USER_GET_MESH_INFO (ROUTIN,'GET','STOP','LATEST',
     &              ' ',' ',CDIR_GLOB,CRESLT, CZ,DZ,IZ,LZ,RZ)
C
C---- Look up number of domains, and loop over domains
C
      CALL PEEKI ('/USER/'//CDIR_GLOB//'/NZN',IONE,NZN, 
     &            'STOP',CRESLT, IZ)
      IF (CRESLT .NE. 'GOOD') GOTO 999
C
      DO IZN = 1, NZN
C
C---- CZONE = solver domain name
C
        CALL PEEKCA ('/USER/'//CDIR_GLOB//'/CZONE',IZN,CZONE, 
     &               'STOP',CRESLT, CZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
C
C---- Convert to user domain name
C
        CALL CONVERT_NAME_S2U ('Domain',CZONE,' ',User_Domain_Name,
     &                         CRESLT, CZ,DZ,IZ,LZ,RZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
C
        CALL MESAGE( 'WRITE', ' ')
        CALL MESAGE( 'WRITE', '-----------------')
        CALL MESAGE( 'WRITE', 'Domain: '//User_Domain_Name)
        CALL MESAGE( 'WRITE', '-----------------')
        CALL MESAGE( 'WRITE', ' ')
C
C---- Get physics info.
C
        CDIR_PHYS = ' '
        CALL USER_GET_PHYS_INFO (ROUTIN,'GET','STOP','LATEST',
     &                CZONE,' ',CDIR_PHYS,CRESLT, CZ,DZ,IZ,LZ,RZ)
C
C---- CPHASE = solver phase name (assumes single phase)
C
        CALL PEEKCA ('/USER/'//CDIR_PHYS//'/CPHASE',IONE,CPHASE, 
     &               'STOP',CRESLT, CZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
C
C---- Convert to user phase name
C
        CALL CONVERT_NAME_S2U ('Phase',CPHASE,' ',User_Phase_Name,
     &                         CRESLT, CZ,DZ,IZ,LZ,RZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
C
C---- Set User Variable Name = User_Phase_Name.RTD
C---- (RTD = Residence Time Distribution).
C
        User_Var_Name = User_Phase_Name(1:LENACT(User_Phase_Name))
     &               // '.RTD'
C
        CALL MESAGE( 'WRITE', ' ')
        CALL MESAGE( 'WRITE', '-----------------')
        CALL MESAGE( 'WRITE', 'Variable: '//User_Var_Name)
        CALL MESAGE( 'WRITE', '-----------------')
        CALL MESAGE( 'WRITE', ' ')

C---- Obtain global values on domain
C
        CALL USER_GET_GVAR (User_Var_Name, User_Domain_Name,
     &                      'ave', CRESLT, VAR, CZ,DZ,IZ,LZ,RZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
        CALL MESAGE( 'WRITE', 'RDT Mean Value = '//CFROMR(VAR))
C
        CALL USER_GET_GVAR (User_Var_Name,User_Domain_Name,
     &                      'maxVal', CRESLT, VAR, CZ,DZ,IZ,LZ,RZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
        CALL MESAGE( 'WRITE', 'RDT Max Value = '//CFROMR(VAR))
C
C---- Obtain zonal meshdata
C
        CDIR_ZONE = ' '
        CALL USER_GET_MESH_INFO (ROUTIN,'GET','STOP','LATEST',
     &                           CZONE,' ',CDIR_ZONE,CRESLT,
     &                           CZ,DZ,IZ,LZ,RZ)
C
C---- Look up number of Boundary Condition Patches (BCP's), 
C---- and loop over BCP's.
C
        CALL PEEKI ('/USER/'//CDIR_ZONE//'/NBCP',IONE,NBCP, 
     &              'STOP',CRESLT, IZ)
        IF (CRESLT .NE. 'GOOD') GOTO 999
C
        DO IBCP = 1, NBCP
C
C---- CBCP = solver BCP name
C
          CALL PEEKCA ('/USER/'//CDIR_ZONE//'/CBCP',IBCP,CBCP, 
     &               'STOP',CRESLT, CZ)
          IF (CRESLT .NE. 'GOOD') GOTO 999
C
C---- Convert to user BCP name
C
          CALL CONVERT_NAME_S2U ('Bcp',CBCP,' ',User_BCP_Name,
     &                           CRESLT, CZ,DZ,IZ,LZ,RZ)
          IF (CRESLT .NE. 'GOOD') GOTO 999
C
          CALL MESAGE( 'WRITE', ' ')
          CALL MESAGE( 'WRITE', '-----------------')
          CALL MESAGE( 'WRITE', 'Boundary: '//User_BCP_Name)
          CALL MESAGE( 'WRITE', '-----------------')
          CALL MESAGE( 'WRITE', ' ')
C
C---- Obtain global values on BCP
C
          CALL USER_GET_GVAR (User_Var_Name,User_BCP_Name, 
     &                        'ave', CRESLT, VAR, CZ,DZ,IZ,LZ,RZ)
          IF (CRESLT .NE. 'GOOD') GOTO 999
          CALL MESAGE( 'WRITE', 'RDT Mean Value = '//CFROMR(VAR))
C
          CALL USER_GET_GVAR (User_Var_Name,User_BCP_Name,
     &                        'maxVal',CRESLT, VAR, CZ,DZ,IZ,LZ,RZ)
          IF (CRESLT .NE. 'GOOD') GOTO 999
          CALL MESAGE( 'WRITE', 'RDT Max Value = '//CFROMR(VAR))
C
C---- End Loop Over BCP's
C
        END DO
C
C---- End Loop Over Zones
C
      END DO
C
C---- Delete Mesh Data
C
      CALL USER_GET_MESH_INFO (ROUTIN,'DELETE','STOP','LATEST',
     &                         ' ',' ',CDIR_GLOB,CRESLT,
     &                         CZ,DZ,IZ,LZ,RZ)
C
999   CONTINUE
      IF (CRESLT .NE. 'GOOD') THEN
        CALL MESAGE( 'WRITE', 'User abort, CRESLT = '//CRESLT)
        CALL CFXSTP (ROUTIN)
      ENDIF
C
C
      END
