!===============================================================================
!
! cg_mcg.F90 - loops in cg_kernel_mcg
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2016 Hinnerk Stueben
!
! 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/>.
!
!-------------------------------------------------------------------------------
# define LANGUAGE_FORTRAN
# include "defs.h"
# include "cg_defs.h"

!-------------------------------------------------------------------------------
# undef  TRACE_NAME
# define TRACE_NAME T(cg_mcg_loop3)
!-------------------------------------------------------------------------------
subroutine T(cg_mcg_loop3)(rtr, r, aap, ak, volh_tot, varsBLOCK)

  implicit none
  integer, intent(in)          :: volh_tot, varsBLOCK
  REAL, intent(in)             :: ak
  spincol_FIELD, intent(in)    :: aap
  spincol_FIELD, intent(inout) :: r
  DOUBLE, intent(out)          :: rtr
  DOUBLE                       :: dtmp1, dtmp2
  integer                      :: i

  TRACE_ENTRY

  rtr = ZERO

#ifndef BLOCK
  !$omp parallel do &
  !$omp default(none) &
  !$omp shared(volh, r, aap, ak) &
  !$omp private(dtmp1, dtmp2) &
  !$omp reduction(+: rtr)
  do i = 1, varsBLOCK
#else
  do i = varsBLOCK
#endif   
     DO c = 1, 3
     DO s2 = 1, 2
     DO s1 = 1, 2
     Spincol(r, s1, s2, c, i) = Spincol(r, s1, s2, c, i) - ak * Spincol(aap, s1, s2, c, i)
     dtmp1 = Re(Spincol(r, s1, s2, c, i))
     dtmp2 = Im(Spincol(r, s1, s2, c, i))
     dtmp1 = dtmp1**2
     dtmp2 = dtmp2**2
     rtr = rtr + dtmp1
     rtr = rtr + dtmp2
     ENDDO
     ENDDO
     ENDDO
  enddo

  TRACE_RETURN
end

!-------------------------------------------------------------------------------
# undef TRACE_NAME
# define TRACE_NAME T(cg_mcg_update_sco)
!-------------------------------------------------------------------------------
subroutine T(cg_mcg_update_sco)(sco1, sco2, r, salpha, sbeta, sxi_new, volh_tot, varsBLOCK)

  implicit none
  integer, intent(in)          :: volh_tot, varsBLOCK
  spincol_FIELD, intent(inout) :: sco1, sco2
  spincol_FIELD, intent(in)    :: r
  DOUBLE, intent(in)           :: salpha, sbeta
  REAL, intent(in)             :: sxi_new

  integer :: i

  TRACE_ENTRY

#ifndef BLOCK
  !$omp parallel do default(none) &
  !$omp shared(volh, sco1, sco2, r, salpha, sbeta, sxi_new)
  do i = 1, varsBLOCK
#else
  do i = varsBLOCK
#endif   
  DO c = 1, 3
  DO s2 = 1, 2
  DO s1 = 1, 2
     Spincol(sco1, s1, s2, c, i) = Spincol(sco1, s1, s2, c, i)         - Spincol(sco2, s1, s2, c, i) * salpha
     Spincol(sco2, s1, s2, c, i) = Spincol(sco2, s1, s2, c, i) * sbeta + Spincol(r,    s1, s2, c, i) * sxi_new
  ENDDO
  ENDDO
  ENDDO
  enddo

  TRACE_RETURN
end

!-------------------------------------------------------------------------------
# undef TRACE_NAME
# define TRACE_NAME T(cg_mcg_update_sco1)
!-------------------------------------------------------------------------------
subroutine T(cg_mcg_update_sco1)(sco1, sco2, salpha, volh_tot, varsBLOCK)

  implicit none
  integer, intent(in)          :: volh_tot, varsBLOCK
  spincol_FIELD, intent(inout) :: sco1, sco2
  DOUBLE, intent(in)           :: salpha

  integer :: i

  TRACE_ENTRY

#ifndef BLOCK
  !$omp parallel do default(none) &
  !$omp shared(volh, sco1, sco2, salpha)
  do i = 1, varsBLOCK
#else
  do i = varsBLOCK
#endif   
  DO c = 1, 3
  DO s2 = 1, 2
  DO s1 = 1, 2
     Spincol(sco1, s1, s2, c, i) = Spincol(sco1, s1, s2, c, i)         - Spincol(sco2, s1, s2, c, i) * salpha
  ENDDO
  ENDDO
  ENDDO
  enddo

  TRACE_RETURN
end

!-------------------------------------------------------------------------------
# undef TRACE_NAME
# define TRACE_NAME T(cg_mcg_update_sco2)
!-------------------------------------------------------------------------------
subroutine T(cg_mcg_update_sco2)(sco2, r, sbeta, sxi_new, volh_tot, varsBLOCK)

  implicit none
  integer, intent(in)          :: volh_tot, varsBLOCK
  spincol_FIELD, intent(inout) :: sco2
  spincol_FIELD, intent(in)    :: r
  DOUBLE, intent(in)           :: sbeta
  REAL, intent(in)             :: sxi_new

  integer :: i

  TRACE_ENTRY

#ifndef BLOCK
  !$omp parallel do default(none) &
  !$omp shared(volh, sco2, r, sbeta, sxi_new)
  do i = 1, varsBLOCK
#else
  do i = varsBLOCK
#endif   
  DO c = 1, 3
  DO s2 = 1, 2
  DO s1 = 1, 2
     Spincol(sco2, s1, s2, c, i) = Spincol(sco2, s1, s2, c, i) * sbeta + Spincol(r,    s1, s2, c, i) * sxi_new
  ENDDO
  ENDDO
  ENDDO
  enddo

  TRACE_RETURN
end

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