!----------------------------------------------------------------------------------------------------------------------------------!
SUBROUTINE gbem_lap_fld_eval(nip,ipts,fldvl)
!----------------------------------------------------------------------------------------------------------------------------------!

!===purpose===! 

!**********************************************************************************************************************************!
!*********************************************  GALERKIN BEM for 3D LAPLACE EQUATION  *********************************************!
!************************************************  PIECEWISE LINEAR APPROXIMATION  ************************************************!
!***************************************  3-NODED TRIANGULAR ELEMENT: ANALYTIC INTEGRATION  ***************************************!
!********************************  Exact integration with Linear distribution on a flat triangle  *********************************!
!***********************************  Given an array of field points ipts(:,i), i=1,2,...,nip  ************************************!
!***********  Determine the potential at all field points ipts(:,i) due to boundary distribution of potential and flux  ***********!
!**********************************************************************************************************************************!

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


!*** Typical triangular element with vertices {y1,y2,y3}

!           y3
!	    *
!	   / \
!	  /   \
!	 /     \
!       /       \
!      /	 \
!     /		  \
!    *-------------*
!  y1               y2

! Oriented edge L1 = [y1,y2] <== Edge1
! Oriented edge L2 = [y2,y3] <== Edge2
! Oriented edge L3 = [y3,y1] <== Edge3

 
USE KINDS
USE PARAMS,   ONLY: pi,fac       ! pi = 3.1415926535897932, fac == Factor of the Green's function: fac = 1/(4*pi)
USE PARAMS,   ONLY: gm_eps,f_eps ! gm_eps == Tolerance on the geometry, f_eps == Tolerance on the argument of a function
USE SRF_GEOM, ONLY: mx_nd_elt    ! mx_nd_elt == Maximum number of nodes per element
USE SRF_GEOM, ONLY: nbnd,nbelt   ! nbnd == Total number of boundary nodes, nbelt == Total number of boundary elements
USE SRF_GEOM, ONLY: belt         ! belt(:,i) == Global indices of boundary nodes on the i-th element
USE SRF_GEOM, ONLY: bnd          ! bnd(:,i) == Cartesian coordinates of the i-th boundary node
USE SRF_GEOM, ONLY: LocCBasisElt ! LocCBasisElt(1:9,k) == Local Companion Basis associated with the k-th boundary element
USE SRF_GEOM, ONLY: xvElt,zvElt  ! xvElt(1,k) == Base of the k-th boundary element
				 ! xvElt(2,k) == Geometric parameter associated with the k-th boundary element
				 ! zvElt(k) == Height of the k-th boundary element
USE SRF_GEOM, ONLY: thetaElt     ! thetaElt(i,k) == Angle at the i-th vertex of the k-th boundary element
USE POTFLUX,  ONLY: phi,flx      ! phi == Vector containing boundary potentials, flx == Vector containing boundary fluxes


IMPLICIT NONE


 integer(kind=k9), intent(in ) :: nip ! nip == Number of interior points
 real   (kind=dp), intent(in ) :: ipts(3,nip) ! ipts(:,i) == Cartesian coordinates of the i-th interior (or field) point
 real   (kind=dp), intent(out) :: fldvl(nip)  ! fldvl(i) == Potential at the i-th interior point

 integer(kind=k9) :: i,k,j,th_id,alloc_st,elQ ! elQ == Global index of the boundary element Q
 integer(kind=k9) :: qq(mx_nd_elt)
 real   (kind=dp) :: e1(3),e2(3),e3(3) ! Orthonormal local basis associated with element Q
 real   (kind=dp) :: aQ,bQ,cQ ! Geometric parameters of element Q: bQ = base, aQ = height,
 			      ! cQ = relative position of the 3rd node of element Q in the local basis {e1,e2,e3}
 real   (kind=dp) :: r1(3),xi,zt,eth,et,ets,q(3),qs(3),d(3),rho(3),rhowv(3),chi(3),dchi(3),gamma(3),gam
 real   (kind=dp) :: theta(3),alpha2,alpha3,Rh(3),Rhs(3),ch11,ch12,ch22,ch23,ch31,ch33
 real   (kind=dp) :: kQ2,kQ3 ! kQ2 == Slope of Edge2 of element Q, kQ3 == Slope of Edge3 of element Q
 real   (kind=dp) :: vecA(3),vecB(3),sncf(2:3),cscf(2:3),ah(3,3),bh(3,3),qetx2(3),etchi(3),qrhowv(3)
 real   (kind=dp) :: p11,p12,p22,p23,p33,p31,x3,z3,rh1s,rh2s,rh3s,p11s,p12s,p33s,p11rh1,p31rh1,omega
 real   (kind=dp) :: cr11,cr12,zn1,zd1,cr22,cr23,zn2,zd2,cr31,cr33,zn3,zd3,p12rh2,p22rh2,p23rh3,p33rh3,theta0,ThetGam
 real   (kind=dp) :: shc(mx_nd_elt),int_p(mx_nd_elt),int_f(mx_nd_elt)
 real(kind=dp), pointer :: int_G(:) ! int_G == Flux influence vector necessary to evaluate the potential at field points
 real(kind=dp), pointer :: int_H(:) ! int_H == Potential influence vector necessary to evaluate the potential at field points
