!= Module MoistBuoyancy_3d
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: moistbuoyancy_3d.f90,v 1.1 2011-04-26 04:08:10 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview
!
!
!== Error Handling
!
!== Bugs
!
!== Note
!
!
!== Future Plans
!
!

module MoistBuoyancy_3d
  !
  !
  !

  !モジュール読み込み 
  use dc_types, only : DP
  use dc_message, only: MessageNotify

  use gridset_3d, only:  SpcNum,            &! 化学種の数
    &                 DimXMin,           &! x 方向の配列の下限
    &                 DimXMax,           &! x 方向の配列の上限
    &                 DimYMin,           &! y 方向の配列の下限
    &                 DimYMax,           &! y 方向の配列の上限
    &                 DimZMin,           &! z 方向の配列の下限
    &                 DimZMax,           &! z 方向の配列の上限
    &                 x_dx,              &! x 方向の格子点間隔
    &                 y_dy,              &! y 方向の格子点間隔
    &                 z_dz                ! z 方向の格子点間隔
  use basicset_3d, only: MolWtDry,          &!乾燥成分の分子量
    &                 Grav,              &!重力加速度
    &                 SpcWetID,          &!凝縮成分のID
    &                 MolWtWet,          &!凝縮成分の分子量
    &                 CpDry,             &!乾燥成分の平均定圧比熱 [J/K kg]
    &                 xyz_PotTempBasicZ,  &!基本場の温位
    &                 xyz_EffMolWtBasicZ, &!基本場の分子量効果
    &                 xyz_ExnerBasicZ,    &!基本場の無次元圧力
    &                 xyza_MixRtBasicZ     !基本場の混合比
  use moistset, only: CondNum,          &!凝結過程の数
    &                 IdxCG,            &!凝結過程(蒸気)の配列添え字
    &                 IdxCC,            &!凝結過程(雲)の配列添え字
    &                 GasNum,           &!気体の数
    &                 IdxG,             &!気体の配列添え字
    &                 IdxNH3,           &!NH3(蒸気)の配列添え字
    &                 IdxH2S             !H2S(蒸気)の配列添え字
  use ChemCalc_3d, only: xyz_LatentHeat,    &!潜熱
    &                 ReactHeatNH4SH     !NH4SH の反応熱
  use xyz_base_module, only : xyz_avr_xyr, xyr_avr_xyz
  use xyz_deriv_module,only : xyr_dz_xyz

  !暗黙の型宣言禁止
  implicit none
  
  !属性の指定
  private
  
  !関数を public に設定
  public MoistBuoy_Init
  public xyz_BuoyMoistKm
  public xyr_BuoyMolWt
  public xyr_BuoyDrag

  !変数の定義
  real(DP)     :: Cm           = 2.0d-1
  real(DP)     :: MixLen       = 0.0d0
  real(DP), allocatable :: xyz_MixRtBasicZPerMolWt(:,:,:)
                              !基本場の混合比 / 分子量 の総和
  real(DP), allocatable :: xyr_MixRtBasicZPerMolWt(:,:,:)
                              !基本場の混合比 / 分子量 の総和
  real(DP), allocatable :: xyz_MixRtBasicZ(:,:,:)
                              !基本場の混合比の総和
  real(DP), allocatable :: xyr_MixRtBasicZ(:,:,:)
                              !基本場の混合比の総和
  
  save Cm, MixLen
  save xyz_MixRtBasicZPerMolWt
  save xyr_MixRtBasicZPerMolWt
  save xyz_MixRtBasicZ
  save xyr_MixRtBasicZ 

contains

!!!------------------------------------------------------------------!!!
  subroutine MoistBuoy_Init( )

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    integer              :: s
    real(DP), allocatable :: xyza_MixRtBasicZPerMolWt(:,:,:,:)
                                  !基本場の混合比 / 分子量
    real(DP) :: MinDelX
    real(DP) :: MinDelY
    real(DP) :: MinDelZ

    !-----------------------------------------------------------
    ! 混合距離
    !-----------------------------------------------------------

    !混合距離
    MinDelX = minval(x_dx)
    MinDelY = minval(y_dy)
    MinDelZ = minval(z_dz)

!    MixLen = ( MinDelX * MinDelY * MinDelZ ) ** (1.0d0/3.0d0)

    MixLen = min( MinDelZ, min( MinDelX, MinDelY ) )

    !-----------------------------------------------------------
    ! 作業配列の初期化. 
    !-----------------------------------------------------------
    allocate( &
      & xyza_MixRtBasicZPerMolWt(DimXMin:DimXMax,DimYMin:DimYMax, DimZMin:DimZMax, GasNum),  &
      & xyr_MixRtBasicZPerMolWt(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),           &
      & xyz_MixRtBasicZPerMolWt(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),           &
      & xyr_MixRtBasicZ(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),                   &
      & xyz_MixRtBasicZ(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)                    &
      & )

    xyza_MixRtBasicZPerMolWt = 0.0d0
    xyr_MixRtBasicZPerMolWt = 0.0d0
    xyz_MixRtBasicZPerMolWt = 0.0d0
    xyr_MixRtBasicZ = 0.0d0
    xyz_MixRtBasicZ = 0.0d0

    call MessageNotify( "M", &
      & "MoistBuoy_Init", "MolWtWet = %f", d=(/pack(MolWtWet(:), .true.) /) )

    do s = 1, GasNum
      xyza_MixRtBasicZPerMolWt(:,:,:,s) = &
        & xyza_MixRtBasicZ(:,:,:,IdxG(s)) / MolWtWet(IdxG(s))
    end do

    xyz_MixRtBasicZPerMolWt = sum(xyza_MixRtBasicZPerMolWt, 4) 
    xyr_MixRtBasicZPerMolWt = xyr_avr_xyz( sum(xyza_MixRtBasicZPerMolWt, 4) )
    xyz_MixRtBasicZ         = sum(xyza_MixRtBasicZ, 4) 
    xyr_MixRtBasicZ         = xyr_avr_xyz( sum(xyza_MixRtBasicZ, 4) )     
    
  end subroutine MoistBuoy_Init


    


  
end module MoistBuoyancy_3d
