!= Module Radiation
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: radiation.f90,v 1.13 2011-02-28 12:00:24 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview
!
!モデルの放射過程を計算するためのパッケージ型モジュール
!具体的には以下の項を計算するための関数を格納する.  
!  * 一様冷却
!
!== Error Handling
!
!== Bugs
!
!== Note
!
!
!== Future Plans
!
!

module Radiation
  !
  !モデルの放射過程を計算するためのパッケージ型モジュール
  !具体的には以下の項を計算するための関数を格納する.  
  !  * 一様冷却
  !

  !モジュール読み込み
  use dc_message, only: MessageNotify
  use debugset,   only: cpurank
  use GridSet,  only: DimXMin,     & !x 方向の配列の下限
    &                 DimXMax,     & !x 方向の配列の上限
    &                 DimZMin,     & !z 方向の配列の下限
    &                 DimZMax,     & !z 方向の配列の上限
    &                 RegZMin,     & !z 方向の配列の上限
    &                 RegZMax,     & !z 方向の配列の上限
    &                 s_Z            !Z 座標軸(スカラー格子点)
  use TimeSet,  only: DayTime        ! 1 日の長さ [s]
  use basicset, only: xz_ExnerBasicZ !エクスナー関数の基本場
!  use StorePotTemp,  only: StorePotTempRad, StorePotTempDamp
  
  !暗黙の型宣言禁止
  implicit none

  !private 属性
  private

  !関数を public にする. 
  public Radiation_init
  public Radiation_init2
  public xz_RadHeatConst
  public xz_RadHeatVary
  public xz_NewtonCool

  !変数定義
  real(8)              :: RadHeatRate = 0.0d0  !一様放射加熱率 [K/day]
  real(8), allocatable :: xz_RadHeight(:,:)    !放射加熱が存在する領域
  real(8)              :: RadHeight1 = 0.0d0
  real(8)              :: RadHeight2 = 0.0d0
  integer              :: k

  !save 属性
  save RadHeatRate, xz_RadHeight
  save RadHeight1, RadHeight2

contains

