SUBROUTINE TYPE225 (TIME,XIN,OUT,T,DTDT,PAR,INFO,ICNTRL,*) 
!************************************************************************
! Object: Power Tower Logic (Single stratified tank)
! Simulation Studio Model: Type225
! 
! Author: Michael J. Wagner
! Editor: Michael J. Wagner
! Date:	 August 17, 2008 last modified: Nov 24, 2009

! COPYRIGHT 2009 NATIONAL RENEWABLE ENERGY LABORATORY


! Parameters
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Output Units  |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1| TSHOURS                        | Number of full-load thermal storage hours                       | hours          | hours
!  2| NUMTOU                         | Number of time-of-use periods                                   | none           | none
! c3| TSLOGIC                        | Dispatch logic for time of use period                           | none           | none
!c21| FFRAC                          | Fossil-fill fraction control                                    | none           | none
! 27| Design_HTF_T_Hot               | Design HTF hot temperature from the tower                       | C              | C
! 28| Design_HTF_T_cold              | Design HTF cold temperature from the power cycle                | C              | C

! Inputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Output Units  |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|                                |                                                                 |                |  
 
! Outputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Output Units  |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|                                |                                                                 |                |  

!************************************************************************
!    TRNSYS acess functions (allow to acess TIME etc.) 
USE TrnsysConstants
USE TrnsysFunctions

!-----------------------------------------------------------------------------------------------------------------------
!    REQUIRED BY THE MULTI-DLL VERSION OF TRNSYS
!DEC$ATTRIBUTES DLLEXPORT :: TYPE225				!SET THE CORRECT TYPE NUMBER HERE
!-----------------------------------------------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------------------------------------------
!    TRNSYS DECLARATIONS
IMPLICIT NONE			!REQUIRES THE USER TO DEFINE ALL VARIABLES BEFORE USING THEM

DOUBLE PRECISION XIN	!THE ARRAY FROM WHICH THE INPUTS TO THIS TYPE WILL BE RETRIEVED
DOUBLE PRECISION OUT	!THE ARRAY WHICH WILL BE USED TO STORE THE OUTPUTS FROM THIS TYPE
DOUBLE PRECISION TIME	!THE CURRENT SIMULATION TIME - YOU MAY USE THIS VARIABLE BUT DO NOT SET IT!
DOUBLE PRECISION PAR	!THE ARRAY FROM WHICH THE PARAMETERS FOR THIS TYPE WILL BE RETRIEVED
DOUBLE PRECISION STORED !THE STORAGE ARRAY FOR HOLDING VARIABLES FROM TIMESTEP TO TIMESTEP
DOUBLE PRECISION T		!AN ARRAY CONTAINING THE RESULTS FROM THE DIFFERENTIAL EQUATION SOLVER
DOUBLE PRECISION DTDT	!AN ARRAY CONTAINING THE DERIVATIVES TO BE PASSED TO THE DIFF.EQ. SOLVER
INTEGER*4 INFO(15)		!THE INFO ARRAY STORES AND PASSES VALUABLE INFORMATION TO AND FROM THIS TYPE
INTEGER*4 NP,NI,NOUT,ND	!VARIABLES FOR THE MAXIMUM NUMBER OF PARAMETERS,INPUTS,OUTPUTS AND DERIVATIVES
INTEGER*4 NPAR,NIN,NDER	!VARIABLES FOR THE CORRECT NUMBER OF PARAMETERS,INPUTS,OUTPUTS AND DERIVATIVES
INTEGER*4 IUNIT,ITYPE	!THE UNIT NUMBER AND TYPE NUMBER FOR THIS COMPONENT
INTEGER*4 ICNTRL		!AN ARRAY FOR HOLDING VALUES OF CONTROL FUNCTIONS WITH THE NEW SOLVER
INTEGER*4 NSTORED		!THE NUMBER OF VARIABLES THAT WILL BE PASSED INTO AND OUT OF STORAGE
CHARACTER*3 OCHECK		!AN ARRAY TO BE FILLED WITH THE CORRECT VARIABLE TYPES FOR THE OUTPUTS
CHARACTER*3 YCHECK		!AN ARRAY TO BE FILLED WITH THE CORRECT VARIABLE TYPES FOR THE INPUTS
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    USER DECLARATIONS - SET THE MAXIMUM NUMBER OF PARAMETERS (NP), INPUTS (NI),
!    OUTPUTS (NOUT), AND DERIVATIVES (ND) THAT MAY BE SUPPLIED FOR THIS TYPE
PARAMETER (NP=28,NI=21,NOUT=8,ND=0,NSTORED=5)
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    REQUIRED TRNSYS DIMENSIONS
DIMENSION XIN(NI),OUT(NOUT),PAR(NP),YCHECK(NI),OCHECK(NOUT),STORED(NSTORED),T(ND),DTDT(ND)
!      INTEGER NITEMS
!-----------------------------------------------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------------------------------------------
!    ADD DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES HERE

