!===============================================================================
!
! bicgstab.F90 - solves "matrix_mult * x = b" and returns number of iterations
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2008 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 bicgstab(x, b, id, 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

  SPINCOL_OVERINDEXED :: r, r0, p, aap, s, aas
  REAL          :: ak, bk, omega
  DOUBLE        :: rtr, rtrold, paap, bb, dtmp, dtmp1, dtmp2, ss(2)
  integer       :: i, niter
  character(72) :: msg

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

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

  TIMING_START(timing_bin_bicgstab)

  bb = 0.0_8
  !$omp parallel do reduction(+: bb) private(dtmp)
  do i = 1, size_sc_field
     dtmp = b(i)
     bb = bb + dtmp**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)
  niter = 1
  rtrold = ZERO
  !$omp parallel do reduction(+: rtrold) private(dtmp)
  do i = 1, size_sc_field
     r(i) = b(i) - r(i)
     r0(i)= r(i)
     p(i) = r(i)
     dtmp = r(i)
     rtrold = rtrold + dtmp**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)
     paap = 0.0_8
     !$omp parallel do reduction(+: paap) private(dtmp1, dtmp2)
     do i = 1, size_sc_field
        dtmp1 = r0(i)
        dtmp2 = aap(i)
        paap = paap + dtmp1 * dtmp2
     enddo
     call global_sum_vec(1, paap)
     ak = rtrold / paap 

     !$omp parallel do
     do i = 1, size_sc_field
        s(i) = r(i) - ak * aap(i)
     enddo

     call wdagw(aas, s, id)

     ss = 0
     !$omp parallel do reduction(+: ss)  private(dtmp1, dtmp2)
     do i = 1, size_sc_field
        dtmp1 = s(i)
        dtmp2 = aas(i)
        ss(1) = ss(1) + dtmp1 * dtmp2
        ss(2) = ss(2) + dtmp2**2
     enddo
     call global_sum_vec(2, ss)
     omega = ss(1)/ss(2)

     ss = 0
     !$omp parallel do reduction(+: ss) private(dtmp1, dtmp2)
     do i = 1, size_sc_field
        x(i) = x(i) + ak * p(i) + omega * s(i)
        r(i) = s(i)             - omega * aas(i)
        dtmp1 = r(i)
        dtmp2 = r0(i)
        ss(1) = ss(1) + dtmp1**2
        ss(2) = ss(2) + dtmp1 * dtmp2
     enddo 
     call global_sum_vec(2, ss)
     rtr =  ss(1)
     if (stop_condition(rtr, res, bb)) goto 9999

     bk = ss(2) / rtrold * ak / omega
     rtrold = ss(2)

     !$omp parallel do
     do i = 1, size_sc_field
        p(i) = r(i) + bk * (p(i) - omega * aap(i))
     enddo 
  enddo

  niter = niter - 1

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

9999 continue

  call update_cg_stat(bicgstab_stat, niter, 0)

  iter = niter

  TIMING_STOP(timing_bin_bicgstab)

  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

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