!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Program Arare
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id$ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!非静力学モデル deepconv/arare. 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!方程式系は準圧縮系.
!
!== Future Plans
!
                                                                 !=end

program arare
                                                                 !=begin
  !== Dependency
  use gt4_history
  use gridset,  only: gridset_init, DimXMin, DimXMax, DimZMin, DimZMax, &
       &              RegZMin, RegZMax
  use fileset,  only: fileset_init, cfgfile, basicfile, distrbfile
  use timeset,  only: timeset_init, NstepShort, NstepLong, &
       &              DelTimeShort, DelTimeLong, TimeDisp
  use expname,  only: expname_init
  use linlib,   only: linlib_init
  use bcset,    only: bcset_init
  use basicset, only: basicset_init
  use distrbset,only: distrbset_init
  use arareprm, only: arareprm_init
  use differentiate, only: ss_dz_fs, fs_dz_ss, &
       &                   sf_dz_fs, fs_dz_sf, &
       &                   fs_dz_fs, fs_dz_fs 
                                                                 !=end
  implicit none

  real(8), allocatable  :: ss_Exner_as(:,:)
  real(8), allocatable  :: fs_VelX_as(:,:)
  real(8), allocatable  :: sf_VelZ_as(:,:)
  real(8), allocatable  :: ss_Exner_ns(:,:)
  real(8), allocatable  :: fs_VelX_ns(:,:)
  real(8), allocatable  :: sf_VelZ_ns(:,:)

  real(8), allocatable  :: ss_DivVel_ns(:,:)
  real(8), allocatable  :: fs_ForceX_nl(:,:)
  real(8), allocatable  :: sf_ForceZ_nl(:,:)
  
  real(8), allocatable  :: ss_PotTemp_bl(:,:)
  real(8), allocatable  :: ss_Exner_bl(:,:)
  real(8), allocatable  :: fs_VelX_bl(:,:)
  real(8), allocatable  :: sf_VelZ_bl(:,:)
  real(8), allocatable  :: ss_PotTemp_nl(:,:)
  real(8), allocatable  :: ss_Exner_nl(:,:)
  real(8), allocatable  :: fs_VelX_nl(:,:)
  real(8), allocatable  :: sf_VelZ_nl(:,:)
  real(8), allocatable  :: ss_PotTemp_al(:,:)
  real(8), allocatable  :: ss_Exner_al(:,:)
  real(8), allocatable  :: fs_VelX_al(:,:)
  real(8), allocatable  :: sf_VelZ_al(:,:)

  !--- test
  real(8), allocatable  :: ss_Km_al(:,:)
  real(8), allocatable  :: ss_Kh_al(:,:)
  real(8), allocatable  :: fs_DiffuseX_nl(:,:)
  real(8), allocatable  :: sf_DiffuseZ_nl(:,:)

  real(8)               :: Time
  integer               :: t, tau

  type(gt_history)      :: ss_reg, fs_reg, sf_reg
  type(gt_history)      :: ss_dim, fs_dim, sf_dim
  

!!!
!!!初期化
!!!
  !--- I/O ファイル名の初期化
  call fileset_init
  
  !--- 格子点情報の初期化
  call gridset_init(cfgfile)
  
  !--- 時間積分の設定の初期化
  call timeset_init(cfgfile)
  
  !--- 実験名の初期化
  call expname_init(cfgfile)
  
  !--- 計算の設定を読み込む
  call arareprm_init(cfgfile)

  !--- 境界条件を設定
  call bcset_init(cfgfile)

  !--- 基本場の値を設定
  call basicset_init(basicfile)

  !--- 擾乱場の値を設定
  call distrbset_init(distrbfile)
  
  !--- 行列計算ライブラリの初期化. 
  !--- 引数はスカラーの z 方向の計算領域の大きさ
  call linlib_init(cfgfile, RegZMax - RegZMin)
  
  !--- 出力ファイルの定義
  call ArareOpenDimFile

!!!
!!! CFL 条件のチェック
!!!
  call cflchk


!!!
!!!変数の初期化
!!!  
  !--- 配列の割り当て, 初期化
  call ArareAlloc


