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


void cbem_lap_input(const char *InfParams, const char *InfData)
{

/* ===purpose=== */

/**********************************************************************************************************************************/
/********************************  COLLOCATION BEM for 3D LAPLACE EQUATION: SINGULAR FORMULATION  *********************************/
/***************************************  3-NODED TRIANGULAR ELEMENT: ANALYTIC INTEGRATION  ***************************************/
/***********************************************  PIECEWISE CONSTANT APPROXIMATION  ***********************************************/
/**********************************************  Data input for 3D Laplace Equation  **********************************************/
/**********************************************************************************************************************************/

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

 
   extern const int mx_nd_elt; /* mx_nd_elt == Maximum number of nodes per element */
   extern int nbnd,nbelt;      /* nbnd == Total number of boundary nodes */
                               /* nbelt == Total number of boundary elements */
   extern int **belt, *eltbc;  /* belt[i][0:2] == Global indices of boundary nodes on the i-th element */
                               /* eltbc[i] = {0,1} ==> {Dirichlet,Neunamm} boundary condition is prescribed on belt[i][0:2] */
   extern double gm_eps,f_eps; /* gm_eps == Tolerance on the geometry */
                               /* f_eps == Tolerance on the argument of a function */
   extern double **bnd;        /* bnd[i][0:2] = Cartesian coordinates of the i-th boundary node */
   extern double *pbv;         /* pbv[i] == Prescribed Boundary Value at the centroid of the i-th boundary element */

   FILE *fp;
   int i;
   


   if ((fp = fopen(InfParams,"r")) != NULL)
   {   /* Read parameters */
       fscanf(fp,"%lf",&gm_eps); /* gm_eps == Tolerance on the geometry */
       fscanf(fp,"%*[^\n]");     /* Read and discard rest of line */
       fscanf(fp,"%lf",&f_eps);  /* f_eps == Tolerance on the argument of a function */
       fscanf(fp,"%*[^\n]");     /* Read and discard rest of line */
       fclose(fp); /* close InfParams */
   }
   else
   {
       fprintf(stderr, "\nError opening file \"%s\".\n",InfParams);
       exit(1);
   }


   if ((fp = fopen(InfData,"r")) != NULL)
   {   // Read parameters
       fscanf(fp,"%d",&nbnd);  /* nbnd == Total number of boundary nodes */
       fscanf(fp,"%*[^\n]");   /* Read and discard rest of line */
       fscanf(fp,"%d",&nbelt); /* nbelt == Total number of boundary elements */
       fscanf(fp,"%*[^\n]");   /* Read and discard rest of line <==> while( (ch = fgetc(fp)) != '\n' ) continue; */
       
       if ( (bnd = (double **)malloc(nbnd*sizeof(double *))) != NULL )  /* If bnd is successfully allocated */
       {
           for (i=0; i<nbnd; i++)    /* Loop over all boundary nodes */
	   {
	       if ( (bnd[i] = (double *)malloc(3*sizeof(double))) != NULL )   /* If bnd[i] is successfully allocated */
	       {   /* Read the Cartesian coordinates of boundary nodes */
	           /* bnd[i][0:2] = Cartesian coordinates of the i-th boundary node */
		   fscanf(fp,"%lf %lf %lf", &bnd[i][0], &bnd[i][1], &bnd[i][2]);
	       }
	       else
	       {
	          printf("cbem_lap_input: Allocate array \"bnd[%d]\" failed\n",i); exit(1); 
	       }
	       
	   }
       }
       else
       {printf("cbem_lap_input: Allocate array \"bnd\" failed\n"); exit(1);}
       

       pbv = (double *)malloc(nbelt*sizeof(double));
       if ( pbv == NULL)   /* If pbv is not successfully allocated */
       {printf("cbem_lap_input: Allocate array \"pbv\" failed\n"); exit(1);}
       
       eltbc = (int *)malloc(nbelt*sizeof(int));
       if ( eltbc == NULL)   /* If eltbc is not successfully allocated */
       {printf("cbem_lap_input: Allocate array \"eltbc\" failed\n"); exit(1);}
       
       belt = (int **)malloc(nbelt*sizeof(int *));
       if ( belt == NULL)   /* If belt is not successfully allocated */
       {printf("cbem_lap_input: Allocate array \"belt\" failed\n"); exit(1);}
       
       for (i=0; i<nbelt; i++)    /* Loop over all boundary elements */
       {
	   if ( (belt[i] = (int *)malloc(mx_nd_elt*sizeof(int))) != NULL )   /* If belt[i] is successfully allocated */
	   {    /* Read the characteristics of each boundary element */
	        fscanf(fp,"%d %d %d %lf %d", &belt[i][0], &belt[i][1], &belt[i][2], &pbv[i], &eltbc[i]);
	   }
	   else
	   {printf("cbem_lap_input: Allocate array \"belt[%d]\" failed\n",i); exit(1);}
       }

       fclose(fp); /* close InfData */
   }
   else
   {
       fprintf(stderr, "\nError opening file \"%s\".\n",InfData);
       exit(1);
   }

}
