text:       Subroutine Type2601
  
  ! Object: WBGT calculation for inside
  ! Simulation Studio Model: Type2601 WBGT inside
  ! 
  
  ! Author: Yuichi Yasuda
  ! Editor: 
  ! Date:	 June 22, 2019
  ! last modified: June 22, 2019
  ! 
  ! 
  ! *** 
  ! *** Model Parameters 
  ! *** 
  !			Nb. of WBGT	- [1;20]
  
  ! *** 
  ! *** Model Inputs 
  ! *** 
  !			Wet Bulb temperature	C [-Inf;+Inf]
  !			Globe or Operative temperature	C [-Inf;+Inf]
  
  ! *** 
  ! *** Model Outputs 
  ! *** 
  !			WBGT	C [-Inf;+Inf]
  
  ! *** 
  ! *** Model Derivatives 
  ! *** 
  
  ! (Comments and routine interface generated by TRNSYS Simulation Studio)
  !************************************************************************
  
  !-----------------------------------------------------------------------------------------------------------------------
  ! This TRNSYS component skeleton was generated from the TRNSYS studio based on the user-supplied parameters, inputs, 
  ! outputs, and derivatives.  The user should check the component formulation carefully and add the content to transform
  ! the parameters, inputs and derivatives into outputs.  Remember, outputs should be the average value over the timestep
  ! and not the value at the end of the timestep; although in many models these are exactly the same values.  Refer to 
  ! existing types for examples of using advanced features inside the model (Formats, Labels etc.)
  !-----------------------------------------------------------------------------------------------------------------------
  
  
        Use TrnsysConstants
        Use TrnsysFunctions
  
  !-----------------------------------------------------------------------------------------------------------------------
  
  !DEC$Attributes DLLexport :: Type2601
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Trnsys Declarations
        Implicit None
  
        Double Precision Timestep,Time
        Integer CurrentUnit,CurrentType
        Integer i
        Integer, Parameter :: MaxWBGT=20  !maximum number of WBGTs per component
  
  !    PARAMETERS
        INTEGER nWBGT
        
  !    INPUTS
        DOUBLE PRECISION wetbulbTemp(MaxWBGT) !湿球温度[C]
        DOUBLE PRECISION globeTemp(MaxWBGT)   !黒球温度（室内では作用温度）[C]
  
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Get the Global Trnsys Simulation Variables
        Time=getSimulationTime()
        Timestep=getSimulationTimeStep()
        CurrentUnit = getCurrentUnit()
        CurrentType = getCurrentType()
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Set the Version Number for This Type
        If(getIsVersionSigningTime()) Then
  		Call SetTypeVersion(17)
  		Return
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Do Any Last Call Manipulations Here
        If(getIsLastCallofSimulation()) Then
  		Return
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Perform Any "After Convergence" Manipulations That May Be Required at the End of Each Timestep
        If(getIsEndOfTimestep()) Then
  		Return
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Do All of the "Very First Call of the Simulation Manipulations" Here
        If(getIsFirstCallofSimulation()) Then
          
          !get the param1, number of WBGT.
          nWBGT = JFIX(getParameterValue(1)+0.1)
          if(nWBGT > MaxWBGT) then
              Call FoundBadParameter(1,'Fatal','The number of the WBGT to be calculated must be between 1 and 20.')
          endif
          
  		!Tell the TRNSYS Engine How This Type Works
  		Call SetNumberofParameters(1)           !The number of parameters that the the model wants
  		Call SetNumberofInputs(2*nWBGT)         !The number of inputs that the the model wants
  		Call SetNumberofDerivatives(0)          !The number of derivatives that the the model wants
  		Call SetNumberofOutputs(nWBGT)          !The number of outputs that the the model produces
  		Call SetIterationMode(1)                !An indicator for the iteration mode (default=1).  Refer to section 8.4.3.5 of the documentation for more details.
  		Call SetNumberStoredVariables(0,0)      !The number of static variables that the model wants stored in the global storage array and the number of dynamic variables that the model wants stored in the global storage array
  		Call SetNumberofDiscreteControls(0)     !The number of discrete control functions set by this model (a value greater than zero requires the user to use Solver 1: Powell's method)
  
          !Set the Correct Input and Output Variable Types
          do i=1, GetNumberofInputs()
              Call SetInputUnits(i,'TE1') !湿球温度、黒球温度（室内では作用温度）[C]
          end do
          
          do i=1, GetNumberofOutputs()
              Call SetOutputUnits(i,'TE1') !WBGT[C]
          end do
  
  		Return
  
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Do All of the First Timestep Manipulations Here - There Are No Iterations at the Intial Time
        If (getIsStartTime()) Then
          nWBGT = JFIX(getParameterValue(1)+0.1)
        
          do i=1, nWBGT
              wetBulbTemp(i)  = GetInputValue(i*2-1)
              globeTemp(i)    = GetInputValue(i*2)
          end do
  	
     !Check the Parameters for Problems (#,ErrorType,Text)
     !Sample Code: If( PAR1 <= 0.) Call FoundBadParameter(1,'Fatal','The first parameter provided to this model is not acceptable.')
  
     !Set the Initial Values of the Outputs (#,Value)
        do i=1, nWBGT   
  		!Call SetOutputValue(1, 0.0) ! WBGT
          Call SetOutputValue(i, 0.7*wetBulbTemp(i)+0.3*globeTemp(i) ) ! WBGT
        end do
  
     !If Needed, Set the Initial Values of the Static Storage Variables (#,Value)
     !Sample Code: SetStaticArrayValue(1,0.d0)
  
     !If Needed, Set the Initial Values of the Dynamic Storage Variables (#,Value)
     !Sample Code: Call SetDynamicArrayValueThisIteration(1,20.d0)
  
     !If Needed, Set the Initial Values of the Discrete Controllers (#,Value)
     !Sample Code for Controller 1 Set to Off: Call SetDesiredDiscreteControlState(1,0) 
  
  		Return
  
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !ReRead the Parameters if Another Unit of This Type Has Been Called Last
        If(getIsReReadParameters()) Then
  		!Read in the Values of the Parameters from the Input File
          nWBGT = JFIX(getParameterValue(1)+0.1)
          Call SetNumberofInputs(2*nWBGT)
  		
        EndIf
  !-----------------------------------------------------------------------------------------------------------------------
  
  !Read the Inputs
        do i=1, nWBGT
          wetBulbTemp(i)  = GetInputValue(i*2-1)
          globeTemp(i)    = GetInputValue(i*2)
        end do
  		
  
  	!Check the Inputs for Problems (#,ErrorType,Text)
  	!Sample Code: If( IN1 <= 0.) Call FoundBadInput(1,'Fatal','The first input provided to this model is not acceptable.')
   
        If(ErrorFound()) Return
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !    *** PERFORM ALL THE CALCULATION HERE FOR THIS MODEL. ***
  !-----------------------------------------------------------------------------------------------------------------------
  
  	!-----------------------------------------------------------------------------------------------------------------------
  	!If Needed, Get the Previous Control States if Discrete Controllers are Being Used (#)
  	!Sample Code: CONTROL_LAST=getPreviousControlState(1)
  	!-----------------------------------------------------------------------------------------------------------------------
  
  	!-----------------------------------------------------------------------------------------------------------------------
  	!If Needed, Get the Values from the Global Storage Array for the Static Variables (#)
  	!Sample Code: STATIC1=getStaticArrayValue(1)
  	!-----------------------------------------------------------------------------------------------------------------------
  
  	!-----------------------------------------------------------------------------------------------------------------------
  	!If Needed, Get the Initial Values of the Dynamic Variables from the Global Storage Array (#)
  	!Sample Code: T_INITIAL_1=getDynamicArrayValueLastTimestep(1)
  	!-----------------------------------------------------------------------------------------------------------------------
  
  	!-----------------------------------------------------------------------------------------------------------------------
  	!Perform All of the Calculations Here to Set the Outputs from the Model Based on the Inputs
  
  	!Sample Code: OUT1=IN1+PAR1
  
  	!If the model requires the solution of numerical derivatives, set these derivatives and get the current solution
  	!Sample Code: T1=getNumericalSolution(1)
  	!Sample Code: T2=getNumericalSolution(2)
  	!Sample Code: DTDT1=3.*T2+7.*T1-15.
  	!Sample Code: DTDT2=-2.*T1+11.*T2+21.
  	!Sample Code: Call SetNumericalDerivative(1,DTDT1)
  	!Sample Code: Call SetNumericalDerivative(2,DTDT2)
  
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !Set the Outputs from this Model (#,Value)
          do i=1, nWBGT
  		    Call SetOutputValue(i, 0.7*wetBulbTemp(i)+0.3*globeTemp(i) ) ! WBGT
          end do
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !If Needed, Store the Desired Disceret Control Signal Values for this Iteration (#,State)
  !Sample Code:  Call SetDesiredDiscreteControlState(1,1)
  !-----------------------------------------------------------------------------------------------------------------------
  
  !-----------------------------------------------------------------------------------------------------------------------
  !If Needed, Store the Final value of the Dynamic Variables in the Global Storage Array (#,Value)
  !Sample Code:  Call SetDynamicArrayValueThisIteration(1,T_FINAL_1)
  !-----------------------------------------------------------------------------------------------------------------------
   
        Return
        End
  !-----------------------------------------------------------------------------------------------------------------------
  
  