!= Module StoreStab_3d
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: storestab_3d.f90,v 1.1 2008-06-19 16:47:53 odakker Exp $ 
! Tag Name::  $Name: arare4-20100910 $
! 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_3d
  !
  !浮力の積算値を保管するための変数型モジュール.
  !
  use dc_types,  only : DP
  use dc_message,only : MessageNotify

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

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

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

  !公開変数
  real(DP), allocatable :: z_StabTemp(:)
  real(DP), allocatable :: z_StabMolWt(:)
  real(DP), allocatable :: xyz_StabTemp(:,:,:)
  real(DP), allocatable :: xyz_StabMolWt(:,:,:)

  save z_StabTemp,  z_StabMolWt
  save xyz_StabTemp, xyz_StabMolWt

contains

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

    call MessageNotify( "M", "StoreStab_Init", "Initialize" )

    allocate(     &
      & z_StabTemp (DimZMin:DimZMax),  & 
      & z_StabMolWt(DimZMin:DimZMax),  &
      & xyz_StabTemp (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),  & 
      & xyz_StabMolWt(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)  &
      &  )
    
    call StoreStabClean()

  end subroutine StoreStab_Init


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


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

    CalNum = TimeDisp / DelTimeLong
   
    z_StabTemp  = a_MeanXY_aaa( xyz_StabTemp  ) / CalNum
    z_StabMolWt = a_MeanXY_aaa( xyz_StabMolWt ) / CalNum

  end subroutine StoreStabMeanXY
  

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

    implicit none

    real(DP), intent(in)  :: xyz_Work (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)              :: xyz_Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    
    xyz_Work2    = xyz_StabTemp + xyz_Work
    xyz_StabTemp = xyz_Work2

  end subroutine StoreStabTemp


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

    implicit none

    real(DP), intent(in)  :: xyz_Work (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)              :: xyz_Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    xyz_Work2     = xyz_StabMolWt + xyz_Work
    xyz_StabMolWt = xyz_Work2
    
  end subroutine StoreStabMolWt

  function a_MeanXY_aaa( var ) 
    !
    ! 水平平均値の計算
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(DP), intent(in) :: var(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
                                     !平均演算の対象となる変数
    real(DP)             :: a_MeanXY_aaa(DimZMin:DimZMax)
                                     !水平平均値
    real(DP)             :: aa_work(DimYMin:DimYMax,DimZMin:DimZMax)
                                     !水平平均値

    aa_work = aa_MeanX_aaa(var)

    a_MeanXY_aaa = sum( aa_work(RegYMin:RegYMax, :), 1 ) &
      &           / real(RegYMax - RegYMin + 1, 8) 

  end function a_MeanXY_aaa
  
  function aa_MeanX_aaa( var ) 
    !
    ! 水平平均値の計算
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(DP), intent(in) :: var(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
                                     !平均演算の対象となる変数
    real(DP)             :: aa_MeanX_aaa(DimYMin:DimYMax,DimZMin:DimZMax)
                                     !水平平均値

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

  end function aa_MeanX_aaa
  
end module StoreStab_3d
