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

!                      Code_Saturne version 4.0.0
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2015 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.

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

!===============================================================================
! Purpose:
! -------

!> \file cs_user_physical_properties.f90
!> \brief Definition of physical variable laws.
!>
!> usphyv
!> \brief Definition of physical variable laws.
!>
!> \section Warning
!>
!> It is \b forbidden to modify turbulent viscosity \c visct here
!> (a specific subroutine is dedicated to that: \ref usvist)
!>
!> - icp = 1 must <b> have been specified </b>
!>    in \ref usipsu if we wish to define a variable specific heat
!>    cpro_cp (otherwise: memory overwrite).
!>
!> - the kivisl field integer key (scalar_diffusivity_id)
!>    must <b> have been specified </b>
!>    in \ref usipsu if we wish to define a variable viscosity
!>    \c viscls.
!>
!>
!> \remarks
!>  - This routine is called at the beginning of each time step
!>    Thus, <b> AT THE FIRST TIME STEP </b> (non-restart case), the only
!>    values initialized before this call are those defined
!>      - in the GUI or  \ref usipsu (cs_user_parameters.f90)
!>             - density    (initialized at \c ro0)
!>             - viscosity  (initialized at \c viscl0)
!>      - in the GUI or \ref cs_user_initialization
!>             - calculation variables (initialized at 0 by defaut
!>             or to the value given in the GUI or in \ref cs_user_initialization)
!>
!>  - We may define here variation laws for cell properties, for:
!>     - density:                                    rom    kg/m3
!>     - density at boundary faces:                  romb   kg/m3)
!>     - molecular viscosity:                        cpro_viscl  kg/(m s)
!>     - specific heat:                              cpro_cp     J/(kg degrees)
!>     - diffusivities associated with scalars:      cpro_vscalt kg/(m s)
!>
!> \b Warning: if the scalar is the temperature, cpro_vscalt corresponds
!> to its conductivity (Lambda) in W/(m K)
!>
!>
!> The types of boundary faces at the previous time step are available
!>   (except at the first time step, where arrays \c itypfb and \c itrifb have
!>   not been initialized yet)
!>
!> It is recommended to keep only the minimum necessary in this file
!>   (i.e. remove all unused example code)
!>
!>
!> \section usphyv_cell_id Cells identification
!>
!> Cells may be identified using the \ref getcel subroutine.
!> The syntax of this subroutine is described in the
!> \ref cs_user_boundary_conditions subroutine,
!> but a more thorough description can be found in the user guide.
!
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! Arguments
!______________________________________________________________________________.
!  mode           name          role                                           !
!______________________________________________________________________________!
!> \param[in]     nvar          total number of variables
!> \param[in]     nscal         total number of scalars
!> \param[in]     mbrom         indicator of filling of romb array
!> \param[in]     dt            time step (per cell)
!_______________________________________________________________________________

subroutine usphyv &
 ( nvar   , nscal  ,                                              &
   mbrom  ,                                                       &
   dt     )

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

!===============================================================================
! Module files
!===============================================================================

use paramx
use pointe
use numvar
use optcal
use cstphy
use entsor
use parall
use period
use ppppar
use ppthch
use ppincl
use field
use mesh

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

implicit none

! Arguments

integer          nvar   , nscal

integer          mbrom

double precision dt(ncelet)

! Local variables

integer          ivart, iel, ifac, ivar
integer          ith, iscal, ii, ifcvsl
double precision vara, varb, varc, varam, varbm, varcm, vardm
double precision                   varal, varbl, varcl, vardl
double precision                   varac, varbc
double precision xvart, xv

double precision ma0, ma1, ma2, ma3, ma4, ka0, ka1, ka2, ka3, ka4, ka5
double precision ica0, ica1, ica2, ica3, ica4
double precision mv0, mv1, kv0, kv1, kv2, icv0, icv1, icv2
double precision const, pref, ma, mv
double precision mu_a, k_a, icp_a, mu_v, k_v, icp_v
double precision aa, phi_av, phi_va, T





