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

module module_d_g_reorder
  complex(8), dimension(:,:, :, :, :),pointer,save :: u_reorder
  complex(4), dimension(:,:, :, :, :),pointer,save :: u_reorder_r4
  integer :: mud(4), save
end

!-------------------------------------------------------------------------------
#ifdef D520
subroutine d_g_init100(u)
#else
subroutine d_g_init(u)
#endif
  use module_d_g_reorder
  use module_nn
  use module_vol
  implicit none
  GAUGE_FIELD :: u
  integer :: eo, oe, mu, i, j, col, c1, c2

  do eo = EVEN, ODD
     oe = 1 - eo
     do mu = 1, DIM
        !$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)
              u_reorder(2, col, i, mu, eo) =       u(2, col, i, eo, mu)
              u_reorder(3, col, i, mu, eo) =       u(3, col, i, eo, mu)
              u_reorder(4, col, i, mu, eo) = conjg(u(col, 1, j, oe, mu))
              u_reorder(5, col, i, mu, eo) = conjg(u(col, 2, j, oe, mu))
              u_reorder(6, col, i, mu, eo) = conjg(u(col, 3, j, oe, mu))
           enddo
        enddo
     enddo
  enddo

  do eo = EVEN, ODD
     do mu = 1, DIM
        !$omp parallel do private(c1, c2)
        do i = 1, volh
           do c1 = 1, 3
           do c2 = 1, 6
              u_reorder_r4(c2, c1, i, mu, eo) = u_reorder(c2, c1, i, mu, eo)
           enddo
           enddo
        enddo
     enddo
  enddo

end

!-------------------------------------------------------------------------------
#ifdef D520
subroutine init_d100()
#else
subroutine init_d()
#endif
  use module_lattice
  use module_d_g_reorder
  use module_vol
  implicit none

  allocate(u_reorder(6,NCOL, volh, DIM, EVEN:ODD))
  allocate(u_reorder_r4(6,NCOL, volh, DIM, EVEN:ODD))
  mud(1) = decomp_direction(1)
  mud(2) = decomp_direction(2)
  mud(3) = decomp_direction(3)
  mud(4) = decomp_direction(4)
end

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