!---------------------------------------------------------------------------
! pmlib_finalise.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
!---------------------------------------------------------------------------
MODULE pmlib_mod_finalise

USE pmlib_mod_parameters
USE pmlib_mod_write

IMPLICIT NONE

  INTERFACE pmlib_finalise
    MODULE PROCEDURE pmlib_finalise
  END INTERFACE

CONTAINS


SUBROUTINE pmlib_finalise(part,mesh,ierr)

USE pmlib_mod_mesh
USE pmlib_mod_particles
USE pmlib_mod_topology

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

!---------------------------------------------------------------------------
! Local variables
!---------------------------------------------------------------------------

!---------------------------------------------------------------------------
! Initiate subroutine
!---------------------------------------------------------------------------
  ierr = 0

!---------------------------------------------------------------------------
! De-allocate particles
!---------------------------------------------------------------------------
  IF( ASSOCIATED(part%pos) ) DEALLOCATE( part%pos )
  IF( ASSOCIATED(part%vel) ) DEALLOCATE( part%vel )
  IF( ASSOCIATED(part%vort) ) DEALLOCATE( part%vort )
  IF( ASSOCIATED(part%dvort)) DEALLOCATE( part%dvort )
  IF( ASSOCIATED(part%vel_rk) ) DEALLOCATE( part%vel_rk )
  IF( ASSOCIATED(part%dvort_rk) ) DEALLOCATE( part%dvort_rk )
  IF (ierr .NE. 0) THEN
    CALL pmlib_write('pmlib_allocate_mesh','Failed to deallocate array.')
    GOTO 9999
  ENDIF

!---------------------------------------------------------------------------
! De-allocate mesh
!---------------------------------------------------------------------------
  IF( ASSOCIATED(mesh%vel) ) DEALLOCATE( mesh%vel )
  IF( ASSOCIATED(mesh%vort) ) DEALLOCATE( mesh%vort )
  IF( ASSOCIATED(mesh%dvort) ) DEALLOCATE( mesh%dvort )
  IF( ASSOCIATED(mesh%pkernel_fft) ) DEALLOCATE(mesh%pkernel_fft)
  IF (ierr .NE. 0) THEN
    CALL pmlib_write('pmlib_allocate_mesh','Failed to deallocate array.')
    GOTO 9999
  ENDIF

!---------------------------------------------------------------------------
! Return
!---------------------------------------------------------------------------
 9999 CONTINUE
RETURN

END SUBROUTINE pmlib_finalise

END MODULE pmlib_mod_finalise