double precision, dimension(:), pointer :: coefap, coefbp
double precision, dimension(:), pointer :: bfpro_rom, cpro_rom
double precision, dimension(:), pointer :: cpro_viscl, cpro_vscalt, cpro_cp, cpro_beta,cpro_vscal
double precision, dimension(:), pointer :: cvar_scalt,cvar_scal

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



!===============================================================================
! 0. Initializations to keep
!===============================================================================

!

pref=101325d0
const= 8.3144621d0
ma=28.9635
mv=18.0



ma0=-9.8601d-1
ma1=9.080125d-2
ma2=-1.17635575d-4
ma3=1.2349703d-7
ma4=-5.7971299d-11

ka0=-2.276501d-3
ka1=1.2598485d-4
ka2=-1.4815235d-7
ka3= 1.73550646d-10
ka4=-1.066657d-13
ka5=2.47663035d-17


ica0=0.103409d1
ica1=-0.284887d-3
ica2=0.7816818d-6
ica3=-0.4970786d-9
ica4=0.1077024d-12


mv0= 8.058131868d1
mv1=4.00054945d-1

kv0=1.761758242d1
kv1=5.558941059d-2
kv2=1.663336663d-4

icv0=1.8691098
icv1=-2.578421578d-4
icv2=1.941058941d-5


  if (iscalt.gt.0) then
    ivart = isca(iscalt)
    call field_get_val_s(ivarfl(ivart), cvar_scalt)
    ivar=isca(1) ! est on ien sur qu c'est 1 ??
    call field_get_val_s(ivarfl(ivar), cvar_scal)   
  endif
  
  
    call field_get_val_s(icrom, cpro_rom)
    call field_get_val_s(iprpfl(iviscl), cpro_viscl)
    call field_get_val_s(iprpfl(icp), cpro_cp)
    
    call field_get_key_int(ivarfl(ivart), kivisl,ifcvsl)
    call field_get_val_s(ifcvsl, cpro_vscalt)



!  ===================================================================
! Calcul propriétés du mélange
!  ===================================================================

  do iel = 1, ncel
    xvart = cvar_scalt(iel)
    xv=cvar_scal(iel)
    
    T=xvart+273.15
    
    mu_a=ma0+ma1*T+ma2*T**2+ma3*T**3+ma4*T**4
    k_a=ka0+ka1*T+ka2*T**2+ka3*T**3+ka4*T**4+ka5*T**5
    icp_a=ica0+ica1*T+ica2*T**2+ica3*T**3+ica4*T**4
    mu_v=mv0+mv1*T
    k_v=kv0+kv1*T+kv2*T**2
    icp_v=icv0+icv1*T+icv2*T**2
    aa=sqrt(2.0)
    phi_av=aa/4*(1+ma/mv)**(-1/2)*(1+(mu_a/mu_v)**(1/2)*(mv/ma)**(1/4))**2
    phi_va=aa/4*(1+mv/ma)**(-1/2)*(1+(mu_v/mu_a)**(1/2)*(ma/mv)**(1/4))**2
    
      if (irangp.ge.0) then
      call parsom(T)
      call parsom(mu_a)
      call parsom(k_a)
      call parsom(icp_a)
      call parsom(mu_v)
      call parsom(k_v)
      call parsom(icp_v)
      call parsom(phi_av)
      call parsom(phi_va)
      endif
        
    cpro_rom(iel)=pref/(const*T)*ma*(1-xv*(1-mv/ma))
    cpro_viscl(iel) =((1-xv)*mu_a)/((1-xv)+xv*phi_av)+(xv*mu_v)/(xv+(1-xv)*phi_va)
    cpro_cp(iel)=icp_a*(1-xv)*ma/(ma*(1-xv)+mv*xv)+icp_v*xv*mv/(ma*(1-xv)+mv*xv)
    cpro_vscalt(iel)=((1-xv)*k_a)/((1-xv)+xv*phi_av)+(xv*k_v)/(xv+(1-xv)*phi_va)
 
  enddo
    

