C*************************************************************
C     Test program for subroutine LIBEMA by Stephen Kirkup      
C*************************************************************C 
C  Copyright 2001- Stephen Kirkup
C  School of Computing Engineering and Physical Sciences
C  University of Central Lancashire - www.uclan.ac.uk 
C  smkirkup@uclan.ac.uk
C  http://www.researchgate.net/profile/Stephen_Kirkup
C
C  This open source code can be found at
C   www.boundary-element-method.com/fortran/LIBEMA_T.FOR 
C
C  Issued under the GNU General Public License 2007, see gpl.txt
C
C  Part of the the author's open source BEM packages. 
C  All codes and manuals can be downloaded from 
C  www.boundary-element-method.com
C
C***************************************************************
C This program is a test for the subroutine LIBEMA. The program computes
C  the solution to the Laplace equation 
C
C                  __ 2         
C                  \/    {\phi}   =  0  
C
C  interior to a sphere by the boundary panel method where {\phi} is 
C  known as the potential.
C
C For the interior problem, the domain lies interior to a closed 
C  boundary S. The boundary condition may be Dirichlet, Robin or 
C  Neumann. It is assumed to have the following general form
C
C            {\alpha}(q) {\phi}(q) + {\beta}(q) v(q) = f(q)
C    
C  where {\phi}(q) is the velocity potential at the point q on S, v(q) 
C  is the derivative of {\phi} with respect to the outward normal to S 
C  at q and {\alpha}, {\beta} and f are real-valued functions defined
C   on S. 
C
C Subroutine LIBEMA accepts a description of the boundary of the domain
C  and the position of the interior points where the solution ({\phi}) 
C  is sought, the boundary condition and returns the solution ({\phi} 
C  and v) on S and the value of {\phi} at the interior points.
C
C
C The test problems
C -----------------
C
C In this test the domain is a sphere of side 1 (metre). The 
C  solution to the problem with a Dirichlet boundary condition 
C  ({\alpha}=1, {\beta}=0) and with a Neumann boundary condition 
C  ({\alpha}=0, beta=1) are sought. 
C
C In the R-z plane, the boundary conditions are specified through 
C  taking the solution to be determined by
C
C                        2      2
C              {\phi} = x   -  y  +  z ,
C
C  which is clearly a solution of the Laplace equation.
C
C
C The boundary is described by a set of NS=32 panels of equal size,
C  so that each side comprises eight panels. The boundary solution
C  points are the centres of the panels. 
C The *s show the interior points at which the solution is sought;
C  the points (0.025,0.025), (0.075,0.025), (0.025,0.075),
C  (0.075,0.075), and (0.05,0.05).

C----------------------------------------------------------------------

C The PARAMETER statement
C -----------------------
C There are four components in the PARAMETER statement.
C integer MAXNS   : The limit on the number of boundary panels.
C integer MAXNV   : The limit on the number of vertices.
C integer MAXNPI  : The limit on the number of interior points.


C External modules related to the package
C ---------------------------------------
C subroutine LIBEMA: Subroutine for solving the interior Laplace
C  equation. (file LIBEMA.FOR contains LIBEMA and subordinate routines)
C subroutine L2LC: Returns the individual discrete Laplace integral
C  operators. (file L2LC.FOR contains L2LC and subordinate routines)
C subroutine GLS: Solves a general linear system of equations.
C  (file GLS.FOR contains CGSL and subordinate routines)
C file GEOM2D.FOR contains the set of relevant geometric subroutines 


C The program 

      PROGRAM LIBEMAT
      IMPLICIT NONE

C VARIABLE DECLARATION
C --------------------

C  PARAMETERs for storing the limits on the dimension of arrays
C   Limit on the number of panels
      INTEGER    MAXNS
      PARAMETER (MAXNS=32)
C   Limit on the number of vertices (equal to the number of panels)
      INTEGER    MAXNV
      PARAMETER (MAXNV=MAXNS)
C   Limit on the number of points interior to the boundary, where 
C    the solutions are sought
      INTEGER    MAXNPI
      PARAMETER (MAXNPI=6)

C  Constants
C   Real scalars: 0, 1, 2, pi
      REAL*8 ZERO,ONE,TWO,FOUR,PI

C  Geometrical description of the boundary(ies)
C   Number of panels and counter
      INTEGER    NS,IS
