!= 繝�ヵ繧ｩ繝ｫ繝医�蝓ｺ譛ｬ蝣ｴ繧定ｨｭ螳壹☆繧九◆繧√�螟画焚蜿ら�蝙九Δ繧ｸ繝･繝ｼ繝ｫ
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: basicset.f90,v 1.18 2014/07/08 01:05:32 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!

module basicset
  !
  != 繝�ヵ繧ｩ繝ｫ繝医�蝓ｺ譛ｬ蝣ｴ繧定ｨｭ螳壹☆繧九◆繧√�螟画焚蜿ら�蝙九Δ繧ｸ繝･繝ｼ繝ｫ
  !
  ! hogeBZ 縺ｨ縺ｪ繧矩�蛻励�, 螟夜Κ縺九ｉ蛟､繧貞�蜉帙☆繧九％縺ｨ縺ｧ蛻晄悄蛹悶ｒ陦後≧縺薙→縺ｫ豕ｨ諢�.
  !

  !繝｢繧ｸ繝･繝ｼ繝ｫ隱ｭ縺ｿ霎ｼ縺ｿ
  use dc_types, only:  DP

  !證鈴ｻ吶�蝙句ｮ｣險遖∵ｭ｢
  implicit none

  !save 螻樊ｧ
  private

  !Public Interface
  real(DP), allocatable, save, public :: xyz_DensBZ(:,:,:)    !蟇�ｺｦ
  real(DP), allocatable, save, public :: pyz_DensBZ(:,:,:)    !蟇�ｺｦ
  real(DP), allocatable, save, public :: xqz_DensBZ(:,:,:)    !蟇�ｺｦ
  real(DP), allocatable, save, public :: xyr_DensBZ(:,:,:)    !蟇�ｺｦ
  real(DP), allocatable, save, public :: xyz_PressBZ(:,:,:)   !辟｡谺｡蜈�悸蜉�
  real(DP), allocatable, save, public :: xyz_ExnerBZ(:,:,:)   !辟｡谺｡蜈�悸蜉�
  real(DP), allocatable, save, public :: xyz_TempBZ(:,:,:)    !貂ｩ蠎ｦ
  real(DP), allocatable, save, public :: xyz_PTempBZ(:,:,:)   !貂ｩ菴�
  real(DP), allocatable, save, public :: xyr_PTempBZ(:,:,:)   !貂ｩ菴�
  real(DP), allocatable, save, public :: xyz_VPTempBZ(:,:,:)  !莉ｮ貂ｩ菴�
  real(DP), allocatable, save, public :: pyz_VPTempBZ(:,:,:)  !莉ｮ貂ｩ菴�
  real(DP), allocatable, save, public :: xqz_VPTempBZ(:,:,:)  !莉ｮ貂ｩ菴�
  real(DP), allocatable, save, public :: xyr_VPTempBZ(:,:,:)  !莉ｮ貂ｩ菴�
  real(DP), allocatable, save, public :: xyz_VelSoundBZ(:,:,:)!髻ｳ騾�
  real(DP), allocatable, save, public :: xyz_VelSW(:,:,:)     !髻ｳ騾�
  real(DP), allocatable, save, public :: xyzf_QMixBZ(:,:,:,:) !蜃晉ｸｮ謌仙�豺ｷ蜷域ｯ�
  real(DP), allocatable, save, public :: xyz_EffMolWtBZ(:,:,:)!蛻�ｭ宣㍼蜉ｹ譫�
  real(DP), allocatable, save, public :: xyz_QMixBZPerMolWt(:,:,:)
                              !蝓ｺ譛ｬ蝣ｴ縺ｮ豺ｷ蜷域ｯ� / 蛻�ｭ宣㍼ 縺ｮ邱丞柱
  real(DP), allocatable, save, public :: xyr_QMixBZPerMolWt(:,:,:)
                              !蝓ｺ譛ｬ蝣ｴ縺ｮ豺ｷ蜷域ｯ� / 蛻�ｭ宣㍼ 縺ｮ邱丞柱
  real(DP), allocatable, save, public :: xyz_QMixBZ(:,:,:)
                              !蝓ｺ譛ｬ蝣ｴ縺ｮ豺ｷ蜷域ｯ斐�邱丞柱
  real(DP), allocatable, save, public :: xyr_QMixBZ(:,:,:)
                              !蝓ｺ譛ｬ蝣ｴ縺ｮ豺ｷ蜷域ｯ斐�邱丞柱

  ! 蜈ｬ髢区焔邯壹″
  public basicset_init

contains

