!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine MassCondense
!
!   * Developer: KITAMORI Taichi 
!   * Version: $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!凝結/蒸発量を計算する.
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!拡散によって雲粒が成長する場合における成長方程式を解く. 
!蒸発量が存在する雲の量よりも多い場合, 蒸発量を存在する雲の量にする. 
!
!== Future Plans
!
                                                                 !=end
subroutine MassCondense(  &
  & ss_LatHeatPerMass_nl,  &
  & ss_PotTemp_nl,     &
  & ss_Exner_nl,       &
  & ss_DensCloud_nl,     &
  & ss_MassCond_nl)
                                                                 !=begin
  !==Dependency
  use dc_trace, only: BeginSub, EndSub  
  use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax
  use physset,  only: GasR, Pi, PressSfc
  use cloudset, only: DensIce, NumAerosol, RadiAerosol, & 
    &                 Kd, SatPressA, SatPressB,  &
    &                 SupSatRatioCr
  use basicset, only: ss_CpBasicZ, ss_PotTempBasicZ,  &
    &                 ss_ExnerBasicZ, ss_DensBasicZ  
                                                                 !=end
  !==暗黙の型宣言を禁止
  implicit none
  
  !==Input
  real(8), intent(in)   :: ss_LatHeatPerMass_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
  real(8), intent(in)   :: ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
  real(8), intent(in)   :: ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax) 
  real(8), intent(in)   :: ss_DensCloud_nl(DimXMin:DimXMax, DimZMin:DimZMax)   

  !==Output
  real(8), intent(out)  :: ss_MassCond_nl(DimXMin:DimXMax, DimZMin:DimZMax)
   
  !==Work
  real(8)               :: ss_SupSatRatio(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)               :: ss_Rh(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)               :: ss_RadiCloud(DimXMin:DimXMax, DimZMin:DimZMax)
  integer               :: i,k

  call BeginSub("MassCondense", &
&           fmt="%c",        &
&           c1="Calculate latent heat per unit mass.")


  ss_SupSatRatio =  &
    &  PressSfc * (ss_ExnerBasicZ + ss_Exner_nl)**(ss_CpBasicZ / GasR) &
    &  / (  exp(  &
    &             SatPressA &
    &           - SatPressB  &
    &             / (ss_ExnerBasicZ + ss_Exner_nl) &
    &             / (ss_PotTempBasicZ + ss_PotTemp_nl) &
    &          )  &
    &    ) &
    &  - 1.0d0

  ss_Rh =  & 
    & ss_LatHeatPerMass_nl**2.0d0 &
    & / (  &
    &      Kd * GasR * (ss_ExnerBasicZ + ss_Exner_nl)** 2.0d0 &
    &      * (ss_PotTempBasicZ + ss_PotTemp_nl)**2.0d0  &
    &   ) 

  ss_RadiCloud =   &
    & (  &
    &      3.0d0 * ss_DensCloud_nl &
    &      / (4.0d0 * Pi * DensIce * NumAerosol * ss_DensBasicZ) & 
    &      + RadiAerosol**3.0d0  &
    &   )**(1.0d0 / 3.0d0) 

  do k= DimZMin, DimZMax
    do i = DimXMin, DimXMax
      if (ss_DensCloud_nl(i,k) == 0 .and. &        
        & ss_SupSatRatio(i,k) < SupSatRatioCr) then
        ! 雲密度 0 のときは過飽和度を越えないと凝結しない
        ss_MassCond_nl(i,k) = 0.0d0
      else
        ! 凝結/蒸発量を計算する
        ss_MassCond_nl(i,k) = &
          &   4.0d0 * Pi * ss_RadiCloud(i,k) &
          & * NumAerosol * ss_DensBasicZ(i,k)  &
          & / ss_Rh(i,k)  &
          & * ss_SupSatRatio(i,k)
      end if
    end do
  end do

  call EndSub("MassCondense")

end subroutine MassCondense
