 SUBROUTINE ini_w(calc,nzonei,zonei)
!=========================== DOCUMENTATION ============================= 
!DOCDEBh*    UTI/PRE/ini_w  *
!
!    NOM
! ini_w(calc,nzonei,zonei)
!    DESCRIPTION  
!*
! Initialisation des ddls
!
! Dof initialization
!*
! 
!    SOURCE
! Ce logiciel est regi par la licence [CeCILL-B]
! This software is governed by the [CeCILL-B] license
!=========================== DEBUT DES DECLARATIONS ==================== 
!.1-----  Implicit, Use
  use num
  IMPLICIT NONE
!.2-----  Declaration
  type(calcul)                                      :: calc   !! Objet calcul / Calcul object
  real(kind=kind(0.d0)),allocatable,dimension(:,:)  :: zonei  !! parametres d initialisation / Initialization parameters
  integer                                           :: nzonei !! parametres d initialisation / Initialization parameters

  integer                                           :: i,j,k
!=========================== DEBUT DU CODE EXECUTABLE ==================
 
! Initialisation sur tout le domaine
      DO i=1,calc%msh%nb_cell
          calc%msh%list_cell(i)%w%vprim%v   =0.d0
          calc%msh%list_cell(i)%w%vbal%v    =0.d0
          calc%msh%list_cell(i)%balance%v   =0.d0
          calc%msh%list_cell(i)%fp%v        =0.d0
          calc%msh%list_cell(i)%w%gprim(1)%v=0.d0
          calc%msh%list_cell(i)%w%gprim(2)%v=0.d0
          calc%msh%list_cell(i)%w%gprim(3)%v=0.d0
          calc%msh%list_cell(i)%w%enti      =0.d0
          calc%msh%list_cell(i)%w%entf      =0.d0
          calc%msh%list_cell(i)%w%pe        =0.d0
          calc%msh%list_cell(i)%w%chi       =0.d0
          !CALL prim2bal(calc%msh%list_cell(i)%w)
      END DO


! Initialisation tube a choc
    if(nzonei.eq.0)then
      DO i=1,calc%msh%nb_cell
        IF (calc%msh%list_cell(i)%center%x.le.0.d0) THEN
            calc%msh%list_cell(i)%w%vprim%v=phy_state(1)%vprim%v
        ELSE
            calc%msh%list_cell(i)%w%vprim%v=phy_state(2)%vprim%v
        END IF
          CALL prim2bal(calc%msh%list_cell(i)%w)
      END DO  
    endif

! Initialisation par zone
    if(nzonei.gt.0)then
      DO i=1,calc%msh%nb_cell
        DO j=1,nzonei
             IF (calc%msh%list_cell(i)%center%x.ge.zonei(1,j).and. &
                 calc%msh%list_cell(i)%center%x.le.zonei(2,j).and. &
                 calc%msh%list_cell(i)%center%y.ge.zonei(3,j).and. &
                 calc%msh%list_cell(i)%center%y.le.zonei(4,j).and. &
                 calc%msh%list_cell(i)%center%z.ge.zonei(5,j).and. &
                 calc%msh%list_cell(i)%center%z.le.zonei(6,j)) THEN
                 do k=1,nvar
                   calc%msh%list_cell(i)%w%vprim%v(k)  = zonei(6+k,j)
                 enddo
                 IF(imodel_phy.eq.2.and.pes_xyz.eq.1)calc%msh%list_cell(i)%w%vprim%v(5)= &
                                                     calc%msh%list_cell(i)%w%vprim%v(5)+ &
                                                     calc%msh%list_cell(i)%w%vprim%v(1)*gpes_phy*&
                                                     (zonei(2,j)-calc%msh%list_cell(i)%center%x)
                 IF(imodel_phy.eq.2.and.pes_xyz.eq.2)calc%msh%list_cell(i)%w%vprim%v(5)=&
                                                     calc%msh%list_cell(i)%w%vprim%v(5)+ &
                                                     calc%msh%list_cell(i)%w%vprim%v(1)*gpes_phy*&
                                                     (zonei(4,j)-calc%msh%list_cell(i)%center%y)
                 IF(imodel_phy.eq.2.and.pes_xyz.eq.3)calc%msh%list_cell(i)%w%vprim%v(5)=&
                                                     calc%msh%list_cell(i)%w%vprim%v(5)+ &
                                                     calc%msh%list_cell(i)%w%vprim%v(1)*gpes_phy*&
                                                     (zonei(6,j)-calc%msh%list_cell(i)%center%z)
              END IF
        ENDDO
        CALL prim2bal(calc%msh%list_cell(i)%w)
      END DO  
    endif