!    PARAMETERS
DOUBLE PRECISION NUMTOU, TSHOURS
DOUBLE PRECISION TSLOGIC_A(6),TSLOGIC_B(6),TSLOGIC_C(6),TSLOGIC(18), FFRAC(6)

!    INPUTS
DOUBLE PRECISION Hour_of_the_day, Start_hour,&
                 End_hour, zenith, Storage_hot_outlet_temp, Min_temp_to_load, &
                 Load_flow_demand, Storage_cold_outlet_temp,&
                 Max_temp_to_heat_source, Flow_from_heat_source, &
                 Temp_from_heat_source, is_hybrid, t_standby, day_shutdown, &
                 m_dot_max, TOUPeriod, DNI_hrly, total_tank_vol, HTF_spcheat,&
                 HTF_density, TES_energy_change, Q_TES_ini, P_ref, TES_T_ave,&
                 Design_HTF_T_Hot,Design_HTF_T_Cold 

!     Local variables
DOUBLE PRECISION Stored_mode, Stored_hour, Hour_to_store, t_standby_remain,timestep, &
                 DNI, TES_charge, qq
INTEGER i

!     OUTPUTS
DOUBLE PRECISION Flow_to_power_cycle, Flow_to_heat_source,&
                 Field_control, hybrid_control, Pump_on, standby_control, Mode,&
                 demand_var_control  

!-----------------------------------------------------------------------------------------------------------------------
! READ IN THE VALUES OF THE PARAMETERS IN SEQUENTIAL ORDER
TSHOURS=PAR(1)
NUMTOU=PAR(2)
!Initialize TSLOGIC 
TSLOGIC=0.; TSLOGIC_A=0.; TSLOGIC_B=0.; TSLOGIC_C=0.;
TSLOGIC(1:18)=PAR(3:20)
FFRAC(1:6)=PAR(21:26)

do i=1,int(NUMTOU)
    TSLOGIC_A(i)=TSLOGIC(1+(i-1)*3)
    TSLOGIC_B(i)=TSLOGIC(2+(i-1)*3)
    TSLOGIC_C(i)=TSLOGIC(3+(i-1)*3)
enddo
Design_HTF_T_Hot=PAR(27)
Design_HTF_T_cold=PAR(28)

!-----------------------------------------------------------------------------------------------------------------------
! RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER

Hour_of_the_day = XIN(1)
Start_hour = XIN(2)
End_hour = XIN(3)
zenith=XIN(4)
Storage_hot_outlet_temp = XIN(5)
Min_temp_to_load = XIN(6)
Load_flow_demand = XIN(7)
Storage_cold_outlet_temp = XIN(8)
Max_temp_to_heat_source = XIN(9)
Flow_from_heat_source = XIN(10)
Temp_from_heat_source = XIN(11)
t_standby = XIN(12)
m_dot_max = XIN(13)
TOUPeriod = XIN(14)
DNI_hrly = XIN(15); DNI=DNI_hrly/3.6;
total_tank_vol = XIN(16)
HTF_spcheat = XIN(17) !kJ/kg-K
HTF_density = XIN(18) !kg/m^3
TES_energy_change = XIN(19) !kJ
P_ref = XIN(20) !kW
TES_T_ave = XIN(21)
    IUNIT=INFO(1)
    ITYPE=INFO(2)
	

