!= Module GridSet
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: gridset.f90,v 1.1 2011-04-26 04:08:11 sugiyama Exp $
! Tag Name::  $Name:  $
! 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

  !暗黙の型宣言禁止
  implicit none

  !save 属性
  private

  !公開変数
  integer, public, save  :: imin = -5   ! x 方向の配列の下限
  integer, public, save  :: imax = 15   ! x 方向の配列の上限
  integer, public, save  :: jmin = -5   ! y 方向の配列の下限
  integer, public, save  :: jmax = 15   ! y 方向の配列の下限
  integer, public, save  :: kmin = -5   ! z 方向の配列の下限
  integer, public, save  :: kmax = 15   ! z 方向の配列の下限
  integer, public, save  :: lmin = 1    ! 物質方向の配列の下限
  integer, public, save  :: lmax = 3    ! 物質方向の配列の下限
  integer, public, save  :: NX = 10     ! 格子点数
  integer, public, save  :: NY = 10     ! 格子点数
  integer, public, save  :: NZ = 10     ! 格子点数
  integer, public, save  :: NF = 3      ! 格子点数
  integer, public, save  :: Xmg = 5     ! 糊代格子点数
  integer, public, save  :: Ymg = 5     ! 糊代格子点数
  integer, public, save  :: Zmg = 5     ! 糊代格子点数
  integer, public, save  :: Fmg = 0     ! 糊代格子点数

  public gridset_init

contains

  subroutine gridset_init(cfgfile)
    !
    !NAMELIST から情報を得て, 格子点を計算する
    !

    !暗黙の型宣言禁止
    implicit none

    !モジュール読み込み

    !入力変数
    character(STRING), intent(in) :: cfgfile

    !内部変数
    integer            :: i, k, unit
    integer, parameter :: kind = 8      !精度を表す

    !-----------------------------------------------------------------
    ! NAMELIST から情報を取得
    !
    NAMELIST /gridset_nml/ NX, NY, NZ, NF, Xmg, Ymg, Zmg, Fmg

    call FileOpen(unit, file=cfgfile, 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

    if ( NF < fmg ) then
      call MessageNotify( "E", "gridset_init", "NF < fmg" )
    end if

    !-----------------------------------------------------------------
    ! 配列の上限・下限を決める
    !
    imin = 1  - xmg
    imax = nx + xmg
    jmin = 1  - ymg
    jmax = ny + ymg
    kmin = 1  - zmg
    kmax = nz + zmg
    lmin = 1  - fmg
    lmax = nf + fmg

    !-----------------------------------------------------------------
    ! 確認
    !
    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", "NF = %d",   i=(/NF/) )
      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", "fmg  = %d", i=(/Fmg/) )
      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/) )
      call MessageNotify( "M", "gridset_init", "lmin = %d", i=(/lmin/) )
      call MessageNotify( "M", "gridset_init", "lmax = %d", i=(/lmax/) )
    end if
  end subroutine gridset_init

end module gridset