!= Module HeatFluxBS1998
!
! Authors::   KAWABATA Takuya
! Version::   $Id: surfaceflux_bs1998.f90,v 1.1 2012-05-30 01:33:03 odakker Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2012. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview
!
! 荳企Κ, 荳矩Κ蠅�阜縺九ｉ縺ｮ荳螳壹�辭ｱ繝ｻ驕句虚驥上�迚ｩ雉ｪ繝輔Λ繝�け繧ｹ繧剃ｸ弱∴縺溷�ｴ蜷医↓繧医ｋ
! 驕句虚驥�, 貂ｩ蠎ｦ, 蜃晉ｵ先�蛻��螟牙喧邇�ｒ險育ｮ励☆繧九Δ繧ｸ繝･繝ｼ繝ｫ.
! Module HeatFluxConst 繧貞�縺ｫ菴懈�
!
!== Error Handling
!
!== Bugs
!
!== Note
!
!
!== Future Plans
!
!
module Surfaceflux_BS1998
  !
  !荳贋ｸ句｢�阜縺ｧ縺ｮ螳壹ヵ繝ｩ繝�け繧ｹ險育ｮ励Δ繧ｸ繝･繝ｼ繝ｫ
  !

  !繝｢繧ｸ繝･繝ｼ繝ｫ隱ｭ縺ｿ霎ｼ縺ｿ
  use dc_types, only: DP, STRING
  use dc_iounit,  only: FileOpen
  use dc_message, only: MessageNotify
  use gtool_historyauto, only: HistoryAutoAddVariable, HistoryAutoPut

  use mpi_wrapper,only: myrank
  use gridset,  only: imin,         & !x 譁ｹ蜷代�驟榊�縺ｮ荳矩剞
    &                 imax,         & !x 譁ｹ蜷代�驟榊�縺ｮ荳企剞
    &                 jmin,         & !y 譁ｹ蜷代�驟榊�縺ｮ荳矩剞
    &                 jmax,         & !y 譁ｹ蜷代�驟榊�縺ｮ荳企剞
    &                 kmin,         & !z 譁ｹ蜷代�驟榊�縺ｮ荳矩剞
    &                 kmax,         & !z 譁ｹ蜷代�驟榊�縺ｮ荳企剞
    &                 nx, ny, nz, ncmax
  use axesset, only:  xyz_dz,         & !z 譁ｹ蜷代�譬ｼ蟄千せ髢馴囈
    &                 xyz_avr_pyz,  &
    &                 xyz_avr_xqz,  &
    &                 pyz_avr_xyz,  &
    &                 xqz_avr_xyz
  use basicset, only: xyz_ExnerBZ,  & !繧ｨ繧ｯ繧ｹ繝翫�髢｢謨ｰ縺ｮ蝓ｺ譛ｬ蝣ｴ
    &                 xyz_PressBZ,  & !
    &                 xyz_PTempBZ,  & !貂ｩ菴阪�蝓ｺ譛ｬ蝣ｴ
    &                 xyz_TempBZ,   & !
    &                 xyzf_QMixBZ,  & !
    &                 xyz_DensBZ      !蝓ｺ譛ｬ蝣ｴ縺ｮ蟇�ｺｦ
  use constants,only: MolWtDry, PressBasis, TempSfc, PressSfc, CpDry, GasRDry
  use composition,only : IdxCC, IdxCG, SpcWetID, CondNum, MolWtWet, SpcWetSymbol
  use chemcalc,only : SvapPress
  use namelist_util, only: namelist_filename
  use timeset, only:  TimeN
  use DExnerDt, only: xyz_DExnerDt_xyz_xyzf
  use Radiation_Simple, only: xyz_PTempRadBS1998

  !證鈴ｻ吶�蝙句ｮ｣險遖∵ｭ｢
  implicit none

  !螻樊ｧ縺ｮ謖�ｮ�
  private

  !髢｢謨ｰ繧� public 縺ｫ險ｭ螳�
  public surfaceflux_bs1998_init
  public surfaceflux_bs1998_forcing

  !螟画焚螳夂ｾｩ
  real(DP), save :: SfcXMomFluxBtm = 0.0d0
  real(DP), save :: SfcXMomFluxTop = 0.0d0
  real(DP), save :: SfcYMomFluxBtm = 0.0d0
  real(DP), save :: SfcYMomFluxTop = 0.0d0
  real(DP), save :: SfcHeatFluxBtm = 0.0d0
  real(DP), save :: SfcHeatFluxTop = 0.0d0
  real(DP), save :: SfcQmixFluxBtm = 0.0d0
  real(DP), save :: SfcQmixFluxTop = 0.0d0

  character(*), parameter:: module_name = 'surfaceflux_bs1998'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name

