!----------------------------------------------------------------------------------------------------------------------------------!
SUBROUTINE gbem_lap_solv
!----------------------------------------------------------------------------------------------------------------------------------!

!===purpose===! 

!**********************************************************************************************************************************!
!**********************************  GALERKIN BEM for 3D LAPLACE EQUATION: SINGULAR FORMULATION  **********************************!
!************************************  3-NODED TRIANGULAR ELEMENT: SEMI-ANALYTIC INTEGRATION  *************************************!
!************************************************  PIECEWISE LINEAR APPROXIMATION  ************************************************!
!***************************************************  Solve the Discretized BIE  **************************************************!
!**********************************************************************************************************************************!

!*** Developer: Sylvain Nintcheu Fata
!*** Version:   V.1.1
!*** Released:  Jun 14, 2012


USE KINDS
USE SRF_GEOM, ONLY: nmltplnd   ! nmltplnd == Number of Multiple nodes (or Number of class of Multiple Nodes) on the boundary mesh
USE SRF_GEOM, ONLY: mltplnd    ! mltplnd(k) == Structure containing information about the k-th class of multiple nodes
USE SRF_GEOM, ONLY: nbnd       ! nbnd == Total number of boundary nodes
USE SRF_COND, ONLY: ndbc       ! ndbc(i) = {0,1} ==> {Dirichlet,Neunamm} boundary condition is prescribed at boundary node bnd(:,i)
USE DISCR_BIE,ONLY: g,rhs      ! g == Discretized BIE matrix or Left-hand side of the BEM equation
                               ! rhs == Right-hand side of the Discretized BIE
USE POTFLUX,  ONLY: phi,flx    ! phi == Vector containing boundary potentials, flx == Vector containing boundary fluxes


IMPLICIT NONE

 integer(kind=k9) :: k,j,info,alloc_st
 integer(kind=k9), pointer :: ipiv(:)



    write(*,100)'           >>>  LU Decomposition of the BIE matrix <<<'    

    allocate(ipiv(nbnd),STAT=alloc_st)
    if ( alloc_st == 0 ) then ! If ipiv is successfully allocated
       call dgetrf(nbnd,nbnd,g,nbnd,ipiv,info) ! LU Decomposition
    else
       write(*,*)'gbem_lap_solv: Allocate array "ipiv" failed with Status=',alloc_st
       stop
    endif

    write(*,100)'           >>>  Back-substitution  <<<'          

    call dgetrs('N',nbnd,1,g,nbnd,ipiv,rhs,nbnd,info) ! Back-substitution
    deallocate(ipiv); deallocate(g)

    write(*,*) 'err ==', info

    loop1: do k=1,nbnd
       if (ndbc(k) == 0) then ! ndbc(k) = 0 ==> Dirichlet data is prescribed at the k-th boundary node
          flx(k) = rhs(k)
       else ! ndbc(k) = 1 ==> Neumann data is prescribed at the k-th boundary node
          phi(k) = rhs(k)
       endif
    enddo loop1

    deallocate(rhs,ndbc)

    if (nmltplnd /= 0) then
         loop2a: do k=1,nmltplnd ! Loop over all class of multiple nodes
	    if (mltplnd(k)%pnd /= -1) then ! if there exist in the k-th class a boundary node with Global
	                                   ! index mltplnd(k)%pnd at which the potential is prescribed
	       loop2b: do j=1,mltplnd(k)%nn ! Loop over all multiple nodes in the k-th class
		  phi(mltplnd(k)%id(j)) = phi(mltplnd(k)%pnd) ! Retrieve prescribed potential from the boundary node mltplnd(k)%pnd
	       enddo loop2b
	    else ! there is no potential node in the k-th class of equivalence: ONLY flux nodes in the k-th class
	       loop2c: do j=2,mltplnd(k)%nn ! Loop over the remaining nodes in the k-th class of multiple nodes
		  phi(mltplnd(k)%id(j)) = phi(mltplnd(k)%id(1)) ! Assign potential by continuity
	       enddo loop2c
	    endif
	    deallocate(mltplnd(k)%id)
         enddo loop2a
        deallocate(mltplnd)
    endif

  100 format (1x, a)

!----------------------------------------------------------------------------------------------------------------------------------!
END SUBROUTINE gbem_lap_solv
!----------------------------------------------------------------------------------------------------------------------------------!
