!===============================================================================
!
! coefficient_sig.F90 - coefficients for gauge action
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2006-2009, 2012 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/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine coefficient_sig(para)
  use module_input
  use typedef_hmc
  use module_counter
  use module_action
  use module_switches
  use module_function_decl
  implicit none
  type(hmc_para), intent(inout) :: para
  integer :: counter_traj, icount
  REAL :: csw

  if (counter%j_traj == 0) then
     select case (gauge_action)
        case ("WILSON")  ; call coefficient_wilson(para)
        case ("TREE")    ; call coefficient_tree(para)
        case ("IWASAKI") ; call coefficient_iwasaki(para)
        case default
           call die("coefficient_sig: " // gauge_action // ": unknown action")
     end select
     call coefficient_beta0123(para%beta)
  endif

end

!!!-------------------------------------------------------------------------------
!!REAL function calculate_plaq_ave(n,icount)
!!
!!  use module_action
!!  use module_function_decl
!!  implicit none
!!
!!  integer, intent(in)  :: n
!!  integer, intent(out) :: icount
!!  integer :: i
!!
!!  calculate_plaq_ave = ZERO
!!  icount = 0
!!
!!  do i = n-last_n_ave_plaq, n-1
!!     if (i >= plaq_start) then
!!        calculate_plaq_ave = calculate_plaq_ave + plaq_hist(i)
!!        icount = icount + 1
!!     endif
!!  enddo
!!  if (icount == 0) then
!!     calculate_plaq_ave = ONE
!!  else
!!     calculate_plaq_ave = calculate_plaq_ave / dble(icount)
!!  endif
!!  if (icount==0 .or. icount /= last_n_ave_plaq) then
!!     if (my_pe() == 0) write(UREC,*) "WARNING: TAD_sig  :", &
!!                   "icount=",icount,"n_ave_plaq=",last_n_ave_plaq
!!  endif
!!
!!end
!!
!-------------------------------------------------------------------------------
subroutine coefficient_beta0123(para_beta)
  use module_action
  implicit none
  REAL, intent(in) :: para_beta

  action%plaq%beta =para_beta*c0
  action%impg%beta1=para_beta*c1
  action%impg%beta2=para_beta*c2
  action%impg%beta3=para_beta*c3
end

!!!-------------------------------------------------------------------------------
!!subroutine coefficient_csw_kappa(csw, para)
!!  use module_action
!!  use typedef_hmc
!!  implicit none
!!  type(hmc_para), intent(inout) :: para
!!  REAL, intent(in) :: csw
!!
!!  if (csw == para%csw ) return
!!
!!  para%csw               = csw
!!  n_clover = 0
!!  n_mtilde = 0
!!  n_dslash = 0
!!  n_pfermi = 0
!!  call init_clover_field(para)
!!  call init_fermi_field(para)
!!
!!end
!!
!-------------------------------------------------------------------------------
subroutine coefficient_wilson(para)
  use typedef_hmc
  use module_action
  implicit none
  type(hmc_para), intent(in) :: para

  c0=ONE;c1=ZERO;c2=ZERO;c3=ZERO
  g2=6.0_8/para%beta
end

!-------------------------------------------------------------------------------
subroutine coefficient_tree(para)

  use typedef_hmc
  use module_action
  implicit none
  type(hmc_para), intent(in) :: para

!!  c0=5.0_8/3.0_8;c1=-ONE/12.0_8;c2=ZERO;c3=ZERO
!!  g2=6.0_8/para%beta
  c0=ONE;c1=-ONE/20.0_8;c2=ZERO;c3=ZERO
  g2=10.0_8/para%beta
end

!-------------------------------------------------------------------------------
subroutine coefficient_iwasaki(para)

  use typedef_hmc
  use module_action
  implicit none
  type(hmc_para), intent(in) :: para

  c0=3.648_8;c1=-0.331_8;c2=ZERO;c3=ZERO
  g2=6.0_8/para%beta
end

!!!-------------------------------------------------------------------------------
!!REAL function csw1_alpha(alpha)
!!
!!  implicit none
!!  REAL, intent(in) :: alpha
!!
!!!  csw1 = 0.00706281_8 + 1.142004_8 * para%alpha &
!!!                      - 4.194470_8 * para%alpha**2
!!!  csw1_alpha = 0.00823_8 + 1.13745_8 * alpha &
!!!                         - 4.18029_8 * alpha**2
!!   csw1_alpha = 0.19624370857181914_8 &
!!              + 1.1374515616446979_8 * alpha &
!!              - 4.1802905917395945_8 * alpha**2
!!
!!
!!
!!! 0.268185958818892985, if alpha = 0.1.
!!!
!!end
!!
!!!-------------------------------------------------------------------------------
!!subroutine read_plaq(para)
!!  use module_input
!!  use module_field
!!  use typedef_para
!!  use module_counter
!!  use module_action
!!  use module_conf_info
!!  use module_function_decl
!!  use module_switches
!!  implicit none
!!
!!  type(type_para), intent(inout) :: para
!!  FILENAME_FORMAT :: fmt
!!  integer :: i, j, iostat
!!  REAL :: plaq
!!  REAL, external :: sg
!!
!!  if (gauge_action == "WILSON") then
!!     switches%improved_gauge = .false.
!!  else
!!     switches%improved_gauge = .true.
!!  endif
!!
!!  do i = 1, para%n_temper
!!     call coefficient_sig(para%hmc(i))
!!     write(para%c_hmc(i)%csw, "(f20.15)") para%hmc(i)%csw
!!  enddo
!!
!!end

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