C   Number of central points (on S) and counter
      INTEGER    NSP,ISP
C   Number of vetices and counter
      INTEGER    NV,IV
C   Index of nodal coordinate for defining boundaries (standard unit is 
C    metres)
      REAL*8     VERTEX(MAXNV,2)
C   The two nodes that define each panel on the boundaries
      INTEGER    SELV(MAXNS,2)
C   The points interior to the boundary(ies) where the solutions 
C    are sought and the directional vectors at those points.
C    [Only necessary if an interior solution is sought.]
C    Number of interior points and counter
      INTEGER    NPI,IPI
C    Coordinates of the interior points
      REAL*8     PINT(MAXNPI,2)


C   Data structures that are used to define each test problem in turn
C    and are input parameters to LIBEMA.
C    SALPHA(j) is assigned the value of {\alpha} at the centre of the 
C     j-th panel.
      REAL*8 SALPHA(MAXNS)
C    SBETA(j) is assigned the value of {\beta} at the centre of the 
C     j-th panel.
      REAL*8 SBETA(MAXNS)
C    SF(j) is assigned the value of f at the centre of the j-th panel.
      REAL*8 SF(MAXNS)

      REAL*8 SFFPHI(MAXNS)
      REAL*8 SFFVEL(MAXNS)
      REAL*8 PFFPHI(MAXNS)

      
C  Validation and control parameters for LIBEMA
C   Switch for particular solution
      LOGICAL    LSOL
C   Validation switch
      LOGICAL    LVALID
C   The maximum absolute error in the parameters that describe the
C    geometry of the boundary.
      REAL*8     EGEOM

C Output from subroutine LIBEMA
C  The velocity potential (phi - the solution) at the centres of the 
C   panels
      REAL*8 SPHI(MAXNS)
C  The normal derivative of the velocity potential at the centres of the
C    panels
      REAL*8 SVEL(MAXNS)
C  The velocity potential (phi - the solution) at interior points
      REAL*8 PIPHI(MAXNPI)

C Workspace for LIBEMA
      REAL*8 L_SS(MAXNS,MAXNS)
      REAL*8 M_SSPLUS(MAXNS,MAXNS)
      REAL*8 L_DS(MAXNPI,MAXNS)
      REAL*8 M_DS(MAXNPI,MAXNS)
      REAL*8 WKSPC1(MAXNS)
      REAL*8 WKSPC2(MAXNS)
      REAL*8 WKSPC3(MAXNS)
      REAL*8 C(MAXNS)
      INTEGER*4 PERM(MAXNS)
      LOGICAL XORY(MAXNS)


C  Counter through the x,y coordinates
      INTEGER    ICOORD

C  The coordinates of the centres of the panels  
      REAL*8     SELCNT(MAXNS,2)

C  Other variables used in specifying the boundary condition
      REAL*8     Z,ZP,R

      REAL*8     EPS

C INITIALISATION
C --------------

C Set constants
      ZERO=0.0D0
      ONE=1.0D0
      TWO=2.0D0
      FOUR=4.0D0
      PI=4.0D0*ATAN(ONE)

      EPS=1.0E-10


C Describe the nodes and panels that make up the boundary
C  :The circle that generates the sphere is divided into NS=18 uniform
C  : panels. VERTEX and SELV are defined anti-clockwise around the
C  : boundary so that the normal to the boundary is assumed to be 
C  : outward
C  :Set up nodes
C  : Set NS, the number of panels
      NS=18
C  : Set NV, the number of vertices (equal to the number of panels)
      NV=NS+1
C  : Set coordinates of the nodes
      DATA ((VERTEX(IV,ICOORD),ICOORD=1,2),IV=1,19)
     * / 0.000D0, 1.000D0,
     *   0.174D0, 0.985D0,
     *   0.342D0, 0.940D0,
     *   0.500D0, 0.866D0,
     *   0.643D0, 0.766D0,
     *   0.766D0, 0.643D0,
     *   0.866D0, 0.500D0,
     *   0.940D0, 0.342D0,
     *   0.985D0, 0.174D0,
     *   1.000D0, 0.000D0,
     *   0.985D0,-0.174D0,
     *   0.940D0,-0.342D0,
     *   0.866D0,-0.500D0,
     *   0.766D0,-0.643D0,
     *   0.643D0,-0.766D0,
     *   0.500D0,-0.866D0,
     *   0.342D0,-0.940D0,
     *   0.174D0,-0.985D0,
     *   0.000D0,-1.000D0 /