!-----------------------------------------------------------------------------------------------------------------------
!    SET THE VERSION INFORMATION FOR TRNSYS
IF(INFO(7).EQ.-2) THEN
    INFO(12)=16
    RETURN 1
ENDIF
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    DO ALL THE VERY LAST CALL OF THE SIMULATION MANIPULATIONS HERE
IF (INFO(8).EQ.-1) THEN
    RETURN 1
ENDIF
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    PERFORM ANY 'AFTER-ITERATION' MANIPULATIONS THAT ARE REQUIRED HERE
!    e.g. save variables to storage array for the next timestep
IF (INFO(13).GT.0) THEN
    STORED(3)=t_standby_remain
    STORED(4)=day_shutdown
    STORED(5)=Q_TES_ini
    CALL setStorageVars(STORED,NSTORED,INFO)
    RETURN 1
ENDIF
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    DO ALL THE VERY FIRST CALL OF THE SIMULATION MANIPULATIONS HERE
IF (INFO(7).EQ.-1) THEN

    ! SET SOME INFO ARRAY VARIABLES TO TELL THE TRNSYS ENGINE HOW THIS TYPE IS TO WORK
    INFO(6)=NOUT				
    INFO(9)=1				
    INFO(10)=0	!STORAGE FOR VERSION 16 HAS BEEN CHANGED				

    ! SET THE REQUIRED NUMBER OF INPUTS, PARAMETERS AND DERIVATIVES THAT THE USER SHOULD SUPPLY IN THE INPUT FILE
    ! IN SOME CASES, THE NUMBER OF VARIABLES MAY DEPEND ON THE VALUE OF PARAMETERS TO THIS MODEL....
    NIN=NI
    NPAR=NP
    NDER=ND

    ! CALL THE TYPE CHECK SUBROUTINE TO COMPARE WHAT THIS COMPONENT REQUIRES TO WHAT IS SUPPLIED IN 
    ! THE TRNSYS INPUT FILE
    CALL TYPECK(1,INFO,NIN,NPAR,NDER)

    ! SET THE NUMBER OF STORAGE SPOTS NEEDED FOR THIS COMPONENT
    CALL setStorageSize(NSTORED,INFO)

    ! initialize the value of the day_shutdown variable
    day_shutdown=0
    ! RETURN TO THE CALLING PROGRAM
    RETURN 1

ENDIF
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    DO ALL OF THE INITIAL TIMESTEP MANIPULATIONS HERE - THERE ARE NO ITERATIONS AT THE INTIAL TIME
IF (TIME .LT. (getSimulationStartTime() + getSimulationTimeStep()/2.D0)) THEN

    ! SET THE UNIT NUMBER FOR FUTURE CALLS
    IUNIT=INFO(1)
    ITYPE=INFO(2)
    ! initialize counting variable
    qq=0.

    ! PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL VALUES OF THE OUTPUTS HERE
    ! Flow_to_power_cycle
    OUT(1)=0.
    ! Flow_to_heat_source
    OUT(2)=0.
    ! Field_control
    OUT(3)=1.
    ! Supplemental heat source control
    OUT(4)=0.			
    ! Pump_on
    OUT(5)=0.
    !  Standby control
    OUT(6)=1.			
    ! Mode
    OUT(7)=0
    !   Demand variable controller (for Type224)
    OUT(8)=P_ref

    ! PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL STORAGE VARIABLES HERE
    t_standby_remain = t_standby
    STORED(1)=0.  !mode
    STORED(2)=0.  !initial hour to store
    STORED(3)=t_standby_remain   !The initial standby time period
    STORED(4)=day_shutdown  !is the cycle off for the day
    STORED(5)=0.  !The initial storage thermal energy
    ! PUT THE STORED ARRAY IN THE GLOBAL STORED ARRAY
    CALL setStorageVars(STORED,NSTORED,INFO)

    ! RETURN TO THE CALLING PROGRAM
    RETURN 1

