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

program arare
                                                                 !=begin
  !== Dependency
  use dc_trace, only: BeginSub, EndSub, DbgMessage
  use dc_message
  use debugset, only: debugset_init, DebugOn
  use gridset,  only: gridset_init, DimXMin, DimXMax, DimZMin, DimZMax, &
       &              RegZMin, RegZMax
  use fileset,  only: fileset_init, cfgfile, ReStartFile
  use timeset,  only: timeset_init, NstepShort, NstepLong, &
       &              DelTimeShort, DelTimeLong, TimeDisp
  use nameset,  only: nameset_init
  use physset,  only: physset_init
  use linlib,   only: linlib_init
  use bcset,    only: bcset_init
  use basicset, only: basicset_file_init
  use arareset, only: arareset_init 
                                                                 !=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  :: fs_ForceVelX_nl(:,:)
  real(8), allocatable  :: sf_ForceVelZ_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(:,:)

  real(8), allocatable  :: ss_Km_bl(:,:)
  real(8), allocatable  :: ss_Km_nl(:,:)
  real(8), allocatable  :: ss_Km_al(:,:)
  real(8), allocatable  :: ss_Kh_bl(:,:)
  real(8), allocatable  :: ss_Kh_nl(:,:)
  real(8), allocatable  :: ss_Kh_al(:,:)

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


!!!
!!!初期化
!!!
  !--- I/O ファイル名の初期化
  call fileset_init

  !--- デバグ設定の初期化
  call debugset_init(cfgfile)

  !--- 格子点情報の初期化
  call gridset_init(cfgfile)
  
  !--- 時間積分の設定の初期化
  call timeset_init(cfgfile)
  
  !--- 実験名の初期化
  call nameset_init(cfgfile)
  
  !--- 物理量の初期化
  call physset_init(cfgfile)
  
  !--- 計算の設定を読み込む
  call arareset_init(cfgfile)

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

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

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

  !--- 基本場の値を設定
  call basicset_file_init( ReStartFile )

  !--- 擾乱場の値を設定
  call disturbset_file( ReStartFile, ReStartTime,   &
       & fs_VelX_nl,     sf_VelZ_nl,  ss_Exner_nl,  &
       & ss_PotTemp_nl,  ss_Km_nl,    ss_Kh_nl,     &
       & fs_VelX_bl,     sf_VelZ_bl,  ss_Exner_bl,  &
       & ss_PotTemp_bl,  ss_Km_bl,    ss_Kh_bl )

  !--- 時刻の初期化
  Time = ReStartTime(2)

  !--- テスト
  ss_PotTemp_nl = 0.0d0;   ss_PotTemp_bl = 0.0d0; 
  ss_Km_nl = 0.0d0;        ss_Km_bl = 0.0d0; 
  ss_Kh_nl = 0.0d0;        ss_Kh_bl = 0.0d0; 


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


!!!
!!!数値積分
!!!
  !--- 初期状態をファイル出力
!  call OutputDataFile( ReStartTime(1), &
!     & fs_VelX_bl, sf_VelZ_bl, ss_Exner_bl, ss_PotTemp_bl, ss_Km_bl, ss_Kh_bl)

!  call OutputDataFile( ReStartTime(2), &
!     & fs_VelX_nl, sf_VelZ_nl, ss_Exner_nl, ss_PotTemp_nl, ss_Km_nl, ss_Kh_nl)

!  !--- 長い時間ステップ. leap-frog スキームを利用
!  do t = 1, NstepLong - ( RestartTime(2) / DelTimeLong )
!     Time = Time + DelTimeLong
!     write(*,*) Time

!     !--- 渦粘性係数, 渦拡散係数の計算
!     call EddyVis_leapfrog(                                  &
!          & fs_VelX_bl, sf_VelZ_bl, ss_PotTemp_bl, ss_Km_bl, &
!          & fs_VelX_nl, sf_VelZ_nl, ss_Km_nl,                &
!          & ss_Km_al,   ss_Kh_al )

!     !--- 長い時間ステップでの温位の計算
!     call Scalar_leapfrog(                         &
!          & ss_PotTemp_bl, ss_Kh_bl,               &
!          & fs_VelX_nl, sf_VelZ_nl, ss_PotTemp_nl, &
!          & ss_PotTemp_al)
     
!     !--- 長い時間ステップでの速度 u の移流, 拡散, 粘性
!     call ForceVelX_leapfrog(                 &
!          & fs_VelX_bl, sf_VelZ_bl, ss_Km_bl, &
!          & fs_VelX_nl, sf_VelZ_nl,           &
!          & fs_ForceVelX_nl )
     
