!===============================================================================
!
! 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 bicgstab2(x, b, id, iter, res, n, m) 
  use module_function_decl
  use module_check_solution
  use module_action
  use module_cg
  use module_vol
  use module_counter
  implicit none
  integer,     intent(in)    :: id, n, m
  complex(8),  intent(inout) :: x(n)
  complex(8),  intent(in)    :: b(n)
  real(8),     intent(in)    :: res
  integer,     intent(out)   :: iter

  real(8) :: bb, rtr,rtrold
  complex(8) :: r(n), r0(m), p(n), ap(n), ar(n), ak, bk, pap, ss1, ss2, omega
  integer :: i, niter
  real(8),    external :: vecnorm2
  complex(8), external :: veccdotc

  TIMING_START(timing_bin_bicgstab)

  bb = vecnorm2(b, m); 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 sc_copy(x, b)
  call mtil(r, x, id)

  rtrold = 0
  !$omp parallel do reduction(+: rtrold)
  do i = 1, m
!     x(i) = 0
     r(i) = b(i) - r(i)
     r0(i)= r(i)
     p(i) = r(i)
     rtrold = rtrold + real(r(i))*real(r(i)) + aimag(r(i))*aimag(r(i))
  enddo
  call global_sum_vec(1, rtrold)
  if (stop_condition(rtrold, res, bb)) goto 9999
  if(my_pe()==0 )write(0,*)"BiCGstab start rtr kappa:", rtrold, action%mtilde(id)%kappa


  do niter = 1, cg_para%maxiter
     call mtil(ap, p, id)
     pap = veccdotc(ap,r0,m); call global_sum_vec(2, pap)
     ak = rtrold / pap

     rtr=0
     !$omp parallel do reduction(+: rtr)
     do i = 1, m
        x(i) = x(i) + ak *  p(i)
        r(i) = r(i) - ak * ap(i)
        rtr  = rtr + real(r(i))*real(r(i)) + aimag(r(i))*aimag(r(i))
     enddo
     call global_sum_vec(1, rtr)
     if (stop_condition(rtr, res, bb)) goto 9999

     call mtil(ar, r, id)

     ss1=veccdotc( r,ar,m); call global_sum_vec(2, ss1)
     ss2=veccdotc(ar,ar,m); call global_sum_vec(2, ss2)
     omega = ss1/ss2

     rtr=0
     !$omp parallel do reduction(+: rtr)
     do i = 1, m
        x(i) = x(i) + omega *  r(i)
        r(i) = r(i) - omega * ar(i)
        rtr  = rtr + real(r(i))*real(r(i)) + aimag(r(i))*aimag(r(i))
     enddo
     call global_sum_vec(1, rtr)
     if (stop_condition(rtr, res, bb)) goto 9999

     ss2=veccdotc(r,r0,m); call global_sum_vec(2, ss2)

     bk = ss2 / rtrold * ak / omega
     rtrold = ss2
     !$omp parallel do
     do i = 1, m
        p(i) = r(i) + bk * (p(i) - omega * ap(i))
     enddo
  enddo

9999 continue
  iter = niter
  if(my_pe()==0 )write(0,*)"BiCGstab done! rtr iter :",rtr, niter
end

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