			SUBROUTINE TYPE229 (TIME,XIN,OUT,T,DTDT,PAR,INFO,ICNTRL,*) 
C************************************************************************
C Object: Salt Properties
C Simulation Studio Model: Type229
C 
C Author: Michael J Wagner
C Editor: 
C Date:	 February 02, 2009 last modified: March 18, 2009
! COPYRIGHT 2009 NATIONAL RENEWABLE ENERGY LABORATORY

! Parameters
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Input Units   |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|  Fluid_number                  |  Integer flag denoting HTF fluid                                |  none          |  none
!  2|  LU_FL                         |  Fluid data logical unit                                        |  None          |  none
!  3|  LuFlEr                        |  Fluid property error file unit                                 |  None          |  none


! Inputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Input Units   |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|  Temperature                   |  Temperature of the HTF                                         |  C             |  K

! Outputs
!-----------------------------------------------------------------------------------------------------------------------------------------
!Nb |  Variable                      |  Description                                                    |  Output Units  |  Internal Units
!---|--------------------------------|-----------------------------------------------------------------|----------------|-----------------
!  1|  c                             |  Fluid specific heat                                            |  kJ/kg-K       |  kJ/kg-K
!  2|  rho                           |  Fluid density                                                  |  kg/m^3        |  kg/m^3
!  3|  mu                            |  Fluid viscosity                                                |  Pa-s          |  Pa-s
!  4|  k                             |  Fluid thermal conductivity                                     |  W/m-K         |  W/m-K


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

C    TRNSYS acess functions (allow to acess TIME etc.) 
      USE TrnsysConstants
      USE TrnsysFunctions
C    Special access functions for fluid properties
      use global_props      

C-----------------------------------------------------------------------------------------------------------------------
C    REQUIRED BY THE MULTI-DLL VERSION OF TRNSYS
      !DEC$ATTRIBUTES DLLEXPORT :: TYPE229				!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=3,NI=1,NOUT=4,ND=0,NSTORED=1)
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    PARAMETERS
      DOUBLE PRECISION Fluid_number 
      integer::LU_FL

C    INPUTS
      DOUBLE PRECISION Temperature

C     Outputs
      DOUBLE PRECISION c, rho, mu, k

C     Locals
      DOUBLE PRECISION specheat, Density, Viscosity, Conductivity 
!      DOUBLE PRECISION,ALLOCATABLE:: fprop(:,:)
!      CHARACTER*40 Coolist(2)
      CHARACTER checkname*200, tc*1
      INTEGER ios,ntot, nufl, i, j, m, actb  !fl_bounds(25)
      LOGICAL Is_there
      
      
C-----------------------------------------------------------------------------------------------------------------------
C       READ IN THE VALUES OF THE PARAMETERS IN SEQUENTIAL ORDER
      Fluid_number=PAR(1)
      LU_FL=int(PAR(2))
      LuFlEr = int(par(3)) !Note LuFlEr is stored globally in the global_props module
      ttime=time  !Store time as a global variable
C-----------------------------------------------------------------------------------------------------------------------
C    RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER

      Temperature=XIN(1)
	   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
         if(allocated(fprop)) deallocate(fprop)
         
         inquire(unit=LuFlEr,opened=is_there)
         if(is_there) close(LuFlEr)
         
	   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)=Gjsav
         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)

      !MJW 7/09 Open the warnings file
      inquire(unit=LuFlEr,name=checkname,opened=is_there)
      !open the file for writing
      if(.not.is_there) then
        open(unit=LuFlEr,file=trim(checkname),status="REPLACE",
     &     position="REWIND")
      endif


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)

        !On the initial timestep call, read in the fluid property file

        call readFluidPropFile(LU_FL)       
        fl_flag = 0. !Always reset the fluid warning flag to zero
        fl_ct = 0.  !and the warning counter too


C       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL VALUES OF THE OUTPUTS HERE
C		 Specific heat capacity
			OUT(1)=1.53
C		 Density
			OUT(2)=1750.
C		 Viscosity
			OUT(3)=1e-5
C		 Conductivity
			OUT(4)=.5

C       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL STORAGE VARIABLES HERE
	   STORED(1)=0. !Gjsav

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

