!= Module initialdata_Toon2002
!
! Authors::   SUGIYAMA Koichiro, ODAKA Masatsugu
! Version::   $Id: initialdata_toon2002.f90,v 1.3 2014/03/04 04:49:40 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]

module initialdata_Toon2002
  !
  ! Toon et al. (2002) 繧呈ｨ｡縺励◆蛻晄悄蝣ｴ繧剃ｸ弱∴繧�
  !

  !繝｢繧ｸ繝･繝ｼ繝ｫ隱ｭ縺ｿ霎ｼ縺ｿ
  use dc_types,  only: DP

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

  !蜀�Κ螟画焚
  real(DP), parameter, private :: AntA    = 27.4d0
  real(DP), parameter, private :: AntB    = 3103.0d0
  real(DP), parameter, private :: TempLTP = 135.0d0
  real(DP), parameter, private :: Dhight  = 6.0d2

  !蛻晄悄蛹悶□縺大�髢�
  public  initialdata_Toon2002_basic

contains

!!!------------------------------------------------------------------------------!!!

  subroutine initialdata_toon2002_basic( z_Temp, z_Press )

    !繝｢繧ｸ繝･繝ｼ繝ｫ隱ｭ縺ｿ霎ｼ縺ｿ
    use dc_types,  only: DP
    use gridset,   only: kmin, kmax,  &!驟榊�繧ｵ繧､繧ｺ (Z 譁ｹ蜷�)
      &                  nz            !迚ｩ逅��伜沺縺ｮ繧ｵ繧､繧ｺ (Z 譁ｹ蜷�)
    use axesset,   only: z_Z,         &!繧ｹ繧ｫ繝ｩ繝ｼ譬ｼ蟄千せ縺ｧ縺ｮ鬮伜ｺｦ
      &                  z_dz          !Z 譁ｹ蜷代�譬ｼ蟄千せ髢馴囈
    use constants, only: GasRDry,     &!荵ｾ辯･謌仙�縺ｮ螳壼悸豈皮�
      &                  CpDry,       &!荵ｾ辯･謌仙�縺ｮ螳壼悸豈皮�
      &                  Grav,        &!驥榊鴨蜉�騾溷ｺｦ
      &                  TempSfc,     &!蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ
      &                  PressSfc      !蝨ｰ陦ｨ髱｢蝨ｧ蜉�

    implicit none

    real(DP), intent(out):: z_Press(kmin:kmax)           !蝨ｧ蜉�
    real(DP), intent(out):: z_Temp(kmin:kmax)            !貂ｩ蠎ｦ
    real(DP)             :: TempLCL
    real(DP)             :: Temp_0,  Temp_1
    real(DP)             :: Press_0, Press_1
    real(DP)             :: Weight1, Weight2
    real(DP)             :: LCL, LTP
    integer              :: k

    ! 荵ｾ辯･譁ｭ辭ｱ邱�, 貉ｿ貎､譁ｭ辭ｱ邱�, 遲画ｸｩ邱壹′莠､繧上ｋ鬮伜ｺｦ繧定ｨ育ｮ励＠,
    ! 蜷��伜沺縺ｧ謌舌ｊ遶九▽蠑上ｒ逕ｨ縺�※貂ｩ蠎ｦ, 蝨ｧ蜉帙ｒ險育ｮ�
    ! 荵ｾ辯･譁ｭ辭ｱ邱壹→貉ｿ貎､譁ｭ辭ｱ邱壹′莠､繧上ｋ鬮伜ｺｦ(LCL)繧貞渚蠕ｩ豕輔〒險育ｮ�
    !
    Press_0 = PressSfc
    Temp_0 = TempSfc
    do
      ! 鬟ｽ蜥梧ｸｩ蠎ｦ (press0 縺ｫ蟇ｾ縺吶ｋ): 譛蛻昴�蝨ｰ陦ｨ髱｢縺ｧ縺ｮ鬟ｽ蜥梧ｸｩ蠎ｦ.
      ! ln(p) = A - B/T
      Temp_1 = AntB / (AntA - dlog(Press_0))

      ! 荵ｾ辯･譁ｭ辭ｱ逧�↓豎ｺ繧√◆蝨ｧ蜉�
      !
      Press_1 = PressSfc * (Temp_1/TempSfc) **(CpDry / GasRDry)

      ! 邨ｶ蟇ｾ隱､蟾ｮ縺碁明蛟､繧医ｊ蟆上＆縺上↑繧後�邨ゆｺ�.
      !
      if (abs(Temp_1 - Temp_0) < epsilon(0.0d0)) then
        LCL = (TempSfc * CpDry) / Grav &
          & * (1.0d0 - (Press_1 / PressSfc)**(GasRDry / CpDry))
        TempLCL = temp_1

        exit
      else
        Temp_0 = Temp_1
        Press_0 = Press_1
      end if
    end do

    ! 貉ｿ貎､譁ｭ辭ｱ邱壹→遲画ｸｩ邱壹′莠､繧上ｋ鬮伜ｺｦ(LTP)繧定ｨ育ｮ�
    !
    LTP = LCL + GasRDry * AntB / Grav * dlog(TempLCL / TempLTP)

    ! 貂ｩ蠎ｦ蝨ｧ蜉帙ｒ豎ｺ繧√ｋ.
    !
    z_Temp(1)  = TempSfc  - Grav * z_Z(1) / CpDry
    z_Press(1) = PressSfc - (Grav * PressSfc * z_dz(1) * 5.0d-1) / (GasRDry * TempSfc)
    do k = 2, nz

      !驥阪∩縺､縺代�髢｢謨ｰ繧堤畑諢�. tanh 繧堤畑縺�ｋ
      Weight1 = ( tanh( (z_Z(k) - LCL ) / Dhight ) + 1.0d0 ) * 5.0d-1

      !驥阪∩縺､縺代�髢｢謨ｰ繧堤畑諢�. tanh 繧堤畑縺�ｋ
      Weight2 = ( tanh( (z_Z(k) - LTP ) / Dhight ) + 1.0d0 ) * 5.0d-1

      !荵ｾ辯･譁ｭ辭ｱ
      if (z_z(k) < LCL) then
        z_Temp(k) = TempSfc - Grav * z_Z(k) / CpDry

      !貉ｿ貎､譁ｭ辭ｱ
      elseif (z_z(k) >= LCL .AND. z_z(k) < LTP) then
        Temp_0 = TempSfc - Grav * z_Z(k) / CpDry
        Temp_1 = TempLCL * exp(-Grav * (z_Z(k) - LCL) / (GasRDry * AntB))
        z_Temp(k) = Temp_0 * ( 1.0d0 - Weight1 ) + Temp_1 * Weight1
!        z_Temp(k)  = TempLCL * exp(-Grav * (z_Z(k) - LCL) / (GasRDry * AntB))

      !遲画ｸｩ
      elseif (z_z(k) >= LTP) then
        Temp_0 = TempLCL * exp(-Grav * (z_Z(k) - LCL) / (GasRDry * AntB))
        z_Temp(k) = Temp_0 * ( 1.0d0 - Weight2 ) + TempLTP * Weight2
!        z_Temp(k) = TempLTP

      end if
    end do

    ! 髱呎ｰｴ蝨ｧ蟷ｳ陦｡縺九ｉ蝨ｧ蜉帙ｒ豎ｺ繧√ｋ
    !
    do k = 2, nz
      z_Press(k) = z_Press(k-1) - (Grav * z_Press(k-1) * z_dz(k-1)) &
        & / ( GasRDry * z_Temp(k-1) )
    end do

  end subroutine initialdata_toon2002_basic

end module initialdata_Toon2002