!!!------------------------------------------------------------------------!!!
  subroutine Radiation_init(cfgfile)
    !
    !NAMELIST から放射強制の設定を取得
    !

    !暗黙の型宣言禁止
    implicit none

    !入力変数
    character(*), intent(in) :: cfgfile
    real(8)                  :: RadHeightUp   !放射強制を与える鉛直領域の上限
    real(8)                  :: RadHeightDown !放射強制を与える鉛直領域の下限
    integer                  :: k             !ループ変数

    ! NAMELIST から情報を取得
    NAMELIST /radiation/ RadHeatRate, RadHeightUp, RadHeightDown

    open (10, FILE=cfgfile)
    read(10, NML=radiation)
    close(10)

    allocate( xz_RadHeight(DimXMin:DimXMax, DimZMin:DimZMax) )

    ! 放射強制が存在する領域の設定
    ! RadHeightDown < s_Z < RadHeightUp の範囲では値が 1 となる
    ! 係数を用意する.
    !
    xz_RadHeight = 1.0d0    

    do k = DimZMin, DimZMax
      if ( s_Z(k) <= RadHeightDown  ) then
        xz_RadHeight(:,k) = 0.0d0 
      elseif( s_Z(k) >= RadHeightUp ) then
        xz_RadHeight(:,k) = 0.0d0 
      end if
    end do

    if (cpurank == 0) then 
      call MessageNotify( "M", &
        & "Radiation", "RadHeatRate = %f", d=(/RadHeatRate/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeightUp = %f", d=(/RadHeightUP/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeightDown= %f", d=(/RadHeightDown/))
    end if
  end subroutine Radiation_init


  subroutine Radiation_init2(cfgfile)
    !
    !NAMELIST から放射強制の設定を取得
    !

    !暗黙の型宣言禁止
    implicit none

    !入力変数
    character(*), intent(in) :: cfgfile
    real(8)                  :: RadHeightUp   !放射強制を与える鉛直領域の上限
    real(8)                  :: RadHeightDown !放射強制を与える鉛直領域の下限
    integer                  :: k             !ループ変数

    ! NAMELIST から情報を取得
    NAMELIST /radiation/ RadHeatRate, RadHeightUp, RadHeightDown, RadHeight1, RadHeight2

    open (10, FILE=cfgfile)
    read(10, NML=radiation)
    close(10)

    allocate( xz_RadHeight(DimXMin:DimXMax, DimZMin:DimZMax) )

    ! 放射強制が存在する領域の設定
    ! RadHeightDown < s_Z < RadHeightUp の範囲では値が 1 となる
    ! 係数を用意する.
    !
    xz_RadHeight = 1.0d0    

    do k = DimZMin, DimZMax
      if ( s_Z(k) <= RadHeightDown  ) then
        xz_RadHeight(:,k) = 0.0d0 
      elseif( s_Z(k) >= RadHeightUp ) then
        xz_RadHeight(:,k) = 0.0d0 
      end if
    end do

    if (cpurank == 0) then 
      call MessageNotify( "M", &
        & "Radiation", "RadHeatRate = %f", d=(/RadHeatRate/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeightUp = %f", d=(/RadHeightUP/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeightDown= %f", d=(/RadHeightDown/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeight1= %f", d=(/RadHeight1/))
      call MessageNotify( "M", &
        & "Radiation", "RadHeight2= %f", d=(/RadHeight2/))
    end if
  end subroutine Radiation_init2


!!!------------------------------------------------------------------------!!!
  function xz_RadHeatConst(xz_Exner)
    !
    ! 温位の放射強制項. 
    ! 地表面から RadHeight で指定された高度までの間で一様放射冷却を与える. 
    !
    
    !暗黙の型宣言を禁止
    implicit none

    !変数定義
    real(8), intent(in)   :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
                                       !エクスナー関数の擾乱場
    real(8)               :: xz_RadHeatConst(DimXMin:DimXMax, DimZMin:DimZMax)
                                       !放射強制

    xz_RadHeatConst = &
      & xz_RadHeight * RadHeatRate  &
      & / ( ( xz_ExnerBasicZ + xz_Exner ) * DayTime )

!    call StorePotTempRad( xz_RadHeatConst )
    
  end function xz_RadHeatConst
  
  
!!!------------------------------------------------------------------------!!!
  function xz_RadHeatVary(xz_Exner)
  !
  ! 温位の放射強制項.
  ! 地表面から RadHeight1 で指定された高度までの間は2[K/day](RadHeatRate)の
  ! 一様な放射冷却を与える.
  ! RadHeight1 から RadHeight2 までは (RadHeight2 - s_Z(k)) / (RadHeight2 - RadHeight1)
  ! の割合で放射冷却の効果を減少させる (RadHeight2でちょうどゼロにする)
  ! Nakajima and Matsuno(1988),中島(1944)を参考にした
  !

  !暗黙の型宣言を禁止
  implicit none

  !変数定義
  real(8), intent(in)   :: xz_Exner(DimXMin:DimXMax, DimZMin:DimZMax)
                                     !エクスナー関数の擾乱場
  real(8)               :: xz_RadHeatVary(DimXMin:DimXMax, DimZMin:DimZMax)
                                     !放射強制

  do k = RegZMin+1, DimZMax
   if (s_Z(k).le.RadHeight1) then
    xz_RadHeatVary(:,k) = &
      & RadHeatRate  &
      & / ( ( xz_ExnerBasicZ(:,k) + xz_Exner(:,k) ) * DayTime )

   else if (s_Z(k).gt.RadHeight1.and.s_Z(k).le.RadHeight2) then
    xz_RadHeatVary(:,k) = &
      & RadHeatRate  * (RadHeight2 - s_Z(k)) &
      & / ( (RadHeight2 - RadHeight1) * ( xz_ExnerBasicZ(:,k) + xz_Exner(:,k) ) * DayTime )

   else if (s_Z(k).gt.RadHeight2) then
    xz_RadHeatVary(:,k) = 0.0d0

   end if
  end do

!  call StorePotTempRad( xz_RadHeatVary )

end function xz_RadHeatVary

!!!------------------------------------------------------------------------!!!
  function xz_NewtonCool(xz_PotTemp)
    !
    ! 温位の放射強制項. 
    ! 地表面から RadHeight で指定された高度までの間で一様放射冷却を与える. 
    !
    
    !暗黙の型宣言を禁止
    implicit none

    !変数定義
    real(8), intent(in)   :: xz_PotTemp(DimXMin:DimXMax, DimZMin:DimZMax)
                                       !温位の擾乱場
    real(8)               :: xz_NewtonCool(DimXMin:DimXMax, DimZMin:DimZMax)
                                       !放射強制
    real(8)               :: xz_PotTempMean(DimXMin:DimXMax, DimZMin:DimZMax)
                                       !温位擾乱の東西平均
    real(8)               :: EFTime    !放射緩和時間
    integer               :: k

    !放射緩和時間は 5 日
    EFTime = 5.0d0 * DayTime  
    
    do k = DimZMin, DimZMax
      xz_PotTempMean(:,k) = sum( xz_PotTemp(:,k) ) / real(DimXMax - DimXMin + 1)
    end do
    
    xz_NewtonCool = - xz_PotTempMean / EFTime 
    
!    call StorePotTempDamp( xz_NewtonCool )

  end function xz_NewtonCool
  
end module Radiation