!*** Note: Below, the Projection of the collocation point ipts(:,i) means the Projection of the collocation point ipts(:,i) on
!*** Note: the plane of element Q in the direction e3. e3 is the unit normal to element Q

 real(kind=dp), external :: pt_tria_tst,gammai,asym_chi ! External functions (see gbem_util.f90)



    loop0: do k=1,nip    ! Loop over all interior points
       fldvl(k) = 0.0_dp ! Initialize the potential fldvl(k) at all interior points
    enddo loop0

    allocate(int_G(nbnd),int_H(nbnd),STAT=alloc_st)
    if ( alloc_st /= 0 ) then  ! If int_G, int_H are not successfully allocated 
       write(*,*)'gbem_lap_fld_eval: Allocate arrays "int_G, int_H" failed with Status=',alloc_st
       stop
    endif
    
    loop1: do j=1,nip ! Loop over all interior points

       loop2: do k=1,nbnd ! Loop over all boundary nodes
          int_G(k) = 0.0_dp; int_H(k) = 0.0_dp
       enddo loop2

       loop3: do elQ=1,nbelt ! Loop over all boundary elements

          loop4: do k=1,mx_nd_elt ! Loop over the nodes of element Q
             qq(k) = belt(k,elQ) ! qq(k) == Global index of the k-th node of the boundary element Q
          enddo loop4

          e1(1) = LocCBasisElt(1,elQ); e1(2) = LocCBasisElt(2,elQ); e1(3) = LocCBasisElt(3,elQ)
          e2(1) = LocCBasisElt(4,elQ); e2(2) = LocCBasisElt(5,elQ); e2(3) = LocCBasisElt(6,elQ)
          e3(1) = LocCBasisElt(7,elQ); e3(2) = LocCBasisElt(8,elQ); e3(3) = LocCBasisElt(9,elQ)

          theta(1) = thetaElt(1,elQ); theta(2) = thetaElt(2,elQ); theta(3) = thetaElt(3,elQ)

           alpha2 = pi-theta(2); alpha3 = pi+theta(1) ! alpha1 = 0.0_dp

           aQ = zvElt(elQ)   ! aQ>0.0_dp always
           bQ = xvElt(1,elQ) ! bQ >0.0_dp always
           cQ = xvElt(2,elQ)

           kQ2 = (cQ-bQ)/aQ; kQ3 = cQ/aQ ! kQ2 == Slope of Side 2 of element Q, kQ3 == Slope of Side 3 of element Q

           cscf(2) = dcos(alpha2); sncf(2) = dsin(alpha2); cscf(3) = dcos(alpha3); sncf(3) = dsin(alpha3)
 
           vecA(1) = -1.0_dp/bQ; vecA(2) =  1.0_dp/bQ; vecA(3) = 0.0_dp
           vecB(1) =  kQ2/bQ   ; vecB(2) = -kQ3/bQ   ; vecB(3) = 1.0_dp/aQ
 
           ah(1,1) = vecA(1); ah(2,1) = (kQ2*sncf(2) - cscf(2))/bQ; ah(3,1) = (kQ2*sncf(3) - cscf(3))/bQ
           ah(1,2) = vecA(2); ah(2,2) = (cscf(2) - kQ3*sncf(2))/bQ; ah(3,2) = (cscf(3) - kQ3*sncf(3))/bQ
           ah(1,3) =  0.0_dp; ah(2,3) = sncf(2)/aQ		  ; ah(3,3) = sncf(3)/aQ
 
           bh(1,1) = -vecB(1); bh(2,1) = -(sncf(2) + kQ2*cscf(2))/bQ; bh(3,1) = -(sncf(3) + kQ2*cscf(3))/bQ
           bh(1,2) = -vecB(2); bh(2,2) =  (sncf(2) + kQ3*cscf(2))/bQ; bh(3,2) =  (sncf(3) + kQ3*cscf(3))/bQ
           bh(1,3) = -vecB(3); bh(2,3) = -cscf(2)/aQ		    ; bh(3,3) = -cscf(3)/aQ
 
            loop6: do k=1,3 ! Loop over all components
               r1(k) = ipts(k,j) - bnd(k,qq(1)) ! r1 = ipts(:,i)-y1
            enddo loop6

              xi = r1(1)*e1(1) + r1(2)*e1(2) + r1(3)*e1(3)
              zt = r1(1)*e2(1) + r1(2)*e2(2) + r1(3)*e2(3)
             eth = r1(1)*e3(1) + r1(2)*e3(2) + r1(3)*e3(3)

              p11 = -xi; p12 = bQ-xi; q(1) = -zt; et = -eth

              if (dabs(et) < gm_eps) et = 0.0_dp

              x3 = cQ+p11; z3 = aQ+q(1)
              p22 = p12*cscf(2) + q(1)*sncf(2); p23 = x3*cscf(2) + z3*sncf(2); q(2) = q(1)*cscf(2) - p12*sncf(2)
              p31 = p11*cscf(3) + q(1)*sncf(3); p33 = x3*cscf(3) + z3*sncf(3); q(3) = q(1)*cscf(3) - p11*sncf(3)

              ets = et*et; p11s = p11*p11; p12s = p12*p12; p33s = p33*p33; qs(1) = q(1)*q(1); qs(2) = q(2)*q(2); qs(3) = q(3)*q(3)

              Rhs(1) = p11s + qs(1); Rhs(2) = p12s + qs(1); Rhs(3) = p33s + qs(3) ! Rhs(3) = x3*x3 + z3*z3
              Rh(1) = dsqrt(Rhs(1)); Rh(2) = dsqrt(Rhs(2)); Rh(3) = dsqrt(Rhs(3))
              rh1s = Rhs(1) + ets; rh2s = Rhs(2) + ets; rh3s = Rhs(3) + ets
              rho(1) = dsqrt(rh1s); rho(2) = dsqrt(rh2s); rho(3) = dsqrt(rh3s)

              shc(3) = -q(1)/aQ; shc(2) = (kQ3*q(1) - p11)/bQ
              shc(1) = 1.0_dp - (shc(2)+shc(3)) ! shc(1) = 1.0_dp + (p11 - kQ2*q(1))/bQ

              if ( dabs(shc(1)) < gm_eps ) then
        	 shc(1) = 0.0_dp; q(2) = 0.0_dp
              endif
 
              if ( dabs(shc(2)) < gm_eps ) then
        	 shc(2) = 0.0_dp; q(3) = 0.0_dp
              elseif ( dabs(shc(2)-1.0_dp) < gm_eps ) then
        	 shc(2) = 1.0_dp
              endif
 
              if ( dabs(shc(3)) < gm_eps ) then
        	 shc(3) = 0.0_dp; q(1) = 0.0_dp
              elseif ( dabs(shc(3)-1.0_dp) < gm_eps ) then
        	 shc(3) = 1.0_dp
              endif


              theta0 = pt_tria_tst(shc(1),shc(2),shc(3),theta,th_id) ! Point in triangle test

              if ( th_id == 4 ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y1
        	 Rh(1) = 0.0_dp
        	 if (et == 0.0_dp) rho(1) = 0.0_dp ! ipts(:,i) = y1
              elseif ( th_id == 5 ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y2
        	 Rh(2) = 0.0_dp
        	 if (et == 0.0_dp) rho(2) = 0.0_dp ! ipts(:,i) = y2
              elseif ( th_id == 6 ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y3
        	 Rh(3) = 0.0_dp
        	 if (et == 0.0_dp) rho(3) = 0.0_dp ! ipts(:,i) = y3
              endif

              qetx2(1) = 2.0_dp*q(1)*et; qetx2(2) = 2.0_dp*q(2)*et; qetx2(3) = 2.0_dp*q(3)*et

              p11rh1 = p11*rho(1); p12rh2 = p12*rho(2)
              p22rh2 = p22*rho(2); p23rh3 = p23*rho(3)
              p33rh3 = p33*rho(3); p31rh1 = p31*rho(1)

             if ( rho(1) == 0.0_dp ) then ! if the collocation point ipts(:,i) is at vertex y1, i.e., ipts(:,i) = y1
               int_p(1) = theta0; int_p(2) = 0.0_dp; int_p(3) = 0.0_dp ! gam = 0.0_dp
               rhowv(2) = rho(2)-rho(3); chi(2) = dlog( (p22+rho(2))/(p23+rho(3)) )
               qrhowv(2) = q(2)*rhowv(2); dchi(2) = qs(2)*chi(2)
               int_f(1) = 0.5_dp*(qrhowv(2)*ah(2,1) - dchi(2)*bh(2,1)) + q(2)*chi(2)
               int_f(2) = 0.5_dp*(qrhowv(2)*ah(2,2) - dchi(2)*bh(2,2))
               int_f(3) = 0.5_dp*(qrhowv(2)*ah(2,3) - dchi(2)*bh(2,3))

             elseif ( rho(2) == 0.0_dp ) then ! if the collocation point ipts(:,i) is at vertex y2, i.e., ipts(:,i) = y2
               int_p(1) = 0.0_dp; int_p(2) = theta0; int_p(3) = 0.0_dp ! gam = 0.0_dp
               rhowv(3) = rho(3)-rho(1); chi(3) = dlog( (p33+rho(3))/(p31+rho(1)) )
               qrhowv(3) = q(3)*rhowv(3); dchi(3) = qs(3)*chi(3)
               int_f(1) = 0.5_dp*(qrhowv(3)*ah(3,1) - dchi(3)*bh(3,1))
               int_f(2) = 0.5_dp*(qrhowv(3)*ah(3,2) - dchi(3)*bh(3,2)) + q(3)*chi(3)
               int_f(3) = 0.5_dp*(qrhowv(3)*ah(3,3) - dchi(3)*bh(3,3))

             elseif ( rho(3) == 0.0_dp ) then ! if the collocation point ipts(:,i) is at vertex y3, i.e., ipts(:,i) = y3
               int_p(1) = 0.0_dp; int_p(2) = 0.0_dp; int_p(3) = theta0 ! gam = 0.0_dp
               rhowv(1) = rho(1)-rho(2); chi(1) = dlog( (p11+rho(1))/(p12+rho(2)) )
               qrhowv(1) = q(1)*rhowv(1); dchi(1) = qs(1)*chi(1)
               int_f(1) = 0.5_dp*(qrhowv(1)*ah(1,1) - dchi(1)*bh(1,1))
               int_f(2) = 0.5_dp*(qrhowv(1)*ah(1,2) - dchi(1)*bh(1,2))
               int_f(3) = 0.5_dp*(qrhowv(1)*ah(1,3) - dchi(1)*bh(1,3)) + q(1)*chi(1)

             else
 		    if ( Rh(1) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y1
 		    	  cr22 = qs(2)*rh2s-p22*p22*ets; cr23 = qs(2)*rh3s-p23*p23*ets
 		    	  zn2 = qetx2(2)*(p23rh3*cr22 - p22rh2*cr23); zd2 = cr22*cr23 + qetx2(2)*qetx2(2)*p22rh2*p23rh3
 		    	  gam = gammai(p22,p23,qetx2(2),cr22,cr23,zn2,zd2) ! gam = gamma(2); gamma(1) = -gamma(3) = +pi or -pi

 		    elseif ( Rh(2) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y2
 		    	  cr33 = qs(3)*rh3s - p33s*ets; cr31 = qs(3)*rh1s - p31*p31*ets
 		    	  zn3 = qetx2(3)*(p31rh1*cr33 - p33rh3*cr31); zd3 = cr31*cr33 + qetx2(3)*qetx2(3)*p31rh1*p33rh3
 		    	  gam = gammai(p33,p31,qetx2(3),cr33,cr31,zn3,zd3) ! gam = gamma(3); gamma(1) = -gamma(2) = +pi or -pi

 		    elseif ( Rh(3) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is at vertex y3
 		    	  cr11 = qs(1)*rh1s - p11s*ets; cr12 = qs(1)*rh2s - p12s*ets
 		    	  zn1 = qetx2(1)*(p12rh2*cr11 - p11rh1*cr12); zd1 = cr11*cr12 + qetx2(1)*qetx2(1)*p11rh1*p12rh2
 		    	  gam = gammai(p11,p12,qetx2(1),cr11,cr12,zn1,zd1) ! gam = gamma(1); gamma(2) = -gamma(3) = +pi or -pi

 		    elseif ( q(1) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is on the line parallel to edge [y1,y2]
 		    	  cr22 = qs(2)*rh2s-p22*p22*ets; cr23 = qs(2)*rh3s-p23*p23*ets
 		    	  zn2 = qetx2(2)*(p23rh3*cr22 - p22rh2*cr23); zd2 = cr22*cr23 + qetx2(2)*qetx2(2)*p22rh2*p23rh3

 		    	  cr33 = qs(3)*rh3s - p33s*ets; cr31 = qs(3)*rh1s - p31*p31*ets
 		    	  zn3 = qetx2(3)*(p31rh1*cr33 - p33rh3*cr31); zd3 = cr31*cr33 + qetx2(3)*qetx2(3)*p31rh1*p33rh3
 
 		    	  gamma(2) = gammai(p22,p23,qetx2(2),cr22,cr23,zn2,zd2)
 		    	  gamma(3) = gammai(p33,p31,qetx2(3),cr33,cr31,zn3,zd3)
 		    	  gam = gamma(2)+gamma(3)

 		    elseif ( q(2) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is on the line parallel to edge [y2,y3]
 		    	  cr11 = qs(1)*rh1s - p11s*ets; cr12 = qs(1)*rh2s - p12s*ets
 		    	  zn1 = qetx2(1)*(p12rh2*cr11 - p11rh1*cr12); zd1 = cr11*cr12 + qetx2(1)*qetx2(1)*p11rh1*p12rh2

 		    	  cr33 = qs(3)*rh3s - p33s*ets; cr31 = qs(3)*rh1s - p31*p31*ets
 		    	  zn3 = qetx2(3)*(p31rh1*cr33 - p33rh3*cr31); zd3 = cr31*cr33 + qetx2(3)*qetx2(3)*p31rh1*p33rh3
 
 		    	  gamma(1) = gammai(p11,p12,qetx2(1),cr11,cr12,zn1,zd1)
 		    	  gamma(3) = gammai(p33,p31,qetx2(3),cr33,cr31,zn3,zd3)
 		    	  gam = gamma(1)+gamma(3)

 		    elseif ( q(3) == 0.0_dp ) then ! if the Projection of the collocation point ipts(:,i) is on the line parallel to edge [y3,y1]
 		    	  cr11 = qs(1)*rh1s - p11s*ets; cr12 = qs(1)*rh2s - p12s*ets
 		    	  zn1 = qetx2(1)*(p12rh2*cr11 - p11rh1*cr12); zd1 = cr11*cr12 + qetx2(1)*qetx2(1)*p11rh1*p12rh2

 		    	  cr22 = qs(2)*rh2s-p22*p22*ets; cr23 = qs(2)*rh3s-p23*p23*ets
 		    	  zn2 = qetx2(2)*(p23rh3*cr22 - p22rh2*cr23); zd2 = cr22*cr23 + qetx2(2)*qetx2(2)*p22rh2*p23rh3
 
 		    	  gamma(1) = gammai(p11,p12,qetx2(1),cr11,cr12,zn1,zd1)
 		    	  gamma(2) = gammai(p22,p23,qetx2(2),cr22,cr23,zn2,zd2)
 		    	  gam = gamma(1)+gamma(2)

 		    else
 		    	  cr11 = qs(1)*rh1s - p11s*ets; cr12 = qs(1)*rh2s - p12s*ets
 		    	  zn1 = qetx2(1)*(p12rh2*cr11 - p11rh1*cr12); zd1 = cr11*cr12 + qetx2(1)*qetx2(1)*p11rh1*p12rh2

 		    	  cr22 = qs(2)*rh2s-p22*p22*ets; cr23 = qs(2)*rh3s-p23*p23*ets
 		    	  zn2 = qetx2(2)*(p23rh3*cr22 - p22rh2*cr23); zd2 = cr22*cr23 + qetx2(2)*qetx2(2)*p22rh2*p23rh3

 		    	  cr33 = qs(3)*rh3s - p33s*ets; cr31 = qs(3)*rh1s - p31*p31*ets
 		    	  zn3 = qetx2(3)*(p31rh1*cr33 - p33rh3*cr31); zd3 = cr31*cr33 + qetx2(3)*qetx2(3)*p31rh1*p33rh3
 
 		    	  gamma(1) = gammai(p11,p12,qetx2(1),cr11,cr12,zn1,zd1)
 		    	  gamma(2) = gammai(p22,p23,qetx2(2),cr22,cr23,zn2,zd2)
 		    	  gamma(3) = gammai(p33,p31,qetx2(3),cr33,cr31,zn3,zd3)
 		    	  gam = gamma(1)+gamma(2)+gamma(3)

 		    endif

        	    rhowv(1) = rho(1)-rho(2); rhowv(2) = rho(2)-rho(3); rhowv(3) = rho(3)-rho(1)

        	    d(1) = qs(1) + ets; d(2) = qs(2) + ets; d(3) = qs(3) + ets

        	    ch11 = p11+rho(1); ch12 = p12+rho(2)
        	    ch22 = p22+rho(2); ch23 = p23+rho(3)
        	    ch31 = p31+rho(1); ch33 = p33+rho(3)

 		    if ( (ch11 >= f_eps) .and. (ch12 >= f_eps) ) then
 		       chi(1) = dlog(ch11/ch12)
 		    else
 		       chi(1) = asym_chi(p11,p12,d(1))
 		    endif

 		    if ( (ch22 >= f_eps) .and. (ch23 >= f_eps) ) then
 		       chi(2) = dlog(ch22/ch23)
 		    else
 		       chi(2) = asym_chi(p22,p23,d(2))
 		    endif

 		    if ( (ch33 >= f_eps) .and. (ch31 >= f_eps) ) then
 		       chi(3) = dlog(ch33/ch31)
 		    else
 		       chi(3) = asym_chi(p33,p31,d(3))
 		    endif

 		    omega = q(1)*chi(1) + q(2)*chi(2) + q(3)*chi(3)

 		    loop7: do k=1,mx_nd_elt
 		       dchi(k) = d(k)*chi(k); qrhowv(k) = q(k)*rhowv(k); etchi(k) = et*chi(k)
 		    enddo loop7
 
 		     if ( et >= 0.0_dp ) then
 		    	ThetGam = 0.5_dp*gam + theta0
 		     else
 		    	ThetGam = 0.5_dp*gam - theta0
 		     endif

 		    loop8a: do k=1,mx_nd_elt ! Loop over all nodes on element Q
 		       int_f(k) = 0.0_dp; int_p(k) = 0.0_dp
 		       loop8b: do i=1,3 ! Loop over all components
 		    	  int_f(k) = int_f(k) + qrhowv(i)*ah(i,k) - dchi(i)*bh(i,k)
 		    	  int_p(k) = int_p(k) - etchi(i)*bh(i,k)
 		       enddo loop8b
 		       int_f(k) = 0.5_dp*int_f(k) + (omega - et*ThetGam)*shc(k)
 		       int_p(k) = int_p(k) - ThetGam*shc(k)
 		    enddo loop8a
 
             endif ! if The collocation point ipts(:,i) is at vertex y1, i.e., ipts(:,i) = y1

           loop9: do k=1,mx_nd_elt
              int_G(qq(k)) = int_G(qq(k)) + int_f(k); int_H(qq(k)) = int_H(qq(k)) + int_p(k)
           enddo loop9
 
       enddo loop3 ! Loop over all boundary elements

       loop10: do k=1,nbnd ! Loop over all boundary nodes
          fldvl(j) = fldvl(j) + int_G(k)*flx(k) - int_H(k)*phi(k) ! fldvl(j) = dot_product(int_G,flx) - dot_product(int_H,phi)
       enddo loop10

    enddo loop1 ! Loop over all interior points    

    loop11: do k=1,nip ! Loop over all interior points
       fldvl(k) = fac*fldvl(k) ! Correct the potential at all interior points by the factor fac
    enddo loop11
    
    deallocate(int_G,int_H)

!----------------------------------------------------------------------------------------------------------------------------------!
END SUBROUTINE gbem_lap_fld_eval
!----------------------------------------------------------------------------------------------------------------------------------!
