% **********************************************************************
%          function l2lc.m by www.boundary-element-method.com          *
% **********************************************************************
% l2lc_tests.m Version 2, Sept 2015. (Version 1 2008).  

% Matlab/Freemat/Octave/Scilab open source codes
% Stephen Kirkup, School of Engineering
% University of Central Lancashire
% http://www.researchgate.net/profile/Stephen_Kirkup

% function l2lc_tests()
% A set of tests for l2lc.m

% Web source of this file
%  http://www.boundary-element-method.com/mfiles/l2lc_tests.m
% A guide to this routine is given at
%  http://www.boundary-element-method.com/mfiles/l2lc_tests_m.pdf
% This is a test file for
%  http://www.boundary-element-method.com/mfiles/l2lc.m

function [l,m,mt,n,lfail]=l2lc(p,vecp,qa,qb,lponel,nq, w, x, lvalid, tolGeom, tolQuad, needl,needm,needmt,needn)
   
   lfail=false;
   if (lvalid)
       passed=OkGeometry2lc(p, vecp, qa, qb, lponel, tolGeom);
       if (~passed)
           'l2lc failed - check input geometry'
           lfail=true;
       end
       passed=OkQuadrature(nq, w, x, -1.0, tolQuad);
       if (~passed)
           'l2lc failed - check input quadrature rule'
           lfail=true;
       end
   end
   
   if (~lfail)
   oo2pi=0.5/pi;
   qbma=vector(qa,qb);
   qlen=dist(qa,qb);
   normq=norm2(qa,qb);
   pqalen=dist(qa,p);
   pqblen=dist(qb,p);
   dnpdq=dotproduct(vecp,normq);
   
   l=0;
   m=0;
   mt=0;
   n=0;
   
   if (lponel)
     if (needl) 
         l=(qlen-(pqalen*log(pqalen)+pqblen*log(pqblen)))*oo2pi;
     end
     m=0;
     mt=0;
     if (needn) 
         n=-(1/pqalen+1/pqblen)*oo2pi;
     end
   else
  
       
     onesnq=ones(1,nq);
  
     qasame=[qa(1)*onesnq; qa(2)*onesnq];
     psame=[p(1)*onesnq; p(2)*onesnq];
     
     delta=[qbma(1)*x'; qbma(2)*x'];
     
     q=qasame+delta;
     rr=psame-q;
     srr=rr.^2;
     
     srr1=srr(1,1:nq);
     srr2=srr(2,1:nq);
     sr=srr1+srr2;
     r=sqrt(sr);
     
     if (needl)
       g=-oo2pi*log(r);
       l=qlen*(w'*g');
     end
  
     if (needm|needmt|needn)
       rnqr=-normq'*rr;
       rnq=rnqr./r;
       rnpr=vecp'*rr;
       rnp=rnpr./r;
       gr=-oo2pi./r;
       wgr=(w.*gr');
     end 
      
     if (needm)  
       m=qlen*(wgr'*rnq'); 
     end
    
     if (needmt)
       mt=qlen*(wgr'*rnp');
     end
    
     if (needn)
       rnprnq=rnp.*rnq;
       dnpnq=vecp'*normq;
       dnpnqsame=dnpnq*onesnq;
       rnpnq=-(dnpnqsame+rnprnq)./r;   
       grr=oo2pi./sr;
       wgrr=w.*grr';
       n=qlen*(wgr'*rnpnq'+wgrr'*rnprnq');
       
     end
   end
    
   end
   
end


 
   