ENDIF
!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!    *** ITS AN ITERATIVE CALL TO THIS COMPONENT ***
!-----------------------------------------------------------------------------------------------------------------------

	    
!-----------------------------------------------------------------------------------------------------------------------
!    RETRIEVE THE VALUES IN THE STORAGE ARRAY FOR THIS ITERATION
CALL getStorageVars(STORED,NSTORED,INFO)
    stored_mode=STORED(1)
    stored_hour=STORED(2)
    t_standby_remain = STORED(3)
    day_shutdown=STORED(4)
    Q_TES_ini=STORED(5)

!-----------------------------------------------------------------------------------------------------------------------
!    *** PERFORM ALL THE CALCULATION HERE FOR THIS MODEL. ***
!-----------------------------------------------------------------------------------------------------------------------

timestep=getSimulationTimeStep()

is_hybrid=0

!     Calculate the intital storage energy
if (qq.eq.0.) then
    qq=qq+1
    Q_TES_ini=(TES_T_ave-Design_HTF_T_Cold)*HTF_spcheat*HTF_density*total_tank_vol
endif

!     Calculate the tank charge level, between 0 and 1
TES_charge = (Q_TES_ini + TES_energy_change)/&
            (HTF_spcheat*HTF_density*total_tank_vol*(Design_HTF_T_Hot - Design_HTF_T_Cold)) 

!     OUTPUTS
call Type225Logic(& !Inputs
            Hour_of_the_day, Start_hour,End_hour, zenith, Storage_hot_outlet_temp, Min_temp_to_load, &
            Load_flow_demand, Storage_cold_outlet_temp,Max_temp_to_heat_source, Flow_from_heat_source, &
            Temp_from_heat_source, is_hybrid, t_standby, m_dot_max,Stored_mode, Stored_hour, &
            t_standby_remain,timestep,TSLOGIC_A, TSLOGIC_B, TSLOGIC_C, FFRAC, TOUPeriod,DNI,TES_charge,&
            P_ref, Flow_to_power_cycle, Flow_to_heat_source,Field_control, hybrid_control, Pump_on, &
            standby_control, Mode, Hour_to_store, TIME, day_shutdown, demand_var_control)

continue

!-----------------------------------------------------------------------------------------------------------------------

!-----------------------------------------------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------------------------------------------
!    SET THE STORAGE ARRAY AT THE END OF THIS ITERATION IF NECESSARY
!     The (mode) and (hour_to_store) are used for convergence, meaning they are called 
!     ..and set during each iteration. The third parameter is not called until after convergence.
STORED(1)=Mode
STORED(2)=Hour_to_store
STORED(4)=day_shutdown
STORED(5)=Q_TES_ini
CALL setStorageVars(STORED,NSTORED,INFO)
!-----------------------------------------------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------------------------------------------
!    REPORT ANY PROBLEMS THAT HAVE BEEN FOUND USING CALLS LIKE THIS:
!      CALL MESSAGES(-1,'put your message here','MESSAGE',IUNIT,ITYPE)
!      CALL MESSAGES(-1,'put your message here','WARNING',IUNIT,ITYPE)
!      CALL MESSAGES(-1,'put your message here','SEVERE',IUNIT,ITYPE)
!      CALL MESSAGES(-1,'put your message here','FATAL',IUNIT,ITYPE)
!-----------------------------------------------------------------------------------------------------------------------
!-----------------------------------------------------------------------------------------------------------------------
!    SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT

