!===============================================================================
!
!  shift_cg.F90 
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2009 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"

!-------------------------------------------------------------------------------
subroutine shift_cg(x, b, id, a, iter, res) 
  use module_function_decl
  use module_check_solution
  use module_action
  use module_cg
  use module_vol
  use module_counter
  implicit none

  SPINCOL_OVERINDEXED,  intent(inout) :: x
  SPINCOL_OVERINDEXED,  intent(in)    :: b
  integer,              intent(out)   :: iter
  integer,              intent(in)    :: id
  DOUBLE,               intent(in)    :: res
  REAL,                 intent(in)    :: a

  SPINCOL_OVERINDEXED :: r, p, aap
  REAL          :: ak, bk
  DOUBLE        :: rtr, rtrold, paap, bb
  integer       :: i, niter
  character(72) :: msg

!
! needed for checking solution
!
  DOUBLE        :: norm(3)
  integer, save :: headline_check_written = 0
  character(*), parameter :: key_chk = "%cgChk"
  character(*), parameter :: fmt_chk_h = "(1x, 2a, a6, 4a20)"
  character(*), parameter :: fmt_chk_b = "(1x, a7, i6, 3e20.10, i20)"

!-------------------------------------------------------------------------------

  TIMING_START(timing_bin_cg)
  iter=0
  niter=0

  bb = 0.0_8
  !$omp parallel do reduction(+: bb)
  do i = 1, size_sc_field
     bb = bb + b(i)**2
  enddo
  call global_sum_vec(1, bb)

  DEBUG2R("norm(b)=   ",  sqrt(bb)/2/action%mtilde(id)%kappa)
  DEBUG2R("rsd    =   ",  dble(res)**2*bb/(2*action%mtilde(id)%kappa)**2)

  call wdagw(r, x, id)
  do i = 1, size_sc_field
     r(i) = r(i) + a * x(i)
  enddo


  rtrold = ZERO
  !$omp parallel do reduction(+: rtrold)
  do i = 1, size_sc_field
     r(i) = b(i) - r(i)
     p(i) = r(i)
     rtrold = rtrold + r(i)**2
  enddo
  call global_sum_vec(1, rtrold)
  if (stop_condition(rtrold, res, bb)) goto 9999

  do niter = 1, cg_para%maxiter
     call wdagw(aap, p, id)
     do i = 1, size_sc_field
        aap(i) = aap(i) + a * p(i)
     enddo

     paap = 0.0_8
     !$omp parallel do reduction(+: paap)
     do i = 1, size_sc_field
        paap = paap + p(i) * aap(i)
     enddo
     call global_sum_vec(1, paap)
     ak = rtrold / paap 

     rtr = 0.0_8
     !$omp parallel do reduction(+: rtr)
     do i = 1, size_sc_field
        x(i) = x(i) + ak * p(i)
        r(i) = r(i) - ak * aap(i)
        rtr = rtr + r(i)**2
     enddo 
     call global_sum_vec(1, rtr)

     if (stop_condition(rtr, res, bb)) goto 9999
     bk = rtr / rtrold
     rtrold = rtr

     call sc_xpby(p, r, bk)  ! p = r + bk * p

  enddo

  niter = niter - 1

  if (cg_para%log /= 2) then
     write(msg, *) "cg(): no convergence; rtr = ", rtr 
     call die(msg)
  endif

9999 continue

  call update_cg_stat(cg_stat, niter, 0)
cg_iterations_total = cg_stat%niter_tot_run

  iter = niter

  TIMING_STOP(timing_bin_cg)

  if (cg_para%check /= 0) then  ! check solution

!!     call check_solution(id, x, b, norm)
!!
!!     if (headline_check_written == 0) then
!!        if (my_pe() == 0) then
!!           write(UREC, fmt_chk_h) &
!!               "T", key_chk, "traj", "rtr", "Rb", "Rx", "iterations"
!!        endif
!!        headline_check_written = 1
!!     endif
!!     
!!     if (my_pe() == 0) then
!!        write(UREC, fmt_chk_b) &
!!           key_chk, counter%traj, rtr, &
!!           norm(1)/norm(2), norm(1)/norm(3), iter
!!     endif

  endif

end

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