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

!                      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 , rho , tt1 , tt2 
double precision cp(130),lambda(130),sigma(130),tt(600),ent(600)

!===============================================================================

!Tungsten specific heat
if (mode .eq.  0) then
  open(unit=impusr(11),file='H-T-Cp_W', status='OLD',form='formatted')
  read(impusr(11),*)rho 
  ligne = 74
  do it = 1, ligne 
    read(impusr(11),*) tt(it),ent(it),cp(it)
  end do 
  close(impusr(11))
  it  = ligne
  eh1 = ent(it)
  if ( enthal .ge. eh1 ) then
    temper = cp (it)
    write(nfecra,*)'W enthal=',enthal,' W TOP Cp=',temper
    return
  endif
  it  = 1
  eh1 = ent(it)
  if ( enthal .le. eh1 ) then
     temper = cp (it)
     write(nfecra,*)'W enthal=',enthal,' W BOT Cp=',temper
     return
  endif
  50 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = ent(it)
    if ( enthal .le. eh1 ) then
      temper = cp(it-1)+ (enthal-eh0)*(cp(it)-cp(it-1))/(eh1-eh0)
      !write(nfecra,*)'W enthal=',enthal,' W Cp=',temper
      return
    endif
  GOTO 50
!Tungsten temperature from cathode enthalpy
else if (mode .eq.  1) then
  open(unit=impusr(11),file='H-T-Cp_W', status='OLD',form='formatted')
  read(impusr(11),*)rho 
  ligne = 74
  do it = 1, ligne 
    read(impusr(11),*) tt(it),ent(it),cp(it)
  end do 
  close(impusr(11))
  it  = ligne
  eh1 = ent(it)
  if ( enthal .ge. eh1 ) then
    temper = tt (it)
    return
  endif
  it  = 1
  eh1 = ent(it)
  if ( enthal .le. eh1 ) then
     temper = tt (it)
     return
  endif
  20 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = ent(it)
    if ( enthal .le. eh1 ) then
      temper = tt(it-1)+ (enthal-eh0)*(tt(it)-tt(it-1))/(eh1-eh0)
      return
    endif
  GOTO 20
!Copper temperature from anode enthalpy
else if (mode .eq.  2) then
  open(unit=impusr(10),file='H-T_Cu2', status='OLD',form='formatted')
  ligne = 30
  do it = 1, ligne 
    read(impusr(10),*) tt(it),ent(it)
  end do 
  close(impusr(10))
  it  = ligne
  eh1 = ent(it)
  if ( enthal .ge. eh1 ) then
    temper = tt (it)
    return
  endif
  it  = 1
  eh1 = ent(it)
  if ( enthal .le. eh1 ) then
    temper = tt (it)
    return
  endif
  10 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = ent(it)
    if ( enthal .le. eh1 ) then
      temper = tt(it-1)+ (enthal-eh0)*(tt(it)-tt(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='prop_W', status='OLD',form='formatted')
  ligne = 34
  do it = 1, ligne 
    read(impusr(12),*) ent(it),sigma(it),lambda(it)
  end do 
  close(impusr(12))
  it  = ligne
  eh1 = ent(it)
  if ( enthal .ge. eh1 ) then
    if (mode.eq.3) then
      temper = sigma (it)
    else
      temper = lambda (it)
    endif
    return
  endif
  it  = 1
  eh1 = ent(it)
  if ( enthal .le. eh1 ) then
    if (mode.eq.3) then
      temper = sigma (it)
    else
      temper = lambda (it)
    endif
     return
  endif
  60 CONTINUE
    it  = it+1
    eh0 = eh1
    eh1 = ent(it)
    if ( enthal .le. eh1 ) then
      if (mode.eq.3) then
        tt1 = sigma (it)
        tt2 = sigma (it-1)
      else
        tt1 = lambda (it)
        tt2 = lambda (it-1)
      endif
      temper = tt2+ (enthal-eh0)*(tt1-tt2)/(eh1-eh0)
      return
    endif
  GOTO 60
endif

return
end subroutine usthht
