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

!                      Code_Saturne version 
!                      --------------------------
! 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_initialization.f90
!>
!> \brief Initialize variables
!>
!> This subroutine is called at beginning of the computation
!> (restart or not) before the loop time step.
!>
!> This subroutine enables to initialize or modify (for restart)
!> unkown variables and time step values.
!>
!> \c rom and \c viscl values are equal to \c ro0 and \c viscl0 or initialize
!> by reading the restart file.
!> variable diffusivity and cp variables (when there are defined) have no value
!> excepted if they are read from a restart file.
!>
!> Physical quantities are defined in the following arrays:
!> \code
!>  propce ! physical quantities defined at cell center
!> \endcode
!>
!> Examples:
!> \code
!>  propce(iel, ipproc(irom  )) ! means rom  (iel)
!>  propce(iel, ipproc(iviscl)) ! means viscl(iel)
!>  propce(iel, ipproc(icp   )) ! means cp   (iel)
!> \endcode
!>
!> Modification of the behaviour law of physical quantities (rom, viscl,
!> viscls, cp) is not done here. It is the purpose of the user subroutine
!> \ref cs_user_physical_properties.
!>
!> \section 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]     dt            time step (per cell)
!_______________________________________________________________________________

subroutine cs_user_initialization &
 ( nvar   , nscal  ,                                              &
   dt     )

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

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

use paramx
use pointe
use numvar
use optcal
use cstphy
use cstnum
use entsor
use parall
use period
use ppppar
use ppthch
use coincl
use cpincl
use ppincl
use atincl
use ctincl
use elincl
use ppcpfu
use cs_coal_incl
use cs_fuel_incl
use mesh
use field
use turbomachinery
use setup
use cfpoin, only:ithvar

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables
integer iel
double precision, parameter:: kvor=1.d0
! INSERT_VARIABLE_DEFINITIONS_HERE

integer, allocatable, dimension(:) :: lstelt
double precision, dimension(:), pointer ::  cpro_rom
double precision, dimension(:,:), pointer :: cvar_vel
double precision, dimension(:), pointer :: cvar_pr
double precision, dimension(:), pointer :: cvar_scal, cvar_tempk, cvar_energ


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


!===============================================================================
! Initialization
!===============================================================================

allocate(lstelt(ncel)) ! temporary array for cells selection
call field_get_val_v(ivarfl(iu), cvar_vel)

! Map field arrays
call field_get_val_s(icrom, cpro_rom)
call field_get_val_s(ivarfl(ipr), cvar_pr)

write(nfecra,*) 'Set up parameters'
write(nfecra,*) 'Re   ', re
write(nfecra,*) 'Ma   ', ma
write(nfecra,*) 'rho  ', rho_ref
write(nfecra,*) 'mu   ', viscosity_mu
write(nfecra,*) 'cp   ', cp_ref ,' cp0 ', cp0
write(nfecra,*) 'cv   ', cv0
write(nfecra,*) 'R    ', rr
write(nfecra,*) 'M    ', MM_ref
write(nfecra,*) 'T    ', T_ref
write(nfecra,*) 'Uinf ', vel_ref
write(nfecra,*) 'Pinf ', p0


if (isuite.eq.0) then
  do iel = 1, ncel
     cvar_vel(1,iel) = uref
     cvar_vel(2,iel) = 0.d0
     cvar_vel(3,iel) = 0.d0
  enddo
    ! 1. Pressure (Pa)
  if(.true.) then
    ithvar = ithvar*2
    do iel = 1, ncel
      cvar_pr(iel) = p0
    enddo
  endif

  ! 2. Density (kg.m-3)
  if(.false.) then
    ithvar = ithvar*3
    do iel = 1, ncel
        cpro_rom(iel) = ro0
    enddo
  endif

  ! 3. Temperature (K -- Warning: Kelvin)
  if(.true.) then
    ithvar = ithvar*5
    call field_get_val_s(ivarfl(isca(itempk)), cvar_tempk)
    do iel = 1, ncel
      cvar_tempk(iel) = t0
    enddo
  endif

  ! 4. Total Energy (J/kg)
  if(.false.) then
    ithvar = ithvar*7
    call field_get_val_s(ivarfl(isca(ienerg)), cvar_energ)
    do iel = 1, ncel
      cvar_energ(iel) = cv0*t0
    enddo
  endif

endif


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

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

deallocate(lstelt) ! temporary array for cells selection

return
end subroutine cs_user_initialization
