!===============================================================================
!
!  shift_cg_mix2.F90 
!
! - for bagel sloppy precision and f90 double precision, 12.02.2009, yn
!-------------------------------------------------------------------------------
!
! 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_mix2(x, b, id, a, iter, res) 
  use module_function_decl
  use module_check_solution
  use module_field
  use module_action
  use module_mre
  use      module_cg
  use      module_counter
  use      module_vol
  implicit none

  type(type_mre), save :: solutions

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

  real(8), dimension(SIZE_COMPLEX*NDIRAC*NCOL*volh_tot) :: r, p, aap   !!,s
  REAL          :: rtr, interval, rtr0, bb
  integer       :: i, niter, iter4, iter4_tot
  character(72) :: msg

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

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

  TIMING_START(timing_bin_cg_mix)
  niter = 0
  iter4 = 0
  iter4_tot = 0

  bb = ZERO
  !$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)*HALF/action%mtilde(id)%kappa)
  DEBUG2R("rsd    =   ",  res**2*bb/(TWO*action%mtilde(id)%kappa)**2)

  call wdagw_r8(r, x, id)
  !$omp parallel do
  do i = 1, size_sc_field
     r(i) = r(i) + a * x(i)
  enddo

  rtr = ZERO
  !$omp parallel do reduction(+: rtr)
  do i = 1, size_sc_field
     r(i) = b(i) - r(i)
     rtr = rtr + r(i)**2
  enddo
  call global_sum_vec(1, rtr)

  rtr0=rtr
  if (criterion == 0) then 
     if ( rtr <= res) goto 9999
     interval = (res/rtr0)**(1.0_8/dble(outer_steps))
  endif
  if (criterion == 1) then
     if (rtr <= res*res*bb ) goto 9999
     interval = (res*res*bb/rtr0)**(1.0_8/dble(outer_steps))
  endif

  do niter = 1, cg_para%maxiter
     call mre_get2(solutions, p, r, id)
     if (criterion == 0) then
        call shift_cg(p, r, id, a, iter4, rtr0*interval**niter)
     else
        call shift_cg(p, r, id, a, iter4, interval)
     endif
     !$omp parallel do
     do i = 1, size_sc_field
        x(i) = x(i) + p(i)
     enddo
     call mre_put(solutions, p, 0)

     call wdagw_r8(r, x, id)
     !$omp parallel do
     do i = 1, size_sc_field
        r(i) = r(i) + a * x(i)
     enddo

     rtr = ZERO
     !$omp parallel do reduction(+: rtr)
     do i = 1, size_sc_field
        r(i) = b(i) - r(i)
        rtr = rtr + r(i)**2
     enddo
     call global_sum_vec(1, rtr)

     iter4_tot = iter4_tot + iter4
     if (stop_condition(rtr, res, bb)) goto 9999
  enddo

  niter = niter - 1

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

9999 continue

  call mre_put(solutions, p, 1)

  call update_cg_stat(cg_stat, niter, 0)

  iter = niter

  TIMING_STOP(timing_bin_cg_mix)

  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", "iter4"
!!        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), &
!!           niter, iter4_tot
!!     endif
  endif

end

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