#include <math.h>
#include <stdio.h>
#include <stdlib.h>


void cbem_lap_init(void)
{

/* ===purpose=== */

/**********************************************************************************************************************************/
/********************************  COLLOCATION BEM for 3D LAPLACE EQUATION: SINGULAR FORMULATION  *********************************/
/***************************************  3-NODED TRIANGULAR ELEMENT: ANALYTIC INTEGRATION  ***************************************/
/***********************************************  PIECEWISE CONSTANT APPROXIMATION  ***********************************************/
/***********************************************  Initialize arrays and parameters  ***********************************************/
/**********************************************************************************************************************************/

/* Developer: Sylvain Nintcheu Fata */
/* Version:   V.1.3                 */
/* Released:  Jun 14, 2012          */

 
   extern int nbelt;          /* nbelt == Total number of boundary elements */
   extern int **belt;         /* belt[k][i] == Global index of the i-th node on the boundary element k, i=0,1,2 */
   extern double fac,pi,pix2; /* pi = 3.1415926535897932, pix2 = 2*pi, fac == Factor of the Green's function: fac = 1/(4*pi) */
   extern double **bnd;       /* bnd[k][0:2] = Cartesian coordinates of the k-th boundary node */
   extern double **repnd;     /* repnd[k][0:2] == Cartesian coordinates of the centroid of the k-th boundary element */
   
   int k;



   pi = 4.0*atan(1.0); /* pi = 3.1415926535897932 */
   pix2 = 2.0*pi;
   fac = 1.0/(4.0*pi); /* fac == Factor of the Green's function */
   
   if ( (repnd = (double **)malloc(nbelt*sizeof(double *))) != NULL ) /* If repnd is successfully allocated */
   {
       for (k=0; k<nbelt; k++) /* Loop over all boundary elements (element = flat triangle) */
       {
           if ( (repnd[k] = (double *)malloc(3*sizeof(double))) != NULL ) /* If repnd[k] is successfully allocated */
	   {   /* Determine representative nodes for each boundary element */
	       /* repnd[k][0:2] == Centroid (representative node) of the k-th boundary triangle */
	       repnd[k][0] = (bnd[belt[k][0]][0] + bnd[belt[k][1]][0] + bnd[belt[k][2]][0])/3.0;
	       repnd[k][1] = (bnd[belt[k][0]][1] + bnd[belt[k][1]][1] + bnd[belt[k][2]][1])/3.0;
	       repnd[k][2] = (bnd[belt[k][0]][2] + bnd[belt[k][1]][2] + bnd[belt[k][2]][2])/3.0;
	       /* End Determine representative nodes for each boundary element */
	   }
	   else
	   {printf("cbem_lap_init: Allocate array \"repnd[%d]\" failed\n",k); exit(1);}
       }
   }
   else
   {printf("cbem_lap_init: Allocate array \"repnd\" failed\n"); exit(1);}

}
