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

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

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

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

  !公開要素
  public StoreBuoy_Init, StoreBuoyMeanX, StoreBuoyClean
  public z_BuoyTemp, z_BuoyMolWt, z_BuoyDrag
  public StoreBuoyTemp, StoreBuoyMolWt, StoreBuoyDrag

  !公開変数
  real(8), allocatable :: z_BuoyTemp(:)
  real(8), allocatable :: z_BuoyMolWt(:)
  real(8), allocatable :: z_BuoyDrag(:)
  real(8), allocatable :: xz_BuoyTemp(:,:)
  real(8), allocatable :: xz_BuoyMolWt(:,:)
  real(8), allocatable :: xz_BuoyDrag(:,:)

  save z_BuoyTemp,  z_BuoyMolWt, z_BuoyDrag
  save xz_BuoyTemp,  xz_BuoyMolWt, xz_BuoyDrag

contains

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

    allocate(     &
      & z_BuoyTemp(DimZMin:DimZMax),   & 
      & z_BuoyMolWt(DimZMin:DimZMax),  &
      & z_BuoyDrag(DimZMin:DimZMax),   &
      & xz_BuoyTemp(DimXMin:DimXMax, DimZMin:DimZMax),   & 
      & xz_BuoyMolWt(DimXMin:DimXMax, DimZMin:DimZMax),  &
      & xz_BuoyDrag(DimXMin:DimXMax, DimZMin:DimZMax)  &
      &  )
    
    call StoreBuoyClean()

  end subroutine StoreBuoy_Init


  subroutine StoreBuoyClean( )
    !保管した値のクリアー
    
    z_BuoyTemp   = 0.0d0
    z_BuoyMolWt  = 0.0d0
    z_BuoyDrag   = 0.0d0
    xz_BuoyTemp  = 0.0d0
    xz_BuoyMolWt = 0.0d0
    xz_BuoyDrag  = 0.0d0
    
  end subroutine StoreBuoyClean


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

    CalNum = TimeDisp / DelTimeLong
   
    z_BuoyTemp  = a_MeanX_aa( xz_BuoyTemp  ) / CalNum
    z_BuoyMolWt = a_MeanX_aa( xz_BuoyMolWt ) / CalNum
    z_BuoyDrag  = a_MeanX_aa( xz_BuoyDrag  ) / CalNum

  end subroutine StoreBuoyMeanX
  

  subroutine StoreBuoyTemp( 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_BuoyTemp + xz_Work 
    xz_BuoyTemp = xz_Work2

  end subroutine StoreBuoyTemp


  subroutine StoreBuoyMolWt( 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_BuoyMolWt + xz_Work 
    xz_BuoyMolWt = xz_Work2

  end subroutine StoreBuoyMolWt


  subroutine StoreBuoyDrag( 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_BuoyDrag + xz_Work 
    xz_BuoyDrag = xz_Work2

  end subroutine StoreBuoyDrag


  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 StoreBuoy
