#include "cfx5ext.h"
dllexport(ac_on)
      SUBROUTINE AC_ON(NLOC,NRET,NARG,RET,ARGS,CRESLT,
     &                 CZ,DZ,IZ,LZ,RZ)
CC
CD Control an air conditioner by a thermometer reading
CC
CC --------------------
CC        Input
CC --------------------
CC
CI  NLOC     : Size of current locale
CI  NRET     : Number of components in result
CI  NARG     : Number of arguments in call
CI  ARGS()   : Argument values, dimensioned (NLOC,NRET)
CC
CC --------------------
CC      Modified
CC --------------------
CC
CC  Stacks possibly.
CC
CC --------------------
CC        Output
CC --------------------
CC
CO  RET()    : Return values, dimensioned (NLOC,NRET)
CO  CRESLT   : Return status, 'GOOD' for success
CC
CC --------------------
CC       Details
CC --------------------
CC
CC The status of the air conditioner is updated once per time step.
CC The return value is the air conditioner status (on=1, off=0).
CC User CEL functions are called on demand by the solver. Note that for
CC parallel runs, functions may not be called every coefficient loop.
CC
CC --------------------
CC Local variables & parameters
CC --------------------
CC
CC  TTHRSTAT    : Thermostat temperature (from input arguments)
CC  TSET        : Temperature set point  (from input arguments)
CC  TDELTA      : Temperature tolerance  (from input arguments)
CC  ATSTEP      : Accumulated time step  (from input arguments)
CC  ATSTEP_LAST : Previous ATSTEP value  (saved variable)
CC  VALUE       : Air conditioner status (saved variable)
CC
CC======================================================================
C
C ------------------------------
C     Preprocessor includes
C ------------------------------
C
#include "parallel_partitioning.h"
C
C ------------------------------
C        Global Parameters
C ------------------------------
C
C
C ------------------------------
C        Argument list
C ------------------------------
C
      INTEGER            NLOC,NARG,NRET
C
      CHARACTER*(4)      CRESLT
C
      REAL               ARGS(NLOC,NARG), RET(NLOC,NRET)
C
#include "cfd_memory_stacks.h"
C
C ------------------------------
C        External routines
C ------------------------------
C
C
C ------------------------------
C        Local Parameters
C ------------------------------
C
      CHARACTER*(*)      ROUTIN
      PARAMETER         (ROUTIN='AC_ON')
C
C ------------------------------
C        Local Variables
C ------------------------------
C
C      CHARACTER*80       CMESG
C
      INTEGER            ILOC, ATSTEP, ATSTEP_LAST
C
      REAL               TSET, TTHRSTAT, VALUE, TDELTA
C
C---- Saved variables
C
      SAVE               VALUE, ATSTEP_LAST
C
C ------------------------------
C        DATA statements
C ------------------------------
C
C-----initialize air conditioner to be off
C
      DATA VALUE /0.0/
C
C----- initialize the variable that keeps track of the last time step
C
      DATA ATSTEP_LAST /1000/
C
C=======================================================================
C
C ---------------------------
C    Executable Statements
C ---------------------------
C
C---- Initialise return value RET() to zero
C
      CALL SET_A_0( RET, NLOC*NRET )
C
C---- Set local variables from input arguments
C
      TTHRSTAT = ARGS(1,1)
      TSET     = ARGS(1,2)
      TDELTA   = ARGS(1,3)
      ATSTEP   = NINT(ARGS(1,4))
C
C---- New time step: Re-assess the air conditioner status
C
      IF (ATSTEP .NE. ATSTEP_LAST) THEN
C
C---- Turn on air conditioner
C
        IF (TTHRSTAT .GE. TSET) THEN
          VALUE = 1.0
C
C---- Turn off air conditioner
C
        ELSE IF (TTHRSTAT .LE. ( TSET - TDELTA ) ) THEN
          VALUE = 0.0
        ENDIF
      ENDIF
C
      ATSTEP_LAST = ATSTEP
C
C---- Set return value
C
      DO ILOC = 1, NLOC
        RET(ILOC,1) = VALUE
      ENDDO
C
C---- Uncomment for further output to the solver output file
C
C      IF (RUNMOD.EQ.__sequential__ .OR. PARMOD.EQ.__master__ ) THEN
C        CMESG = 'Air Conditioner Control'
C        CALL Out_Section_Header (CMESG, '-')
C        WRITE(CMESG,*)' Time step = ',ATSTEP
C        CALL WRTTXT(CMESG,1)
C        WRITE(CMESG,*)'Thermostat Temperature  = ',TTHRSTAT
C        CALL WRTTXT(CMESG,1)
C        WRITE(CMESG,*)'Return Value = ', VALUE
C        CALL WRTTXT(CMESG,1)
C      ENDIF
C
C---- Set success flag.
C
      CRESLT = 'GOOD'
C
      END