!!!-----------------------------------------------------------------!!!
  subroutine basicset_init(                                     &
    &   xyz_Press, xyz_Exner, xyz_Temp, xyz_PTemp, xyz_Dens,    &
    &   xyz_VelSound, xyzf_QMix, xyz_EffMolWt                   &
    & )
    !
    != 蝓ｺ譛ｬ蝣ｴ縺ｮ蛟､繧貞､夜Κ縺九ｉ蜿門ｾ励☆繧�.
    !
    ! dry 縺ｮ蝣ｴ蜷医�豺ｷ蜷域ｯ斐ｄ荵ｾ辯･謌仙�縺ｨ蜃晉ｵ先�蛻��蟄伜惠豈斐ｒ髯ｽ縺ｫ菴ｿ繧上↑縺��縺ｧ,
    ! 縺薙ｌ繧峨�螟画焚縺ｯ optional 縺ｫ縺励※縺�ｋ.
    !

    !繝｢繧ｸ繝･繝ｼ繝ｫ蜻ｼ縺ｳ蜃ｺ縺�
    use dc_types,   only:  DP
    use gridset,    only:  imin,       &! 驟榊�縺ｮ X 譁ｹ蜷代�荳矩剞
      &                    imax,       &! 驟榊�縺ｮ X 譁ｹ蜷代�荳企剞
      &                    jmin,       &! 驟榊�縺ｮ Z 譁ｹ蜷代�荳矩剞
      &                    jmax,       &! 驟榊�縺ｮ Z 譁ｹ蜷代�荳企剞
      &                    kmin,       &! 驟榊�縺ｮ Z 譁ｹ蜷代�荳矩剞
      &                    kmax,       &! 驟榊�縺ｮ Z 譁ｹ蜷代�荳企剞
      &                    ncmax        ! 蛹門ｭｦ遞ｮ縺ｮ謨ｰ
    use composition, only: GasNum,     &!豌嶺ｽ薙�謨ｰ
      &                    IdxG,       &!豌嶺ｽ薙�驟榊�豺ｻ縺亥ｭ�
      &                    MolWtWet     !蜃晉ｸｮ謌仙�縺ｮ蛻�ｭ宣㍼

    !證鈴ｻ吶�蝙句ｮ｣險遖∵ｭ｢
    implicit none

    !蜈･蜉帛､画焚
    real(DP), intent(in) :: xyz_Press(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in) :: xyz_Exner(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in) :: xyz_Temp(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in) :: xyz_PTemp(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in) :: xyz_Dens(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in) :: xyz_VelSound(imin:imax,jmin:jmax,kmin:kmax)
    real(DP), intent(in), optional :: xyzf_QMix(imin:imax,jmin:jmax,kmin:kmax,ncmax)
    real(DP), intent(in), optional :: xyz_EffMolWt(imin:imax,jmin:jmax,kmin:kmax)

    real(DP)             :: xyzf_QMixBZPerMolWt(imin:imax,jmin:jmax,kmin:kmax,GasNum)
    integer              :: s
    integer              :: i, j, k

    ! 驟榊�縺ｮ蛻晄悄蛹�
    call basicset_array_init

    !蛟､縺ｮ蜈･蜉�
    xyz_PressBZ    = xyz_Press
    xyz_ExnerBZ    = xyz_Exner
    xyz_TempBZ     = xyz_Temp
    xyz_PTempBZ    = xyz_PTemp
    xyz_DensBZ     = xyz_Dens
    xyz_VelSoundBZ = xyz_VelSound
    xyz_VelSW      = xyz_VelSound  ! 驟榊�蜷阪ｒ遏ｭ縺上☆繧九◆繧√↓.

    if (present(xyz_EffMolWt)) xyz_EffMolWtBZ = xyz_EffMolWt

    if (present(xyzf_QMix)) then
      xyzf_QMixBZ = xyzf_QMix
      xyzf_QMixBZPerMolWt = 0.0d0

      do s = 1, GasNum
         xyzf_QMixBZPerMolWt(:,:,:,s) = &
           & xyzf_QMixBZ(:,:,:,IdxG(s)) / MolWtWet(IdxG(s))
      end do

      xyz_QMixBZPerMolWt = sum(xyzf_QMixBZPerMolWt, 4)
      xyz_QMixBZ         = sum(xyzf_QMixBZ, 4)

!      xyr_QMixBZPerMolWt = xyr_xyz( xyz_QMixBZPerMolWt )
!      xyr_QMixBZ         = xyr_xyz( xyz_QMixBZ )

      do k = kmin, kmax-1
        xyr_QMixBZPerMolWt(:,:,k) = ( xyz_QMixBZPerMolWt(:,:,k) + xyz_QMixBZPerMolWt(:,:,k+1) ) * 5.0d-1
        xyr_QMixBZ(:,:,k)         = ( xyz_QMixBZ(:,:,k) + xyz_QMixBZ(:,:,k+1) ) * 5.0d-1
      end do

    end if

    xyz_VPTempBZ = xyz_PTempBZ / xyz_EffMolWtBZ

