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

!                      Code_Saturne version 4.0.3
!                      --------------------------
! 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_modules.f90
!>
!> \brief User-defined module: it allows to create any user array.
!>
!> 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

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

  ! Example: allocatable user arrays

  double precision, dimension(:), allocatable :: velxu
  double precision, dimension(:), allocatable :: velyu
  double precision, dimension(:), allocatable :: velzu
  double precision, dimension(:), allocatable :: epslu
  double precision, dimension(:), allocatable :: rixxu
  double precision, dimension(:), allocatable :: riyyu
  double precision, dimension(:), allocatable :: rizzu

  double precision, dimension(:), allocatable :: velxl
  double precision, dimension(:), allocatable :: velyl
  double precision, dimension(:), allocatable :: velzl
  double precision, dimension(:), allocatable :: rixxl
  double precision, dimension(:), allocatable :: riyyl
  double precision, dimension(:), allocatable :: rizzl
contains

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

  ! Allocate arrays

  subroutine init_user_module(n1, n2)

    ! Arguments

    integer, intent(in) :: n1
    integer, intent(in) :: n2

    ! Local variables

    integer :: err = 0

    if (n1 > 0) then
      if (.not. allocated(velxu)) then
        allocate(velxu(n1), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(velyu)) then
        allocate(velyu(n1), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(velzu)) then
        allocate(velzu(n1), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(rixxu)) then
        allocate(rixxu(n1), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(riyyu)) then
        allocate(riyyu(n1), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(rizzu)) then
        allocate(rizzu(n1), stat=err)
      endif
    endif

    if (n2 > 0) then 
      if (err .eq. 0 .and. .not. allocated(velxl)) then
        allocate(velxl(n2), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(velyl)) then
        allocate(velyl(n2), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(velzl)) then
        allocate(velzl(n2), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(rixxl)) then
        allocate(rixxl(n2), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(riyyl)) then
        allocate(riyyl(n2), stat=err)
      endif

      if (err .eq. 0 .and. .not. allocated(rizzl)) then
        allocate(rizzl(n2), stat=err)
      endif
    endif

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

    return

  end subroutine init_user_module

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

  ! Free related arrays

  subroutine finalize_user_module

    if (allocated(velxu)) then
      deallocate(velxu)
    endif

    if (allocated(velyu)) then
      deallocate(velyu)
    endif

    if (allocated(velzu)) then
      deallocate(velzu)
    endif

    if (allocated(rixxu)) then
      deallocate(rixxu)
    endif

    if (allocated(riyyu)) then
      deallocate(riyyu)
    endif

    if (allocated(rizzu)) then
      deallocate(rizzu)
    endif

    if (allocated(velxl)) then
      deallocate(velxl)
    endif

    if (allocated(velyl)) then
      deallocate(velyl)
    endif

    if (allocated(velzl)) then
      deallocate(velzl)
    endif

    if (allocated(rixxl)) then
      deallocate(rixxl)
    endif

    if (allocated(riyyl)) then
      deallocate(riyyl)
    endif

    if (allocated(rizzl)) then
      deallocate(rizzl)
   endif

  end subroutine finalize_user_module

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

end module user_module

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

!> (DOXYGEN_SHOULD_SKIP_THIS) \endcond