! Flow_to_power_cycle
OUT(1)=Flow_to_power_cycle
! Flow_to_heat_source
OUT(2)=Flow_to_heat_source
! Field_control
OUT(3)=Field_control
! Supplemental heat source control
OUT(4)=hybrid_control			
! Pump_on
OUT(5)=Pump_on
! Standby control
OUT(6)=standby_control			
! Mode
OUT(7)=Mode
! Demand variable control
OUT(8)=demand_var_control

!-----------------------------------------------------------------------------------------------------------------------
!    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
RETURN 1
END
!-----------------------------------------------------------------------------------------------------------------------


!********************************************************************************************************

subroutine Type225Logic(&  !Inputs
            Hour_of_the_day, Start_hour, End_hour, zenith, Storage_hot_outlet_temp, Min_temp_to_load, &
            Load_flow_demand, Storage_cold_outlet_temp, Max_temp_to_heat_source, Flow_from_heat_source, &
            Temp_from_heat_source, is_hybrid, t_standby, m_dot_max, Stored_mode, Stored_hour, &
            t_standby_remain,timestep,TSLOGIC_A, TSLOGIC_B, TSLOGIC_C, FFRAC, TOUPeriod,DNI,TES_charge,&
            P_ref,Flow_to_power_cycle, Flow_to_heat_source, Field_control, hybrid_control, Pump_on,  &
            standby_control, Mode , Hour_to_store, TIME,day_shutdown,demand_var_control)

implicit none

! This subroutine provides the logic for the power tower storage (single tank model) and power cycle.
! (TYPE 225)

!Declare input variables
double precision::  Hour_of_the_day, Start_hour, End_hour, zenith, Storage_hot_outlet_temp, Min_temp_to_load, &
                    Load_flow_demand, Storage_cold_outlet_temp, Max_temp_to_heat_source, Flow_from_heat_source, &
                    Temp_from_heat_source, is_hybrid, t_standby, m_dot_max, Stored_mode, Stored_hour, TIME,&
                    TSLOGIC_A(6), TSLOGIC_B(6), TSLOGIC_C(6), TOUPeriod,DNI,TES_charge,P_ref, TSELECT(6), FFRAC(6), &
                    t_standby_remain, timestep, day_shutdown, derate, cycle_pl_control

double precision,intent(out)::  Flow_to_power_cycle, Flow_to_heat_source, Field_control, hybrid_control, Pump_on,  &
                                standby_control, Mode, Hour_to_store, demand_var_control

!___Define mode selection logic___
!do a check to make sure the cycle hasn't been shut down for the day
!if ((day_shutdown.eq.1).and.(hour_of_the_day.eq.0.)) then
    day_shutdown=0
!endif
!if (day_shutdown.eq.1) then
!    Mode=4.
!    goto 100
!endif
!do a check to make sure of convergence
Hour_to_store = Hour_of_the_day
if (Hour_to_store.eq.Stored_hour) then 
    Mode = Stored_mode
    goto 100
else
    derate=0.97
endif

if(DNI.gt.150) then   !The solar resource is available above 150 W/m^2
    TSELECT=TSLOGIC_B
else                  !Use the dispatch for no solar resource
    TSELECT=TSLOGIC_A
endif

if (TES_charge.ge.TSELECT(int(TOUperiod))) then
    if (Storage_hot_outlet_temp.gt.Min_temp_to_load) then       !Is the power cycle temp requirement met?
        if(Storage_cold_outlet_temp.lt.Max_temp_to_heat_source) then   !Is the storage-to-tower temp ok?
            Mode = 1.  !Normal operation
        else
            Mode = 2.   !Tank is overcharged, tower OFF and let the power cycle run
        endif
    else    !Power cycle inlet temperature is too low
        if(is_hybrid.gt.0) then  !The plant is hybrid
            Mode = 3. !supplement the flow with an external heat source
        else
            if((t_standby_remain.gt.0).and.(zenith.lt.80).and.(zenith.ne.0.)) then 
                !--> the standby time has expired, or its after sunset (zenith is set to zero when >90)
                Mode = 6. !go to standby mode
            else
                Mode = 4.  !Let the tower cycle fluid to charge the tank
                day_shutdown=1
            endif
        endif
    endif
