!===============================================================================
!
! cg_mm.F90 - matrix multiplication
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2012-2016 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/>.
!
!-------------------------------------------------------------------------------
# define LANGUAGE_FORTRAN
# include "defs.h"
# include "cg_defs.h"

# undef d
# undef d_dag

!-------------------------------------------------------------------------------
# define TRACE_NAME T(cg_mm)
!-------------------------------------------------------------------------------
subroutine T(cg_mm)(out, in, which_matrix)

  use module_cg_arrays
  use module_cg_conf
  use module_cg_para
  use module_cg_procs, exclude => T(cg_mm)  ! avoid name clash
  use typedef_simd
  use T(module_vol)
  use module_switches
  implicit none
  spincol_FIELD, intent(out) :: out
  spincol_FIELD, intent(in)  :: in
  integer, intent(in)        :: which_matrix
 
  REAL :: b, rho, csw_kappa

  TRACE_ENTRY

  b = -cg_para%kappa**2 / (ONE + cg_para%h**2)
  rho = cg_para%nf2_rho(1,1)
  csw_kappa = cg_para%csw * cg_para%kappa

                         call cg_procs%d(ODD, EVEN, cg_arrays%tmp1, in, cg_conf%u, cg_arrays%a)
  if (csw_kappa /= ZERO) call cg_procs%co(cg_conf%c_odd, cg_arrays%tmp1, volh_tot, volh)
                         call cg_procs%d(EVEN, ODD, cg_arrays%tmp2, cg_arrays%tmp1, cg_conf%u, cg_arrays%a)
  if (csw_kappa /= ZERO) then
                         call cg_procs%c2(cg_arrays%tmp2, b, cg_conf%c_even, in, volh_tot, volh)
                         else
                         call cg_procs%xpby(cg_arrays%tmp2, in, b, volh_tot, volh)
                         endif

  if (which_matrix == CG_MATRIX_W) call cg_procs%axpy(cg_arrays%tmp2, in, rho, volh_tot, volh)

                         call cg_procs%d_dag(ODD, EVEN, cg_arrays%tmp1, cg_arrays%tmp2, cg_conf%u, cg_arrays%a)
  if (csw_kappa /= ZERO) call cg_procs%co(cg_conf%c_odd, cg_arrays%tmp1, volh_tot, volh)
                         call cg_procs%d_dag(EVEN, ODD, out, cg_arrays%tmp1, cg_conf%u, cg_arrays%a)
  if (csw_kappa /= ZERO) then
                         call cg_procs%c2(out, b, cg_conf%c_even, cg_arrays%tmp2, volh_tot, volh)
                         else
                         call cg_procs%xpby(out, cg_arrays%tmp2, b, volh_tot, volh)
                         endif

  if (which_matrix == CG_MATRIX_W) call cg_procs%axpy(out, cg_arrays%tmp2, rho, volh_tot, volh)

  TRACE_RETURN
end
 
!===============================================================================
