!===============================================================================
!
! iteration_count_is.F90  -  counts iterations of solver
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2003 Hinnerk Stueben
!               2006-2010 Yoshifumi Nakamura
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
module module_iteration_count_is
  integer, dimension(0:20), save :: it_f = 0, it_d = 0, it_h = 0
  integer, dimension(0:20), save :: it_f4= 0, it_d4= 0, it_h4= 0
  integer, save ::n_iter = 9
end

!-------------------------------------------------------------------------------
subroutine iteration_count_is_f(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_f(i) = it_f(i) + iter
end
!-------------------------------------------------------------------------------
subroutine iteration_count_is_d(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_d(i) = it_d(i) + iter
end
!-------------------------------------------------------------------------------
subroutine iteration_count_is_h(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_h(i) = it_h(i) + iter
end

!-------------------------------------------------------------------------------
subroutine iteration_count_is_f4(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_f4(i) = it_f4(i) + iter
end
!-------------------------------------------------------------------------------
subroutine iteration_count_is_d4(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_d4(i) = it_d4(i) + iter
end
!-------------------------------------------------------------------------------
subroutine iteration_count_is_h4(iter,i)
  use module_iteration_count_is
  implicit none
  integer :: iter,i
  it_h4(i) = it_h4(i) + iter
end

!-----------------------------------------------------------------------------
subroutine iteration_count_init()
  use module_iteration_count_is
  implicit none

  it_f = 0
  it_d = 0
  it_h = 0
  it_f4 = 0
  it_d4 = 0
  it_h4 = 0

end

!-----------------------------------------------------------------------------
subroutine iteration_count_is_write(unit)
  use module_dd
  use module_counter
  use module_function_decl
  use module_iteration_count_is
  use module_switches

  implicit none

  integer, intent(in)     :: unit
  integer, save           :: written = 0 

  character(*), parameter :: keyf   = "%it  "
  character(*), parameter :: keyd   = "%itD "
  character(*), parameter :: keyh   = "%itH "
  character(*), parameter :: keyf4  = "%it4 "
  character(*), parameter :: keyd4  = "%itD4"
  character(*), parameter :: keyh4  = "%itH4"
  character(len=30) :: fmt_h
  character(len=30) :: fmt_b

  integer ::i
  character(9), dimension(0:n_iter+10) :: i_name
  real(8) :: itmp(2,0:n_iter)

     i_name(0)  = "  iter_SF"
     i_name(1)  = "  iter_F1"
     i_name(2)  = "  iter_F2"
     i_name(3)  = "  iter_F3"
     i_name(4)  = "  iter_F4"
     i_name(5)  = "  iter_F5"
     i_name(6)  = "  iter_F6"
     i_name(7)  = "  iter_F7"
     i_name(8)  = "  iter_F8"
     i_name(9)  = "  iter_F9"
!     fmt_h = "(1x, 2a, a7, 30x,   a, "// char(n_iter+48) //"a)"
!     fmt_b = "(2x, a4, i7, 30x, i12, "// char(n_iter+48) //"i12)"
     fmt_h = "(1x, 2a, a10, 25x, a , 9a )"
     fmt_b = "(2x, a5, i10, 25x, i9, 9i9)"


  if (written == 0 .and. my_pe() == 0) then
     write(unit, fmt_h) "T", keyf , "traj", i_name(0), (i_name(i), i = 1, n_iter)
  endif
  if (my_pe() == 0) write(unit, fmt_b) keyf , counter%traj, it_f(0), (it_f(i), i = 1, n_iter)
  if (my_pe() == 0) write(unit, fmt_b) keyf4, counter%traj, it_f4(0), (it_f4(i), i = 1, n_iter)


  if (ddable) then
  itmp(1,0:n_iter)=it_d( 0:n_iter)
  itmp(2,0:n_iter)=it_d4(0:n_iter)
  call global_sum_vec( (n_iter+1)*2, itmp)
  it_d( 0:n_iter)=nint(itmp(1,0:n_iter)/dble(ld%lat%num_ip*num_pes()))
  it_d4(0:n_iter)=nint(itmp(2,0:n_iter)/dble(ld%lat%num_ip*num_pes()))
  if (my_pe() == 0) write(unit, fmt_b) keyd , counter%traj, it_d(0), (it_d(i), i = 1, n_iter)
  if (my_pe() == 0) write(unit, fmt_b) keyd4, counter%traj, it_d4(0), (it_d4(i), i = 1, n_iter)
  if (my_pe() == 0) write(unit, fmt_b) keyh , counter%traj, it_h(0), (it_h(i), i = 1, n_iter)
  if (my_pe() == 0) write(unit, fmt_b) keyh4, counter%traj, it_h4(0), (it_h4(i), i = 1, n_iter)
  endif

  written = written + 1
  it_f = 0
  it_d = 0
  it_h = 0
  it_f4 = 0
  it_d4 = 0
  it_h4 = 0
end

!===============================================================================
