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

!                      Code_Saturne version 5.0.4
!                      --------------------------
! 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_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

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

#include "fluidian.h"
#define FLD_VERBOSE 2

module fluidian_indoor

  ! -------------------
  ! TRAIN
  ! -------------------
  !integer :: nbTrains
  INTEGER(kind=c_int)  :: nbTrains
  bind(C, name='nbtrains') :: nbTrains
 
  INTEGER, DIMENSION(:,:), POINTER :: train_elt => null()
    
contains

 
!< [allocate]
  ! Allocate arrays
  subroutine init_train_elt(n, m)

    ! Arguments
    integer, intent(in) :: n,m

    ! Local variables
    integer :: err = 0

    if (err .eq. 0 .and. .not.associated(train_elt)) then
      allocate(train_elt(n, m), stat=err)
      write(*,*) 'train_elt is associated'
    endif

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

    return

  end subroutine init_train_elt
!< [allocate]
  !=============================================================================
!< [c_pointer]
  ! Pass pointer to train_elt array to C

  function get_train_elt_pointer() result(r) &
    bind(C, name='get_train_elt_pointer')
    use, intrinsic :: iso_c_binding
    implicit none
    type(c_ptr) :: r
    if (associated(train_elt)) then
      write(*,*) 'get train_elt C pointer to train_elt(1,1) = ',train_elt(1,1)
      r = c_loc(train_elt(1,1))
    else
      r = c_null_ptr
    endif
  end function get_train_elt_pointer
!< [c_pointer]
  !=============================================================================
!< [free]
  ! Free related arrays

  subroutine finalize_train_elt

    if (associated(train_elt)) then
      deallocate(train_elt)
    endif

  end subroutine finalize_train_elt
!< [free]

!---------------------------------------------------------------------------------------------------------
end module
