!= Module StoreStab
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: storestab.f90,v 1.1 2007-08-24 06:29:20 sugiyama Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!浮力に関する積算値を保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module StoreStab
  !
  !浮力の積算値を保管するための変数型モジュール.
  !

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

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

  !公開要素
  public StoreStab_Init, StoreStabMeanX, StoreStabClean
  public z_StabTemp, z_StabMolWt
  public StoreStabTemp, StoreStabMolWt

  !公開変数
  real(8), allocatable :: z_StabTemp(:)
  real(8), allocatable :: z_StabMolWt(:)
  real(8), allocatable :: xz_StabTemp(:,:)
  real(8), allocatable :: xz_StabMolWt(:,:)

  save z_StabTemp,  z_StabMolWt
  save xz_StabTemp, xz_StabMolWt

contains

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

    allocate(     &
      & z_StabTemp(DimZMin:DimZMax),   & 
      & z_StabMolWt(DimZMin:DimZMax),  &
      & xz_StabTemp(DimXMin:DimXMax, DimZMin:DimZMax),  & 
      & xz_StabMolWt(DimXMin:DimXMax, DimZMin:DimZMax)  &
      &  )
    
    call StoreStabClean()

  end subroutine StoreStab_Init


  subroutine StoreStabClean( )
    !保管した値のクリアー
    
    z_StabTemp   = 0.0d0
    z_StabMolWt  = 0.0d0
    xz_StabTemp  = 0.0d0
    xz_StabMolWt = 0.0d0
    
  end subroutine StoreStabClean


  subroutine StoreStabMeanX( )
    !保管した値の水平平均値
 
    real(8) :: CalNum

    CalNum = TimeDisp / DelTimeLong
   
    z_StabTemp  = a_MeanX_aa( xz_StabTemp  ) / CalNum
    z_StabMolWt = a_MeanX_aa( xz_StabMolWt ) / CalNum

  end subroutine StoreStabMeanX
  

  subroutine StoreStabTemp( xz_Work )
    !温度の寄与を保管

    implicit none

    real(8), intent(in)  :: xz_Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: xz_Work2(DimXMin:DimXMax, DimZMin:DimZMax)
    
    xz_Work2    = xz_StabTemp + xz_Work
    xz_StabTemp = xz_Work2

  end subroutine StoreStabTemp


  subroutine StoreStabMolWt( xz_Work )
    !分子量の寄与を保管

    implicit none

    real(8), intent(in)  :: xz_Work(DimXMin:DimXMax, DimZMin:DimZMax)
    real(8)              :: xz_Work2(DimXMin:DimXMax, DimZMin:DimZMax)

    xz_Work2     = xz_StabMolWt + xz_Work
    xz_StabMolWt = xz_Work2
    
  end subroutine StoreStabMolWt

  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 StoreStab
