!===================================================================================================================================
! Copyright (c) 2025 GVEC Contributors, Max Planck Institute for Plasma Physics
! License: MIT
!===================================================================================================================================
#include "defines.h"

!===================================================================================================================================
!>
!!# Module ** functional **
!!
!! contains the routines to initialize and finalize the functional
!!
!===================================================================================================================================
MODULE MODgvec_functional
! MODULES
USE MODgvec_Globals    ,ONLY:wp,Unit_stdOut,abort
USE MODgvec_c_functional, ONLY: t_functional
IMPLICIT NONE

PUBLIC

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

CONTAINS


!===================================================================================================================================
!> initialize the type functional with number of elements
!!
!===================================================================================================================================
SUBROUTINE InitFunctional(sf, which_functional)
! MODULES
USE MODgvec_MHD3D, ONLY :t_functional_mhd3d
IMPLICIT NONE
!-----------------------------------------------------------------------------------------------------------------------------------
! INPUT VARIABLES
  INTEGER       , INTENT(IN   ) :: which_functional
!-----------------------------------------------------------------------------------------------------------------------------------
! OUTPUT VARIABLES
  CLASS(t_functional), ALLOCATABLE,INTENT(INOUT) :: sf !! self
!-----------------------------------------------------------------------------------------------------------------------------------
! LOCAL VARIABLES
!===================================================================================================================================
  SELECT CASE(which_functional)
  CASE(1)
    ALLOCATE(t_functional_mhd3d :: sf)
  CASE DEFAULT
    CALL abort(__STAMP__, &
         "this functional choice does not exist (MHD3D=1) !")
  END SELECT

  sf%which_functional=which_functional
  CALL sf%init()

END SUBROUTINE InitFunctional


!===================================================================================================================================
!> finalize the type functional
!!
!===================================================================================================================================
SUBROUTINE FinalizeFunctional(sf)
! MODULES
IMPLICIT NONE
!-----------------------------------------------------------------------------------------------------------------------------------
! INPUT VARIABLES
!-----------------------------------------------------------------------------------------------------------------------------------
! OUTPUT VARIABLES
  CLASS(t_functional), ALLOCATABLE,INTENT(INOUT) :: sf !! self
!-----------------------------------------------------------------------------------------------------------------------------------
! LOCAL VARIABLES
!===================================================================================================================================
  CALL sf%free()

END SUBROUTINE FinalizeFunctional

END MODULE MODgvec_functional
