!===============================================================================
!
! xyzt2i.F90 - maps local coordinates (x,y,z,t) to even/odd index
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 1998-2007 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/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
integer function xyzt2i_dd(x_in , me, ddtype)
  
  !   x_in := (x,y,z,t) 
  !  -1 <= x(mu) <= N(mu) ; mu = 1,2,3,4
  !  xyzt2i >= 1

  use module_function_decl
  use module_dd
  implicit none
  type(ddlatvol) :: ddtype

  integer, dimension (DIM), intent(in) :: x_in
  integer, dimension (DIM)             :: dir, i, m, x
  integer                              :: count, mu, me
  integer, external                    :: ieo, n_sites_dd, i_periodic, ilex


  count = 0
  do mu = 1, DIM

     x(mu) = x_in(mu)

     ASSERT(x(mu) >=-1 .and. x(mu) <= ddtype%n(mu))

     if (ddtype%single(mu)) x(mu) = i_periodic(x(mu), ddtype%n(mu))

     if (x(mu) == -1) then
        dir(mu) = -1
     elseif (x(mu) == ddtype%n(mu)) then
        dir(mu) = 1
     else
        dir(mu) = 0
        count = count + 1
     endif

     if (dir(mu) /= 0) then
        i(mu) = 0
        m(mu) = 1
     else
        i(mu) = x(mu)
        m(mu) = ddtype%n(mu)
     endif
  enddo
  
  if (count == DIM) then
     xyzt2i_dd = ddtype%offset(0,0,0,0) + ieo(DIM, x, ddtype%n) + 1
  else
     ASSERT(ddtype%num_ip /= 1)

     if (dir(1) /= 0) then
         ASSERT(n_sites_dd(DIM, dir, ddtype%n, ddtype%single) == n_sites_dd(DIM, dir, ddtype%nh, ddtype%single))
         ASSERT(ilex(DIM, i, m) <= n_sites_dd(DIM, dir, ddtype%nh, ddtype%single))
         xyzt2i_dd = ddtype%offset(dir(1),dir(2),dir(3),dir(4)) + ilex(DIM, i, m) + 1
     else
         xyzt2i_dd = ddtype%offset(dir(1),dir(2),dir(3),dir(4)) + ieo(DIM, i, m) + 1
     endif
  endif

end

!-------------------------------------------------------------------------------
integer function std_xyzt2i_dd(x,me,ddtype)
  use module_dd
  use module_lattice
  implicit none
  type(ddlatvol) :: ddtype
  integer, dimension (DIM), intent(in) :: x
  integer, dimension (DIM)             :: x_act
  integer, external                    :: xyzt2i_dd
  integer :: me
  x_act(1) = x(gamma_index(1))
  x_act(2) = x(gamma_index(2))
  x_act(3) = x(gamma_index(3))
  x_act(4) = x(gamma_index(4))

  std_xyzt2i_dd = xyzt2i_dd(x_act,me,ddtype)
end

!-------------------------------------------------------------------------------
!!!subroutine i2xyzt_dd(ieo, eo, xyzt, me, ddtype)
!!!
!!!  use module_dd
!!!  use module_vol
!!!  implicit none
!!!  type(ddlatvol) :: ddtype
!!!  integer, intent(in)  :: ieo, eo, me
!!!  integer, intent(out) :: xyzt(DIM)
!!!  integer              :: x, y, z, t, i, e, mu, start(DIM), end(DIM)
!!!  integer, parameter   :: out_of_range = 2000000000
!!!  integer, external    :: xyzt2i_dd, e_o_dd
!!!
!!!
!!!
!!!  if (.not. associated(ddtype%list)) then
!!!     allocate(ddtype%list(DIM, ddtype%volh_tot, EVEN:ODD))
!!!
!!!     ddtype%list = out_of_range
!!!
!!!     do mu = 1, DIM
!!!        if (ddtype%single(mu)) then
!!!           start(mu) = 0
!!!           end(mu) = ddtype%n(mu) - 1
!!!        else
!!!           start(mu) = -1
!!!           end(mu) = ddtype%n(mu)
!!!        endif
!!!     enddo
!!!
!!!     do t = start(4), end(4)
!!!     do z = start(3), end(3)
!!!     do y = start(2), end(2)
!!!     do x = start(1), end(1)
!!!        i = xyzt2i_dd((/x,y,z,t/),me,ddtype)
!!!        e = e_o_dd((/x,y,z,t/),me,ddtype)
!!!        ddtype%list(1, i, e) = x
!!!        ddtype%list(2, i, e) = y
!!!        ddtype%list(3, i, e) = z
!!!        ddtype%list(4, i, e) = t
!!!     enddo
!!!     enddo
!!!     enddo
!!!     enddo
!!!  endif
!!!  
!!!  xyzt(1) = ddtype%list(1, ieo, eo)
!!!  xyzt(2) = ddtype%list(2, ieo, eo)
!!!  xyzt(3) = ddtype%list(3, ieo, eo)
!!!  xyzt(4) = ddtype%list(4, ieo, eo)
!!!
!!!end
				       
!===============================================================================
