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

!                      Code_Saturne version 6.0
!                      ------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2019 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_modules.f90
!>
!> \brief User-defined module: it allows to create any user array.
!>
!> See \subpage cs_user_modules for examples.
!>
!> This file is compiled before all other user Fortran files.
!> To ensure this, it must not be renamed.
!>
!> The user may define an arbitrary number of modules here, even though
!> only one is defined in the example.
!
!> \cond DOXYGEN_SHOULD_SKIP_THIS

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

module user_module

implicit none

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! ajouts KD/CF 16/2/21
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!!!!!!! PARAMETRES PHYSIQUES !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! Declarations des grandeurs non parametrees
  double precision :: ru_tphys

! Declaration des grandeurs parametrables
  double precision, parameter :: ru_reyref = 1.d6 ! ru_U_inf * ru_Ly / nu
  double precision, parameter :: ru_U_inf = 7.d1 ! vitesse de l'eclt amont
  double precision, parameter :: ru_int_turb = 1.d-2 ! intensite turbulente
  double precision, parameter :: pii = 3.14159d0



!!!!!!!! PARAMETRES GEOMETRIQUES !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! Declaration des grandeurs parametrables
  integer, parameter :: iu_Nx = 1000
  integer, parameter :: iu_Ny = 79
  integer, parameter :: iu_Nz = 100
  double precision :: ru_totvol
  double precision, parameter :: ru_Lx = 5.d-1
  double precision, parameter :: ru_Ly = 1.d-1
  double precision, parameter :: ru_Lz = 2.d-2
  double precision, parameter :: ru_prog = 1.2d0


!!!!!!!!! CONSTANTES HTLES ET MODELE DE TURBULENCE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  double precision, parameter :: ru_gamma = 2.d0/3.d0
  double precision, parameter :: ru_beta0 = 4.8d-1
  double precision, parameter :: ru_cmu = 9.d-2
  integer, parameter :: ihtles = 1

!!!!!!!! VARIABLES HTLES NON CONSTANTES !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  double precision, dimension(:),pointer :: psiomg => null()
  double precision, dimension(:),pointer :: rk => null()


contains

  !=============================================================================
!< [allocate]
  ! Allocate arrays

  subroutine init_user_module(ncel, ncelet)

    ! Arguments

    integer, intent(in) :: ncel, ncelet
  
    ! Local variables

    integer :: err = 0

    if ((err .eq. 0).and.(.not.associated(psiomg))) then
      allocate(psiomg(ncelet), stat=err)
      allocate(rk(ncelet), stat=err)
    endif

    if (err /= 0) then
      write (*, *) "Error allocating array."
      call csexit(err)
    endif

    return

  end subroutine init_user_module

!!< [allocate]
!  !=============================================================================
!!< [c_pointer]
!  ! Pass pointer to psiomg array to C
!
  function get_user_module_psiomg() result(r) &
    bind(C, name='get_user_module_psiomg')
    use, intrinsic :: iso_c_binding
    implicit none
    type(c_ptr) :: r
    if (associated(psiomg)) then
      r = c_loc(psiomg(1))
    else
      r = c_null_ptr
    endif
  end function get_user_module_psiomg
!!< [c_pointer]
!  !=============================================================================
!  ! Pass pointer to rk array to C
!
  function get_user_module_rk() result(r) &
    bind(C, name='get_user_module_rk')
    use, intrinsic :: iso_c_binding
    implicit none
    type(c_ptr) :: r
    if (associated(rk)) then
      r = c_loc(rk(1))
    else
      r = c_null_ptr
    endif
  end function get_user_module_rk
!!< [c_pointer]
  !=============================================================================

  !< [free]
  ! Free related arrays

  subroutine finalize_user_module

    if (associated(psiomg)) then
      deallocate(psiomg)
    endif
    if (associated(rk)) then
      deallocate(rk)
    endif

  end subroutine finalize_user_module

  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! end ajouts KD/CF 16/2/21
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

end module user_module

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

!> (DOXYGEN_SHOULD_SKIP_THIS) \endcond
