!= Module StoreDensCloud
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: storedenscloud_1d.f90,v 1.1 2011-02-23 17:20:48 yamasita Exp $
! Tag Name::  $Name: arare4-20120911 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview
!
! 雲密度に関する積算値を保管するための変数型モジュール(主成分凝結計算用).
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module StoreDensCloud
  !
  !雲密度に関する積算値を保管するための変数型モジュール.
  !

  !モジュールの読み込み
  use gridset, only:   DimXMin,     & ! x 方向の配列の下限
    &                  DimXMax,     & ! x 方向の配列の上限
    &                  DimZMin,     & ! z 方向の配列の下限
    &                  DimZMax,     & ! z 方向の配列の上限
    &                  SpcNum,      & ! number of species
    &                  RegXMin,     & ! x 方向の物理領域の下限
    &                  RegXMax        ! x 方向の物理領域の上限
  use TimeSet, only:   TimeDisp,    & ! 出力時間間隔
    &                  DelTimeShort   ! 短い時間ステップ

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

  !公開要素
  public StoreDensCloud_Init, StoreDensCloudMeanX, StoreDensCloudClean
  public z_DensCloudAdv, z_DensCloudTurb, z_DensCloudDiff, &
    &    z_DensCloudCond, z_DensCloudFill
  public StoreDensCloudAdv,  StoreDensCloudTurb, StoreDensCloudDiff, &
    &    StoreDensCloudCond, StoreDensCloudFill

  !変数
  real(8), allocatable :: z_DensCloudAdv(:)
  real(8), allocatable :: z_DensCloudTurb(:)
  real(8), allocatable :: z_DensCloudDiff(:)
  real(8), allocatable :: z_DensCloudCond(:)
  real(8), allocatable :: z_DensCloudFill(:)
  real(8), allocatable :: xz_DensCloudAdv(:,:)
  real(8), allocatable :: xz_DensCloudTurb(:,:)
  real(8), allocatable :: xz_DensCloudDiff(:,:)
  real(8), allocatable :: xz_DensCloudCond(:,:)
  real(8), allocatable :: xz_DensCloudFill(:,:)

  !save 属性
  save xz_DensCloudAdv, xz_DensCloudTurb, xz_DensCloudDiff, xz_DensCloudCond, xz_DensCloudFill
  save z_DensCloudAdv, z_DensCloudTurb, z_DensCloudDiff, z_DensCloudCond, z_DensCloudFill

contains

  subroutine StoreDensCloud_Init( )
    !初期化ルーチン

    allocate(     &
      & z_DensCloudAdv(DimZMin:DimZMax),  &
      & z_DensCloudTurb(DimZMin:DimZMax), &
      & z_DensCloudDiff(DimZMin:DimZMax), &
      & z_DensCloudCond(DimZMin:DimZMax), &
      & z_DensCloudFill(DimZMin:DimZMax), &
      & xz_DensCloudAdv(DimXMin:DimXMax, DimZMin:DimZMax),  &
      & xz_DensCloudTurb(DimXMin:DimXMax, DimZMin:DimZMax), &
      & xz_DensCloudDiff(DimXMin:DimXMax, DimZMin:DimZMax), &
      & xz_DensCloudCond(DimXMin:DimXMax, DimZMin:DimZMax), &
      & xz_DensCloudFill(DimXMin:DimXMax, DimZMin:DimZMax)  &
      &  )

    call StoreDensCloudClean

  end subroutine StoreDensCloud_Init

  subroutine StoreDensCloudClean( )
    !保管した値のクリアー

    z_DensCloudAdv  = 0.0d0
    z_DensCloudTurb = 0.0d0
    z_DensCloudDiff = 0.0d0
    z_DensCloudCond = 0.0d0
    z_DensCloudFill = 0.0d0

    xz_DensCloudAdv  = 0.0d0
    xz_DensCloudTurb = 0.0d0
    xz_DensCloudDiff = 0.0d0
    xz_DensCloudCond = 0.0d0
    xz_DensCloudFill = 0.0d0

!    CalNum  = 1.0d-40    !ゼロ割を禁止.
  end subroutine StoreDensCloudClean

  subroutine StoreDensCloudMeanX()
    !保管した値の水平平均値 [K/s]
     real(8) :: CalNumShort

    CalNumShort = 2.0d0 * TimeDisp / DelTimeShort

    z_DensCloudAdv  = a_MeanX_aa( xz_DensCloudAdv )   / CalNumShort
    z_DensCloudTurb = a_MeanX_aa( xz_DensCloudTurb )  / CalNumShort
    z_DensCloudDiff = a_MeanX_aa( xz_DensCloudDiff )  / CalNumShort
    z_DensCloudCond = a_MeanX_aa( xz_DensCloudCond )  / CalNumShort
    z_DensCloudFill = a_MeanX_aa( xz_DensCloudFill )  / CalNumShort

  end subroutine StoreDensCloudMeanX

  subroutine StoreDensCloudAdv( Work )
    !移流項の保管

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    Work2  = xz_DensCloudAdv + Work
    xz_DensCloudAdv = Work2

  end subroutine StoreDensCloudAdv

  subroutine StoreDensCloudTurb( Work )
    !乱流項の保管

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    Work2   = xz_DensCloudTurb + Work
    xz_DensCloudTurb = Work2

  end subroutine StoreDensCloudTurb

  subroutine StoreDensCloudDiff( Work )
    !数値拡散項の保管

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    Work2   = xz_DensCloudDiff + Work
    xz_DensCloudDiff = Work2

  end subroutine StoreDensCloudDiff

  subroutine StoreDensCloudCond( Work )
    !潜熱・蒸発熱の保管

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    Work2   = xz_DensCloudCond + Work
    xz_DensCloudCond = Work2

  end subroutine StoreDensCloudCond

  subroutine StoreDensCloudFill( Work )
    !穴埋めプログラムによる変化

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    Work2     = Work + xz_DensCloudFill
    xz_DensCloudFill = Work2

  end subroutine StoreDensCloudFill

  function a_MeanX_aa( var )
    !
    ! 水平平均値の計算
    !

    !暗黙の型宣言禁止
    implicit none

    !変数定義
    real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
                                     !平均演算の対象となる変数
    real(8)              :: a_MeanX_aa(DimZMin:DimZMax)
                                     !水平平均値

    a_MeanX_aa = sum( var(RegXMin+1:RegXMax, :), 1 ) &
      &           / real(RegXMax - RegXMin, 8)

  end function a_MeanX_aa

end module StoreDensCloud