!  fs_VelX_al = ss_dz_fs( fs_VelX_nl )
!  fs_VelX_nl = fs_dz_ss( fs_VelX_al )

  fs_VelX_al = sf_dz_fs( fs_VelX_nl )  !test3
  fs_VelX_nl = fs_dz_sf( fs_VelX_al )

!  fs_VelX_al = fs_dz_fs( fs_VelX_nl ) !test2
!  fs_VelX_nl = fs_dz_fs( fs_VelX_al )

  call ArareOutputDimFile
  call ArareCloseDimFile



contains

!!!
!!!短いタイムステップで計算した値を長いタイムステップでの値に代入
!!!


!!!
!!!配列の割り当て, 初期化
!!!
  subroutine ArareAlloc
    use distrbset, only: fs_VelXDistrb, sf_VelZDistrb, &
         &               ss_ExnerDistrb, ss_PotTempDistrb

    allocate(ss_Exner_as(DimXMin:DimXMax, DimZMin:DimZMax),  &
         & fs_VelX_as(DimXMin:DimXMax, DimZMin:DimZMax)  , &
         & sf_VelZ_as(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_VelX_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_VelZ_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Exner_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_PotTemp_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & fs_ForceX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_ForceZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_DivVel_ns(DimXMin:DimXMax, DimZMin:DimZMax), &
         !--- test
         & fs_DiffuseX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_DiffuseZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_al(DimXMin:DimXMax, DimZMin:DimZMax) )

    fs_VelX_al = 0.0d0
    fs_VelX_nl = 0.0d0
    fs_VelX_bl = 0.0d0

    sf_VelZ_al = 0.0d0
    sf_VelZ_nl = 0.0d0
    sf_VelZ_bl = 0.0d0

    ss_Exner_al = 0.0d0
    ss_Exner_nl = 0.0d0
    ss_Exner_bl = 0.0d0
    
    ss_PotTemp_al = 0.0d0
    ss_PotTemp_nl = 0.0d0
    ss_PotTemp_bl = 0.0d0

    fs_VelX_as = 0.0d0
    fs_VelX_ns = 0.0d0

    sf_VelZ_as = 0.0d0
    sf_VelZ_ns = 0.0d0

    ss_Exner_as = 0.0d0
    ss_Exner_ns = 0.0d0

    fs_ForceX_nl = 0.0d0
    sf_ForceZ_nl = 0.0d0

    ss_DivVel_ns = 0.0d0

    Time = 0.0d0

    !速度場の初期値
    fs_VelX_al  = fs_VelXDistrb
    fs_VelX_nl  = fs_VelXDistrb
    sf_VelZ_al = sf_VelZDistrb
    sf_VelZ_nl = sf_VelZDistrb

    !無次元圧力の初期値
    ss_Exner_al = ss_ExnerDistrb
    ss_Exner_nl = ss_ExnerDistrb

    !温位の初期値
    ss_PotTemp_al = ss_PotTempDistrb
    ss_PotTemp_nl = ss_PotTempDistrb

  end subroutine ArareAlloc




!!!
!!!出力ファイルの定義. 全領域
!!!
  subroutine ArareOpenDimFile
    use gridset, only: f_X, f_Z, s_X, s_Z
    use expname, only: exptitle, expsrc, expinst
    use fileset, only: fs_DimFile, sf_DimFile, ss_DimFile
    
    integer::   N
    
    N = size(f_X, 1)

    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = fs_DimFile,                              &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/N, N, 0/),                           &
         & longnames=(/'X-coordinate',                     &
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/), origin=0.0,              &
         & interval=0.0, &
         & history = fs_dim)
    
    ! 変数出力
    call HistoryPut('x', f_X, fs_dim)
    call HistoryPut('z', s_Z, fs_dim)
    
    ! ヒストリー作成
    call HistoryCreate(                                    &
         & file = sf_DimFile,                              &
         & title = exptitle,                               &
         & source = expsrc,                                &
         & institution = expinst,                          &
         & dims=(/'x','z','t'/),                           &
         & dimsizes=(/N, N, 0/),                           &
         & longnames=(/'X-coordinate',                     & 
         &             'Z-coordinate',                     &
         &             'Time        '/),                   &
         & units=(/'m','m','s'/),                          &
         & origin=0.0,                                     &
         & interval=0.0,                                   &
         & history = sf_dim)
    
    ! 変数出力
    call HistoryPut('x', s_X, sf_dim)
    call HistoryPut('z', f_Z, sf_dim)

    ! ヒストリー作成
    call HistoryCreate(                                  &
         & file = ss_DimFile,                            &
         & title = exptitle,                             &
         & source = expsrc,                              &
         & institution = expinst,                        &
         & dims=(/'x','z','t'/),                         &
         & dimsizes=(/N, N, 0/),                         &
         & longnames=(/'X-coordinate',                   &
         &             'Z-coordinate',                   &
         &             'Time        '/),                 &
         & units=(/'m','m','s'/), origin=0.0,            &
         & interval=0.0,                                 &
         & history = ss_dim)
    
    ! 変数出力
    call HistoryPut('x', s_X, ss_dim)
    call HistoryPut('z', s_Z, ss_dim)
    
    ! 無次元圧力
    call HistoryAddVariable(                             &
         & varname='Exner', dims=(/'x','z','t'/),        &
         & longname='nondimensional pressure', units='1',&
         & xtype='double',                               & 
         & history = ss_dim)    
    ! 仮温位
    call HistoryAddVariable(                            &
         & varname='PotTemp', dims=(/'x','z','t'/),      &
         & longname='virtual potential temperature',    &
         & units='K', xtype='double',                   & 
         & history = ss_dim)    
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelX', dims=(/'x','z','t'/),        &
         & longname='zonal velocity',                   &
         & units='m/s', xtype='double',                 & 
         & history = fs_dim)        
    ! 速度
    call HistoryAddVariable(                            &
         & varname='VelZ', dims=(/'x','z','t'/),        &
         & longname='vertical velocity',                &
         & units='m/s', xtype='double',                 & 
         & history = sf_dim)    

    !--- test
    ! 粘性拡散係数
    call HistoryAddVariable(                            &
         & varname='Km', dims=(/'x','z','t'/),        &
         & longname='viscosity',                &
         & units='1', xtype='double',                 & 
         & history = ss_dim)    
    ! 拡散項
    call HistoryAddVariable(                            &
         & varname='Du', dims=(/'x','z','t'/),        &
         & longname='diffusion',                &
         & units='1', xtype='double',                 & 
         & history = fs_dim)    
    ! 拡散項
    call HistoryAddVariable(                            &
         & varname='Dw', dims=(/'x','z','t'/),        &
         & longname='diffusion',                &
         & units='1', xtype='double',                 & 
         & history = sf_dim)    
  end subroutine ArareOpenDimFile