!     !--- 長い時間ステップでの速度 w の移流, 拡散, 粘性
!     call ForceVelZ_leapfrog(                       &
!          & fs_VelX_bl, sf_VelZ_bl, ss_Km_bl,       &
!          & fs_VelX_nl, sf_VelZ_nl, ss_PotTemp_nl,  &
!          & sf_ForceVelZ_nl )
     
     !--- 短いタイムステップの初期値を作る
     fs_VelX_ns  = fs_VelX_bl
     sf_VelZ_ns  = sf_VelZ_bl
     ss_Exner_ns = ss_Exner_bl 
     
     !--- 短いタイムステップでの積分

     do tau = 1, NstepShort
        Time = Time + DelTimeLong
        write(*,*) Time

        !--- 速度 u の計算
        call VelX_forward( &
             & fs_VelX_ns, sf_VelZ_ns, ss_Exner_ns, &
             & fs_ForceVelX_nl, fs_VelX_as)
        
        !--- エクスナー関数の計算
        call Exner_backward( &
             & fs_VelX_ns, sf_VelZ_ns, ss_Exner_ns, sf_ForceVelZ_nl, &
             & fs_VelX_as, &
             & ss_Exner_as )
        
        !--- 速度 w の計算
        call VelZ_backward( &
             & fs_VelX_ns,  sf_VelZ_ns,  ss_Exner_ns, sf_ForceVelZ_nl, &
             & ss_Exner_as, &
             & sf_VelZ_as)
        
        !--- ループを回すための処置
        ss_Exner_ns  = ss_Exner_as 
        fs_VelX_ns   = fs_VelX_as
        sf_VelZ_ns   = sf_VelZ_as
        
        !--- 音波計算用のテスト
        call OutputDataFile( Time, &
             &  fs_VelX_ns,    sf_VelZ_ns, ss_Exner_ns, &
             &  ss_PotTemp_nl, ss_Km_nl,   ss_Kh_nl )
        
     end do
     
!     !--- 最終的な速度を長い時間ステップでのものとみなす
!     ss_Exner_al  = ss_Exner_as 
!     fs_VelX_al   = fs_VelX_as
!     sf_VelZ_al   = sf_VelZ_as     
     
!     !--- アッセリンの時間フィルタ
!     call AsselinTimeFilter(ss_PotTemp_al, ss_PotTemp_nl, ss_PotTemp_bl )
!     call AsselinTimeFilter(ss_Exner_al,   ss_Exner_nl,   ss_Exner_bl )
!     call AsselinTimeFilter(fs_VelX_al,    fs_VelX_nl,    fs_VelX_bl )
!     call AsselinTimeFilter(sf_VelZ_al,    sf_VelZ_nl,    sf_VelZ_bl )

!     !--- ループを回すための処置
!     ss_PotTemp_bl = ss_PotTemp_nl
!     ss_Exner_bl   = ss_Exner_nl 
!     fs_VelX_bl    = fs_VelX_nl
!     sf_VelZ_bl    = sf_VelZ_nl
!     ss_Km_bl      = ss_Km_nl
!     ss_Kh_bl      = ss_Kh_nl
!     ss_PotTemp_nl = ss_PotTemp_al
!     ss_Exner_nl   = ss_Exner_al 
!     fs_VelX_nl    = fs_VelX_al
!     sf_VelZ_nl    = sf_VelZ_al
!     ss_Km_nl      = ss_Km_al
!     ss_Kh_nl      = ss_Kh_al

!     write(*,*) Time, TimeDisp, mod( Time, TimeDisp )
     
     !--- ファイル出力

!     if ( mod( Time, TimeDisp ) == 0 ) then 
!        call DbgMessage(fmt="Time = %f", d=(/Time/))
!        call OutputDataFile( Time, &
!             &  fs_VelX_nl,    sf_VelZ_nl, ss_Exner_nl, &
!             &  ss_PotTemp_nl, ss_Km_nl,   ss_Kh_nl )
!     end if
     
!  end do
  
  !--- 出力ファイルのクローズ
  call CloseDataFile

  !--- リスタートファイルの作成
  ReStartTime(1) = Time - DelTimeLong
  ReStartTime(2) = Time
  call MakeReStartFile( ReStartTime,               &
       & fs_VelX_nl,    sf_VelZ_nl,  ss_Exner_nl,  &
       & ss_PotTemp_nl, ss_Km_nl,    ss_Kh_nl,     & 
       & fs_VelX_bl,    sf_VelZ_bl,  ss_Exner_bl,  &
       & ss_PotTemp_bl, ss_Km_bl,    ss_Kh_bl )

contains

!!!
!!!配列の割り当て, 初期化
!!!
  subroutine ArareAlloc

    call BeginSub("ArareAlloc", &
&                 fmt="%c",     &
&                 c1="Allocate and initialize allocatable variables.")

    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_ForceVelX_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & sf_ForceVelZ_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_nl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Km_al(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_bl(DimXMin:DimXMax, DimZMin:DimZMax), &
         & ss_Kh_nl(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_ForceVelX_nl = 0.0d0
    sf_ForceVelZ_nl = 0.0d0

    ss_Km_bl = 0.0d0
    ss_Km_nl = 0.0d0
    ss_Km_al = 0.0d0
    ss_Kh_bl = 0.0d0
    ss_Kh_nl = 0.0d0
    ss_Kh_al = 0.0d0

    Time = 0.0d0

    call EndSub("ArareAlloc")

  end subroutine ArareAlloc

end program arare