!  ===================================================================
! Calcul diffusivité scalaire
!  ===================================================================

!     call field_get_key_id(ivarfl(ivar), kivisl,ifcvsl)
!     call field_get_val_s(ifcvsl, cpro_vscal)


      if (iscalt.gt.0) then
        ivart = isca(iscalt)
        call field_get_val_s(ivarfl(ivart), cvar_scalt)
      else
        write(nfecra,9010) iscalt
        call csexit (1)
      endif
      
      
      
     call field_get_key_int(ivarfl(ivar), kivisl, ifcvsl)
      if (ifcvsl.ge.0) then
        call field_get_val_s(ifcvsl, cpro_vscal)
      else
        cpro_vscal => NULL()
      endif

      ! --- Stop if Lambda is not variable

      if (ifcvsl.lt.0) then
        write(nfecra,1010) iscal
        call csexit (1)
      endif


      do iel = 1, ncel
      xvart = cvar_scalt(iel)
      cpro_vscal(iel)= 21.2d-6*(1-0.007*xvart)
      enddo







!  ===================================================================
! Calcul diffusivité scalaire ALTERNATIF
!  ===================================================================





!   do ii = 1, nscaus ! Loop on scalars
!
!     ! --- Number of user scalar 'ii' in the lsit of scalars
!     iscal = ii
!
!     ! --- If it is a thermal variable, it has already been handled above
!     ith = 0
!     if (iscal.eq.iscalt) ith = 1
!
!     ! --- If the variable is a fluctuation, its diffusivity is the same
!     !       as that of the scalar to which it is attached:
!     !       there is nothing to do here, we move on to the next variable
!     !       without setting cpro_vscalt(iel).
!
!     ! We only handle here non-thermal variables which are not fluctuations
!     if (ith.eq.0.and.iscavr(iscal).le.0) then
!
!       ! Position of variables, coefficients
!       ! -----------------------------------
!
!       ! --- Number of the thermal variable
!       !       To use user scalar 2 instead, write 'ivart = isca(2)'
!
!       if (iscalt.gt.0) then
!         ivart = isca(2)
!         call field_get_val_s(ivarfl(ivart), cvar_scalt)
!       else
!         write(nfecra,9010) iscalt
!         call csexit (1)
!       endif
!
!       ! --- Scalar's Lambda
!
!       call field_get_key_int(ivarfl(isca(iscal)), kivisl, ifcvsl)
!       if (ifcvsl.ge.0) then
!         call field_get_val_s(ifcvsl, cpro_vscalt)
!       else
!         cpro_vscalt => NULL()
!       endif
!
!       ! --- Stop if Lambda is not variable
!
!       if (ifcvsl.lt.0) then
!         write(nfecra,1010) iscal
!         call csexit (1)
!       endif
!
!  
!
!       do iel = 1, ncel
!         xvart = cvar_scalt(iel)
! 	T=xvart+273.15
!         cpro_vscalt(iel) = 21.2d-6*(1-0.007*T)
!       enddo
!
!     endif ! --- Tests on 'ith' and 'iscavr'
!
!   enddo ! --- Loop on scalars














!--------
! Formats
!--------

