!===============================================================================
!
! cg_ww.F90 - matrix multiplication in BQCDi
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2006-2008 Yoshifumi Nakamura
!               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 d
# undef d_dag

#ifndef OMTDTD
# error "cg_ww needs OMTDTD"
#endif

#ifdef BAGEL
# error "cg_ww does not work with BAGEL"
#endif

!-------------------------------------------------------------------------------
# define TRACE_NAME T(cg_ww)
!-------------------------------------------------------------------------------
subroutine T(cg_ww)(out, in, id)

  use module_action
  use module_field
  use module_cg_arrays
  use module_cg_conf
  use module_cg_para
  use module_cg_procs, exclude => T(cg_ww)  ! avoid name clash
  use typedef_simd
  use T(module_vol)
  use module_switches
  implicit none
  spincol_FIELD, intent(out) :: out
  spincol_FIELD, intent(in)  :: in
  integer, intent(in)        :: id
 
  REAL :: kappa, b, h, rho
  COMPLEX :: fac1, fac2
  COMPLEX, parameter :: zone = cmplx(1,0)
  integer :: cid
  logical :: chem

  TRACE_ENTRY

  kappa = action%mtilde(id)%kappa
  b     = action%mtilde(id)%b
  h     = action%mtilde(id)%h
  rho   = action%mtilde(id)%rho
  cid   = action%mtilde(id)%cid
  chem  = (chemi /= 0 .or. chemi_i /=0)

  CHECK (h == 0)

  if (chem) then
     fac1=exp(cmplx( chemi, chemi_i))
     fac2=exp(cmplx(-chemi,-chemi_i))
     call emuu4(gauge(2)%u, fac1, fac2, .false.)
     call cg_procs%copyin_gauge(cg_conf%u, gauge(2)%u)
  endif

  !-- W mult (or M~ mult) -----------------------------------------------------
  if (kappa /= 0) then
     call cg_procs%d(so, se, cg_arrays%tmp1, in, cg_conf%u, cg_arrays%a)
     if (cid /= 0) call cg_procs%co(cg_conf%c_odd, cg_arrays%tmp1, volh_tot, volh)
     if (h /= 0)   call h_mult_b(-h, cg_arrays%tmp1, volh)
     call cg_procs%d(se, so, cg_arrays%tmp2, cg_arrays%tmp1, cg_conf%u, cg_arrays%a)
  endif

  if (cid /= 0) call cg_procs%co(cg_conf%c_even, cg_arrays%tmp2, volh_tot, volh)
  if (h /= 0)   call h_mult_b(h, cg_arrays%tmp2, volh)
  call cg_procs%xpby(cg_arrays%tmp2, in, b, volh_tot, volh)

  if (rho /= 0) call cg_procs%axpy(cg_arrays%tmp2, in, rho, volh_tot, volh)

  !-- W+ mult (or M~+ mult) ---------------------------------------------------
  if (cid /= 0) call cg_procs%c2(out, ZERO, cg_conf%c_even, cg_arrays%tmp2, volh_tot, volh)
  if (h /= 0) call h_mult_b(-h, out, volh)

  if (kappa /= 0) then
     call cg_procs%d_dag(so, se, cg_arrays%tmp1, out, cg_conf%u, cg_arrays%a)
     if (cid /= 0) call cg_procs%co(cg_conf%c_odd, cg_arrays%tmp1, volh_tot, volh)
     if (h /= 0) call h_mult_b(h, cg_arrays%tmp1, volh)
     call cg_procs%d_dag(se, so, out, cg_arrays%tmp1, cg_conf%u, cg_arrays%a)
  endif

  call cg_procs%xpby(out, cg_arrays%tmp2, b, volh_tot, volh)

  if (rho /= 0) call cg_procs%axpy(out, cg_arrays%tmp2, rho, volh_tot, volh)

  !----------------------------------------------------------------------------
  if (chem) call emuu4(gauge(2)%u, zone, zone, .false.)

  TRACE_RETURN
end
 
!===============================================================================
