!= deepconv/arare 湿潤大気対流計算用主プログラム (三次元版)
!
!= deepconv/arare main program for moist atmospheric convection (3D)
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: arare_3d.f90,v 1.10 2009-02-28 08:19:10 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!

program arare
  !
  ! 非静力学モデル deepconv/arare 湿潤大気対流計算用主プログラム (三次元版)
  !

  ! モジュール引用  use statement 
  !

  ! gtool5 関連 
  ! gtool5 modules
  !
  use dc_types,          only: STRING, DP
  use dc_message,        only: MessageNotify
  use gtool_history,     only: HistoryPut
  use gtool_historyauto, only: HistoryAutoPut, HistoryAutoAddVariable

  ! 初期設定モジュール
  ! Initialize module
  !
  use mpi_wrapper, only : MPIWrapperInit,                       &
    &                     MPIWrapperFinalize, myrank
  use argset,      only : argset_init
  use clockset,    only : ClocksetInit,                         &
    &                     ClocksetClose,    ClocksetPredict,    &
    &                     ClockSetPreStart, ClockSetLoopStart,  &
    &                     ClockSetPreStop,  ClockSetLoopStop
  use gridset,     only : gridset_init,                         &
    &                     imin, imax, jmin, jmax, kmin, kmax,   &
    &                     nx, ny, nz, ncmax,                    &
    &                     DimXMin, DimXMax, DimYMin, DimYMax, DimZMin, DimZMax, & !!![不要]!!!
    &                     SpcNum, FileXMin, FileXMax, FileYMin, FileYMax, FileZMin, FileZMax  !!![不要]!!!
  use timeset,  only: timeset_init, TimesetDelTimeHalf, TimesetProgress, &
    &                 TimeA, TimeN, NstepShort, NstepOutput, &
    &                 EndTime, FlagInitialRun, &
    &                 DelTimeLong, DelTimeShort, DelTimeOutput, tfil  !!![不要]!!!
  use axesset,  only: axesset_init
  use xyz_base_module, only: xyz_avr_pyz, xyz_avr_xyr, xyz_avr_xqz
  use constants,only: constants_init
  use composition, only: composition_init, SpcWetSymbol
  use fileset,  only: fileset_init
  use basicset_3d, only: xyza_MixRtBasicZ, xyz_DensBasicZ, xyz_EffMolWtBasicZ, &
    &                 xyz_PotTempBasicZ, xyz_TempBasicZ, xyz_PressBasicZ, &
    &                 xyz_VelSoundBasicZ, xyz_ExnerBasicZ
  use ChemCalc_3d, only: ChemCalc_init
  use namelist_util, only: NmlutilInit, namelist_filename

  ! 化学量計算モジュール 
  ! Chemical calculation modules
  !
  use chemdata,      only: chemdata_init

  ! 力学過程計算用関数モジュール
  ! Dynamical processes module
  !
!  use DynFunc_3d,    only: xyz_AdvScalar, xyza_AdvScalar, xyz_AdvKm, &
!    &                         pyz_AdvVelX, xqz_AdvVelY,              &
!    &                         xyr_Buoy, xyr_AdvVelZ, pyz_GradPi, xqz_GradPi
!  use DynImpFunc_3d, only: xyz_Exner_init, xyz_Exner, xyr_GradPi
  use DynamicsHEVI_v3, only: Dynamics_Init,           &
    &                     Dynamics_Long_forcing,   &
    &                     Dynamics_Short_forcing

  ! 乱流拡散計算用モジュール
  ! Turbulent diffusion module
  !
  use Turbulence_kw1978,  only: Turbulence_KW1978_Init, Turbulence_KW1978_Forcing
!  use Turbulence_3d,  only: Turbulence_Init, xyz_BuoyKm, xyz_TurbKm, &
!    &                       xyz_TurbScalar, xyza_TurbScalar, pyz_TurbVelX, &
!    &                       xyr_TurbVelZ  , xyz_ShearKm    , xyz_DispKm,   &
!    &                       xyz_DispHeat  , xqz_TurbVelY   , EddyViscosity

!  ! 境界からのフラックス計算用モジュール
!  ! Surface flux module
!  !
!  use HeatFlux_3d,    only: xyz_HeatFluxBulk, pyz_MomFluxBulk, xqz_MomFluxBulk

  ! 境界からのフラックス計算用モジュール
  ! Surface flux module
  !
  use HeatFlux_3d,   only: xyz_HeatFluxDiff, xyza_MixRtFluxDiff

  ! 放射強制計算用モジュール
  ! Radiative forceing module
  !
  use Radiation_3d,   only: Radiation_init, xyz_RadHeatConst, xyz_NewtonCool

  ! 湿潤過程計算用モジュール
  ! Moist processes modules
  !
!  use moistset,       only: moistset_init
!  use MoistAdjust_3d, only: MoistAdjustSvapPress, MoistAdjustNH4SH
!  use WarmRainPrm_3d, only: WarmRainPrm_Init, xyz_Rain2GasHeat, xyza_Rain2Gas, &
!    &                       xyza_Rain2GasNH4SH, xyz_Rain2GasHeatNH4SH,         &
!    &                       xyza_Cloud2Rain, xyza_FallRain
  use WarmRainPrm_3d, only: WarmRainPrm_Init, xyza_FallRain
!  use MoistBuoyancy_3d,only: MoistBuoy_Init, xyz_BuoyMoistKm, xyr_BuoyMolWt, &
!    &                        xyr_BuoyDrag
!
  use Cloudphys_K1969, only: Cloudphys_K1969_Init, Cloudphys_K1969_forcing 

  ! 安定度の計算
  ! Static stability calculation module
  !
!  use ECCM_3D,        only: ECCM_Stab

  ! 数値拡散/摩擦計算用モジュール
  ! Numerical diffussion /dumping module
  !
