!---------------------------------------------------------------------------
! pmlib_mesh_allocate.f
!   version: 2D single-core
!---------------------------------------------------------------------------
!                  __    __
!    ___  _ _     / /__ / /
!   | _ \/ ` \ _ / // // _ \
!   | ,_/_,_,_\  \_/\_/\___/
!   |_|                      Mads M. Hejlesen* and Jens H. Walther*^
!                            * Technical University of Denmark
!                            ^ ETH-Zurich
!
!---------------------------------------------------------------------------
! Copyright (c) 2014 Technical University of Denmark, 
!                    Department of Mechanical Engineering
!
! 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, see <http://www.gnu.org/licenses/>
!
! Contact: mmhej@mek.dtu.dk or jhwa@mek.dtu.dk
!---------------------------------------------------------------------------
SUBROUTINE pmlib_mesh_allocate(topo,mesh,ierr)

USE pmlib_mod_topology

IMPLICIT NONE
!---------------------------------------------------------------------------
! Arguments
!---------------------------------------------------------------------------
  TYPE(class_topology)                      :: topo
  TYPE(class_mesh)                          :: mesh
  INTEGER, INTENT(OUT)                      :: ierr

!---------------------------------------------------------------------------
! Local variables
!---------------------------------------------------------------------------
  INTEGER,DIMENSION(pmlib_ndim)             :: ncell
  INTEGER,DIMENSION(2*pmlib_ndim)           :: nghost

!---------------------------------------------------------------------------
! Allocate the multi level, multi patch type (not the actual mesh)
!---------------------------------------------------------------------------
  IF( ASSOCIATED(mesh%vort) ) DEALLOCATE( mesh%vort, stat=ierr)
  IF( ASSOCIATED(mesh%vel) ) DEALLOCATE( mesh%vel, stat=ierr)
  IF( ASSOCIATED(mesh%dvort)) DEALLOCATE( mesh%dvort, stat=ierr)

  IF (ierr .NE. 0) THEN
    CALL pmlib_write('pmlib_allocate_mesh','Failed to deallocate array.')
    GOTO 9999
  ENDIF

!---------------------------------------------------------------------------
! Allocate the mesh
!---------------------------------------------------------------------------
  ncell  = topo%ncell
  nghost = topo%nghost

  ALLOCATE( mesh%vel(pmlib_ndim, &
          & 1-nghost(1):ncell(1)+nghost(2), &
          & 1-nghost(3):ncell(2)+nghost(4)), &
          & stat=ierr)

  ALLOCATE( mesh%vort(pmlib_nvort, &
          & 1-nghost(1):ncell(1)+nghost(2), &
          & 1-nghost(3):ncell(2)+nghost(4)), &
          & stat=ierr)

  ALLOCATE( mesh%dvort(pmlib_nvort, &
          & 1-nghost(1):ncell(1)+nghost(2), &
          & 1-nghost(3):ncell(2)+nghost(4)), &
          & stat=ierr)

  IF (ierr .NE. 0) THEN
    CALL pmlib_write('pmlib_allocate_mesh','Failed to allocate array.')
    GOTO 9999
  ENDIF


!---------------------------------------------------------------------------
! Return
!---------------------------------------------------------------------------
 9999 CONTINUE
RETURN
END SUBROUTINE pmlib_mesh_allocate
