!===============================================================================
!
! ddsg.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2008 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 ddsg(p, conf, step, para)

  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
  SU3 :: w
  P_GAUGE_FIELD, save            :: uuu, ff
  integer :: i, eo, mu

  if (para%beta == ZERO) return

!!  TIMING_IMP_2P1_START(timing_imp_2p1_bin_ddsg)

  ALLOCATE_G_FIELD(uuu)
  ALLOCATE_G_FIELD(ff)

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

  do mu = 1, DIM
     do eo = EVEN, ODD
        !$omp parallel do private(w)
        do i = 1, volh
           call staple(w, dgauge(1)%u, i, eo, mu)
!           call u_scale(w, s)
           call uu(uuu(1, 1, i, eo, mu), dgauge(1)%u(1, 1, i, eo, mu), w)
        enddo
     enddo
  enddo
  call xbound_g_field(uuu)
  do mu = 1, DIM
     do eo = EVEN, ODD
        !$omp parallel do
        do i = 1, volh
           call ulam_space(ff(1, 1, i, eo, mu), dgauge(1)%u, uuu, i, eo, mu)
        enddo
     enddo
  enddo
  call force_to_p(p, s, dgauge(1)%u, ff)
!!  call hmc_forces_new(p, step, i_sg)

!!  TIMING_IMP_2P1_STOP(timing_imp_2p1_bin_ddsg)

end

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