
subroutine largeTStep_RK2_calcAtildeNl( &
  & cptr_AtildeNl, &
  & cptr_xyz_RhoNl, cptr_VhNl, cptr_WNl, &
  & cptr_tfcoordz, cptr_derivate, cptr_vzderivate &
  & )

  !
  !
  !
  use, intrinsic :: iso_c_binding

  use Field_IcGrid3D_Manager, only: &
    & Field_IcGrid3D

  use TFCoord_IcGrid3D_helper, only: &
    & TFCoord_IcGrid3D

  use Derivate_Field_IcGrid2D_Manager, only: &
    & Derivate_Field_IcGrid2D

  use VDerivate_Field_IcGrid3D_Manager, only: &
    & VDerivate_Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & calc_AtildeNl

  !
  !
  implicit none

  type(c_ptr), value :: cptr_AtildeNl
  type(c_ptr), value :: cptr_xyz_RhoNl
  type(c_ptr), value :: cptr_VhNl
  type(c_ptr), value :: cptr_WNl
  type(c_ptr), value :: cptr_tfcoordz
  type(c_ptr), value :: cptr_derivate
  type(c_ptr), value :: cptr_vzderivate

  !
  !
  !

  type(Field_IcGrid3D), pointer :: ptr_AtildeNl
  type(Field_IcGrid3D), pointer :: ptr_xyz_RhoNl
  type(Field_IcGrid3D), pointer :: ptr_VhNl
  type(Field_IcGrid3D), pointer :: ptr_WNl
  type(TFCoord_IcGrid3D), pointer :: ptr_tfcoordz
  type(Derivate_Field_IcGrid2D), pointer :: ptr_derivate
  type(VDerivate_Field_IcGrid3D), pointer :: ptr_vzderivate

  !
  !

	call c_f_pointer(cptr_AtildeNl, ptr_AtildeNl)
  call c_f_pointer(cptr_xyz_RhoNl, ptr_xyz_RhoNl)
  call c_f_pointer(cptr_VhNl, ptr_VhNl)
  call c_f_pointer(cptr_WNl, ptr_WNl)
  call c_f_pointer(cptr_tfcoordz, ptr_tfcoordz)
  call c_f_pointer(cptr_derivate, ptr_derivate)
  call c_f_pointer(cptr_vzderivate, ptr_vzderivate)

  call calc_AtildeNl( &
    & ptr_AtildeNl, &
    & ptr_xyz_RhoNl, ptr_VhNl, ptr_WNl, &
    & ptr_tfcoordz, ptr_derivate, ptr_vzderivate )

end subroutine largeTStep_RK2_calcAtildeNl


