!===============================================================================
!
! dsg_stout.F90 
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2006-2007 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 dsg_stout(p, conf, step, para)
  use module_field

  use typedef_hmc
  use module_hmc_forces
  use module_p_interface
  use module_vol
  implicit none

  type(hmc_para),  intent(in)    :: para
  type(hmc_conf),  intent(in)    :: conf
  GENERATOR_FIELD, intent(inout) :: p
  REAL,            intent(in)    :: step
  REAL                           :: s
  P_GAUGE_FIELD, save            :: uuu
  integer :: i, eo, mu

  if (para%beta == ZERO) return
  if (para%n_stout == 0) return

  TIMING_IMP_2P1_START(timing_imp_2p1_bin_dsg_stout)

  ALLOCATE_G_FIELD(uuu)

  call hmc_forces_old(p)
  s = -step * para%beta / THREE

  do mu = 1, DIM
     do eo = EVEN, ODD
        !$omp parallel do
        do i = 1, volh
           call staple(uuu(1, 1, i, eo, mu), stoutd(para%n_stout)%u, i, eo, mu)
        enddo
     enddo
  enddo
  call dstout(uuu, 1)
  call force_to_p(p, s, gauged(1)%u, uuu)

  call hmc_forces_new(p, step, i_sg)

  TIMING_IMP_2P1_STOP(timing_imp_2p1_bin_dsg_stout)

end

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