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

!                      Code_Saturne version 4.2.1
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2016 EDF S.A.
!
! This program is free software; you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the Free Software
! Foundation; either version 2 of the License, or (at your option) any later
! version.
!
! This program is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
! FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
! details.
!
! You should have received a copy of the GNU General Public License along with
! this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
! Street, Fifth Floor, Boston, MA 02110-1301, USA.

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

subroutine usthht &
!================

 ( mode   , enthal , temper  )

!===============================================================================
! FONCTION :
! --------
!    ROUTINE UTILISATEUR
!    LOI ENTHALPIE   -> TEMPERATURE (MODE =  1)
!    LOI TEMPERATURE -> ENTHALPIE   (MODE = -1)
!-------------------------------------------------------------------------------
! Arguments
!__________________.____._____.________________________________________________.
! name             !type!mode ! role                                           !
!__________________!____!_____!________________________________________________!
! mode             ! e  ! <-- !  -1 : t -> h  ;   1 : h -> t                   !
! enthal           ! r  ! <-- ! enthalpie                                      !
! temper           ! r  ! <-- ! temperature                                    !
!__________________!____!_____!________________________________________________!
!     Type: i (integer), r (real), s (string), a (array), l (logical),
!           and composite types (ex: ra real array)
!     mode: <-- input, --> output, <-> modifies data, --- work array
!===============================================================================
!===============================================================================
! Module files
!===============================================================================
use paramx
use entsor
use parall
use period
!===============================================================================
implicit none
! Arguments
integer          mode
double precision enthal, temper

! Local variables
integer         it, ligne
double precision eh1 , eh0, tt1 , tt2 
!double precision rho, cp(130),lambda(130),sigma(130),tt(600),ent(600)
!Metals properties
integer          ntab1, ntab2, ntab3
parameter       (ntab1=74)
parameter       (ntab2=34)
parameter       (ntab3=30)
double precision wtt1(ntab1), went1(ntab1), wcp1(ntab1)
double precision wtt2(ntab2), wsigma(ntab2), wlambda(ntab2)
double precision cutt3(ntab3),cuent3(ntab3)
!Tugsten T->H and T->Cp data. With big fall of Cp at the end of the interval
data            wtt1 / 298.15,  323.15,  373.15,  423.15,  450.00,  473.15,  523.15,  573.15,     &
                       623.15,  673.15,  723.15,  773.15,  823.15,  873.15,  923.15,  973.15,     &
                      1023.15, 1073.15, 1123.15, 1173.15, 1223.15, 1273.15, 1323.15, 1373.15,     &
                      1423.15, 1473.15, 1523.15, 1573.15, 1623.15, 1673.15, 1723.15, 1773.15,     &
                      1823.15, 1873.15, 1923.15, 1973.15, 2023.15, 2073.15, 2123.15, 2173.15,     &
                      2223.15, 2273.15, 2323.15, 2373.15, 2423.15, 2473.15, 2500.00, 2523.15,     &
                      2573.15, 2623.15, 2673.15, 2723.15, 2773.15, 2823.15, 2873.15, 2923.15,     &
                      2973.15, 3000.00, 3023.15, 3073.15, 3123.15, 3173.15, 3223.15, 3273.15,     &
                      3323.15, 3373.15, 3423.15, 3473.15, 3523.15, 3573.15, 3623.15, 3673.15,     &
                      3680.00, 3680.00/
data           went1 /  3.32E+03,  10.01E+03,  16.79E+03,  20.45E+03,  23.63E+03,  30.52E+03,     &
                       37.47E+03,  44.49E+03,  51.56E+03,  58.69E+03,  65.88E+03,  73.13E+03,     &
                       80.44E+03,  87.81E+03,  95.25E+03, 102.74E+03, 110.29E+03, 117.91E+03,     &
                      125.59E+03, 133.33E+03, 141.13E+03, 149.00E+03, 156.93E+03, 164.92E+03,     &
                      172.97E+03, 181.09E+03, 189.28E+03, 197.52E+03, 205.83E+03, 214.21E+03,     &
                      222.65E+03, 231.16E+03, 239.73E+03, 248.37E+03, 257.07E+03, 265.84E+03,     &
                      274.67E+03, 283.58E+03, 292.55E+03, 301.58E+03, 310.69E+03, 319.86E+03,     &
                      329.10E+03, 338.41E+03, 347.78E+03, 352.84E+03, 357.22E+03, 366.72E+03,     &
                      376.31E+03, 386.00E+03, 395.84E+03, 405.83E+03, 416.02E+03, 426.41E+03,     &
                      437.04E+03, 447.91E+03, 453.86E+03, 459.06E+03, 470.53E+03, 482.33E+03,     &
                      494.47E+03, 506.97E+03, 519.85E+03, 533.14E+03, 546.87E+03, 561.10E+03,     &
                      575.87E+03, 591.23E+03, 607.23E+03, 623.95E+03, 641.43E+03, 643.89E+03,     &
                      928.44E+03, 936.78E+03/
