!----------------------------------------------------------------------------------------------------------------------------------!
SUBROUTINE cbem_lap_lrhs
!----------------------------------------------------------------------------------------------------------------------------------!

!===purpose===! 

!**********************************************************************************************************************************!
!********************************  COLLOCATION BEM for 3D LAPLACE EQUATION: SINGULAR FORMULATION  *********************************!
!***************************************  3-NODED TRIANGULAR ELEMENT: ANALYTIC INTEGRATION  ***************************************!
!***********************************************  PIECEWISE CONSTANT APPROXIMATION  ***********************************************!
!**********************************  Form the left- and right-hand sides of the discretized BIE  **********************************!
!**********************************************************************************************************************************!

!*** Developer: Sylvain Nintcheu Fata, Oak Ridge National Laboratory
!*** Version:   V.1.2
!*** Released:  Apr 14, 2011


USE KINDS
USE SRF_GEOM,  ONLY: nbelt   ! nbelt == Total number of boundary elements
USE SRF_COND,  ONLY: eltbc   ! eltbc(k) = {0,1} ==> {Dirichlet,Neunamm} boundary condition is prescribed on element k
USE DISCR_BIE, ONLY: h,g     ! h == Potential influence matrix, g == Flux influence matrix
USE DISCR_BIE, ONLY: rhs     ! rhs == Right-hand side of the Discretized BIE
USE POTFLUX,   ONLY: phi,flx ! phi == Vector containing boundary potentials
                             ! flx == Vector containing boundary fluxes
USE POTFLUX,   ONLY: pbv     ! pbv == Vector containing Prescribed Boundary Values


IMPLICIT NONE

 integer(kind=k9) :: k,i,alloc_st



!*** Initialization: Prescribed boundary conditions ==> flux and potential vectors
    allocate(phi(nbelt), flx(nbelt),STAT=alloc_st) 
    if ( alloc_st == 0 ) then ! If phi,flx are successfully allocated
          loop0: do k=1,nbelt ! Loop over all boundary elements
             if (eltbc(k) == 0) then ! eltbc(k) = 0 ==> Dirichlet data is prescribed on element k
               phi(k) = pbv(k); flx(k) = 0.0_dp
             else ! eltbc(k) = 1 ==> Neumann data is prescribed on element k
               phi(k) = 0.0_dp; flx(k) = pbv(k)
             endif
          enddo loop0
    else
          write(*,*)'cbem_lap_lrhs: Allocate arrays "phi,flx" failed with Status=',alloc_st
	  stop
    endif

    deallocate(pbv)

    allocate(rhs(nbelt),STAT=alloc_st)
    if ( alloc_st == 0 ) then ! If rhs is successfully allocated
       ! rhs = matmul(h,phi) - matmul(g,flx)
       call dgemv('N',nbelt,nbelt,1.0_dp,g,nbelt,flx,1, 0.0_dp,rhs,1) !rhs = matmul(g,flx)
       call dgemv('N',nbelt,nbelt,1.0_dp,h,nbelt,phi,1,-1.0_dp,rhs,1) !rhs = matmul(h,phi) - rhs
    else
       write(*,*)'cbem_lap_lrhs: Allocate array "rhs" failed with Status=',alloc_st
       stop
    endif

!*** Rearrange columns: Form the Left-hand side coefficient matrix in g
    loop1a: do k=1,nbelt
       if(eltbc(k) == 1) then ! if Neumann data is prescribed on element k
          loop1b: do i=1,nbelt
	     g(i,k) = -h(i,k)
	  enddo loop1b
       endif
    enddo loop1a

    deallocate(h)

!----------------------------------------------------------------------------------------------------------------------------------!
END SUBROUTINE cbem_lap_lrhs
!----------------------------------------------------------------------------------------------------------------------------------!
