!===============================================================================
!
! clover_action.F90  -  calculates: -2 Tr(log(T_oo))
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 1998-2006 Hinnerk Stueben
!               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"
# include "clover.h"
# include "clover12.h"

!-------------------------------------------------------------------------------
REAL function clover_action(b)

  use typedef_clover
  use module_vol
  implicit none

  type(type_clover_b) :: b(2, volh)
  integer             :: i
  REAL                :: s, global_sum, clover_det
  

  s = ZERO

  !$omp parallel do reduction(+: s)
  do i = 1, volh
     s = s + log(clover_det(b(1, i)) * clover_det(b(2, i)))
  enddo

  clover_action = TWO * global_sum(s)

end

!-------------------------------------------------------------------------------

REAL function clover_det(b)  ! returns (1 / det)

  use typedef_clover
  implicit none
  type(type_clover_b) :: b

  clover_det = B11 * B22 * B33 * B44 * B55 * B66

end

!-------------------------------------------------------------------------------
REAL function clover_action12(det) ! input det is not 1/det, but det

  use typedef_clover
  use module_vol
  implicit none

  COMPLEX :: det(volh)
  integer :: i
  REAL    :: s, global_sum, clover_det12
  

  s = ZERO

  !$omp parallel do reduction(+: s)
  do i = 1, volh
     s = s + log(ONE/Re(det(i)*conjg(det(i))))
  enddo

  clover_action12 = TWO * global_sum(s) / TWO

end

!!!-------------------------------------------------------------------------------
!!
!!REAL function clover_det12(a)  ! returns (1 / det)
!!
!!  use typedef_clover
!!  implicit none
!!  type(type_clover12) :: a
!!
!!
!!
!!  clover_det12 = b%i(1,1) * b%i(2,2) * b%i(3,3) * b%i(4,4) * b%i(5,5) * b%i(6,6) * &
!!                 b%i(7,7) * b%i(8,8) * b%i(9,9) * b%i(10,10) * b%i(11,11) * b%i(12,12)
!!
!!end

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