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

!                      Code_Saturne version 5.0.3
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2017 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
!>
!> See \subpage cs_user_initialization for examples.
!>
!> 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.
!>
!> 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 usphyv (in cs_user_physical_properties.f90)
!>
!> \c rom and \c viscl values are equal to \c ro0 and \c viscl0 or initialize
!> by reading the restart file.
!> Variables diffusivity and specific heat (when they are defined) have no value
!> except if they are read from a restart file.
!>
!> \par cs_user_initialization_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.
!>
!> Field cell values arrays can be retrieved using the appropriate access
!> functions as described \ref field "here".
!>
!> Example of field ids:
!> - Density:                        \c irom
!> - Dynamic molecular viscosity:    \c iviscl
!> - Turbulent viscosity:            \c ivisct
!> - Specific heat:                  \c icp
!> - Diffusivity(lambda):            \c field_get_key_int(ivarfl(isca(iscal)),
!>                                      kivisl, ...)
!>
!
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! 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_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
use turbomachinery

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)
double precision enth_fluid,enth_cathode,enth_anode,enth_hotcol


! Local variables

! INSERT_VARIABLE_DEFINITIONS_HERE
integer          ifcn!,ifcpotr  !pointers
integer, allocatable, dimension(:) :: lstelt
integer          iel, ilelt, nlelt
double precision, dimension(:), pointer :: cvar_scalt,cpro_celln!,cvar_potr
double precision rayo
!double precision potmin,potmax,dpotcat,dpotand,dzcat,zmin,zmax,dzand,dzfluid,dpotfl
!===============================================================================     
 
 
!===============================================================================
! Initialization
!===============================================================================
!===============================================================================
! Variables initialization:
!
!   ONLY done if there is no restart computation
!===============================================================================
!Cell Numbers field assignment. Cell Numbers field is defined in cs_user_parameters.f90
call field_get_id_try('CellNumber', ifcn)              !Obtaining pointer to Cell Numbers.
write(*,'(A39,I3)')'Cell Numbers field assignment. Field Id=',ifcn
if (ifcn.ge.0) call field_get_val_s(ifcn, cpro_celln) !obtaining Cell Numbers field
do iel = 1, ncel
  cpro_celln(iel)=iel                                  !
end do

if (isuite.eq.0) then
  allocate(lstelt(ncel)) ! temporary array for cells selection
  call field_get_val_s(ivarfl(isca(ihm)), cvar_scalt) !pointer to enthalpy
  !Enthalpy initialization values:
  enth_fluid = 1.4d4        !300K for Argon
  enth_cathode=1.06d5 !1000K for Tungsten
  enth_anode = 1.4d4
  enth_hotcol= 0.59663E+07  !10000K for Argon
  write(*,'(A31,I3)')'Initialization of enthalpy. Id=',ivarfl(isca(ihm))
  write(*,'(A19,E12.3)')'    Fluid Enthalpy =',enth_fluid
  write(*,'(A19,E12.3)')'  Cathode Enthalpy =',enth_cathode
  write(*,'(A19,E12.3)')'    Anode Enthalpy =',enth_anode
  write(*,'(A19,E12.3)')'HotColumn Enthalpy =',enth_hotcol
  
  !call field_get_id_try('current_re_3', ifcurz)
  !if (ifcurz.ge.0) call field_get_val_s(ifcurz, cpro_curz)
  !if (ifcurz.eq.0) print*,'No current_re_3 id. cs_user_initialization.f90' !error message just in case. 
   
  !call field_get_id_try('elec_pot_r', ifcpotr)
  !if (ifcpotr.ge.0) call field_get_val_s(ifcpotr, cvar_potr)
  !if (ifcpotr.eq.0) print*,'No elec_pot_r id. cs_user_initialization.f90' !error message just in case. 
  !potmin =-32.426d0
  !potmax =0d0
  !dpotcat=2.6d-2
  !dpotand=2d-4
  !dzcat = 1.9d-2
  !zmin = -1.9d-2
  !zmax =  2.2d-2
  !dzand = 0.4d-2
  !dzfluid=zmax-zmin-dzcat-dzand !1.8d-2
  !dpotfl=potmax-potmin-dpotcat-dpotand
  do iel = 1, ncel
    cvar_scalt(iel) = enth_fluid
    !rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
    !if(rayo.lt.0.3d-3 .and. ifcurz.ge.0) cpro_curz(iel)= -5d5     !TEST OF INITIALIZATION WITH NONZERO Z CURRENT

    !if(xyzcen(3,iel).lt.0d0) cvar_potr(iel)=potmin+(xyzcen(3,iel)-zmin)*dpotcat/dzcat   !Phi=Phi0+(z-zmin)*dPhicat/dzcat
    !if(xyzcen(3,iel).gt.1.8d-2) cvar_potr(iel)=potmin+(xyzcen(3,iel)-zmax)*dpotand/dzand
    !if(xyzcen(3,iel).ge.0d0 .and. xyzcen(3,iel).le.1.8d-2) then
    !  cvar_potr(iel)=potmin+dpotcat+(xyzcen(3,iel)-dzcat)*dpotfl/dzfluid
    !endif
    
  !  if(xyzcen(3,iel).ge. 2.15e-02 .and. rayo.lt.1.5d-3) cvar_scalt(iel) = 0.59663E+07
  !  if(xyzcen(3,iel).le.-1.85e-02 .and. rayo.lt.1.0d-3) cvar_scalt(iel) = 0.59663E+07
  !  !if(xyzcen(3,iel).le.0.0 .and. xyzcen(3,iel).ge.-1.85e-02) cvar_scalt(iel) = 0.59663E+07
  enddo
  call getcel('domaine_cathode',nlelt,lstelt) 
  do ilelt = 1, nlelt
    iel = lstelt(ilelt)
    cvar_scalt(iel) = enth_cathode
    !if(xyzcen(3,iel).ge.-6d-4) cvar_scalt(iel)=enth_cathode*(1d0+3d0*(xyzcen(3,iel)+6d-4)/6d-4)
  enddo
  call getcel('chute_cathodique',nlelt,lstelt) 
  do ilelt = 1, nlelt
    iel = lstelt(ilelt)
    rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
    if(rayo.lt.0.5d-3) cvar_scalt(iel) = enth_hotcol
  enddo
  call getcel('domaine_fluide',nlelt,lstelt) 
  do ilelt = 1, nlelt
    iel = lstelt(ilelt)
    rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
    if(rayo.lt.0.5d-3) cvar_scalt(iel) = enth_hotcol
  enddo
  call getcel('chute_anodique',nlelt,lstelt) 
  do ilelt = 1, nlelt
    iel = lstelt(ilelt)
    rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
    if(rayo.lt.0.5d-3) cvar_scalt(iel) = enth_hotcol
  enddo
  call getcel('anode',nlelt,lstelt) 
  do ilelt = 1, nlelt
    iel = lstelt(ilelt)
    cvar_scalt(iel) = enth_anode
  enddo
  !NO POTENTIAL INITIALIZATION (real, imaginary and vector)
  deallocate(lstelt) ! temporary array for cells selection
endif
!--------
! Formats
!--------

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



return
end subroutine cs_user_f_initialization
