!= Module GridSet
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: gridset.f90,v 1.6 2011-06-17 19:04:00 sugiyama Exp $ 
! Tag Name::  $Name: arare5-20111010 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!引数に与えられた NAMELIST ファイルから, 格子点情報を取得し, 
!保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module gridset
  !
  !引数に与えられた NAMELIST ファイルから, 格子点情報を取得し, 
  !保管するための変数参照型モジュール
  !

  !モジュール読み込み
  use dc_types,    only: DP, STRING
  use dc_iounit,   only: FileOpen
  use dc_message,  only: MessageNotify
  use mpi_wrapper, only: myrank
  use namelist_util, only: namelist_filename
  
  !暗黙の型宣言禁止
  implicit none
  
  !save 属性
  private
  
  !公開変数
  integer, public, save :: NX = 10 ! x 方向格子点数
  integer, public, save :: NY = 10 ! y 方向格子点数
  integer, public, save :: NZ = 10 ! z 方向格子点数
  integer, public, save :: NCMAX = 3  ! 組成配列要素数
  integer, public, save :: Xmg = 5 ! x 方向糊代格子点数
  integer, public, save :: Ymg = 5 ! y 方向糊代格子点数
  integer, public, save :: Zmg = 5 ! z 方向糊代格子点数
  integer, public, save :: imin    ! x 方向の配列の下限 
  integer, public, save :: imax    ! x 方向の配列の上限
  integer, public, save :: jmin    ! y 方向の配列の下限 
  integer, public, save :: jmax    ! y 方向の配列の下限 
  integer, public, save :: kmin    ! z 方向の配列の下限 
  integer, public, save :: kmax    ! z 方向の配列の下限 

  public gridset_init

contains

  subroutine gridset_init
    !
    !設定ファイルから情報を読み込み格子点数を計算する
    !

    !暗黙の型宣言禁止
    implicit none

    !内部変数
    integer            :: unit                !設定ファイル用装置番号

    !-----------------------------------------------------------------
    ! 設定ファイルから情報を読み込み
    !
    NAMELIST /gridset_nml/ NX, NY, NZ, NCMAX, Xmg, Ymg, Zmg
    
    call FileOpen(unit, file=namelist_filename, mode='r')
    read(unit, NML=gridset_nml)
    close(unit)
    
    if ( NX < xmg ) then
      call MessageNotify( "E", "gridset_init", "NX < Xmg" )
    end if
    
    if ( NY < ymg ) then
      call MessageNotify( "E", "gridset_init", "NY < Ymg" )
    end if
    
    if ( NZ < zmg ) then
      call MessageNotify( "E", "gridset_init", "NZ < Zmg" )
    end if

    !-----------------------------------------------------------------
    ! 配列の上限・下限を決める
    !
    imin = 1  - xmg
    imax = nx + xmg    
    jmin = 1  - ymg
    jmax = ny + ymg    
    kmin = 1  - zmg
    kmax = nz + zmg    
    
    !-----------------------------------------------------------------    
    !"myrank == 0" に該当する計算ノードが, 読み込んだ情報を出力
    !
    if (myrank == 0) then 
      call MessageNotify( "M", "gridset_init", "NX = %d",   i=(/NX/) )
      call MessageNotify( "M", "gridset_init", "NY = %d",   i=(/NY/) )
      call MessageNotify( "M", "gridset_init", "NZ = %d",   i=(/NZ/) )
      call MessageNotify( "M", "gridset_init", "NCMAX = %d",   i=(/NCMAX/) )
      call MessageNotify( "M", "gridset_init", "xmg  = %d", i=(/Xmg/) )
      call MessageNotify( "M", "gridset_init", "ymg  = %d", i=(/Ymg/) )
      call MessageNotify( "M", "gridset_init", "zmg  = %d", i=(/Zmg/) )
      call MessageNotify( "M", "gridset_init", "imin = %d", i=(/imin/) )
      call MessageNotify( "M", "gridset_init", "imax = %d", i=(/imax/) )
      call MessageNotify( "M", "gridset_init", "jmin = %d", i=(/jmin/) )
      call MessageNotify( "M", "gridset_init", "jmax = %d", i=(/jmax/) )
      call MessageNotify( "M", "gridset_init", "kmin = %d", i=(/kmin/) )
      call MessageNotify( "M", "gridset_init", "kmax = %d", i=(/kmax/) )
    end if
  end subroutine gridset_init
  
end module gridset