!!!
!!!ファイル出力. 長い時間ステップの値を出力. 全領域
!!!
  subroutine ArareOutputDimFile
    call HistoryPut('t', Time, ss_dim)
    call HistoryPut('t', Time, fs_dim)
    call HistoryPut('t', Time, sf_dim)

!    call HistoryPut('Exner', ss_Exner_al, ss_dim)
    call HistoryPut('Exner', fs_VelX_al, ss_dim)
    call HistoryPut('PotTemp', ss_PotTemp_al, ss_dim)
!    call HistoryPut('VelX',  fs_VelX_al, fs_dim)
    call HistoryPut('VelX',  fs_VelX_nl, fs_dim)
    call HistoryPut('VelZ',  sf_VelZ_al, sf_dim)

    !--- test
    call HistoryPut('Km', ss_Km_al, ss_dim)
    call HistoryPut('Du', fs_DiffuseX_nl, fs_dim)
    call HistoryPut('Dw', sf_DiffuseZ_nl, sf_dim)
  end subroutine ArareOutputDimFile


!!!
!!!出力ファイルのクローズ. 全領域
!!!  
  subroutine ArareCloseDimFile
    call HistoryClose( ss_dim )
    call HistoryClose( fs_dim )
    call HistoryClose( sf_dim )
  end subroutine ArareCloseDimFile


end program arare