subroutine largeTStep_RK2_calcCtildeNl( &
  & cptr_CtildeNl, &
  & cptr_VhNl, cptr_WNl, &
  & Omega &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use IcGrid3D_FVM_Manager, only:&
    & IcGrid3D_FVM

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D
    
  use TFCoord_IcGrid3D_helper, only:&
    & TFCoord_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & calc_CtildeNl

  implicit none

  type(c_ptr), value :: cptr_CtildeNl
  type(c_ptr), value :: cptr_VhNl
  type(c_ptr), value :: cptr_WNl
  real(DP), value :: Omega

  type(Field_IcGrid3D), pointer :: ptr_CtildeNl
  type(Field_IcGrid3D), pointer :: ptr_VhNl
  type(Field_IcGrid3D), pointer :: ptr_WNl
	
	call c_f_pointer(cptr_CtildeNl, ptr_CtildeNl)
	call c_f_pointer(cptr_VhNl, ptr_VhNl)
	call c_f_pointer(cptr_WNl, ptr_WNl)
	

  call calc_CtildeNl( &
    & ptr_CtildeNl, &
    & ptr_VhNl, ptr_WNl, &
    & Omega )

end subroutine largeTStep_RK2_calcCtildeNl


subroutine largeTStep_RK2_calcgtildeNl( &
  & cptr_gtildeNl, &
  & cptr_xyz_RhoNl, &
  & cptr_buoyancyNl, &
  & Grav &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & calc_gtildeNl

  implicit none

  type(c_ptr), value :: cptr_gtildeNl
  type(c_ptr), value :: cptr_xyz_RhoNl
  type(c_ptr), value :: cptr_buoyancyNl
  real(DP), value :: Grav

  type(Field_IcGrid3D), pointer :: ptr_gtildeNl
  type(Field_IcGrid3D), pointer :: ptr_xyz_RhoNl
  type(Field_IcGrid3D), pointer :: ptr_buoyancyNl

	call c_f_pointer(cptr_gtildeNl, ptr_gtildeNl)
	call c_f_pointer(cptr_xyz_RhoNl, ptr_xyz_RhoNl)
	call c_f_pointer(cptr_buoyancyNl, ptr_buoyancyNl)
	
	
  call calc_gtildeNl( &
    & ptr_gtildeNl, &
    & ptr_xyz_RhoNl, ptr_buoyancyNl, &
    & Grav )

end subroutine largeTStep_RK2_calcgtildeNl

subroutine largeTStep_RK2_calcHGradPNl( &
  & cptr_xyz_HGradPNl, &
  & cptr_xyz_PNl, &
  & cptr_tfcoordz, cptr_derivate, cptr_vzderivate &
  & )

  use, intrinsic :: iso_c_binding

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use TFCoord_IcGrid3D_helper, only:&
    & TFCoord_IcGrid3D

  use Derivate_Field_IcGrid2D_Manager, only:&
    & Derivate_Field_IcGrid2D

  use VDerivate_Field_IcGrid3D_Manager, only:&
    & VDerivate_Field_IcGrid3D

  use dc_types, only: DP

  use large_time_step_RK2_mod, only: &
    & calc_HGradPNl

  implicit none

  type(c_ptr), value :: cptr_xyz_HGradPNl
  type(c_ptr), value :: cptr_xyz_PNl
  type(c_ptr), value :: cptr_tfcoordz
  type(c_ptr), value :: cptr_derivate
  type(c_ptr), value :: cptr_vzderivate

  type(Field_IcGrid3D), pointer :: ptr_xyz_HGradPNl
  type(Field_IcGrid3D), pointer :: ptr_xyz_PNl
  type(TFCoord_IcGrid3D), pointer :: ptr_tfcoordz
  type(Derivate_Field_IcGrid2D), pointer :: ptr_derivate
  type(VDerivate_Field_IcGrid3D), pointer :: ptr_vzderivate


  call c_f_pointer(cptr_xyz_HGradPNl, ptr_xyz_HGradPNl)
  call c_f_pointer(cptr_xyz_PNl, ptr_xyz_PNl)
  call c_f_pointer(cptr_tfcoordz, ptr_tfcoordz)
  call c_f_pointer(cptr_derivate, ptr_derivate)
  call c_f_pointer(cptr_vzderivate, ptr_vzderivate)


  call calc_HGradPNl( &
    & ptr_xyz_HGradPNl, &
    & ptr_xyz_PNl, &
    & ptr_tfcoordz, ptr_derivate, ptr_vzderivate )

end subroutine largeTstep_RK2_calcHGradPNl

subroutine largeTStep_RK2_calchNl( &
  & cptr_xyz_hNl, cptr_xyz_ENl, &
  & cptr_xyz_RhoNl, &
  & Cp, Cv &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & calc_hNl

  implicit none

  type(c_ptr), value :: cptr_xyz_hNl
  type(c_ptr), value :: cptr_xyz_ENl
  type(c_ptr), value :: cptr_xyz_RhoNl
  real(DP), value :: Cp
  real(DP), value :: Cv

  type(Field_IcGrid3D), pointer :: ptr_xyz_hNl
  type(Field_IcGrid3D), pointer :: ptr_xyz_ENl
  type(Field_IcGrid3D), pointer :: ptr_xyz_RhoNl

  call c_f_pointer(cptr_xyz_hNl, ptr_xyz_hNl)
  call c_f_pointer(cptr_xyz_ENl, ptr_xyz_ENl)
  call c_f_pointer(cptr_xyz_RhoNl, ptr_xyz_RhoNl)

  call calc_hNl( &
    & ptr_xyz_hNl, ptr_xyz_ENl, &
    & ptr_xyz_RhoNl, &
    & Cp, Cv )

end subroutine largeTStep_RK2_calchNl


subroutine largeTStep_RK2_calcRhoNl( &
  & cptr_xyz_RhoNl, &
  & cptr_xyz_RhoBasic, cptr_xyz_RNl &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D
    
  use large_time_step_RK2_mod, only: &
    & calc_RhoNl

  implicit none

  type(c_ptr), value :: cptr_xyz_RhoNl
  type(c_ptr), value :: cptr_xyz_RhoBasic
  type(c_ptr), value :: cptr_xyz_RNl

  type(Field_IcGrid3D), pointer :: ptr_xyz_RhoNl
  type(Field_IcGrid3D), pointer :: ptr_xyz_RhoBasic
  type(Field_IcGrid3D), pointer :: ptr_xyz_RNl

  call c_f_pointer(cptr_xyz_RhoNl, ptr_xyz_RhoNl)
  call c_f_pointer(cptr_xyz_RhoBasic, ptr_xyz_RhoBasic)
  call c_f_pointer(cptr_xyz_RNl, ptr_xyz_RNl)

  call calc_RhoNl( &
    & ptr_xyz_RhoNl, &
    & ptr_xyz_RhoBasic, ptr_xyz_RNl )

end subroutine largeTStep_RK2_calcRhoNl


subroutine largeTstep_calc_GR_GVh_GW( &
  & cptr_GR, cptr_GVh, cptr_GW, &
  & cptr_AtildeNl, cptr_CtildeNl, &
  & hydrostatic_option &
  & )

  use, intrinsic :: iso_c_binding

  use Field_IcGrid3D_Manager, only:  &
    & Field_IcGrid3D

  use TFCoord_IcGrid3D_helper, only:  &
    & TFCoord_IcGrid3D

  use Derivate_Field_IcGrid2D_Manager, only:  &
    & Derivate_Field_IcGrid2D

  use dc_types, only: DP

  use large_time_step_RK2_mod, only: &
    & calc_GR_GVh_GW

  implicit none

  type(c_ptr), value :: cptr_GR  
  type(c_ptr), value :: cptr_GVh
  type(c_ptr), value :: cptr_GW
  type(c_ptr), value :: cptr_AtildeNl
  type(c_ptr), value :: cptr_CtildeNl
  integer, value :: hydrostatic_option

  logical :: lo_hydrostatic_option
  type(Field_IcGrid3D), pointer :: ptr_GR
  type(Field_IcGrid3D), pointer :: ptr_GVh
  type(Field_IcGrid3D), pointer :: ptr_GW
  type(Field_IcGrid3D), pointer :: ptr_AtildeNl
  type(Field_IcGrid3D), pointer :: ptr_CtildeNl

  call c_f_pointer(cptr_GR, ptr_GR)
  call c_f_pointer(cptr_GVh, ptr_GVh)
  call c_f_pointer(cptr_GW, ptr_GW)
  call c_f_pointer(cptr_AtildeNl, ptr_AtildeNl)
  call c_f_pointer(cptr_CtildeNl, ptr_CtildeNl)

  if ( hydrostatic_option == 0 ) then
    lo_hydrostatic_option = .false.
  else
    lo_hydrostatic_option = .true.
  end if

!  write(*,*) 'hydn option:', lo_hydrostatic_option
  call calc_GR_GVh_GW( &
    & ptr_GR, ptr_GVh, ptr_GW, &
    & ptr_AtildeNl, ptr_CtildeNl, &
    & lo_hydrostatic_option )

end subroutine largeTstep_calc_GR_GVh_GW


subroutine largeTstep_RK2_update_variable( &
  & cptr_variableAl, cptr_variableNl, cptr_variableNs &
  & )

  use, intrinsic :: iso_c_binding

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & update_variable

  implicit none

  type(c_ptr), value :: cptr_variableAl
  type(c_ptr), value :: cptr_variableNl
  type(c_ptr), value :: cptr_variableNs

  type(Field_IcGrid3D), pointer :: ptr_variableAl
  type(Field_IcGrid3D), pointer :: ptr_variableNl
  type(Field_IcGrid3D), pointer :: ptr_variableNs

  call c_f_pointer(cptr_variableAl, ptr_variableAl)
  call c_f_pointer(cptr_variableNl, ptr_variableNl)
  call c_f_pointer(cptr_variableNs, ptr_variableNs)

  call update_variable(ptr_variableAl, ptr_variableNl, ptr_variableNs)

end subroutine largeTstep_RK2_update_variable

subroutine largeTstep_RK2_calcbuoyancyNl( &
  & cptr_buoyancyNl, &
  & cptr_PNl, cptr_RNl, &
  & cptr_tfcoordz, cptr_vzderivate, &
  & Grav &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use TFCoord_IcGrid3D_helper, only:&
    & TFCoord_IcGrid3D

  use Derivate_Field_IcGrid2D_Manager, only:&
    & Derivate_Field_IcGrid2D

  use VDerivate_Field_IcGrid3D_Manager, only:&
    & VDerivate_Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & calc_buoyancyNl

  implicit none

  type(c_ptr), value :: cptr_buoyancyNl
  type(c_ptr), value :: cptr_PNl
  type(c_ptr), value :: cptr_RNl
  type(c_ptr), value :: cptr_tfcoordz
  type(c_ptr), value :: cptr_vzderivate
  real(DP), value :: Grav

  type(Field_IcGrid3D), pointer :: ptr_buoyancyNl
  type(Field_IcGrid3D), pointer :: ptr_PNl
  type(Field_IcGrid3D), pointer :: ptr_RNl
  type(TFCoord_IcGrid3D), pointer :: ptr_tfcoordz
  type(VDerivate_Field_IcGrid3D), pointer :: ptr_vzderivate

  call c_f_pointer(cptr_buoyancyNl, ptr_buoyancyNl)
  call c_f_pointer(cptr_PNl, ptr_PNl)
  call c_f_pointer(cptr_RNl, ptr_RNl)
  call c_f_pointer(cptr_tfcoordz, ptr_tfcoordz)
  call c_f_pointer(cptr_vzderivate, ptr_vzderivate)

  call calc_buoyancyNl( &
    & ptr_buoyancyNl, &
    & ptr_PNl, ptr_RNl, &
    & ptr_tfcoordz, ptr_vzderivate, Grav )

end subroutine largeTstep_RK2_calcbuoyancyNl

subroutine largeTstep_RK2_apply_numerical_filter( &
  & cptr_R, cptr_Vh, cptr_W, cptr_E, cptr_Rho, &
  & cptr_HVDiff_R, cptr_HVDiff_Vh, cptr_HVDiff_W, cptr_HVDiff_Ea, &
  & cptr_tfcoordz, cptr_tfcoordr, & 
  & cptr_derivate, cptr_vzderivate, cptr_vrderivate, &
  & gamma_h, gamma_v, DelTimeLong &
  & )

  use, intrinsic :: iso_c_binding

  use dc_types, only: &
    & DP

  use Field_IcGrid3D_Manager, only:&
    & Field_IcGrid3D

  use TFCoord_IcGrid3D_helper, only:&
    & TFCoord_IcGrid3D

  use Derivate_Field_IcGrid2D_Manager, only:&
    & Derivate_Field_IcGrid2D

  use VDerivate_Field_IcGrid3D_Manager, only:&
    & VDerivate_Field_IcGrid3D

  use large_time_step_RK2_mod, only: &
    & apply_numerical_filter

  implicit none

  type(c_ptr), value :: cptr_R, cptr_Vh, cptr_W, cptr_E, cptr_Rho
  type(c_ptr), value :: cptr_HVDiff_R, cptr_HVDiff_Vh, cptr_HVDiff_W, cptr_HVDiff_Ea
  type(c_ptr), value :: cptr_tfcoordz, cptr_tfcoordr
  type(c_ptr), value :: cptr_derivate, cptr_vzderivate, cptr_vrderivate
  real(DP), value :: gamma_h, gamma_v, DelTimeLong

  type(Field_IcGrid3D), pointer :: ptr_HVDiff_R, ptr_HVDiff_Vh, ptr_HVDiff_W, ptr_HVDiff_Ea
  type(Field_IcGrid3D), pointer :: ptr_R, ptr_Vh, ptr_W, ptr_E, ptr_Rho
  type(TFCoord_IcGrid3D), pointer :: ptr_tfcoordz, ptr_tfcoordr
  type(Derivate_Field_IcGrid2D), pointer :: ptr_derivate
  type(VDerivate_Field_IcGrid3D), pointer :: ptr_vzderivate, ptr_vrderivate

  call c_f_pointer(cptr_R, ptr_R)
  call c_f_pointer(cptr_Vh, ptr_Vh)
  call c_f_pointer(cptr_W, ptr_W)
  call c_f_pointer(cptr_E, ptr_E)
  call c_f_pointer(cptr_Rho, ptr_Rho)
  call c_f_pointer(cptr_HVDiff_R, ptr_HVDiff_R)
  call c_f_pointer(cptr_HVDiff_Vh, ptr_HVDiff_Vh)
  call c_f_pointer(cptr_HVDiff_W, ptr_HVDiff_W)
  call c_f_pointer(cptr_HVDiff_Ea, ptr_HVDiff_Ea)
  call c_f_pointer(cptr_tfcoordz, ptr_tfcoordz)
  call c_f_pointer(cptr_tfcoordr, ptr_tfcoordr)
  call c_f_pointer(cptr_derivate, ptr_derivate)
  call c_f_pointer(cptr_vzderivate, ptr_vzderivate)
  call c_f_pointer(cptr_vrderivate, ptr_vrderivate)

  call apply_numerical_filter(& 
    & ptr_R, ptr_Vh, ptr_W, ptr_E, ptr_Rho, &
    & ptr_HVDiff_R, ptr_HVDiff_Vh, ptr_HVDiff_W, ptr_HVDiff_Ea, &
    & ptr_tfcoordz, ptr_tfcoordr, &
    & ptr_derivate, ptr_vzderivate, ptr_vrderivate, &
    & gamma_h, gamma_v, DelTimeLong & 
    & )
  
end subroutine largeTstep_RK2_apply_numerical_filter
