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

!VERS

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

! INSERT_VARIABLE_DEFINITIONS_HERE

integer, allocatable, dimension(:) :: lstelt
character(len=80) :: f_name

double precision, dimension(:), pointer :: chm, cycoel, cpotr, cpoti
double precision, dimension(:,:), pointer :: cpotva

double precision rayo
double precision tinit, hinit, coefe(ngazem), hhot
integer idim, iel, iesp, mode

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


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

allocate(lstelt(ncel)) ! temporary array for cells selection

!===============================================================================
! Initialization
!    (only at the beginning of the calculation)
!===============================================================================

if ( isuite.eq.0 ) then

! --> Enthalpy = H(T0) ou 0

!     For electric arc,
!     for the whole compution domain enthalpy is set to H(T0) of the 1st
!     constituant of the gas
!
!     For Joule jeating by direct conduction,
!     enthalpy is set to zero, and the user will enter his H(T) function
!     tabulation.
!
!   --  HINIT calculations

  hhot = 0.4075d07

  if ( ippmod(ielarc).ge.1 ) then
    mode = -1
    tinit = t0
    coefe(1) = 1.d0
    if ( ngazg .gt. 1 ) then
      do iesp = 2, ngazg
        coefe(iesp) = 0.d0
      enddo
    endif
    call elthht(mode,coefe,hinit,tinit)
  else
    mode = -1
    tinit = t0
    call usthht(mode,hinit,tinit)
  endif

!    -- Enthalpy value

  call field_get_val_s(ivarfl(isca(iscalt)), chm)

  do iel = 1, ncel
    chm(iel) = hinit
    rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
    if(rayo .le. 0.8d-3) then
      chm(iel) = hhot
    endif
  enddo


! --> Mass fraction  = 1 ou 0

  if ( ngazg .gt. 1 ) then
    write(f_name,'(a13,i2.2)') 'esl_fraction_', 1
    call field_get_val_s_by_name(trim(f_name), cycoel)
    do iel = 1, ncel
      cycoel(iel) = 1.d0
    enddo
    do iesp = 2, ngazg-1
      write(f_name,'(a13,i2.2)') 'esl_fraction_',iesp
      call field_get_val_s_by_name(trim(f_name), cycoel)
      do iel = 1, ncel
        cycoel(iel) = 0.d0
      enddo
    enddo
  endif


! --> Electric potentials = 0

  !call field_get_val_s(ivarfl(isca(ipotr)), cpotr)
  call field_get_val_s_by_name('elec_pot_r', cpotr)

!     -- Real Component
  do iel = 1, ncel
    cpotr(iel) = 0.d0
  enddo

!     -- Imaginary (for Joule heating by direct conduction)
  if ( ippmod(ieljou).eq.2 .or. ippmod(ieljou).eq.4 ) then
    !call field_get_val_s(ivarfl(isca(ipoti)), cpoti)
    call field_get_val_s_by_name('elec_pot_i', cpoti)
    do iel = 1, ncel
      cpoti(iel) = 0.d0
    enddo
  endif

!     -- Vector potential (3D electric arc 3D)
  if ( ippmod(ielarc).ge.2 ) then
      call field_get_val_v_by_name('vec_potential', cpotva)
      do iel = 1, ncel
        do idim = 1, 3
          cpotva(idim,iel) = 0.d0
        enddo
      enddo
  endif

endif

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

deallocate(lstelt) ! temporary array for cells selection

return
end subroutine cs_user_f_initialization