data            wcp1 /133.10, 134.80, 136.18, 136.76, 137.30, 138.47, 139.65, 140.83,     &
                      142.02, 143.21, 144.41, 145.62, 146.83, 148.04, 149.26, 150.49,     &
                      151.72, 152.95, 154.19, 155.44, 156.69, 157.94, 159.20, 160.47,     &
                      161.74, 163.02, 164.30, 165.58, 166.87, 168.17, 169.47, 170.78,     &
                      172.09, 173.40, 174.72, 176.05, 177.38, 178.72, 180.06, 181.40,     &
                      182.75, 184.11, 185.47, 186.84, 188.21, 188.95, 188.94, 189.36,     &
                      190.73, 192.70, 195.21, 198.24, 201.76, 205.73, 210.13, 214.94,     &
                      220.12, 223.07, 226.11, 232.68, 239.35, 246.29, 253.65, 261.56,     &
                      270.14, 279.52, 289.79, 301.06, 313.43, 326.97, 341.77, 357.91,     &
                      360.23, 193.44/ !Fall of Cp at the end is crystal-liquid transition
!Tungsten T->Sigma and T->Lambda data
data            wtt2 / 300.0,  400.0,  500.0,  600.0,  700.0,  800.0,  900.0, 1000.0,     &
                      1100.0, 1200.0, 1300.0, 1400.0, 1500.0, 1600.0, 1700.0, 1800.0,     &
                      1900.0, 2000.0, 2100.0, 2200.0, 2300.0, 2400.0, 2500.0, 2600.0,     &
                      2700.0, 2800.0, 2900.0, 3000.0, 3100.0, 3200.0, 3300.0, 3400.0,     &
                      3500.0, 3600.0/
data          wsigma /1.801801802E+07, 1.253132832E+07, 9.496676163E+06, 7.570022710E+06,     &
                      6.257822278E+06, 5.307855626E+06, 4.591368228E+06, 4.035512510E+06,     &
                      3.588087549E+06, 3.229974160E+06, 2.927400468E+06, 2.676659529E+06,     &
                      2.463357556E+06, 2.281542323E+06, 2.122466306E+06, 1.984126984E+06,     &
                      1.860465116E+06, 1.751313485E+06, 1.654259719E+06, 1.567398119E+06,     &
                      1.488095238E+06, 1.416430595E+06, 1.352265044E+06, 1.293661061E+06,     &
                      1.239925604E+06, 1.190476190E+06, 1.144819691E+06, 1.102535832E+06,     &
                      1.063264221E+06, 1.026694045E+06, 9.925558313E+05, 9.606147935E+05,     &
                      9.306654258E+05, 9.025270758E+05/
data         wlambda /172.0, 157.0, 146.0, 138.0, 132.0, 127.0, 123.0, 120.0,     &
                      123.0, 114.0, 112.0, 110.0, 108.5, 107.0, 106.0, 105.0,     &
                      103.5, 102.0, 101.5, 101.0, 100.0,  99.0,  98.5,  98.0,     &
                       97.5,  97.0,  97.0,  97.0,  97.0,  97.0,  97.0,  97.0,     &
                       97.0,  97.0/
!Copper T->H data
data           cutt3 /300.0,  350.0,  400.0,  450.0,  500.0,  550.0,  600.0,  650.0,     &
                      700.0,  750.0,  800.0,  850.0,  900.0,  950.0, 1000.0, 1050.0,     &
                     1100.0, 1150.0, 1200.0, 1250.0, 1300.0, 1350.0, 1400.0, 1450.0,     &
                     1500.0, 1550.0, 1600.0, 1650.0, 1700.0, 1750.0/
data          cuent3 / 14000.0,  34125.0,  54500.0,  75125.0,  96000.0, 117125.0,     &
                      138500.0, 160125.0, 182000.0, 204125.0, 226500.0, 249125.0,     &
                      272000.0, 295125.0, 318500.0, 342125.0, 366000.0, 390125.0,     &
                      414500.0, 439125.0, 464000.0, 489125.0, 571700.0, 597050.0,     &
                      622400.0, 647750.0, 673100.0, 698450.0, 723800.0, 749150.0/
!===============================================================================

!Tungsten specific heat from cathode enthalpy 
if (mode.eq.0) then
  !open(unit=impusr(11),file='T_H_Cp_W', status='OLD',form='formatted')
  !read(impusr(11),*)rho 
  ligne = ntab1
  !do it = 1, ligne 
    !read(impusr(11),*) tt(it),ent(it),cp(it) 
    !write(nfecra,1012)tt(it)
    !write(nfecra,1013)ent(it)
  !end do 
  !close(impusr(11))
  it  = ligne
  eh1 = went1(it)
  if ( enthal .ge. eh1 ) then
    temper = wcp1 (it)
    !write(nfecra,*)'W enthal=',enthal,' W TOP Cp=',temper
    return
  endif
  it  = 1
  eh1 = went1(it)
  if ( enthal .le. eh1 ) then
     temper = wcp1 (it)
     !write(nfecra,*)'W enthal=',enthal,' W BOT Cp=',temper
     return
  endif
  50 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = went1(it)
    if ( enthal .le. eh1 ) then
      temper = wcp1(it-1)+ (enthal-eh0)*(wcp1(it)-wcp1(it-1))/(eh1-eh0)
      !write(nfecra,*)'W enthal=',enthal,' W Cp=',temper
      return
    endif
  GOTO 50
