!===============================================================================
!
! vec_mult.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2014 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_vecd_field
  real(8), dimension(:, :, :, :, :, :), pointer, save :: ru
end

!-------------------------------------------------------------------------------
subroutine u2ru(u)
  use module_vecd_field
  use module_vol
  implicit none
  complex(8), dimension (3, 3, volh_tot, 0:1, 4) :: u
  integer :: eo, oe, mu, i, c1, c2

  if (.not. associated(ru)) allocate(ru(volh,2,3,3,4,0:1))

  do eo = 0, 1
     oe = 1 - eo
     do mu = 1, 4
        !$omp parallel do private(c1, c2)
        do i = 1, volh
           do c1 = 1, 3
           do c2 = 1, 3
              ru(i, 1, c1, c2, mu, eo) = real(u(c1, c2, i, eo, mu))
              ru(i, 2, c1, c2, mu, eo) =aimag(u(c1, c2, i, eo, mu))
           enddo
           enddo
        enddo
     enddo
  enddo

end

!-------------------------------------------------------------------------------
subroutine vec_mult_test()
  use module_vol
  use module_action
  use module_field
  implicit none
  complex(8), dimension (4, 3, volh_tot) :: in, out, tmp
  real(8), dimension (volh_tot, 2, 4, 3) :: rin, rout

  call ran_gauss_volh(NDIRAC * NCOL, in, HALF, EVEN)
  call d(so, se, out, in, gauge(2)%u(1,1,1,0,1))

  call sc2rsc(in, rin) 
  call u2ru(gauge(2)%u(1,1,1,0,1)) 
  call vecd(so, se, rout, rin)
  call rsc2sc(rout, tmp) 
  call compare_sc(out, tmp, 24*volh)

stop

end

!-------------------------------------------------------------------------------
subroutine sc2rsc(sc, rsc)
  use module_vol
  implicit none
  integer :: i, s, c
  complex(8), dimension (4, 3, volh_tot) :: sc
  real(8), dimension (volh_tot, 2, 4, 3) :: rsc

  do i = 1, volh
  do c = 1, 3
  do s = 1, 4
     rsc(i, 1, s, c)= real(sc(s, c, i))
     rsc(i, 2, s, c)=aimag(sc(s, c, i))
  enddo
  enddo
  enddo

end

!-------------------------------------------------------------------------------
subroutine rsc2sc(rsc, sc)
  use module_vol
  implicit none
  integer :: i, s, c
  complex(8), dimension (4, 3, volh_tot) :: sc
  real(8), dimension (volh_tot, 2, 4, 3) :: rsc

  do i = 1, volh
  do c = 1, 3
  do s = 1, 4
     sc(s, c, i) = cmplx(rsc(i, 1, s, c), rsc(i, 2, s, c))
  enddo
  enddo
  enddo

end

