!===============================================================================
!
! dsg.F90  -  p(j,x,mu) := p(j,x,mu) - step * D_{x,mu,j} S_g
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 1998-2013 Hinnerk Stueben
!               2007-2013 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(p, u, plaq, step)

  use module_field
  use module_action_type
  use module_vol
  use module_staple
  use module_switches
  implicit none

  GENERATOR_FIELD, intent(inout) :: p
  GAUGE_FIELD, intent(in)        :: u
  type(type_plaq), intent(in)    :: plaq
  REAL,            intent(in)    :: step
  REAL                           :: s
  SU3                            :: uuu, w
  integer                        :: mu, eo, i

  if (plaq%beta == ZERO) return
  if (step == ZERO) return

  TIMING_START(timing_bin_dsg)
  DEBUG2S("Start: dsg")

  call hmc_forces_old(p)

  s = -step * plaq%beta / THREE

  do mu = 1, DIM
     do eo = EVEN, ODD
        !$omp parallel do private(uuu, w)
        do i = 1, volh
           call staple(uuu, u, i, eo, mu)
           call uu(w, u(1, 1, i, eo, mu), uuu)
           call im_tr_j(p(1, i, eo, mu), w, s)
        enddo
     enddo
  enddo

  !!if (switches%qed) then
  !!   p = ZERO
  !!endif

  if (switches%boundary_sf) call dsg_sfdiff(p, u, plaq, step)
#ifdef BQCD_DDHMC
  call nonactive(p) !! for ddhmc
#endif
  call hmc_forces_new(p, step, plaq%fid)

  DEBUG2S("End: dsg")
  TIMING_STOP(timing_bin_dsg)
end

!-------------------------------------------------------------------------------
subroutine dsg_qed(p, a, step, beta)

  use module_vol
  use module_switches
  implicit none

  U1_GENERATOR_FIELD, intent(inout) :: p
  U1_GENERATOR_FIELD, intent(in)    :: a
  REAL, intent(in)                  :: step, beta
  REAL                              :: s, aaa
  integer                           :: mu, eo, i

  if (.not. switches%qed) return

  DEBUG2S("Start: dsg_qed")

  s = step * beta

  do mu = 1, DIM
     do eo = EVEN, ODD
        !$omp parallel do private(aaa)
        do i = 1, volh
           call staple_qed(aaa, a, i, eo, mu)
           p(i, eo, mu) = p(i, eo, mu) - s * (SIX * a(i, eo, mu) + aaa)
        enddo
     enddo
  enddo

  DEBUG2S("End: dsg_qed")
end

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