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

!                      Code_Saturne version 4.0.0-rc2
!                      --------------------------
! 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 Basic example
!>
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! 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

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables

!< [loc_var_dec]
integer          iel, iutile, iscal
integer, allocatable, dimension(:) :: lstelt
double precision, dimension(:), pointer :: cvar_scal
!< [loc_var_dec]

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

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

!< [alloc]
allocate(lstelt(ncel)) ! temporary array for cells selection
!< [alloc]

!===============================================================================
! Variables initialization:
!
!   isca(1) is the number related to the first user-defined scalar variable.
!   cvar_scal(iel) is the value of this variable in cell number iel.
!
!   ONLY done if there is no restart computation
!===============================================================================

!< [init]
if (isuite.eq.0) then
  
!  if (itytur.eq.2) then
!    call field_get_val_s(ivarfl(ik), cvar_k)
!    call field_get_val_s(ivarfl(iep), cvar_ep)
!  endif

  if(nscaus.gt.0) then
    ! For each scalar
    do iscal = 1, nscaus
      ! If the scalar is associated to the considered phase iphas
!      if(iphsca(iscal).eq.iphas) then
        call field_get_val_s(ivarfl(isca(iscal)), cvar_scal)
        ! Initialize each cell value
        do iel = 1, ncel
          cvar_scal(iel) = 0.d0
        enddo
!      endif
    enddo
  endif

  do iel = 1, ncel
!    if (itytur.eq.2) then
!      cvar_k(iel)  = 8.64d-6
!      cvar_ep(iel) = 4.23d-8
!    endif
!    cvar_vel(1,iel) = 0.d0
!    cvar_vel(2,iel) = 0.d0
!    cvar_vel(3,iel) = 0.d0
    dt (iel) = 1.d0
  enddo

!  call field_get_val_s(ivarfl(isca(1)), cvar_scal)
!  do iel = 1, ncel
!    cvar_scal(iel) = 1.d0
!  enddo

!  call field_get_val_s(ivarfl(isca(2)), cvar_scal)

!  do iel = 1, ncel
!    cvar_scal(iel) = 25.d0
!  enddo

!  call field_get_val_s(ivarfl(isca(3)), cvar_scal)

!  do iel = 1, ncel
!    cvar_scal(iel) = 0.d0
!  enddo

!  call field_get_val_s(ivarfl(isca(4)), cvar_scal)

!  do iel = 1, ncel
!    cvar_scal(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
!< [init]

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

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

!< [finalize]
deallocate(lstelt)  ! temporary array for cells selection
!< [finalize]

return
end subroutine cs_user_initialization