C  :Describe the panels that make up the two boundarys
C  : Set NS, the number of panels
      NS=18
C  : Set nodal indices that describe the panels of the boundarys.
C  :  The indices refer to the nodes in VERTEX. The order of the
C  :  nodes in SELV dictates that the normal is outward from the 
C  :  boundary into the domain.
      DATA ((SELV(IS,ICOORD),ICOORD=1,2),IS=1,18)
     * /  1,  2,
     *    2,  3,
     *    3,  4,
     *    4,  5,
     *    5,  6,
     *    6,  7,
     *    7,  8,
     *    8,  9,
     *    9,  10,
     *   10,  11,
     *   11,  12,
     *   12,  13,
     *   13,  14,
     *   14,  15,
     *   15,  16,
     *   16,  17,
     *   17,  18,
     *   18,  19 /
       
C Set the centres of the panels, the central points
      DO IS=1,NS
        SELCNT(IS,1)=(VERTEX(SELV(IS,1),1)
     *   +VERTEX(SELV(IS,2),1))/TWO
        SELCNT(IS,2)=(VERTEX(SELV(IS,1),2)
     *   +VERTEX(SELV(IS,2),2))/TWO
      END DO


C Set the points in the domain where the solutions are sought, PINT. 
      NPI=4
      DATA ((PINT(IPI,ICOORD),ICOORD=1,2),IPI=1,4)
     *  /  0.000D0,     0.000D0,
     *     0.000D0,     0.500D0,
     *     0.000D0,    -0.500D0,
     *     0.500D0,     0.000D0 /


C The number of points on the boundary is equal to the number of 
C  panels
      NSP=NS
        


C  :The test problem is an osciallating sphere so that                
C    {\phi}= z 
C  :This gives the following v on S 
C     v = z
 
      ZP=0.25D0
      DO 250 ISP=1,NSP/2
        R=SELCNT(ISP,1)
        Z=SELCNT(ISP,2)
        SALPHA(ISP)=1.0D0
        SBETA(ISP)=0.0D0
        SF(ISP)= Z
250   CONTINUE     
      DO 260 ISP=NSP/2+1,NSP
        R=SELCNT(ISP,1)
        Z=SELCNT(ISP,2)
        SALPHA(ISP)=0.0D0
        SBETA(ISP)=1.0D0
        SF(ISP)= Z
260   CONTINUE     
      DO 640 ISP=1,NSP
        SFFPHI(ISP)=0.0D0
        SFFVEL(ISP)=0.0D0
640   CONTINUE
      DO 650 IPI=1,NPI
        PFFPHI(IPI)=0.0D0
650   CONTINUE


C Set up validation and control parameters
C  :Switch for particular solution
      LSOL=.TRUE.
C  :Switch on the validation of LIBEMA
      LVALID=.TRUE.
C  :Set EGEOM
      EGEOM=1.0D-6


C   Set up particular alpha and beta functions for this wavenumber
C    and type of boundary condition

       
          CALL LIBEMA(MAXNV,NV,VERTEX,MAXNS,NS,SELV,
     *               MAXNPI,NPI,PINT,
     *               SALPHA,SBETA,SF,SFFPHI,SFFVEL,PFFPHI,
     *               LSOL,LVALID,EGEOM,
     *               SPHI,SVEL,PIPHI,
     *               L_SS,M_SSPLUS,L_DS,M_DS,
     *               PERM,XORY,C,WKSPC1,WKSPC2,WKSPC3)


C Output the solutions
C  Open file for the output data
      OPEN(UNIT=20,FILE='LIBEMA.OUT')

      WRITE(20,*) 'LIBEMA: Computed solutions in the domain'
      WRITE(20,*)
      WRITE(20,*) '   P(1)      P(2)      Computed    Exact'
      DO 800 IPI=1,NPI
        WRITE(20,999) PINT(IPI,1),PINT(IPI,2),PIPHI(IPI),PINT(IPI,2)
800   CONTINUE
999   FORMAT(4F10.4)
      CLOSE(20)

      END

     