			SUBROUTINE TYPE228 (TIME,XIN,OUT,T,DTDT,PAR,INFO,ICNTRL,*) 
C************************************************************************
C Object: Power Tower Parasitics
C Simulation Studio Model: Type228
C 
C Author: Michael J Wagner
C Editor: 
C Date:	 January 27, 2009 
C Last modified: May 22, 2009
! COPYRIGHT 2009 NATIONAL RENEWABLE ENERGY LABORATORY

! Parameters
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Input Units   |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!NONE

! Inputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Input Units   |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|  P_storage_pump                |  Storage pump power, rated per MWt of storage use               |  MWe/MWt       |  MWe/MWt
!  2|  Flow_from_storage             |  Flow rate from storage                                         |  kg/hr         |  kg/hr
!  3|  P_plant_balance               |  Balance of plant parasitic power fraction                      |  MWe/MWe       |  MWe/MWe
!  4|  P_cooling_tower               |  Cooling tower parasitic power fraction                         |  MWe/MWe       |  MWe/MWe
!  5|  Tower_pump_power              |  Reported tower pump power                                      |  MWe           |  MWe
!  6|  P_helio_track                 |  Reported heliostat tracking power                              |  MWe           |  MWe
!  7|  Plant_power_output            |  Reported plant power output                                    |  MWe           |  MWe
!  8|  Power_cycle_efficiency        |  Power cycle efficiency                                         |  none          |  none
!  9|  Piping_loss                   |  Thermal loss per meter of piping                               |  Wt/m          |  Wt/m
! 10|  Piping_length                 |  Total length of exposed piping                                 |  m             |  m
! 11|  design_power                  |  Power production at design conditions                          |  MWe           |  MWe
! 12|  P_cold_tank                   |  Cold tank heater parasitic power                               |  MWe           |  MWe
! 13|  P_hot_tank                    |  Hot tank heater parasitic power                                |  MWe           |  MWe
! 14|  P_tower_conv                  |  Reported tower convection loss                                 |  MWt           |  MWt
! 15|  P_tower_rad                   |  Reported tower radiation loss                                  |  MWt           |  MWt
! 16|  Recirc_htr_eff                |  Recirculation heater efficiency                                |  none          |  none
! 17|  Recirc_source                 |  Recirculation heater control                                   |  none          |  none
! 18|  design_eff                    |  Power cycle efficiency at design                               |  none          |  none
! 19|  ref_htf_flow                  |  HTF flow rate through the power cycle at design                |  kg/hr         |  kg/hr

! Outputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Output Units  |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|  P_storage_pump_tot            |  Total storage pump parasitic power                             |  MWe           |  MWe
!  2|  P_plant_balance_tot           |  Total balance of plant parasitic power                         |  MWe           |  MWe
!  3|  P_cooling_tower_tot           |  Total cooling tower parasitic power                            |  MWe           |  MWe
!  4|  P_piping_tot                  |  Total piping loss parasitic power                              |  MWe           |  MWe
!  5|  P_parasitics                  |  Overall parasitic losses                                       |  MWe           |  MWe
!  6|  -not named-                   |  Power to the grid after parasitic losses                       |  MWe           |  MWe
!  7|  P_tank_heater                 |  Total tank heater parasitic power                              |  MWe           |  MWe
!  8|  P_power_par                   |  Total tower heater parasitic loss                              |  MWe           |  MWe


C************************************************************************

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

C-----------------------------------------------------------------------------------------------------------------------
C    REQUIRED BY THE MULTI-DLL VERSION OF TRNSYS
      !DEC$ATTRIBUTES DLLEXPORT :: TYPE228				!SET THE CORRECT TYPE NUMBER HERE
C-----------------------------------------------------------------------------------------------------------------------
C-----------------------------------------------------------------------------------------------------------------------
C    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
C-----------------------------------------------------------------------------------------------------------------------

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

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

C    INPUTS
      DOUBLE PRECISION P_storage_pump, Flow_from_storage,
     & P_plant_balance, P_cooling_tower, Plant_power_output,
     & Tower_pump_power, P_helio_track,Power_cycle_efficiency,
     & Piping_loss, Piping_length, design_power,P_tank_heater,
     & P_cold_tank, P_hot_tank, P_tower_conv, P_tower_rad, 
     & recirc_htr_eff, recirc_source, design_eff, ref_htf_flow
     
C     LOCALS
      DOUBLE PRECISION P_piping_tot     

C     OUTPUTS
      DOUBLE PRECISION P_storage_pump_tot, P_plant_balance_tot,
     & P_cooling_tower_tot, P_parasitics, P_tower_par