!Tungsten specific heat from cathode temperature 
else if (mode.eq.10) then
  !open(unit=impusr(11),file='T_H_Cp_W', status='OLD',form='formatted')
  !read(impusr(11),*)rho 
  ligne = ntab1
  !do it = 1, ligne 
    !read(impusr(11),*) tt(it),ent(it),cp(it) 
    !write(nfecra,1012)tt(it)
    !write(nfecra,1013)ent(it)
  !end do 
  !close(impusr(11))
  it  = ligne
  eh1 = wtt1(it)
  if ( enthal .ge. eh1 ) then
    temper = wcp1 (it)
    !write(nfecra,*)'W enthal=',enthal,' W TOP Cp=',temper
    return
  endif
  it  = 1
  eh1 = wtt1(it)
  if ( enthal .le. eh1 ) then
     temper = wcp1 (it)
     !write(nfecra,*)'W enthal=',enthal,' W BOT Cp=',temper
     return
  endif
  30 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = wtt1(it)
    if ( enthal .le. eh1 ) then
      temper = wcp1(it-1)+ (enthal-eh0)*(wcp1(it)-wcp1(it-1))/(eh1-eh0)
      !write(nfecra,*)'W enthal=',enthal,' W Cp=',temper
      return
    endif
  GOTO 30
!Tungsten temperature from cathode enthalpy
else if (mode.eq.1) then
  !open(unit=impusr(11),file='T_H_Cp_W', status='OLD',form='formatted')
  !read(impusr(11),*)rho 
  ligne = ntab1
  !do it = 1, ligne 
     !read(impusr(11),*) tt(it),ent(it),cp(it) 
     !write(nfecra,1012)tt(it)
     !write(nfecra,1013)ent(it) 
  !end do 
  !close(impusr(11))
  it  = ligne
  eh1 = went1(it)
  if ( enthal .ge. eh1 ) then
    temper = wtt1 (it)
    return
  endif
  it  = 1
  eh1 = went1(it)
  if ( enthal .le. eh1 ) then
     temper = wtt1 (it)
     return
  endif
  20 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = went1(it)
    if ( enthal .le. eh1 ) then
      temper = wtt1(it-1)+ (enthal-eh0)*(wtt1(it)-wtt1(it-1))/(eh1-eh0)
      return
    endif
  GOTO 20
!Copper temperature from anode enthalpy
else if (mode.eq.2) then
  !open(unit=impusr(10),file='T_H_Cu2', status='OLD',form='formatted')
  ligne = ntab3
  !do it = 1, ligne 
    !read(impusr(10),*) tt(it),ent(it) 
    !write(nfecra,1012)tt(it)
    !write(nfecra,1013)ent(it)
  !end do 
  !close(impusr(10))
  it  = ligne
  eh1 = cuent3(it)
  if ( enthal .ge. eh1 ) then
    temper = cutt3 (it)
    return
  endif
  it  = 1
  eh1 = cuent3(it)
  if ( enthal .le. eh1 ) then
    temper = cutt3 (it)
    return
  endif
  10 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = cuent3(it)
    if ( enthal .le. eh1 ) then
      temper = cutt3(it-1)+ (enthal-eh0)*(cutt3(it)-cutt3(it-1))/(eh1-eh0)
      return
    endif
  GOTO 10
!3 Tungsten electrical conductivity in S/m from temperature
!4 Tungsten thermal conductivity from temperature
else if (mode.eq.3 .or. mode.eq.4) then
  !open(unit=impusr(12),file='T_Sig_Lam_W', status='OLD',form='formatted')
  ligne = ntab2
  !do it = 1, ligne 
    !read(impusr(12),*) ent(it),sigma(it),lambda(it)
    !write(nfecra,1012)tt(II)
    !write(nfecra,1013)ent(II)
  !end do 
  !close(impusr(12))
  it  = ligne
  eh1 = wtt2(it)
  if ( enthal .ge. eh1 ) then
    if (mode.eq.3) then
      temper = wsigma (it)
    else
      temper = wlambda (it)
    endif
    return
  endif
  it  = 1
  eh1 = wtt2(it)
  if ( enthal .le. eh1 ) then
    if (mode.eq.3) then
      temper = wsigma (it)
    else
      temper = wlambda (it)
    endif
     return
  endif
  60 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = wtt2(it)
    if ( enthal .le. eh1 ) then
      if (mode.eq.3) then
        tt1 = wsigma (it)
        tt2 = wsigma (it-1)
      else
        tt1 = wlambda (it)
        tt2 = wlambda (it-1)
      endif
      temper = tt2+ (enthal-eh0)*(tt1-tt2)/(eh1-eh0)
      return
    endif
  GOTO 60
endif

return
end subroutine usthht