else   !The charge requirement is not met
    if (Storage_cold_outlet_temp.lt.Max_temp_to_heat_source) then   !Is the storage-to-tower temp ok?
        Mode = 4.  !Let the tank charge during the cycle-off period
    else
        Mode = 5.  !The  tank is fully charged, now OFF and wait for operating hours to begin
    endif
endif


!___Define the modes of operation___
100   select case (int(Mode))
case(1) !Mode 1: Normal operation
    Flow_to_power_cycle = Load_flow_demand
    Flow_to_heat_source = Flow_from_heat_source
    !If the flow exceeds the maximum flow rate to the tower, 
    !..partially defocus the field
    if (hour_to_store.ne.stored_hour) then  !First call, start at FC=1
        Field_control=1
    else        !Iterative calls, calculate a defocus factor
        if (Flow_to_heat_source.ge.m_dot_max) then
            Field_control=Field_control*m_dot_max/Flow_to_heat_source
        else
            Field_control=Field_control
        endif
    endif
    hybrid_control = 0.
    Pump_on = 1.
    standby_control = 1.
    t_standby_remain = t_standby
    demand_var_control=P_ref*TSLOGIC_C(int(TOUperiod))
    cycle_pl_control=1.  !1=demand, 2=part load
case(2) !Mode 2: Hot flow
    Flow_to_power_cycle = Load_flow_demand
    Flow_to_heat_source = Flow_from_heat_source
    Field_control = 0.
    hybrid_control = 0.
    Pump_on = 1.
    standby_control = 1.
    t_standby_remain = t_standby
    demand_var_control=P_ref*TSLOGIC_C(int(TOUPeriod))
    cycle_pl_control=1.  !1=demand, 2=part load
case(3)  !Mode 3: Running cycle with supplemental HS 
    Flow_to_power_cycle = Load_flow_demand
    Flow_to_heat_source = Flow_from_heat_source
    Field_control = 1. 
    hybrid_control = 1. 
    Pump_on = 1. 
    standby_control = 1.
    t_standby_remain = t_standby
    demand_var_control=P_ref*TSLOGIC_C(int(TOUperiod))
    cycle_pl_control=1.  !1=demand, 2=part load
case(4)  !Mode 4: Charging, cycle off
    Flow_to_power_cycle = 0.
    Flow_to_heat_source = Flow_from_heat_source
    !If the flow exceeds the maximum flow rate to the tower, 
    !..partially defocus the field
    if (hour_to_store.ne.stored_hour) then
        Field_control=1
    else
        if (Flow_to_heat_source.ge.m_dot_max)then
            Field_control=min(derate*m_dot_max/Flow_to_heat_source,Field_control)
            derate=derate-.035
        else
            Field_control=Field_control
        endif
    endif
    hybrid_control = 0.
    Pump_on = 0.
    standby_control = 3.
    t_standby_remain = t_standby
    demand_var_control=P_ref
    cycle_pl_control=1.  !1=demand, 2=part load
case(5)  !Mode 5: All off
    Flow_to_power_cycle = 0.
    Flow_to_heat_source = 0.
    Field_control = 0.
    hybrid_control = 0.
    Pump_on = 0.
    standby_control = 3.
    t_standby_remain = t_standby
    demand_var_control=P_ref
    cycle_pl_control=1.  !1=demand, 2=part load
case(6) !Mode 6: Standby
    Flow_to_power_cycle = Load_flow_demand
    Flow_to_heat_source = Flow_from_heat_source
    Field_control = 1. 
    hybrid_control = 1. 
    Pump_on = 1. 
    t_standby_remain = t_standby_remain-timestep
    if(t_standby_remain.le.0.) then
        standby_control = 3.
        flow_to_power_cycle = 0.
    else
        standby_control = 2.
    endif
    demand_var_control=0.
    cycle_pl_control=1.  !1=demand, 2=part load
end select
continue

end subroutine