!===============================================================================
!
! m_tilde-v3.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2007-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 mtil(out, in, id)
  use module_vol
  use module_action
  use module_field
  implicit none
  SPINCOL_FIELD, intent(out) :: out
  SPINCOL_FIELD, intent(in) :: in
  integer, intent(in) :: id
  SPINCOL_FIELD :: tmp
  REAL :: b, h
  COMPLEX :: fac1, fac2
  COMPLEX, parameter :: zone = cmplx(1,0)

  b = action%mtilde(id)%b
  h = action%mtilde(id)%h

  if (chemi /= 0 .or. chemi_i /=0) then
     fac1=exp(cmplx( chemi, chemi_i))
     fac2=exp(cmplx(-chemi,-chemi_i))
     call emuu4(gauge(2)%u, fac1, fac2, .false.)
  endif

  if (action%mtilde(id)%kappa /= 0) then
#ifdef BAGEL
     TIMING_START(timing_bin_bagel_d)
     call bagel_d(tmp, bagel_u, in, se, 0)
     TIMING_STOP(timing_bin_bagel_d)
#else
     call d(so, se, tmp, in, gauge(2)%u(1,1,1,0,1))
#endif

     if (action%mtilde(id)%cid /= 0) call clover_inv_mult(tmp, action%mtilde(id)%cid, so)
     if (action%mtilde(id)%h  /= 0) call h_mult_b(-h, tmp, volh)

#ifdef BAGEL
     TIMING_START(timing_bin_bagel_d)
     call bagel_d(out, bagel_u, tmp, so, 0)
     TIMING_STOP(timing_bin_bagel_d)
#else
     call d(se, so, out, tmp, gauge(2)%u(1,1,1,0,1))
#endif
  endif

#ifdef OMTDTD
  if (action%mtilde(id)%cid /= 0) call clover_inv_mult(out, action%mtilde(id)%cid, se)
  if (action%mtilde(id)%h /= 0) call h_mult_b( h, out, volh)
  call sc_xpby(out, in, b)
#else
  if (action%mtilde(id)%cid /= 0) then
     call clover_mult(tmp, in, action%mtilde(id)%cid, se)
     call sc_xpby(out, tmp, b)
  else
     call sc_xpby(out, in, b)
     if (action%mtilde(id)%h /= 0) call h_mult_a(out, h, in, volh)
  endif
#endif

  if (chemi /= 0 .or. chemi_i /=0)call emuu4(gauge(2)%u, zone, zone, .false.)

end

!-------------------------------------------------------------------------------
subroutine mtil_dag(out, in, id)
  use module_vol
  use module_action
  use module_field
  implicit none
  SPINCOL_FIELD, intent(out) :: out
  SPINCOL_FIELD, intent(in) :: in
  integer, intent(in) :: id
  SPINCOL_FIELD :: tmp
  REAL :: b, h
  COMPLEX :: fac1, fac2
  COMPLEX, parameter :: zone = cmplx(1,0)

  b = action%mtilde(id)%b
  h = action%mtilde(id)%h

  if (chemi /= 0 .or. chemi_i /=0) then
     fac1=exp(cmplx(-chemi, chemi_i))
     fac2=exp(cmplx( chemi,-chemi_i))
     call emuu4(gauge(2)%u, fac1, fac2, .false.)
  endif

#ifdef OMTDTD
  call sc_copy(out,in)
  if (action%mtilde(id)%cid /= 0) call clover_dag_inv_mult(out, action%mtilde(id)%cid, se)
  if (action%mtilde(id)%h /= 0) call h_mult_b(-h, out, volh)

  if (action%mtilde(id)%kappa /= 0) then
#ifdef BAGEL
     TIMING_START(timing_bin_bagel_d)
     call bagel_d(tmp, bagel_u, out, se, 1)
     TIMING_STOP(timing_bin_bagel_d)
#else
     call d_dag(so, se, tmp, out, gauge(2)%u(1,1,1,0,1))
#endif

#else
  if (action%mtilde(id)%kappa /= 0) then
#ifdef BAGEL
     TIMING_START(timing_bin_bagel_d)
     call bagel_d(tmp, bagel_u, in, se, 1)
     TIMING_STOP(timing_bin_bagel_d)
#else
     call d_dag(so, se, tmp, in, gauge(2)%u(1,1,1,0,1))
#endif

#endif

     if (action%mtilde(id)%cid /= 0) call clover_dag_inv_mult(tmp, action%mtilde(id)%cid, so)
     if (action%mtilde(id)%h /= 0) call h_mult_b(h, tmp, volh)

#ifdef BAGEL
     TIMING_START(timing_bin_bagel_d)
     call bagel_d(out, bagel_u, tmp, so, 1)
     TIMING_STOP(timing_bin_bagel_d)
#else
     call d_dag(se, so, out, tmp, gauge(2)%u(1,1,1,0,1))
#endif

  endif

#ifdef OMTDTD
  call sc_xpby(out, in, b)
#else
  if (action%mtilde(id)%cid /= 0) then
     call clover_dag_mult(tmp, in, action%mtilde(id)%cid, se)
     call sc_xpby(out, tmp, b)
  else
     call sc_xpby(out, in, b)
     if (action%mtilde(id)%h /= 0) call h_mult_a(out, -h, in, volh)
  endif
#endif

  if (chemi /= 0 .or. chemi_i /=0)call emuu4(gauge(2)%u, zone, zone, .false.)

end

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