!  use NumDiffusion4th_3d,only: NumDiffusion_Init, xyz_NumDiffScalar, &
!    &                       xyz_NumDiffKm, xyza_NumDiffScalar, &
!    &                       pyz_NumDiffVelX, xqz_NumDiffVelY,  &
!    &                       xyr_NumDiffVelZ 
!  use damping_3d,     only: damping_init, xyz_Sponge, pyz_Sponge, &
!    &                       xqz_Sponge, xyr_Sponge

  ! 下請けモジュール
  ! Utility modules
  !
  use damping_3d_v2,        only: damping_v2_init, SpongeLayer_forcing
  use fillnegative_3d, only: FillNegative_init, xyza_FillNegative_xyza
  use cflcheck,       only : CFLCheckTimeShort, &
    &                        CFLCheckTimeLongVelX, &
    &                        CFLCheckTimeLongVelY, &
    &                        CFLCheckTimeLongVelZ
!  use xyz_bc_module,  only : BoundaryXCyc_xyz, BoundaryYCyc_xyz, &
!    &                        BoundaryZSym_xyz, BoundaryZCyc_xyz, &
!    &                        BoundaryXCyc_pyz, BoundaryYCyc_pyz, &
!    &                        BoundaryZSym_pyz, BoundaryZCyc_pyz, &
!    &                        BoundaryXCyc_xqz, BoundaryYCyc_xqz, &
!    &                        BoundaryZSym_xqz, BoundaryZCyc_xqz, &
!    &                        BoundaryXCyc_xyr, BoundaryYCyc_xyr, &
!    &                        BoundaryZSym_xyr, BoundaryZCyc_xyr, &
!    &                        BoundaryZAsym_xyr
  use setmargin, only: SetMargin_init, SetMargin_xyzf, SetMargin_xyz, &
    &                  SetMargin_pyz, SetMargin_xqz, SetMargin_xyr

  ! ファイル入出力モジュール
  ! File I/O module
  !
  use RestartFileIO_3d, only : ReStartFileio_init, ReStartFileio_Finalize, &
    &                          ReStartFileio_BasicZ_Get, ReStartFileio_Var_Get, rstat
  use HistoryFileIO_3d, only: HistoryFileIO_init, HistoryFileIO_finalize


  implicit none

  ! 内部変数
  ! Internal variables
  !
  real(DP), allocatable :: xyz_VelXNl(:,:,:)
  real(DP), allocatable :: xyz_VelYNl(:,:,:)
  real(DP), allocatable :: xyz_VelZNl(:,:,:)

  real(DP), allocatable :: pyz_VelXBl(:,:,:)    
                             ! $ u (t-\Delta t) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXNl(:,:,:)    
                             ! $ u (t) $          東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXAl(:,:,:)    
                             ! $ u (t+\Delta t) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXNs(:,:,:)    
                             ! $ u (\tau) $ 東西風 ; zonal wind
  real(DP), allocatable :: pyz_VelXAs(:,:,:)    
                             ! $ u (\tau +\Delta \tau) $ 東西風 ; zonal wind
  real(DP), allocatable :: xqz_VelYBl(:,:,:)    
                             ! $ v (t-\Delta t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYNl(:,:,:)    
                             ! $ v (t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYAl(:,:,:)    
                             ! $ v (t+\Delta t) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYNs(:,:,:)   
                             ! $ v (\tau -\tau) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xqz_VelYAs(:,:,:)
                             ! $ v (\tau) $ 南北風 ; meridonal wind
  real(DP), allocatable :: xyr_VelZBl(:,:,:)    
                             ! $ w (t-\Delta t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZNl(:,:,:)    
                             ! $ w (t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZAl(:,:,:)    
                             ! $ w (t+\Delta t) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZNs(:,:,:)    
                             ! $ w (\tau) $ 鉛直風 ; vertical wind
  real(DP), allocatable :: xyr_VelZAs(:,:,:) 
                             ! $ w (\tau +\Delta \tau)  鉛直風 ; vertical wind
  real(DP), allocatable :: xyz_ExnerBl(:,:,:)   
                             ! $ \pi (t-\Delta t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerNl(:,:,:)   
                             ! $ \pi (t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerAl(:,:,:)
                             ! $ \pi (t+\Delta t) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerNs(:,:,:)   
                             ! $ \pi (\tau -\Delta \tau) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_ExnerAs(:,:,:)   
                             ! $ \pi (\tau) $ 圧力関数 ; Exner function
  real(DP), allocatable :: xyz_PotTempBl(:,:,:) 
                             ! $ \theta (t-\Delta t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempNl(:,:,:) 
                             ! $ \theta (t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempAl(:,:,:) 
                             ! $ \theta (t+\Delta t) $ 温位 ; Potential temp.
  real(DP), allocatable :: xyz_PotTempWork(:,:,:) 
                             ! 温位 $ \theta $ の作業配列 ; Work array
  real(DP), allocatable :: xyz_KmBl(:,:,:)
                             ! $ Km (t-\Delta t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KmNl(:,:,:)
                             ! $ K_m (t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KmAl(:,:,:)
                             ! $ K_m (t+\Delta t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhBl(:,:,:)      
                             ! $ K_h (t-\Delta t) $ 乱流拡散係数
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhNl(:,:,:)
                             ! $ K_h (t) $ 乱流拡散係数 
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyz_KhAl(:,:,:)
                             ! $ K_h (t+\Delta t) $ 乱流拡散係数
                             ! Turbulent diffusion coeff. 
  real(DP), allocatable :: xyza_MixRtBl(:,:,:,:) 
                             ! $ q (t-\Delta t) $ 湿潤量の混合比
                             ! Mixing ratio of moist variables.
  real(DP), allocatable :: xyza_MixRtNl(:,:,:,:) 
                             ! $ q (t) $ 湿潤量の混合比 
                             ! Mixing ratio of moist variables
  real(DP), allocatable :: xyza_MixRtAl(:,:,:,:) ! 
                             ! $ q (t+\Delta t) $ 湿潤量の混合比 
                             !Mixing ratio of moist variables
  real(DP), allocatable :: xyza_MixRtWork(:,:,:,:)
                             ! 湿潤量の作業配列
                             ! Work array for mixing ratio.
  real(DP), allocatable :: xyza_MixRtOrg(:,:,:,:)
                             ! 湿潤量の作業配列
                             ! Work array for mixing ratio.
  real(DP), allocatable :: xyz_PotTempOrg(:,:,:)
                             ! 温位の作業配列
                             ! Work array for potential temperature
  real(DP), allocatable :: pyz_AccelVelXNl(:,:,:) 
                             ! 気圧傾度力を除いた $u$ の変化率
                             ! Tendency of $u$ except for pressure gradient term
  real(DP), allocatable :: xqz_AccelVelYNl(:,:,:) 
                             ! 気圧傾度力を除いた $v$ の変化率
                             ! Tendency of $v$ except for pressure gradient term
  real(DP), allocatable :: xyr_AccelVelZNl(:,:,:) 
                             ! 気圧傾度力を除いた $w$ の変化率
                             ! Tendency of $w$ except for pressure gradient term
  real(DP), allocatable :: xyz_DExnerDtNl(:,:,:) 
                             ! エクスナー関数の変化率
                             ! Tendency of exner function
  real(DP), allocatable :: xyz_DExnerDtNs(:,:,:) 
                             ! エクスナー関数の変化率
                             ! Tendency of exner function
  real(DP), allocatable :: xyza_DelMixRt(:,:,:,:)
                             ! 湿潤量の混合比 $ q $ の増分
                             ! Mixing ratio variation.
  real(DP)              :: DelTimeLFrog
                             ! リープフロッグスキーム用時間格子間隔
                             ! Time interval for Leap-frog scheme

  integer :: & 
    & t,     & 
    & tau,   & ! do ループ変数 ; do loop variable  
    & t1,    & ! do ループ変数 ; do loop variable  
    & t2,    & ! do ループ変数 ; do loop variable 
    & s        ! do ループ変数 ; do loop variable 

  real(8), allocatable :: DKmDt1(:,:,:), DKmDt2(:,:,:), DKmDt3(:,:,:), DKmDt4(:,:,:), DKmDt5(:,:,:), DKmDt6(:,:,:), DKmDt7(:,:,:)
  real(8), allocatable :: DPTempDt1(:,:,:), DPTempDt2(:,:,:), DPTempDt3(:,:,:), DPTempDt4(:,:,:)
  real(8), allocatable :: DPTempDt5(:,:,:), DPTempDt6(:,:,:), DPTempDt7(:,:,:)
  real(8), allocatable :: DUDt1(:,:,:), DUDt2(:,:,:), DUDt3(:,:,:), DUDt4(:,:,:), DUDt5(:,:,:), DUDt6(:,:,:), DUDt7(:,:,:)
  real(8), allocatable :: DVDt1(:,:,:), DVDt2(:,:,:), DVDt3(:,:,:), DVDt4(:,:,:), DVDt5(:,:,:), DVDt6(:,:,:), DVDt7(:,:,:)
  real(8), allocatable :: DWDt1(:,:,:), DWDt2(:,:,:), DWDt3(:,:,:), DWDt4(:,:,:), DWDt5(:,:,:), DWDt6(:,:,:), DWDt7(:,:,:)
  real(8), allocatable :: DQMixDt1(:,:,:,:), DQMixDt2(:,:,:,:), DQMixDt3(:,:,:,:), DQMixDt4(:,:,:,:), DQMixDt5(:,:,:,:)
  real(8), allocatable :: DExnerDt1(:,:,:)
  real(8), allocatable :: aaa_tmp1(:,:,:), aaa_tmp2(:,:,:)
  
  logical :: CloudFlag = .true.
  logical :: TurbFlag  = .true.
  logical :: BuoyMoistFlag = .true.

  !------------------------------------------
  ! 初期化手続き Initialize procedure 
  !
  call MainInit

  !------------------------------------------
  ! 時間積分 time integration 
  !
  t = 1
  do while (TimeN <= EndTime ) 

      ! 時刻の設定
      ! Time setting.
      !
      DelTimeLFrog = DelTimeLong * 2.0d0

      ! 初期化
      !
      call ClearTendency

      ! スポンジ層
      !
      call SpongeLayer_forcing(                                              &
         &   pyz_VelXBl, xqz_VelYBl, xyr_VelZBl, xyz_PotTempBl, xyz_ExnerBl, &
         &   DUDt2, DVDt2, DWDt3, DPTempDt4, DExnerDt1                       &
         & )

      ! 移流拡散
      !
      call Dynamics_Long_forcing(       &
        & pyz_VelXBl,    pyz_VelXNl,    & ! (in)
        & xqz_VelYBl,    xqz_VelYNl,    & ! (in)
        & xyr_VelZBl,    xyr_VelZNl,    & ! (in)
        & xyz_PotTempBl, xyz_PotTempNl, & ! (in)
        & xyz_ExnerBl,   xyz_ExnerNl,   & ! (in)
        & xyza_MixRtBl,  xyza_MixRtNl,  & ! (in)
        & xyz_KmBl,      xyz_KmNl,      & ! (in)
        & DUDt1,                        & ! (inout)
        & DVDt1,                        & ! (inout)
        & DWDt1,                        & ! (inout)
        & DPTempDt1,                    & ! (inout)
        & DExnerDt1,                    & ! (inout)
        & DQMixDt1,                     & ! (inout)
        & DKmDt1                        & ! (inout)
        & )
      
      ! 渦拡散係数の移流拡散
      ! Advection and diffusion of turbulent diffusion coefficient.
      !
      if (TurbFlag) then 
      call turbulence_KW1978_forcing(               &
         & pyz_VelXBl, xqz_VelYBl, xyr_VelZBl,      &
         & xyz_PotTempBl, xyz_ExnerBl, xyza_MixRtBl,&
         & xyz_KmBl, xyz_KhBl, aaa_tmp1,            &
         & DUDt4, DVDt4, DWDt5,                     &
         & DPTempDt5, DExnerDt1, DQMixDt4,          &
         & DKmDt3, aaa_tmp2                         &
         & )
      
      ! 積分
      !
!      xyz_KmAl = xyz_KmBl + DelTimeLFrog * ( DKmDt3 + DKmDt1 )
      xyz_KmAl = xyz_KmBl + DelTimeLFrog * ( DKmDt1 + DKmDt3 )

      ! 値の上限下限の設定
      !  * 値は正になることを保証する
      !  * 値の上限は 800 とする. 中島健介(1994, 学位論文)参照
      !
      ! Upper and lower bound value are specified.
      !
      xyz_KmAl = max( 0.0d0, min( xyz_KmAl, 800.0d0 ) )

      ! 境界条件 ; Boundary condition
      !
      call SetMargin_xyz( xyz_KmAl )  
      
      else

      xyz_KmBl = 0.0d0
      xyz_KmNl = 0.0d0
      xyz_KmAl = 0.0d0
      xyz_KhAl = 0.0d0

      end if

      ! スカラーに対する渦拡散係数の計算 
      ! Specify turbulent diffusion coefficient for scalar variables.
      !
      xyz_KhAl = 3.0d0 * xyz_KmAl
      
      ! 温位の移流拡散の計算 
      ! Advection and diffusion of potential temperature.
      !
      DPTempDt3 = + xyz_RadHeatConst( xyz_ExnerBl )                   
      DPTempDt7 = + xyz_HeatFluxDiff( xyz_PotTempNl )

      ! 積分
      !
!      xyz_PotTempAl = xyz_PotTempBl + DelTimeLFrog * ( DPTempDt5 + DPTempDt3 + DPTempDt7 + DPTempDt4 + DPTempDt1 )
      xyz_PotTempAl = xyz_PotTempBl + DelTimeLFrog * ( DPTempDt4 + DPTempDt1 + DPTempDt5 + DPTempDt3 + DPTempDt7 )

!      call HistoryAutoPut(TimeN, 'PotTempRad',  DPTempDt3(FileXMin:FileXMax,FileYMin:FileYMax,FileZMin:FileZMax))
!      call HistoryAutoPut(TimeN, 'PotTempFlux', DPTempDt7(FileXMin:FileXMax,FileYMin:FileYMax,FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'DPTempDtFlux', DPTempDt7(FileXMin:FileXMax,FileYMin:FileYMax,FileZMin:FileZMax))

      ! 境界条件 ; Boundary condition
      !
      call SetMargin_xyz( xyz_PotTempAl )  ! (inout) 
    
      ! 凝縮成分混合比の移流拡散 
      ! Advection and diffusion of vapor, cloud and rain mixing ratios.
      !
!      DQMixDt3 = + xyza_FallRain(xyza_MixRtBl)
      DQMixDt5 = + xyza_MixRtFluxDiff(xyza_MixRtNl)

!      xyza_MixRtAl = xyza_MixRtBl + DelTimeLFrog * ( DQMixDt4 + DQMixDt5 + DQMixDt1 + DQMixDt3 )
!      xyza_MixRtAl = xyza_MixRtBl + DelTimeLFrog * ( DQMixDt4 + DQMixDt5 + DQMixDt1 )
      xyza_MixRtAl = xyza_MixRtBl + DelTimeLFrog * ( DQMixDt1 + DQMixDt4 + DQMixDt5 )

      do s = 1, SpcNum
!        call HistoryAutoPut(TimeN, 'D'//trim(SpcWetSymbol(s))//'DtFall', &
!          & DQMixDt3(FileXMin:FileXMax,FileYMin:FileYMax, FileZMin:FileZMax, s))
        call HistoryAutoPut(TimeN, 'D'//trim(SpcWetSymbol(s))//'DtFlux',  &
          & DQMixDt5(FileXMin:FileXMax,FileYMin:FileYMax, FileZMin:FileZMax, s))
      end do

      ! 境界条件 Boundary condition
      !
      call SetMargin_xyzf( xyza_MixRtAl )            ! (inout)

      ! 移流によって負になった部分を埋める
      ! Negative values due to advection are corrected.
      !
      xyza_MixRtWork = xyza_MixRtAl
      xyza_MixRtAl   = xyza_FillNegative_xyza( xyza_MixRtWork ) 
      
      ! 埋めた/削った量を保管
      ! Correction value is stored.
      !
      DQMixDt1 = (xyza_MixRtAl - xyza_MixRtWork) / DelTimeLFrog
      do s = 1, SpcNum
        call HistoryAutoPut(TimeN, 'D'//trim(SpcWetSymbol(s))//'DtFill1',  &
          & DQMixDt1(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax, s))
      end do      

      ! 埋め切れなかった部分をゼロにする. Fill2 に保管
      ! Negative values mixing ratios are corrected.
      xyza_MixRtWork = xyza_MixRtAl
      xyza_MixRtAl = max( - xyza_MixRtBasicZ, xyza_MixRtWork )
      DQMixDt1 = (xyza_MixRtAl - xyza_MixRtWork) / DelTimeLFrog 
      do s = 1, SpcNum
        call HistoryAutoPut(TimeN, 'D'//trim(SpcWetSymbol(s))//'DtFill2',  &
          & DQMixDt1(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax, s))
      end do

      ! 境界条件 Boundary condition
      !
      call SetMargin_xyzf( xyza_MixRtAl )  ! (inout)

      !!!------------------------------------------------------
      !!! 凝結過程
      !!!
      if (CloudFlag) then 
!        call ArareCloudPhys

         DExnerDt1 = 0.0d0  ! 作業配列

         call Cloudphys_K1969_forcing(      &
           &   xyz_ExnerNl,                 &!(in)
           &   xyza_MixRtNl,                &!(in)
!           &   xyz_DExnerDtNl,              &!(inout)
           &   DExnerDt1,                   &!(inout)
           &   xyz_PotTempAl, xyza_MixRtAl  &!(inout)
           & )
      end if

      ! 速度の移流拡散.
      ! Advection and diffusion of velocity components.
      !
!      pyz_AccelVelXNl = DUDt4 + DUDt2 + DUDt1
!      xqz_AccelVelYNl = DVDt4 + DVDt2 + DVDt1 
!      xyr_AccelVelZNl = DWDt5 + DWDt1 + DWDt3 
      pyz_AccelVelXNl = DUDt2 + DUDt1 + DUDt4
      xqz_AccelVelYNl = DVDt2 + DVDt1 + DVDt4 
      xyr_AccelVelZNl = DWDt3 + DWDt1 + DWDt5 
      xyz_DExnerDtNl    = 0.0d0
      xyz_DExnerDtNs    = 0.0d0

      call SetMargin_xyr( xyr_AccelVelZNl )

      ! 短い時間ステップの初期値作成.
      ! Initial values set up for time integration with short time step.
      !
      pyz_VelXNs  = pyz_VelXBl
      xqz_VelYNs  = xqz_VelYBl
      xyr_VelZNs  = xyr_VelZBl
      xyz_ExnerNs = xyz_ExnerBl

      ! 短い時間ステップの時間積分. オイラー法を利用.
      ! Time integration with short time step.
      !
      Euler: do tau = 1, NstepShort

        call Dynamics_Short_forcing(  &
          &  pyz_VelXNs,          & ! (in)
          &  xqz_VelYNs,          & ! (in)
          &  xyr_VelZNs,          & ! (in)
          &  xyz_ExnerNs,         & ! (in)
          &  pyz_AccelVelXNl,     & ! (in)
          &  xqz_AccelVelYNl,     & ! (in)
          &  xyr_AccelVelZNl,     & ! (in)
          &  xyz_DExnerDtNl,      & ! (in)
          &  xyz_DExnerDtNs,      & ! (in)
          &  pyz_VelXAs,          & ! (out)
          &  xqz_VelYAs,          & ! (out)
          &  xyr_VelZAs,          & ! (out)
          &  xyz_ExnerAs          & ! (out)
          & )
         
        ! 短い時間ステップのループを回すための処置
        ! Renew prognostic variables for next short time step integration.
        !
        xyz_ExnerNs  = xyz_ExnerAs
        pyz_VelXNs   = pyz_VelXAs
        xqz_VelYNs   = xqz_VelYAs
        xyr_VelZNs   = xyr_VelZAs
         
      end do Euler
   
      ! 最終的な短い時間ステップでの値を長い時間ステップでの値とみなす
      ! Renew prognostic variables for next long time step integration.
      !
      xyz_ExnerAl  = xyz_ExnerAs
      pyz_VelXAl   = pyz_VelXAs
      xqz_VelYAl   = xqz_VelYAs
      xyr_VelZAl   = xyr_VelZAs
     
      ! 時間フィルタ. 
      ! Time filter. 
      !
      call AsselinTimeFilter 

      ! ヒストリファイル出力. 時間フィルタをかけた後の数値を出力. 
      ! Out put to history file.
      !
      xyz_VelXNl = xyz_avr_pyz(pyz_VelXNl)
      xyz_VelYNl = xyz_avr_xqz(xqz_VelYNl)
      xyz_VelZNl = xyz_avr_xyr(xyr_VelZNl)
      
      call HistoryAutoPut(TimeN, 'PotTemp', xyz_PotTempNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'Exner', xyz_ExnerNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'VelX',  xyz_VelXNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'VelY',  xyz_VelYNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'VelZ',  xyz_VelZNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'Km',    xyz_KmNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      call HistoryAutoPut(TimeN, 'Kh',    xyz_KhNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax))
      do s = 1, SpcNum
        call HistoryAutoPut(TimeN, 'D'//trim(SpcWetSymbol(s)), xyza_MixRtNl(FileXMin:FileXMax, FileYMin:FileYMax, FileZMin:FileZMax, s))
      end do


  ! リスタートファイルの作成
  ! Make restartfile.
  !
    if (mod(t, NstepOutput) == 0) then 

      ! ファイル出力
      !
      call HistoryPut( 't',       TimeN,        rstat)
      call HistoryPut( 'VelX',    pyz_VelXNl,    rstat)
      call HistoryPut( 'VelY',    xqz_VelYNl,    rstat)
      call HistoryPut( 'VelZ',    xyr_VelZNl,    rstat)
      call HistoryPut( 'Exner',   xyz_ExnerNl,   rstat)
      call HistoryPut( 'PotTemp', xyz_PotTempNl, rstat)
      call HistoryPut( 'Km',      xyz_KmNl,      rstat)
      call HistoryPut( 'Kh',      xyz_KhNl,      rstat)
      call HistoryPut( 'MixRt',   xyza_MixRtNl,  rstat)

      call HistoryPut( 't',       TimeA,         rstat)
      call HistoryPut( 'VelX',    pyz_VelXAl,    rstat)
      call HistoryPut( 'VelY',    xqz_VelYAl,    rstat)
      call HistoryPut( 'VelZ',    xyr_VelZAl,    rstat)
      call HistoryPut( 'Exner',   xyz_ExnerAl,   rstat)
      call HistoryPut( 'PotTemp', xyz_PotTempAl, rstat)
      call HistoryPut( 'Km',      xyz_KmAl,      rstat)
      call HistoryPut( 'Kh',      xyz_KhAl,      rstat)
      call HistoryPut( 'MixRt',   xyza_MixRtAl, rstat)

      ! Show CPU time 
      ! 
      call ClocksetPredict
      
    end if

      ! 長い時間ステップのループを回すための処置.
      ! Renew prognostic variables for next long time step integration.
      !
      xyz_PotTempBl = xyz_PotTempNl
      xyza_MixRtBl  = xyza_MixRtNl
      xyz_ExnerBl   = xyz_ExnerNl
      pyz_VelXBl    = pyz_VelXNl
      xqz_VelYBl    = xqz_VelYNl
      xyr_VelZBl    = xyr_VelZNl
      xyz_KmBl      = xyz_KmNl
      xyz_KhBl      = xyz_KhNl
    
      xyz_PotTempNl = xyz_PotTempAl
      xyza_MixRtNl  = xyza_MixRtAl
      xyz_ExnerNl   = xyz_ExnerAl
      pyz_VelXNl    = pyz_VelXAl
      xqz_VelYNl    = xqz_VelYAl
      xyr_VelZNl    = xyr_VelZAl
      xyz_KmNl      = xyz_KmAl
      xyz_KhNl      = xyz_KhAl

      ! 時刻の進行
      ! Progress time
      !
      call TimesetProgress
      t = t + 1
  end do

  !----------------------------------------------------      
  ! 出力ファイルのクローズ
  ! Close out put files.
  !
  call HistoryFileIO_finalize
  call ReStartFileIO_finalize

  !----------------------------------------------------
  ! MPI END
  !
  call MPIWrapperFinalize

  !----------------------------------------------------
  ! CPU Time
  ! 
  call ClocksetLoopStop
  call ClocksetClose
  
contains

  !-----------------------------------------------------------------------
  subroutine AsselinTimeFilter
    !
    ! 時間フィルター; Asselin のタイムフィルターを利用
    !   t = 0.0 の場合には tfil = 0.0d0, それ以外は tfil = 1.0d-1
    !   (t = 0 の時はオイラー法で, それ以外はリープフロッグ法で積分するため)
    !
    use TimeSet, only: tfil

    ! 暗黙の型宣言禁止    
    !
    implicit none

    real(8) :: tfil2

    tfil2 = 1.0d0 - 2.0d0 * tfil 

    pyz_VelXNl  =  tfil * ( pyz_VelXBl    + pyz_VelXAl )    + tfil2 * pyz_VelXNl
    xqz_VelYNl  =  tfil * ( xqz_VelYBl    + xqz_VelYAl)     + tfil2 * xqz_VelYNl
    xyr_VelZNl  =  tfil * ( xyr_VelZBl    + xyr_VelZAl )    + tfil2 * xyr_VelZNl
    xyz_PotTempNl= tfil * ( xyz_PotTempBl + xyz_PotTempAl ) + tfil2 * xyz_PotTempNl
    xyz_ExnerNl =  tfil * ( xyz_ExnerBl   + xyz_ExnerAl )   + tfil2 * xyz_ExnerNl 
    xyz_KmNl    =  tfil * ( xyz_KmBl      + xyz_KmAl )      + tfil2 * xyz_KmNl
    xyz_KhNl    =  tfil * ( xyz_KhBl      + xyz_KhAl )      + tfil2 * xyz_KhNl
    xyza_MixRtNl=  tfil * ( xyza_MixRtBl  + xyza_MixRtAl )  + tfil2 * xyza_MixRtNl
    
  end subroutine AsselinTimeFilter

  !-----------------------------------------------------------------------
  subroutine MainInit
    ! 初期化手続き ; Initialize procedure 
    !

    ! NAMELIST ファイル名の読み込み
    ! Loading NAMELIST file.
    !
    implicit none

    ! 変数の設定
    !
    character(STRING) :: cfgfile  ! NAMELIST ファイル名

    ! MPI
    !
    call MPIWrapperInit

    ! 時間計測
    !
    call ClocksetInit
    call ClocksetPreStart
    
    ! NAMELIST ファイル名の読み込み
    ! Loading NAMELIST file.
    !
    call argset_init( cfgfile ) !(out)

    ! NAMELIST ファイル名のモジュールへの登録
    ! Loading NAMELIST file.
    !
    call NmlutilInit( cfgfile ) !(in)
    
    ! 時間積分の初期化
    ! Initialization of time integration.
    !
    call timeset_init

    ! 格子点情報の初期化
    ! Initialization of grid arrangement.
    !
    call gridset_init
    
    ! 化学定数の初期化
    ! Initialization of chemical constatns.
    !
    call chemdata_init

    ! 定数の情報の初期化
    ! Initialization of constant variables.
    !
    call constants_init

    ! 軸の情報の初期化
    ! Initialization of axis variables.
    !
    call axesset_init

    ! I/O ファイル名の初期化
    ! Initialization of output file name. 
    !
    call fileset_init
    
    ! 湿潤過程共有変数の初期化
    ! Initialization of common variables for moist process.
    !
    call composition_init

    ! ヒストリーファイル・リスタートファイルの初期化
    ! Initialize restart & history files.
    !
    call HistoryFileio_init
    call ReStartFileio_init
   
    ! マージンの設定の初期化
    ! Initialization of margin
    !
    call SetMargin_Init

    ! 内部変数の初期化
    ! Initialization of internal variables.
    !
    call ArareAlloc

    ! 初期値の代入 
    ! * ReStartFile が設定されている場合にはファイルを読み込む. 
    !   設定されていない場合にはデフォルトの基本場と擾乱場を作る. 
    !
    ! Initial value set up.
    ! * Read restartfile if it is specified. If not, make default basic
    !   state and disturbance.
    !
    if (myrank == 0) call MessageNotify( "M", "main", "Initial value setup." )

    ! 基本場, 擾乱場の初期値を netCDF ファイルから取得する.
    ! 
    call ReStartFileio_BasicZ_Get
    call ReStartFileio_Var_Get(     &
      & pyz_VelXBl,  pyz_VelXNl,    & ! (out)
      & xqz_VelYBl,  xqz_VelYNl,    & ! (out)
      & xyr_VelZBl,  xyr_VelZNl,    & ! (out)
      & xyz_PotTempBl, xyz_PotTempNl,   & ! (out)
      & xyz_ExnerBl, xyz_ExnerNl,   & ! (out)
      & xyza_MixRtBl,xyza_MixRtNl,  & ! (out)
      & xyz_KmBl,    xyz_KmNl,      & ! (out)
      & xyz_KhBl,    xyz_KhNl       & ! (out)
      & )

    ! 化学計算ルーチンの初期化
    ! Initialization of chemical routines.
    !
    call chemcalc_init

    ! 数値摩擦係数の初期化
    ! Initialization of numerical friction coefficient.
    !
!    call Damping_Init( &
!      & cfgfile        & ! (in)
!      & )      
    call Damping_v2_Init

    ! 数値拡散項の初期化
    ! Initialization of numerical diffusion term.
    !
!    call NumDiffusion_Init( &
!      & cfgfile             & ! (in)
!      & )          

    ! 力学モジュールの初期化
    ! Initialization of dynamical modules
    !
    call Dynamics_Init
    
    ! 乱流拡散項の初期化
    ! Initialization of turbulent diffusion term.
    !
!    call Turbulence_Init( )
    call Turbulence_KW1978_Init

    ! 暖かい雨のパラメタリゼーションの初期化 
    ! Initialization of warmrain parameterization.
    !
    call WarmRainPrm_Init( &
      & cfgfile            & ! (in)
      & )  

    call Cloudphys_K1969_Init

    ! 負の湿潤量の補填計算の初期化
    ! Initialization of negative moist value correction.
    !
!    call FillNegative_Init( &
!      & xyza_MixRtBasicZ,    & ! (in)
!      & xyz_DensBasicZ       & ! (in)
!      & )
    call FillNegative_Init

    ! 放射強制の初期化
    !  Initialization of radiative forcing.
    !
    call Radiation_Init( &
      & cfgfile          & ! (in)    
      & )

    ! 湿潤気塊の浮力計算の初期化
    ! Initialization of moist buoyancy calculation.
    !
!    call MoistBuoy_Init( ) 

    ! 圧力計算用係数行列の初期化 
    ! Initialization of coefficient matrix for exner function calculation.
    !
!    call xyz_Exner_Init( )              
    
    ! 音波に対する CFL 条件のチェック
    ! CFL condtion check for sound wave.
    !
    call CFLCheckTimeShort( &
      & xyz_VelSoundBasicZ   & ! (in)
      & )

    ! CPU time
    !
    call ClocksetPreStop

!    ! 保存量のモニタリング
!    !
!    call EnergyMonit_init

    ! 最初の一回目の場合はオイラー法で回す.
    !
    if ( FlagInitialRun ) call TimesetDelTimeHalf

  end subroutine MainInit
  
  
  !-----------------------------------------------------------------------
  subroutine ArareAlloc
    !
    !初期化として, 配列を定義し, 値としてゼロを代入する.
    !

    !暗黙の型宣言禁止
    implicit none

    integer :: s

    !配列割り当て
    allocate(                                                       &
      & xyz_VelXNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_VelYNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_VelZNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & pyz_VelXBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & pyz_VelXNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & pyz_VelXAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & pyz_VelXNs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & pyz_VelXAs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xqz_VelYBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xqz_VelYNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xqz_VelYAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xqz_VelYNs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xqz_VelYAs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyr_VelZBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyr_VelZNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyr_VelZAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyr_VelZNs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyr_VelZAs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyz_ExnerBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_ExnerNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_ExnerAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_ExnerNs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_ExnerAs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyz_PotTempOrg(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_PotTempWork(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_PotTempBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_PotTempNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_PotTempAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyz_KmBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_KmNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_KmAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyz_KhBl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_KhNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyz_KhAl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
!
      & xyza_MixRtOrg(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum),  &
      & xyza_MixRtWork(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum), &
      & xyza_MixRtBl  (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum), &
      & xyza_MixRtNl  (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum), &
      & xyza_MixRtAl  (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum), &
!
      & pyz_AccelVelXNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xqz_AccelVelYNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),&
      & xyr_AccelVelZNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),& 
      & xyz_DExnerDtNl(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),& 
      & xyz_DExnerDtNs(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax),& 
!
      & xyza_DelMixRt(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax,SpcNum) )

    pyz_VelXNs  = 0.0d0;    pyz_VelXAs = 0.0d0    
    xqz_VelYNs  = 0.0d0;    xqz_VelYAs = 0.0d0    
    xyr_VelZNs  = 0.0d0;    xyr_VelZAs = 0.0d0
    xyz_ExnerNs = 0.0d0;    xyz_ExnerAs = 0.0d0

    pyz_VelXBl  = 0.0d0;    pyz_VelXNl  = 0.0d0;    pyz_VelXAl  = 0.0d0
    xqz_VelYBl  = 0.0d0;    xqz_VelYNl  = 0.0d0;    xqz_VelYAl  = 0.0d0
    xyr_VelZBl  = 0.0d0;    xyr_VelZNl  = 0.0d0;    xyr_VelZAl  = 0.0d0
    xyz_ExnerBl = 0.0d0;    xyz_ExnerNl = 0.0d0;    xyz_ExnerAl = 0.0d0
    xyz_KmBl    = 0.0d0;    xyz_KmNl    = 0.0d0;    xyz_KmAl    = 0.0d0
    xyz_KhBl    = 0.0d0;    xyz_KhNl    = 0.0d0;    xyz_KhAl    = 0.0d0
    xyz_PotTempBl = 0.0d0;  xyz_PotTempNl = 0.0d0;  xyz_PotTempAl = 0.0d0
    xyza_MixRtBl = 0.0d0;   xyza_MixRtNl = 0.0d0;   xyza_MixRtAl = 0.0d0

    xyz_PotTempWork = 0.0d0 
    xyza_MixRtWork = 0.0d0 

    xyz_PotTempOrg = 0.0d0 
    xyza_MixRtOrg = 0.0d0 

    pyz_AccelVelXNl = 0.0d0
    xqz_AccelVelYNl = 0.0d0
    xyr_AccelVelZNl = 0.0d0
    xyz_DExnerDtNl  = 0.0d0
    xyz_DExnerDtNs  = 0.0d0

    xyza_DelMixRt = 0.0d0

    DelTimeLFrog = 0.0d0

    allocate(                                            &
         & DKmDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt6(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DKmDt7(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DKmDt1 = 0.0d0
    DKmDt2 = 0.0d0
    DKmDt3 = 0.0d0
    DKmDt4 = 0.0d0
    DKmDt5 = 0.0d0
    DKmDt6 = 0.0d0
    DKmDt7 = 0.0d0

    allocate(                                            &
         & DPTempDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt6(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DPTempDt7(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DPTempDt1 = 0.0d0
    DPTempDt2 = 0.0d0
    DPTempDt3 = 0.0d0
    DPTempDt4 = 0.0d0
    DPTempDt5 = 0.0d0
    DPTempDt6 = 0.0d0
    DPTempDt7 = 0.0d0

    allocate(                                            &
         & DUDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt6(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DUDt7(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DUDt1 = 0.0d0
    DUDt2 = 0.0d0
    DUDt3 = 0.0d0
    DUDt4 = 0.0d0
    DUDt5 = 0.0d0
    DUDt6 = 0.0d0
    DUDt7 = 0.0d0

    allocate(                                            &
         & DVDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt6(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DVDt7(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DVDt1 = 0.0d0
    DVDt2 = 0.0d0
    DVDt3 = 0.0d0
    DVDt4 = 0.0d0
    DVDt5 = 0.0d0
    DVDt6 = 0.0d0
    DVDt7 = 0.0d0

    allocate(                                            &
         & DWDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt6(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),        &
         & DWDt7(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DWDt1 = 0.0d0
    DWDt2 = 0.0d0
    DWDt3 = 0.0d0
    DWDt4 = 0.0d0
    DWDt5 = 0.0d0
    DWDt6 = 0.0d0
    DWDt7 = 0.0d0

    allocate(                                            &
         & DQMixDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax, SpcNum),        &
         & DQMixDt2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax, SpcNum),        &
         & DQMixDt3(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax, SpcNum),        &
         & DQMixDt4(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax, SpcNum),        &
         & DQMixDt5(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax, SpcNum)         &
         &) 
    DQMixDt1 = 0.0d0
    DQMixDt2 = 0.0d0
    DQMixDt3 = 0.0d0
    DQMixDt4 = 0.0d0
    DQMixDt5 = 0.0d0

    allocate(                                            &
         & DExnerDt1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    DExnerDt1 = 0.0d0

    allocate(                                            &
         & aaa_tmp1(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax),         &
         & aaa_tmp2(DimXMin:DimXMax, DimYMin:DimYMax, DimZMin:DimZMax)         &
         &) 
    aaa_tmp1 = 0.0d0
    aaa_tmp2 = 0.0d0

    
    call HistoryAutoAddVariable(  &
      & varname='DPTempDtFlux',    &
      & dims=(/'x','y','z','t'/), &
      & longname='potential temperature surface flux',  &
      & units='K',            &
      & xtype='double' )

    call HistoryAutoAddVariable(  &
      & varname='PotTempFlux',&
      & dims=(/'x','y','z','t'/),     &
      & longname='Surface Flux term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )
    
    call HistoryAutoAddVariable(  &
      & varname='PotTempRad', &
      & dims=(/'x','y','z','t'/),     &
      & longname='Radiation term of potential temperature', &
      & units='K.s-1"',    &
      & xtype='double' )

    do s = 1, ncmax
      call HistoryAutoAddVariable(  &
        & varname='D'//trim(SpcWetSymbol(s))//'DtFlux', & 
        & dims=(/'x','y','z','t'/),     &
        & longname='Surface Flux term of '          &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',    &
        & xtype='double' )
    end do

  end subroutine ArareAlloc


  subroutine ClearTendency

    DKmDt1 = 0.0d0; DKmDt2 = 0.0d0; DKmDt3 = 0.0d0; DKmDt4 = 0.0d0
    DKmDt5 = 0.0d0; DKmDt6 = 0.0d0; DKmDt7 = 0.0d0
    DPTempDt1 = 0.0d0; DPTempDt2 = 0.0d0; DPTempDt3 = 0.0d0; DPTempDt4 = 0.0d0
    DPTempDt5 = 0.0d0; DPTempDt6 = 0.0d0; DPTempDt7 = 0.0d0
    DUDt1 = 0.0d0; DUDt2 = 0.0d0; DUDt3 = 0.0d0; DUDt4 = 0.0d0
    DUDt5 = 0.0d0; DUDt6 = 0.0d0; DUDt7 = 0.0d0
    DVDt1 = 0.0d0; DVDt2 = 0.0d0; DVDt3 = 0.0d0; DVDt4 = 0.0d0
    DVDt5 = 0.0d0; DVDt6 = 0.0d0; DVDt7 = 0.0d0
    DWDt1 = 0.0d0; DWDt2 = 0.0d0; DWDt3 = 0.0d0; DWDt4 = 0.0d0
    DWDt5 = 0.0d0; DWDt6 = 0.0d0; DWDt7 = 0.0d0
    DQMixDt1 = 0.0d0; DQMixDt2 = 0.0d0; DQMixDt3 = 0.0d0
    DQMixDt4 = 0.0d0; DQMixDt5 = 0.0d0
    DExnerDt1 = 0.0d0
    aaa_tmp1 = 0.0d0; aaa_tmp2 = 0.0d0
    
  end subroutine ClearTendency
  
!-----------------------------------------------------------------------
end program arare