!    xyr_PTempBZ  = xyr_xyz( xyz_PTempBZ )
!    xyr_VPTempBZ = xyr_xyz( xyz_VPTempBZ )
!    xyr_DensBZ   = xyr_xyz( xyz_DensBZ )

    do k = kmin, kmax-1
      xyr_PTempBZ (:,:,k) = ( xyz_PTempBZ (:,:,k) + xyz_PTempBZ (:,:,k+1) ) * 5.0d-1
      xyr_VPTempBZ(:,:,k) = ( xyz_VPTempBZ(:,:,k) + xyz_VPTempBZ(:,:,k+1) ) * 5.0d-1
      xyr_DensBZ  (:,:,k) = ( xyz_DensBZ  (:,:,k) + xyz_DensBZ  (:,:,k+1) ) * 5.0d-1
    end do

    ! 豌ｴ蟷ｳ荳讒倥↑縺ｮ縺ｧ, 蟷ｳ蝮�桃菴懊�蠢�ｦ√↑縺�
    !
    pyz_DensBZ   = xyz_Dens
    xqz_DensBZ   = xyz_Dens
    pyz_VPTempBZ = xyz_VPTempBZ
    xqz_VPTempBZ = xyz_VPTempBZ

  contains

    subroutine basicset_array_init
      !
      ! *BZ 縺ｪ驟榊�縺ｮ蛻晄悄蛹�
      !

      real(DP), parameter :: InitialValue = 1.0d10

      allocate( &
        & xyz_DensBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & pyz_DensBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & xqz_DensBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & xyr_DensBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & xyz_PressBZ(imin:imax,jmin:jmax,kmin:kmax),       &
        & xyz_ExnerBZ(imin:imax,jmin:jmax,kmin:kmax),       &
        & xyz_TempBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & xyz_PTempBZ (imin:imax,jmin:jmax,kmin:kmax),      &
        & xyr_PTempBZ (imin:imax,jmin:jmax,kmin:kmax),      &
        & xyz_VPTempBZ (imin:imax,jmin:jmax,kmin:kmax),     &
        & pyz_VPTempBZ (imin:imax,jmin:jmax,kmin:kmax),     &
        & xqz_VPTempBZ (imin:imax,jmin:jmax,kmin:kmax),     &
        & xyr_VPTempBZ (imin:imax,jmin:jmax,kmin:kmax),     &
        & xyz_VelSoundBZ(imin:imax,jmin:jmax,kmin:kmax),    &
        & xyz_VelSW(imin:imax,jmin:jmax,kmin:kmax),         &
        & xyzf_QMixBZ(imin:imax,jmin:jmax,kmin:kmax,ncmax), &
        & xyz_EffMolWtBZ(imin:imax,jmin:jmax,kmin:kmax)     &
        & )
      allocate( &
        & xyr_QMixBZPerMolWt(imin:imax,jmin:jmax,kmin:kmax),&
        & xyz_QMixBZPerMolWt(imin:imax,jmin:jmax,kmin:kmax),&
        & xyr_QMixBZ(imin:imax,jmin:jmax,kmin:kmax),        &
        & xyz_QMixBZ(imin:imax,jmin:jmax,kmin:kmax)         &
        & )

      ! 蛟､縺ｮ遒ｺ螳�
      xyz_DensBZ     = InitialValue
      pyz_DensBZ     = InitialValue
      xqz_DensBZ     = InitialValue
      xyr_DensBZ     = InitialValue
      xyz_PressBZ    = InitialValue
      xyz_ExnerBZ    = InitialValue
      xyz_TempBZ     = InitialValue
      xyz_PTempBZ    = InitialValue
      xyz_VPTempBZ   = InitialValue
      pyz_VPTempBZ   = InitialValue
      xqz_VPTempBZ   = InitialValue
      xyr_VPTempBZ   = InitialValue
      xyz_VelSoundBZ = InitialValue
      xyz_VelSW      = InitialValue
      xyzf_QMixBZ    = InitialValue
      xyz_EffMolWtBZ = 1.0d0 ! dry 縺ｮ蝣ｴ蜷医�蠢�★ 1.0

      xyr_QMixBZPerMolWt = InitialValue
      xyz_QMixBZPerMolWt = InitialValue
      xyr_QMixBZ = InitialValue
      xyz_QMixBZ = InitialValue

    end subroutine basicset_array_init

  end subroutine basicset_init

end module basicset