#if defined(_CS_LANG_FR)

 1000 format(                                                     &
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/,&
'@ @@ ATTENTION : ARRET LORS DU CALCUL DES GRANDEURS PHYSIQUES',/,&
'@    =========                                               ',/,&
'@    DONNEES DE CALCUL INCOHERENTES                          ',/,&
'@                                                            ',/,&
'@      usipsu indique que la chaleur specifique est uniforme ',/,&
'@        ICP = ',I10   ,' alors que                          ',/,&
'@      usphyv impose une chaleur specifique variable.        ',/,&
'@                                                            ',/,&
'@    Le calcul ne sera pas execute.                          ',/,&
'@                                                            ',/,&
'@    Modifier usipsu ou usphyv.                              ',/,&
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/)
 1010 format(                                                     &
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/,&
'@ @@ ATTENTION : ARRET LORS DU CALCUL DES GRANDEURS PHYSIQUES',/,&
'@    =========                                               ',/,&
'@    DONNEES DE CALCUL INCOHERENTES                          ',/,&
'@                                                            ',/,&
'@    Pour le scalaire ', i10                                  ,/,&
'@      la diffusivite est uniforme alors que                 ',/,&
'@      usphyv impose une diffusivite variable.               ',/,&
'@                                                            ',/,&
'@    Le calcul ne sera pas execute.                          ',/,&
'@                                                            ',/,&
'@    Modifier usipsu ou usphyv.                              ',/,&
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/)
 9010 format(                                                     &
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/,&
'@ @@ ATTENTION : ARRET LORS DU CALCUL DES GRANDEURS PHYSIQUES',/,&
'@    =========                                               ',/,&
'@    APPEL A csexit DANS LE SOUS PROGRAMME usphyv            ',/,&
'@                                                            ',/,&
'@    La variable dont dependent les proprietes physiques ne  ',/,&
'@      semble pas etre une variable de calcul.               ',/,&
'@    En effet, on cherche a utiliser la temperature alors que',/,&
'@      ISCALT = ',I10                                         ,/,&
'@    Le calcul ne sera pas execute.                          ',/,&
'@                                                            ',/,&
'@    Verifier le codage de usphyv (et le test lors de la     ',/,&
'@      definition de IVART).                                 ',/,&
'@    Verifier la definition des variables de calcul dans     ',/,&
'@      usipsu. Si un scalaire doit jouer le role de la       ',/,&
'@      temperature, verifier que ISCALT a ete renseigne.     ',/,&
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@                                                            ',/)

#else

 1000 format(                                                     &
'@',/,                                                            &
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/,                                                            &
'@ @@ WARNING:  stop when computing physical quantities',/,       &
'@    =======',/,                                                 &
'@    Inconsistent calculation data',/,                           &
'@',/,                                                            &
'@      usipsu specifies that the specific heat is uniform',/,    &
'@        icp = ',i10   ,' while',/,                              &
'@      usphyv prescribes a variable specific heat.',/,           &
'@',/,                                                            &
'@    The calculation will not be run.',/,                        &
'@',/,                                                            &
'@    Modify usipsu or usphyv.',/,                                &
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/)
 1010 format(                                                     &
'@',/,                                                            &
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/,                                                            &
'@ @@ WARNING:  stop when computing physical quantities',/,       &
'@    =======',/,                                                 &
'@    Inconsistent calculation data',/,                           &
'@',/,                                                            &
'@    For scalar', i10,/,                                         &
'@      the diffusivity is uniform while',/,                      &
'@      usphyv prescribes a variable diffusivity.',/,             &
'@',/,                                                            &
'@    The calculation will not be run.',/,                        &
'@',/,                                                            &
'@    Modify usipsu or usphyv.',/,                                &
'@                                                            ',/,&
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/)
 9010 format(                                                     &
'@',/,                                                            &
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/,                                                            &
'@ @@ WARNING:  stop when computing physical quantities',/,       &
'@    =======',/,                                                 &
'@',/,                                                            &
'@    The variable on which physical properties depend does',/,   &
'@      seem to be a calculation variable.',/,                    &
'@    Indeed, we are trying to use the temperature while',/,      &
'@      iscalt = ',i10                                  ,/,&
'@',/,                                                            &
'@    The calculation will not be run.',/,                        &
'@',/,                                                            &
'@    Check the programming in usphyv (and the test when',/,      &
'@      defining ivart).',/,                                      &
'@    Check the definition of calculation variables in',/,        &
'@      usipsu. If a scalar should represent the,',/,             &
'@      temperature, check that iscalt has been defined',/,       &
'@',/,                                                            &
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@',/,&
'@',/)

#endif

!----
! End
!----

return
end subroutine usphyv


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