C       RETURN TO THE CALLING PROGRAM
         RETURN 1

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

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


! The following scheme is used to define the substance number.  Some fluids are
! blocked off for the trough model, and some are blocked off for future use. 
! Any fluid number specified greater than 35 will be a user-defined fluid.
!    1.) Air
!    2.) Stainless_AISI316
!    3.) Water (liquid)
!    4.) Steam
!    5.) CO2
!    6.) Salt (68% KCl, 32% MgCl2)
!    7.) Salt (8% NaF, 92% NaBF4)
!    8.) Salt (25% KF, 75% KBF4)
!    9.) Salt (31% RbF, 69% RbBF4)
!    10.) Salt (46.5% LiF, 11.5%NaF, 42%KF)
!    11.) Salt (49% LiF, 29% NaF, 29% ZrF4)
!    12.) Salt (58% KF, 42% ZrF4)
!    13.) Salt (58% LiCl, 42% RbCl)
!    14.) Salt (58% NaCl, 42% MgCl2)
!    15.) Salt (59.5% LiCl, 40.5% KCl)
!    16.) Salt (59.5% NaF, 40.5% ZrF4)
!    17.) Salt (60% NaNO3, 40% KNO3)
!    18.) Nitrate Salt**
!    19.) Caloria HT 43**
!    20.) Hitec XL**
!    21.) Therminol VP-1**
!    22.) Hitec**
!    23.) Dowtherm Q**
!    24.) Dowtherm RP**
!    25.) -blank-
!    26.) -blank-
!    27.) -blank-
!    28.) -blank-
!    29.) -blank-
!    30.) -blank-
!    31.) -blank-
!    32.) -blank-
!    33.) -blank-
!    34.) -blank-
!    35.) -blank-
!    36+) User specified (lookup tables)

!     Note that the fluid properties are stored in the FPROP array in the following order:
!*****************************************************************
!|  #    |   1   |   2   |   3   |   4   |   5   |   6   |   7   |
!|-------|-------|-------|-------|-------|-------|-------|-------|
!| Name  |   T   |   Cp  |  rho  |   Mu  |   Nu  |   k   |   h   |
!| Units |   C   |kJ/kg-K| kg/m3 |  Pa-s |  m2-s | W/m-K |  J/kg |
!|-------|-------|-------|-------|-------|-------|-------|-------|
!|   1   |   :   |   :   |   :   |   :   |   :   |   :   |   :   |
!|   2   |   :   |   :   |   :   |   :   |   :   |   :   |   :   |
!*****************************************************************

     

C       GET THE STORED ARRAY FROM THE GLOBAL STORED ARRAY
         CALL getStorageVars(STORED,NSTORED,INFO)
	   Gjsav=STORED(1)  !Gjsav is a module variable, shared between this and the subfunctions


      c=specheat(Fluid_number,Temperature+273.15)
      rho=Density(Fluid_number,Temperature+273.15,dble(1.))
      mu=Viscosity(Fluid_number,Temperature+273.15)
      k=Conductivity(Fluid_number,Temperature+273.15)




C     Send the updated shared Gjsav back to storage
      STORED(1)=Gjsav
      call setStorageVars(STORED,NSTORED,INFO)



C-----------------------------------------------------------------------------------------------------------------------

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

C		 Specific heat capacity
			OUT(1)=c
C		 Density
			OUT(2)=rho
C		 Viscosity
			OUT(3)=mu