C-----------------------------------------------------------------------------------------------------------------------
C    RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER
        
        
        P_storage_pump=XIN(1) !MWe/MWt
        Flow_from_storage = XIN(2)  !kg/hr
        P_plant_balance=XIN(3) !MWe/MWe
        P_cooling_tower=XIN(4)  !MWe/MWe
        Tower_pump_power=XIN(5) !MWe
        P_helio_track=XIN(6) !input from 221 as MWe
        Plant_power_output=XIN(7) !MWe
        Power_cycle_efficiency=XIN(8)
        Piping_loss=XIN(9)  !W/m
        Piping_length=XIN(10)
        design_power=XIN(11)  !MWe
        P_cold_tank=XIN(12) !MWt
        P_hot_tank=XIN(13) !MWt
        P_tower_conv=XIN(14) !MWt
        P_tower_rad=XIN(15) !MWt
        Recirc_htr_eff=XIN(16) !-
        Recirc_source=XIN(17) !-> 0=No recirculation, 1=Storage, 2=electric heater**
        design_eff=XIN(18)  !Design cycle efficiency
        ref_htf_flow=XIN(19)    !HTF flow rate at design [kg/hr]
	   IUNIT=INFO(1)
	   ITYPE=INFO(2)

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

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

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

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

C       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				

C       SET THE REQUIRED NUMBER OF INPUTS, PARAMETERS AND DERIVATIVES THAT THE USER SHOULD SUPPLY IN THE INPUT FILE
C       IN SOME CASES, THE NUMBER OF VARIABLES MAY DEPEND ON THE VALUE OF PARAMETERS TO THIS MODEL....
         NIN=NI
	   NPAR=NP
	   NDER=ND
	       
C       CALL THE TYPE CHECK SUBROUTINE TO COMPARE WHAT THIS COMPONENT REQUIRES TO WHAT IS SUPPLIED IN 
C       THE TRNSYS INPUT FILE
	   CALL TYPECK(1,INFO,NIN,NPAR,NDER)
C       SET THE NUMBER OF STORAGE SPOTS NEEDED FOR THIS COMPONENT
!	   CALL setStorageSize(NSTORED,INFO)

C       RETURN TO THE CALLING PROGRAM
         RETURN 1

      ENDIF
C-----------------------------------------------------------------------------------------------------------------------

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

C       SET THE UNIT NUMBER FOR FUTURE CALLS
         IUNIT=INFO(1)
         ITYPE=INFO(2)

C       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL VALUES OF THE OUTPUTS HERE
C		 P_storage_pump_tot
			OUT(1)=0
C		 P_plant_balance_tot
			OUT(2)=0
C		 P_cooling_tower_tot
			OUT(3)=0
C        Piping loss (MWe)
            OUT(4)=0			
C		 P_parasitics
			OUT(5)=0
C        Power to the grid
            OUT(6)=0
C        P_tank_heater
            OUT(7)=0  
C        P_tower_par
            OUT(8)=0.                        

!        STORED(1)=0. !mode

C       PUT THE STORED ARRAY IN THE GLOBAL STORED ARRAY
!         CALL setStorageVars(STORED,NITEMS,INFO)


C       RETURN TO THE CALLING PROGRAM
         RETURN 1

      ENDIF
C-----------------------------------------------------------------------------------------------------------------------
C    RETRIEVE THE VALUES IN THE STORAGE ARRAY FOR THIS ITERATION
!	CALL getStorageVars(STORED,NSTORED,INFO)
!      cycle_mode=STORED(1)

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

C		ADD YOUR COMPONENT EQUATIONS HERE; BASICALLY THE EQUATIONS THAT WILL
C		CALCULATE THE OUTPUTS BASED ON THE PARAMETERS AND THE INPUTS.	REFER TO
C		CHAPTER 3 OF THE TRNSYS VOLUME 1 MANUAL FOR DETAILED INFORMATION ON
C		WRITING TRNSYS COMPONENTS.


      P_storage_pump_tot=P_storage_pump*Flow_from_storage/
     &                 max(Ref_HTF_flow,1.e-6)*design_power/design_eff  !MWe

      P_plant_balance_tot=P_plant_balance*Plant_power_output !MWe

      P_cooling_tower_tot=P_cooling_tower   !Report back what is provided by the power block component

      P_piping_tot = Piping_loss*Piping_length*
     &               Power_cycle_efficiency*Plant_power_output/
     &               (design_power*1.e6)  !MWe
     
      P_tank_heater = (P_cold_tank + P_hot_tank) !MWe 
      
      if(recirc_source.eq.2.) then  !Electric heater
        P_tower_par = (P_tower_conv+P_tower_rad)/recirc_htr_eff
      else   !No recirculation needed, or its from the hot tank.. so no action here
        P_tower_par = 0.
      endif

      P_parasitics=P_storage_pump_tot+P_plant_balance_tot+
     & P_cooling_tower_tot+Tower_pump_power+
     & P_helio_track+P_piping_tot+P_tank_heater+P_tower_par


C-----------------------------------------------------------------------------------------------------------------------
C    SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT

C		 P_storage_pump_tot
			OUT(1)=P_storage_pump_tot
C		 P_plant_balance_tot
			OUT(2)=P_plant_balance_tot
C		 P_cooling_tower_tot
			OUT(3)=P_cooling_tower_tot
C        Piping loss
            OUT(4)=P_piping_tot			
C		 P_parasitics
			OUT(5)=P_parasitics
C          Power to the grid
              OUT(6)=Plant_power_output-P_parasitics
C        P_tank_heater
            OUT(7)=P_tank_heater              
C        P_tower_par
            OUT(8)=P_tower_par
              
C-----------------------------------------------------------------------------------------------------------------------
C    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
      RETURN 1
      END
C-----------------------------------------------------------------------------------------------------------------------
