!= Module Clockset
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: clockset.f90,v 1.2 2011-10-14 12:14:32 yot Exp $ 
! Tag Name::  $Name: arare5-20130116 $
! 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 clockset
  !
  !引数に与えられた NAMELIST ファイルから, 時刻に関する情報を取得し, 
  !保管するための変数型モジュール
  !
  
  !モジュール読み込み
  use dc_types,   only: DP, STRING
  use dc_message, only: MessageNotify
  use dc_clock,   only : CLOCK, DCClockCreate, &
    & DCClockClose, DCClockStart, DCClockStop, &
    & DCClockResult, DCClockPredict, &
    & operator(+)                             ! Access module (モジュール指定)
  use mpi_wrapper, only: myrank
  use timeset,     only: TimeA, RestartTime, IntegPeriod

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private
  
  ! Public Interface
  type(CLOCK), save :: clock_init, clock_loop  ! Variables for CPU time counting 
                                               ! CPU 時間計測用変数
  public ClocksetInit, ClocksetClose, ClocksetPredict
  public ClockSetPreStart, ClockSetPreStop, ClockSetLoopStart, ClocksetLoopStop

contains
   
  subroutine ClocksetInit
    ! 時間計測 初期化
    !
    implicit none
    
    ! 初期化ルーチン用の時刻計測初期化
    !
    call DCClockCreate( &           ! Initialize (初期化)
      & clk = clock_init, &         ! (out)
      & name = 'initialization' )   ! (in)

    ! 時間発展ループ用の時刻計測初期化
    !
    call DCClockCreate( &           ! Initialize (初期化)
      & clk = clock_loop, &         ! (out)
      & name = 'time-integration' ) ! (in)

  end subroutine ClocksetInit


  subroutine ClocksetPreStart
    ! 時刻計測開始
    !
    implicit none
    
    call DCClockStart(clk = clock_init) ! (inout)   ! Start CPU time counting 
    
  end subroutine ClocksetPreStart
  

  subroutine ClocksetLoopStart
    ! 時刻計測開始
    !
    implicit none

    call DCClockStart(clk = clock_loop) ! (inout)   ! Start CPU time counting 

  end subroutine ClocksetLoopStart


  subroutine ClocksetPreStop
    ! 時刻計測開始
    !
    implicit none
    
    call DCClockStop(clk = clock_init) ! (inout)    ! Stop CPU time counting 
                                                    ! (CPU 時間計測終了)    
  end subroutine ClocksetPreStop


  subroutine ClocksetLoopStop
    ! 時刻計測開始
    !
    implicit none

    call DCClockStop(clk = clock_loop) ! (inout)    ! Stop CPU time counting 
                                                    ! (CPU 時間計測終了)    

  end subroutine ClocksetLoopStop


  subroutine ClocksetPredict
    ! 時刻計測開始
    !
    implicit none
    real(4) :: progress 

    progress = real((TimeA - RestartTime) / IntegPeriod, 4)

    if (myrank == 0) then 
      write(*,*) ""
      call MessageNotify( "M", "main", "Time = %f", d=(/TimeA/) )

      ! CPU time measurement stops, temporarily.
      call ClocksetLoopStop

      call DCClockPredict( &                       ! Estimate remaining time (残り時間の予測)
        &   clk = clock_init + clock_loop,       & ! (in)
        &   progress = progress                  & ! (in) 
        &  )

      ! CPU time measurement starts again.
      call ClocksetLoopStart

    end if

  end subroutine ClocksetPredict


  subroutine ClocksetClose
    ! 時刻計測終了
    !
    implicit none

    if (myrank == 0) then     
      call DCClockResult( &                    ! Display total CPU time (全 CPU 時間の表示)
        & clks = (/clock_init, clock_loop/), & ! (in)
        & total_auto = .true. )                ! (in)
    end if
    call DCClockClose( clk = clock_init )    ! (inout)       ! Finalize (後処理)
    call DCClockClose( clk = clock_loop )    ! (inout)       ! Finalize (後処理)
    
  end subroutine ClocksetClose
  

end module clockset