contains
!!!------------------------------------------------------------------------!!!
  subroutine Surfaceflux_BS1998_init
    !
    !NAMELIST 縺九ｉ蠢�ｦ√↑諠��ｱ繧定ｪｭ縺ｿ蜿悶ｊ, 蜃ｺ蜉帛､画焚縺ｮ險ｭ螳壹ｒ陦後≧.
    !

    !證鈴ｻ吶�蝙句ｮ｣險遖∵ｭ｢
    implicit none

    !蜀�Κ螟画焚
    integer    :: l, unit

    !---------------------------------------------------------------
    ! NAMELIST 縺九ｉ諠��ｱ繧貞叙蠕�
    !
    NAMELIST /surfaceflux_bs1998_nml/ &
      & SfcXMomFluxBtm, SfcXMomFluxTop, SfcYMomFluxBtm, SfcYMomFluxTop, &
      & SfcHeatFluxBtm, SfcHeatFluxTop, SfcQmixFluxBtm, SfcQmixFluxTop

    call FileOpen(unit, file=namelist_filename, mode='r')
    read(unit, NML=surfaceflux_bs1998_nml)
    close(unit)

    if (myrank == 0) then
      call MessageNotify( "M", module_name, "SfcXMomFluxBtm = %f", &
        &                  d=(/SfcXMomFluxBtm/) )
      call MessageNotify( "M", module_name, "SfcXMomFluxTop = %f", &
        &                  d=(/SfcXMomFluxTop/) )
      call MessageNotify( "M", module_name, "SfcYMomFluxBtm = %f", &
        &                  d=(/SfcYMomFluxBtm/) )
      call MessageNotify( "M", module_name, "SfcYMomFluxTop = %f", &
        &                  d=(/SfcYMomFluxTop/) )
      call MessageNotify( "M", module_name, "SfcHeatFluxBtm = %f", &
        &                  d=(/SfcHeatFluxBtm/) )
      call MessageNotify( "M", module_name, "SfcHeatFluxTop = %f", &
        &                  d=(/SfcHeatFluxTop/) )
      call MessageNotify( "M", module_name, "SfcQmixFluxBtm = %f", &
        &                  d=(/SfcQmixFluxBtm/) )
      call MessageNotify( "M", module_name, "SfcQmixFluxTop = %f", &
        &                  d=(/SfcQmixFluxTop/) )
    end if

    call HistoryAutoAddVariable(      &
      & varname='PTempSfcFlux',       &
      & dims=(/'x','y','z','t'/),         &
      & longname='surface potential temperature flux (heat flux divided by density and specific heat)', &
      & units='K.m.s-1',             &
      & xtype='float')

    call HistoryAutoAddVariable(      &
      & varname='ExnerSfcFlux',       &
      & dims=(/'x','y','z','t'/),         &
      & longname='surface exner function flux (heat flux divided by density and specific heat)', &
      & units='s-1',             &
      & xtype='float')

    call HistoryAutoAddVariable(  &
      & varname='VelXSfcFlux',    &
      & dims=(/'x','y','z','t'/),     &
      & longname='surface flux of x-component of velocity (momentum flux divided by density)', &
      & units='m2.s-2',           &
      & xtype='float')

    call HistoryAutoAddVariable(  &
      & varname='VelYSfcFlux',    &
      & dims=(/'x','y','z','t'/),     &
      & longname='surface flux of y-component of velocity (momentum flux divided by density)', &
      & units='m2.s-2',           &
      & xtype='float')

    do l = 1, ncmax
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_SfcFlux', &
        & dims=(/'x','y','z','t'/),     &
        & longname='surface flux of '          &
        &           //trim(SpcWetSymbol(l))//' mixing ratio (mass flux divided by density)',  &
        & units='m.s-1',    &
        & xtype='float')
    end do
    call HistoryAutoAddVariable(  &
      & varname='PTempSfc',         &
      & dims=(/'x','y','z','t'/), &
      & longname='potential temperature tendency by surface flux', &
      & units='K.s-1',            &
      & xtype='float')

    call HistoryAutoAddVariable(  &
      & varname='ExnerSfc',         &
      & dims=(/'x','y','z','t'/), &
      & longname='exner function tendency by surface flux', &
      & units='K.s-1',            &
      & xtype='float')

    call HistoryAutoAddVariable(  &
      & varname='VelXSfc',         &
      & dims=(/'x','y','z','t'/), &
      & longname='x-component velocity tendency by surface flux', &
      & units='m.s-2',            &
      & xtype='float')

    call HistoryAutoAddVariable(  &
      & varname='VelYSfc',         &
      & dims=(/'x','y','z','t'/), &
      & longname='y-component velocity tendency by surface flux', &
      & units='m.s-2',            &
      & xtype='float')

    do l = 1, ncmax
      call HistoryAutoAddVariable(  &
        & varname=trim(SpcWetSymbol(l))//'_Sfc', &
        & dims=(/'x','y','z','t'/),     &
        & longname=trim(SpcWetSymbol(l))//' mixing ratio tendency by surface flux',  &
        & units='s-1',    &
        & xtype='float')
    end do
    call HistoryAutoAddVariable(       &
      & varname='SfcHeatFlux',         &
      & dims=(/'x','y','z','t'/),          &
      & longname='surface heat flux',  &
      & units='W.m-2',                 &
      & xtype='float')

    call HistoryAutoAddVariable(                      &
      & varname='SfcXMomFlux',                        &
      & dims=(/'x','y','z','t'/),                         &
      & longname='surface x-component momentum flux', &
      & units='kg.m-2.s-1',                           &
      & xtype='float')

    call HistoryAutoAddVariable(                      &
      & varname='SfcYMomFlux',                        &
      & dims=(/'x','y','z','t'/),                         &
      & longname='surface y-component momentum flux', &
      & units='kg.m-2.s-1',                           &
      & xtype='float')

    do l = 1, ncmax
      call HistoryAutoAddVariable(                               &
        & varname=trim(SpcWetSymbol(l))//'_SfcMassFlux',         &
        & dims=(/'x','y','z','t'/),                              &
        & longname=trim(SpcWetSymbol(l))//' surface mass flux',  &
        & units='kg.m-2.s-1',                                    &
        & xtype='float')
    end do
  end subroutine Surfaceflux_BS1998_init