! Initialisation par une fonction utilisateur
    if(nzonei.lt.0)then
      DO i=1,calc%msh%nb_cell
          CALL fct_iniw(calc%msh%list_cell(i)%center%x, &
                    calc%msh%list_cell(i)%center%y, &
                    calc%msh%list_cell(i)%center%z, &
                    calc%msh%list_cell(i)%w%vprim, &
                    -nzonei)
          CALL prim2bal(calc%msh%list_cell(i)%w)        
      END DO  
    endif

!----------------------------------------------------------------
! Si presence de solides
!----------------------------------------------------------------
      IF (calc%nb_sol .NE. 0) THEN
          DO j = 1,calc%nb_sol
               k=0
               DO i = 1,calc%msh%nb_cell
                    IF (         &
                    calc%msh%list_cell(i)%center%x<calc%list_sol(j)%xmax_s  &
                    .AND. calc%msh%list_cell(i)%center%x>calc%list_sol(j)%xmin_s  &
                    .AND. calc%msh%list_cell(i)%center%y<calc%list_sol(j)%ymax_s  &
                    .AND. calc%msh%list_cell(i)%center%y>calc%list_sol(j)%ymin_s  &
                    .AND. calc%msh%list_cell(i)%center%z<calc%list_sol(j)%zmax_s  &
                    .AND. calc%msh%list_cell(i)%center%z>calc%list_sol(j)%zmin_s) &
                    CALL raycasting(calc%list_sol(j),calc%msh%list_cell(i)%center%x,&
                                                     calc%msh%list_cell(i)%center%y,&
                                                     calc%msh%list_cell(i)%center%z,&
                                                     calc%msh%list_cell(i)%w%chi)
                    IF (calc%msh%list_cell(i)%w%chi .EQ. calc%list_sol(j)%id) then
                      k=k+1
                      calc%msh%list_cell(i)%w%vprim%v(1)=calc%list_sol(j)%rho
                      CALL prim2bal(calc%msh%list_cell(i)%w)
                    endif
               END DO
               calc%list_sol(j)%nb_cell = k
               write( *,1010)j,calc%msh%numdom,calc%list_sol(j)%nb_cell
1010           format('Solide ',i3,'  from domain',i3,'  number of cells affected',i9)
          END DO
      END IF
!----------------------------------------------------------------
! Cas particulier du modele de SaintVenant
!----------------------------------------------------------------
   IF(imodel_phy.ne.1)RETURN
   SELECT CASE (ibathy)
      CASE(0) ! Fond plat
          write(*,*)'INI_W-Info Flat Bottom and no friction'

      CASE(1) ! Initialisation de la bathymetrie par fonction utilisateur
          write(*,*)'INI_W-Info Bathymetry and friction  defined by user fonction number:',num_bathy
          DO i=1,calc%msh%nb_cell
              CALL fct_bathy(calc%msh%list_cell(i)%center%x, &
                             calc%msh%list_cell(i)%center%y, &
                             calc%msh%list_cell(i)%center%z, &
                             calc%msh%list_cell(i)%w%vprim%v(5), & 
                             calc%msh%list_cell(i)%w%gprim(1)%v(5),calc%msh%list_cell(i)%w%gprim(1)%v(5),&
                             calc%msh%list_cell(i)%w%vprim%v(6))
          END DO  

!----------------------------------------
      CASE (2) ! Initialisation de la bathymetrie par interpolation lineaire par bloc
          write(*,*)'INI_W-Info Bathymetry and friction defined linearly per block on file:',fich_bathy
          call calc_bathy(calc)

!------------------------------------------------------
      !~CASE (3) ! Initialisation de la bathymetrie par bloc sur le raffinement max
      !~    write(*,*)'INI_W-Info Bathymetry and friction defined per block at the highest mesh refinement level on file:',fich_bathy
      !~    
      CASE DEFAULT
         call print_err('READ_BATH','Unforeseen case')
          
  END SELECT
  ! if(ngrad_bathy.eq.1) call grad_bathy(calc)  !!!!!!!!!!!!!! A FAIRE  !!!!!!!!!!!!!!!!!!!!!!!!!
      
      


!===========================   FIN DE LA ROUTINE    ====================

END SUBROUTINE ini_w
!DOCFIN 
