!===============================================================================
!
! chemicalp.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"

!-------------------------------------------------------------------------------
#ifndef PRECISION_R4
subroutine apply_mp_chemu(oe, oo, ie, io, mid, chemu, i)! d^i M / (d mu)^i
  use module_field
  use module_vol
  use module_action
  implicit none
  COMPLEX, intent(in) :: chemu
  integer, intent(in) :: mid, i
  SPINCOL_FIELD, intent(out) :: oe, oo
  SPINCOL_FIELD, intent(in)  :: ie, io
  COMPLEX :: fac1, fac2

  if (i <= 0) call die("apply_mp_chemu: i<=0")

  fac1 = - action%mtilde(mid)%kappa * exp( chemu)
  if (mod(i,2)==0) then
     fac2 = - action%mtilde(mid)%kappa * exp(-chemu)
  else
     fac2 =   action%mtilde(mid)%kappa * exp(-chemu)
  endif

  call d_tfac(ODD, EVEN, oo, ie, gauge(2)%u, fac1, fac2)
  call d_tfac(EVEN, ODD, oe, io, gauge(2)%u, fac1, fac2)

!  call emuu4(gauge(2)%u, fac1, fac2, .true.)
!  call d(ODD, EVEN, oo, ie, gauge(2)%u)
!  call d(EVEN, ODD, oe, io, gauge(2)%u)
!  oo = - action%mtilde(mid)%kappa * oo
!  oe = - action%mtilde(mid)%kappa * oe
!  call d_g_init(gauge(2)%u)

end
#endif
!-------------------------------------------------------------------------------
#ifdef LIBDI
subroutine emuu4(u, fac1, fac2, uzero)
  use module_nn
  use module_vol
  use module_lattice
  use module_d_g_reorder
  implicit none
  GAUGE_FIELD, intent(in) :: u
  COMPLEX, intent(in) :: fac1, fac2
  logical :: uzero
  integer :: eo, oe, i, j, mu, col, c1, c2

  if (uzero)u_reorder=0
  mu = gamma_index(4)

  do eo = EVEN, ODD
     oe = 1 - eo
     !$omp parallel do private(j, col)
     do i = 1, volh
        j=nn(i,eo,mu,BWD)
        do col = 1, NCOL
           u_reorder(1, col, i, mu, eo) =       u(1, col, i, eo, mu)  *fac1
           u_reorder(2, col, i, mu, eo) =       u(2, col, i, eo, mu)  *fac1
           u_reorder(3, col, i, mu, eo) =       u(3, col, i, eo, mu)  *fac1
           u_reorder(4, col, i, mu, eo) = conjg(u(col, 1, j, oe, mu)) *fac2
           u_reorder(5, col, i, mu, eo) = conjg(u(col, 2, j, oe, mu)) *fac2
           u_reorder(6, col, i, mu, eo) = conjg(u(col, 3, j, oe, mu)) *fac2
        enddo
     enddo
  enddo

end
#else
subroutine emuu4(u, fac1, fac2, uzero)
  use module_nn
  use module_vol
  use module_lattice
  implicit none
  GAUGE_FIELD, intent(in) :: u
  COMPLEX, intent(in) :: fac1, fac2
  logical :: uzero
call die("emuu4: works only when LIBDI")
end
#endif
!===============================================================================