!!!------------------------------------------------------------------------!!!
  subroutine Surfaceflux_BS1998_forcing( &
    &   pyz_DVelXDt, xqz_DVelYDt, xyz_DPTempDt, xyz_DExnerDt, xyzf_DQMixDt &
    & )
    !

    !證鈴ｻ吶�蝙句ｮ｣險遖∵ｭ｢
    implicit none

    !螟画焚螳夂ｾｩ
    real(DP), intent(inout):: pyz_DVelXDt(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(inout):: xqz_DVelYDt(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(inout):: xyz_DPTempDt(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(inout):: xyz_DExnerDt(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(inout):: xyzf_DQMixDt(imin:imax,jmin:jmax,kmin:kmax, ncmax)

    real(DP) :: pyr_VelXflux (imin:imax,jmin:jmax,kmin:kmax) !騾溷ｺｦ繝輔Λ繝�け繧ｹ
    real(DP) :: xqr_VelYflux (imin:imax,jmin:jmax,kmin:kmax) !騾溷ｺｦ繝輔Λ繝�け繧ｹ
    real(DP) :: xyr_PTempFlux(imin:imax,jmin:jmax,kmin:kmax) !貂ｩ蠎ｦ繝輔Λ繝�け繧ｹ
    real(DP) :: xyr_ExnerFlux(imin:imax,jmin:jmax,kmin:kmax) !Exner 髢｢謨ｰ蠑ｷ蛻ｶ鬆�
    real(DP) :: xyrf_QMixFlux(imin:imax,jmin:jmax,kmin:kmax,ncmax)
                                                             !迚ｩ雉ｪ繝輔Λ繝�け繧ｹ
    real(DP) :: pyr_SfcXMomFlux(imin:imax,jmin:jmax,kmin:kmax) !驕句虚驥上ヵ繝ｩ繝�け繧ｹ
    real(DP) :: xqr_SfcYMomFlux(imin:imax,jmin:jmax,kmin:kmax) !驕句虚驥上ヵ繝ｩ繝�け繧ｹ
    real(DP) :: xyr_SfcHeatFlux(imin:imax,jmin:jmax,kmin:kmax) !辭ｱ繝輔Λ繝�け繧ｹ

    real(DP) :: pyz_DVelXDtFlux (imin:imax,jmin:jmax,kmin:kmax)
    real(DP) :: xqz_DVelYDtFlux (imin:imax,jmin:jmax,kmin:kmax)
    real(DP) :: xyz_DPTempDtFlux(imin:imax,jmin:jmax,kmin:kmax)
    real(DP) :: xyz_DExnerDtFlux(imin:imax,jmin:jmax,kmin:kmax)
    real(DP) :: xyzf_DQMixDtFlux(imin:imax,jmin:jmax,kmin:kmax, ncmax)

    integer  :: s             !繝ｫ繝ｼ繝怜､画焚

!-----

    ! NAMLIST 縺ｧ險ｭ螳壹＠縺溷､繧剃ｻ｣蜈･
    !
!    xyr_SfcHeatFlux(:,:,1)  = - xyz_PTempRadBS1998(:,:,1) * CpDry * xyz_DensBZ(:,:,1) * xyz_dz(:,:,1)
!    xyr_SfcHeatFlux(:,:,nz) = - xyz_PTempRadBS1998(:,:,nz) * CpDry * xyz_DensBZ(:,:,nz) * xyz_dz(:,:,nz)
!    write(*,*) 'xyr_SfcHeatFluxBtm: ', xyr_SfcHeatFlux(1,1,1)
!    write(*,*) 'xyr_SfcHeatFluxTop: ', xyr_SfcHeatFlux(1,1,nz)

    xyr_SfcHeatFlux(:,:,1)  = SfcHeatFluxBtm
    xyr_SfcHeatFlux(:,:,nz) = SfcHeatFluxTop
!    write(*,*) 'xyr_SfcHeatFluxBtmNamelist: ', xyr_SfcHeatFlux(1,1,1)
!    write(*,*) 'xyr_SfcHeatFluxTopNamelist: ', xyr_SfcHeatFlux(1,1,nz)

    pyr_SfcXMomFlux(:,:,1)  = SfcXMomFluxBtm
    pyr_SfcXMomFlux(:,:,nz) = SfcXMomFluxTop

    xqr_SfcYMomFlux(:,:,1)  = SfcYMomFluxBtm
    xqr_SfcYMomFlux(:,:,nz) = SfcYMomFluxTop

    xyrf_QMixFlux(:,:,1,:)  = SfcQmixFluxBtm
    xyrf_QMixFlux(:,:,nz,:) = SfcQmixFluxTop

    ! 騾溷ｺｦ繝輔Λ繝�け繧ｹ, 貂ｩ蠎ｦ繝輔Λ繝�け繧ｹ縺ｸ縺ｮ螟画鋤
    !
    pyr_VelXFlux  = pyr_SfcXMomFlux / xyz_DensBz
    xqr_VelYFlux  = xqr_SfcYMomFlux / xyz_DensBz
    xyr_PtempFlux = xyr_SfcHeatFlux / xyz_DensBz / CpDry

    ! Exner 髢｢謨ｰ莠亥�ｱ蠑上�蠑ｷ蛻ｶ鬆��險育ｮ�
    !
    xyr_ExnerFlux = xyz_DExnerDt_xyz_xyzf(xyr_PTempFlux, xyrf_QMixFlux)

    ! 譎る俣螟牙喧邇��險育ｮ�
    !
    xyz_DPTempDtFlux = xyr_PTempFlux / xyz_dz
    xyz_DExnerDtFlux = xyr_ExnerFlux / xyz_dz
    do s = 1, ncmax
      xyzf_DQMixDtFlux(:,:,:,s) = xyrf_QMixFlux(:,:,:,s) / xyz_dz
    end do
    pyz_DVelXDtFlux  = pyr_VelXFlux / xyz_dz
    xqz_DVelYDtFlux  = xqr_VelYFlux / xyz_dz

    ! 莉ｮ蠑墓焚驟榊�縺ｸ縺ｮ蜉�邂�
    !
    xyz_DPTempDt = xyz_DPTempDt + xyz_DPTempDtFlux
    xyz_DExnerDt = xyz_DExnerDt + xyz_DExnerDtFlux
    do s = 1, ncmax
      xyzf_DQMixDt(:,:,:,s) = xyzf_DQMixDt(:,:,:,s) + xyzf_DQMixDtFlux(:,:,:,s)
    end do
    pyz_DVelXDt = pyz_DVelXDt + pyz_DVelXDtFlux
    xqz_DVelYDt = xqz_DVelYDt + xqz_DVelYDtFlux

    ! 蜃ｺ蜉�
    !
    call HistoryAutoPut(TimeN, 'PTempSfc', xyz_DPTempDtFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'ExnerSfc', xyz_DExnerDtFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'VelXSfc',  pyz_DVelXDtFlux (1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'VelYSfc',  xqz_DVelYDtFlux (1:nx,1:ny,1:nz))
    do s = 1, ncmax
      call HistoryAutoPut(TimeN, trim(SpcWetSymbol(s))//'_Sfc', &
        & xyzf_DQMixDtFlux(1:nx,1:ny,1:nz,s))
    end do
    call HistoryAutoPut(TimeN, 'PTempSfcFlux', xyr_PTempFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'ExnerSfcFlux', xyr_ExnerFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'VelXSfcFlux',  pyr_VelXFlux (1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'VelYSfcFlux',  xqr_VelYFlux (1:nx,1:ny,1:nz))
    do s = 1, ncmax
      call HistoryAutoPut(TimeN, trim(SpcWetSymbol(s))//'_SfcFlux', &
        & xyrf_QMixFlux(1:nx,1:ny,1:nz,s))
    end do

    call HistoryAutoPut(TimeN, 'SfcHeatFlux', xyr_SfcHeatFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'SfcXMomFlux', pyr_SfcXMomFlux(1:nx,1:ny,1:nz))
    call HistoryAutoPut(TimeN, 'SfcXMomFlux', xqr_SfcYMomFlux(1:nx,1:ny,1:nz))
    do s = 1, ncmax
      call HistoryAutoPut(TimeN, trim(SpcWetSymbol(s))//'_SfcMassFlux', &
        & xyz_DensBZ(1:nx,1:ny,1:nz) * xyrf_QMixFlux(1:nx,1:ny,1:nz,s))
    end do

  end subroutine Surfaceflux_BS1998_forcing

end module Surfaceflux_BS1998