C		 Conductivity
			OUT(4)=k

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



	double precision function Density(fnumd,T,P)
	use global_props
	
	implicit none
	!This function accepts as inputs temperature [K] and pressure [Pa]
	!This function outputs in units of [kg/m^3]
	double precision::v,R_air,xlo,xhi
	double precision,intent(in)::T,P,fnumd
      double precision,dimension(size(fprop(1,:)))::dxx,dyy !Create dummy arrays
	integer::fnum,lb,ub,dum,t_warn
      Density=0.
      fnum=nint(fnumd)
      
      select case(fnum)
      case(1)   !    1.) Air
	    R_air = 287.                            !Gas constant [J/kg-K]
          v = R_air*T/P
	    Density = 1./v
      case(2)   !    2.) Stainless_AISI316
          Density=8349.38 - 0.341708*T - 0.0000865128*T**2  !EES
      case(3)   !    3.) Water (liquid)
          continue 
      case(4)   !    4.) Steam
          continue
      case(5)   !    5.) CO2
          continue
      case(6)   !    6.) Salt (68% KCl, 32% MgCl2)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (68% KCl, 32% MgCl2)")
	  Density = 1E-10*T**3 - 3E-07*T**2 - 0.4739*T + 2384.2
      case(7)   !    7.) Salt (8% NaF, 92% NaBF4)
        xlo=658.; xhi=969.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (8% NaF, 92% NaBF4)")
	  Density = 8E-09*T**3 - 2E-05*T**2 - 0.6867*T + 2438.5
      case(8)   !    8.) Salt (25% KF, 75% KBF4)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (25% KF, 75% KBF4)")
	  Density = 2E-08*T**3 - 6E-05*T**2 - 0.7701*T + 2466.1
      case(9)   !    9.) Salt (31% RbF, 69% RbBF4)
        xlo=715.; xhi=1343.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (31% RbF, 69% RbBF4)")
	  Density = -1E-08*T**3 + 4E-05*T**2 - 1.0836*T + 3242.6
      case(10)   !    10.) Salt (46.5% LiF, 11.5%NaF, 42%KF)
        xlo=727.; xhi=1843.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (46.5% LiF, 11.5%NaF, 42%KF)")
	  Density =  -2E-09*T**3 + 1E-05*T**2 - 0.7427*T + 2734.7
      case(11)   !    11.) Salt (49% LiF, 29% NaF, 29% ZrF4)
        xlo=709.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (49% LiF, 29% NaF, 29% ZrF4)")
	  Density = -2E-11*T**3 + 1E-07*T**2 - 0.5172*T + 3674.3
      case(12)   !    12.) Salt (58% KF, 42% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% KF, 42% ZrF4)")
	  Density =  -6E-10*T**3 + 4E-06*T**2 - 0.8931*T + 3661.3
      case(13)   !    13.) Salt (58% LiCl, 42% RbCl)
        xlo=586.; xhi=1323.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% LiCl, 42% RbCl)")
	  Density = -8E-10*T**3 + 1E-06*T**2 - 0.689*T + 2929.5
      case(14)   !    14.) Salt (58% NaCl, 42% MgCl2)
        xlo=718.; xhi=1738.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% NaCl, 42% MgCl2)")
	  Density = -5E-09*T**3 + 2E-05*T**2 - 0.5298*T + 2444.1
      case(15)   !    15.) Salt (59.5% LiCl, 40.5% KCl)
        xlo=628.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% LiCl, 40.5% KCl)")
	  Density = 1E-09*T**3 - 5E-06*T**2 - 0.864*T + 2112.6
      case(16)   !    16.) Salt (59.5% NaF, 40.5% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% NaF, 40.5% ZrF4)")
	  Density =  -5E-09*T**3 + 2E-05*T**2 - 0.9144*T + 3837.
      case(17)   !    17.) Salt (60% NaNO3, 40% KNO3)
        xlo=533.; xhi=866.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (60% NaNO3, 40% KNO3)")
	  Density = -1E-07*T**3 + 0.0002*T**2 - 0.7875*T + 2299.4
      case(18:35) !  18-35.) Not used in this model
        continue
      case(36:) !Any integer greater than 35
        !Call the user-defined property table
        lb=fl_bounds(fnum-35)
        ub=fl_bounds(fnum-35+1)-1
        if(ub.lt.lb) ub=size(fprop(1,:))
        dxx(:)=fprop(1,lb:ub)
        dyy(:)=fprop(3,lb:ub)
        call interp(T,size(dxx),dxx,dyy,Gjsav,Density)
        if((Gjsav.eq.ub).or.(Gjsav.eq.lb)) dum=t_warn(T,dxx(lb),dxx(ub)
     &                                         ,"User-specified fluid")
      end select
      
	end function

	!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

	double precision function specheat(fnumd,T,P)
      use global_props
	implicit none
	!This function outputs in units of [kJ/kg-K]
	double precision::xlo,xhi
	double precision,intent(in)::T,P,fnumd
	double precision,dimension(size(fprop(1,:)))::dxx,dyy !Create dummy arrays
	integer::fnum,lb,ub,dum,t_warn
      specheat=0.
      fnum=nint(fnumd)
      
      select case(fnum)
      case(1)   !    1.) Air
	  specheat = 3E-13*T**4 - 1E-09*T**3 + 1E-06*T**2 - 0.0006*T + 1.0766
      case(2)   !    2.) Stainless_AISI316
        specheat = 0.368455 + 0.000399548*T - 1.70558E-07*T**2 !EES
      case(3)   !    3.) Water (liquid)
          continue 
      case(4)   !    4.) Steam
          continue
      case(5)   !    5.) CO2
          continue
      case(6)   !    6.) Salt (68% KCl, 32% MgCl2)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (68% KCl, 32% MgCl2)")
	  specheat = 1.156
      case(7)   !    7.) Salt (8% NaF, 92% NaBF4)
        xlo=658.; xhi=969.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (8% NaF, 92% NaBF4)")
	  specheat = 1.507
      case(8)   !    8.) Salt (25% KF, 75% KBF4)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (25% KF, 75% KBF4)")
	  specheat = 1.306
      case(9)   !    9.) Salt (31% RbF, 69% RbBF4)
        xlo=715.; xhi=1343.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (31% RbF, 69% RbBF4)")
	  specheat = 9.127
      case(10)   !    10.) Salt (46.5% LiF, 11.5%NaF, 42%KF)
        xlo=727.; xhi=1843.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (46.5% LiF, 11.5%NaF, 42%KF)")
	  specheat = 2.010
      case(11)   !    11.) Salt (49% LiF, 29% NaF, 29% ZrF4)
        xlo=709.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (49% LiF, 29% NaF, 29% ZrF4)")
	  specheat = 1.239
      case(12)   !    12.) Salt (58% KF, 42% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% KF, 42% ZrF4)")
	  specheat = 1.051
      case(13)   !    13.) Salt (58% LiCl, 42% RbCl)
        xlo=586.; xhi=1323.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% LiCl, 42% RbCl)")
	  specheat = 8.918
      case(14)   !    14.) Salt (58% NaCl, 42% MgCl2)
        xlo=718.; xhi=1738.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% NaCl, 42% MgCl2)")
	  specheat = 1.080
      case(15)   !    15.) Salt (59.5% LiCl, 40.5% KCl)
        xlo=628.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% LiCl, 40.5% KCl)")
	  specheat = 1.202
      case(16)   !    16.) Salt (59.5% NaF, 40.5% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% NaF, 40.5% ZrF4)")
	  specheat = 1.172
      case(17)   !    17.) Salt (60% NaNO3, 40% KNO3)
        xlo=533.; xhi=866.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (60% NaNO3, 40% KNO3)")
	  specheat = -1E-10*T**3 + 2E-07*T**2 + 5E-06*T + 1.4387
      case(18:35) !  18-35.) Not used in this model
        continue
      case(36:) !Any integer greater than 35
        !Call the user-defined property table
        lb=fl_bounds(fnum-35)
        ub=fl_bounds(fnum-35+1)-1
        if(ub.lt.lb) ub=size(fprop(1,:))
        dxx(:)=fprop(1,lb:ub)
        dyy(:)=fprop(2,lb:ub)
        call interp(T,size(dxx),dxx,dyy,Gjsav,specheat)
                if((Gjsav.eq.ub).or.(Gjsav.eq.lb)) dum=t_warn(T,dxx(lb),dxx(ub)
     &                                         ,"User-specified fluid")
      end select


	end function

	!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

	double precision function Viscosity(fnumd,T,P)
      use global_props
      implicit none
	!This function outputs in units of [Pa-s]
	double precision::Tx,myconvert,xlo,xhi
	double precision,intent(in)::T,P,fnumd
	double precision,dimension(size(fprop(1,:)))::dxx,dyy !Create dummy arrays
	integer::fnum,lb,ub,dum,t_warn
      viscosity=0.
      fnum=nint(fnumd)
      
      select case(fnum)
      case(1)   !    1.) Air
	  Viscosity = -7E-18*T**4 + 3E-14*T**3 - 5E-11*T**2 + 7E-08*T + 2E-06
      case(2)   !    2.) Stainless_AISI316
          continue
      case(3)   !    3.) Water (liquid)
          continue 
      case(4)   !    4.) Steam
          continue
      case(5)   !    5.) CO2
          continue
      case(6)   !    6.) Salt (68% KCl, 32% MgCl2)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (68% KCl, 32% MgCl2)")
	  Viscosity = .0146*exp(2230./T)*myconvert('cP','kg/m-s')
      case(7)   !    7.) Salt (8% NaF, 92% NaBF4)
        xlo=658.; xhi=969.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (8% NaF, 92% NaBF4)")
	  Viscosity = .0877*exp(2240./T)*myconvert('cP','kg/m-s')
      case(8)   !    8.) Salt (25% KF, 75% KBF4)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (25% KF, 75% KBF4)")
	  Viscosity = .0431*exp(3060./T)*myconvert('cP','kg/m-s')
      case(9)   !    9.) Salt (31% RbF, 69% RbBF4)
        xlo=715.; xhi=1343.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (31% RbF, 69% RbBF4)")
	  Viscosity = .0009
      case(10)   !    10.) Salt (46.5% LiF, 11.5%NaF, 42%KF)
        xlo=727.; xhi=1843.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (46.5% LiF, 11.5%NaF, 42%KF)")
	  Viscosity = .0400*exp(4170./T)*myconvert('cP','kg/m-s')
      case(11)   !    11.) Salt (49% LiF, 29% NaF, 29% ZrF4)
        xlo=709.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (49% LiF, 29% NaF, 29% ZrF4)")
	  Viscosity = .0069
      case(12)   !    12.) Salt (58% KF, 42% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% KF, 42% ZrF4)")
	  Viscosity = .0159*exp(3179./T)*myconvert('cP','kg/m-s')
      case(13)   !    13.) Salt (58% LiCl, 42% RbCl)
        xlo=586.; xhi=1323.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% LiCl, 42% RbCl)")
	  Viscosity = .0861*exp(2517./T)*myconvert('cP','kg/m-s')          !
      case(14)   !    14.) Salt (58% NaCl, 42% MgCl2)
        xlo=718.; xhi=1738.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% NaCl, 42% MgCl2)")
	  Viscosity = .0286*exp(1441./T)*myconvert('cP','kg/m-s')
      case(15)   !    15.) Salt (59.5% LiCl, 40.5% KCl)
        xlo=628.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% LiCl, 40.5% KCl)")
	  Viscosity = .0861*exp(2517./T)*myconvert('cP','kg/m-s')          !
      case(16)   !    16.) Salt (59.5% NaF, 40.5% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% NaF, 40.5% ZrF4)")
	  Viscosity = .0767*exp(3977./T)*myconvert('cP','kg/m-s')
      case(17)   !    17.) Salt (60% NaNO3, 40% KNO3)
        xlo=533.; xhi=866.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (60% NaNO3, 40% KNO3)")
	  Tx=T-273.15  !This particular equation is in terms of degrees celsius
	  Viscosity = -1.473302E-10*Tx**3 + 2.279989E-07*Tx**2 - 
     & 1.199514E-04*Tx + 2.270616E-02
      case(18:35) !  18-35.) Not used in this model
        continue
      case(36:) !Any integer greater than 35
        !Call the user-defined property table
        lb=fl_bounds(fnum-35)
        ub=fl_bounds(fnum-35+1)-1
        if(ub.lt.lb) ub=size(fprop(1,:))
        dxx(:)=fprop(1,lb:ub)
        dyy(:)=fprop(4,lb:ub)
        call interp(T,size(dxx),dxx,dyy,Gjsav,Viscosity)
        if((Gjsav.eq.ub).or.(Gjsav.eq.lb)) dum=t_warn(T,dxx(lb),dxx(ub)
     &                                         ,"User-specified fluid")
      end select


      continue
	end function

	!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

	double precision function Conductivity(fnumd,T,P)
      use global_props	
	implicit none
	!This function outputs in units of [W/m-K]
	double precision::xlo,xhi
	double precision,intent(in)::T,P,fnumd
	double precision,dimension(size(fprop(1,:)))::dxx,dyy !Create dummy arrays
      integer::fnum,lb,ub,dum,t_warn
      conductivity=0.
      fnum=nint(fnumd)
      
      select case(fnum)
      case(1)   !    1.) Air
	  Conductivity = -3E-16*T**4 + 5E-12*T**3 - 3E-08*T**2 + 9E-05*T +0.001
      case(2)   !    2.) Stainless_AISI316
	  Conductivity = 3E-09*T**3 - 8E-06*T**2 + 0.0177*T + 7.7765
      case(3)   !    3.) Water (liquid)
          continue 
      case(4)   !    4.) Steam
          continue
      case(5)   !    5.) CO2
          continue
      case(6)   !    6.) Salt (68% KCl, 32% MgCl2)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (68% KCl, 32% MgCl2)")
	  Conductivity = 0.39
      case(7)   !    7.) Salt (8% NaF, 92% NaBF4)
        xlo=658.; xhi=969.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (8% NaF, 92% NaBF4)")
	  Conductivity = 0.5
      case(8)   !    8.) Salt (25% KF, 75% KBF4)
        xlo=699.; xhi=1691.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (25% KF, 75% KBF4)")
	  Conductivity = 0.4
      case(9)   !    9.) Salt (31% RbF, 69% RbBF4)
        xlo=715.; xhi=1343.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (31% RbF, 69% RbBF4)")
	  Conductivity = 0.28
      case(10)   !    10.) Salt (46.5% LiF, 11.5%NaF, 42%KF)
        xlo=727.; xhi=1843.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (46.5% LiF, 11.5%NaF, 42%KF)")
	  Conductivity = 0.92
      case(11)   !    11.) Salt (49% LiF, 29% NaF, 29% ZrF4)
        xlo=709.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (49% LiF, 29% NaF, 29% ZrF4)")
	  Conductivity = 0.53
      case(12)   !    12.) Salt (58% KF, 42% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% KF, 42% ZrF4)")
	  Conductivity = 0.45
      case(13)   !    13.) Salt (58% LiCl, 42% RbCl)
        xlo=586.; xhi=1323.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% LiCl, 42% RbCl)")
	  Conductivity = 0.39
      case(14)   !    14.) Salt (58% NaCl, 42% MgCl2)
        xlo=718.; xhi=1738.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (58% NaCl, 42% MgCl2)")
	  Conductivity = 0.43
      case(15)   !    15.) Salt (59.5% LiCl, 40.5% KCl)
        xlo=628.; xhi=1673.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% LiCl, 40.5% KCl)")
	  Conductivity = 0.43
      case(16)   !    16.) Salt (59.5% NaF, 40.5% ZrF4)
        xlo=773.; xhi=1623.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (59.5% NaF, 40.5% ZrF4)")
	  Conductivity = 0.49
      case(17)   !    17.) Salt (60% NaNO3, 40% KNO3)
        xlo=533.; xhi=866.;
        if(T.lt.xlo.or.T.gt.xhi) 
     &    dum=t_warn(T,xlo,xhi,"Salt (60% NaNO3, 40% KNO3)")
	  Conductivity = -1E-11*T**3 + 3E-08*T**2 + 0.0002*T + 0.3922
      case(18:35) !  18-35.) Not used in this model
        continue
      case(36:) !Any integer greater than 35
        !Call the user-defined property table
        lb=fl_bounds(fnum-35)
        ub=fl_bounds(fnum-35+1)-1
        if(ub.lt.lb) ub=size(fprop(1,:))
        dxx(:)=fprop(1,lb:ub)
        dyy(:)=fprop(6,lb:ub)
        call interp(T,size(dxx),dxx,dyy,Gjsav,Conductivity)
        if((Gjsav.eq.ub).or.(Gjsav.eq.lb)) dum=t_warn(T,dxx(lb),dxx(ub)
     &                                         ,"User-specified fluid")
      end select


	end function
	
