!===============================================================================
!
! xbound_axion_mpi.F90 - boundary exchange of axion field with MPI 
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2017 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_xbound_axion

  use module_xbound
  implicit none
  integer, save            :: n_bound = 0
  type (type_xbound), save :: b(max_bound)
end

!-------------------------------------------------------------------------------
subroutine init_xbound_axion()

  use module_xbound_axion
  use module_switches
  implicit none

  integer :: x, y, z, t

  if (.not. switches%axion) return

  do t = -1, 1
  do z = -1, 1
  do y = -1, 1
  do x = -1, 1
     call init_xch_bound(n_bound, b, 1, x, y, z, t)
  enddo
  enddo
  enddo
  enddo
end

!-------------------------------------------------------------------------------
subroutine xbound_axion_field(a)

  use module_function_decl
  use module_vol
  use module_switches
  implicit none

  AXION_FIELD, intent(inout) :: a
  integer :: eo

  if (.not. switches%axion) return
  if (num_pes() == 1) return

  TIMING_START(timing_bin_xbound_axion)

  do eo = EVEN, ODD
     call xbound_axion(a, eo)
  enddo

  TIMING_STOP(timing_bin_xbound_axion)

end

!-------------------------------------------------------------------------------
subroutine xbound_axion(a, eo)

  use module_function_decl
  use module_input
  use module_vol
  use module_switches
  implicit none

  AXION_FIELD, intent(inout) :: a
  integer, intent(in) :: eo

  if (.not. switches%axion) return
  if (num_pes() == 1) return

  if (input%tuning_xbound_g_i == 0) then  ! take same mode as in 'xbound_g'
     call xbound_axion_sr(a, eo)
  else
     call xbound_axion_i(a, eo)
  endif

end

!-------------------------------------------------------------------------------
subroutine xbound_axion_sr(a, eo)  ! sendrecv 

  use module_xbound_axion
  use module_vol
  implicit none

  AXION_FIELD, intent(inout) :: a
  integer, intent(in) :: eo
  integer :: i

  do i = 1, n_bound
     call mpi_sendrecv( &
         a(b(i)%i_source, eo), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         a(b(i)%i_target, eo), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, status, ierror)
  enddo

end

!-------------------------------------------------------------------------------
subroutine xbound_axion_i(a, eo)  ! 'i'mmediate MPI calls

  use module_xbound_axion
  use module_vol
  implicit none

  AXION_FIELD, intent(inout) :: a
  integer, intent(in) :: eo
  integer :: i, n_request

  n_request = 0

  do i = 1, n_bound
     n_request = n_request + 1
     call mpi_irecv( &
         a(b(i)%i_target, eo), b(i)%size, BQCD_REAL, b(i)%pe_source, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)

     n_request = n_request + 1
     call mpi_isend( &
         a(b(i)%i_source, eo), 1, b(i)%vector_type,  b(i)%pe_target, 0,&
         MPI_COMM_WORLD, request(n_request), ierror)
  enddo

  call mpi_waitall(n_request, request, status, ierror)
end

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