Exemplo n.º 1
0
int main( int argc, char *argv[])
{
  /* field variables */
  LSMLIB_REAL *phi;
  LSMLIB_REAL *distance_function;
  LSMLIB_REAL *mask = 0;

  /* grid parameters */
  LSMLIB_REAL X_lo[2] = {-1.0,-1.0};
  LSMLIB_REAL X_hi[2] = {1.0,1.0};
  LSMLIB_REAL dx[2];
  int N;
  int i,j;
  int idx;
  int num_gridpts;
  int grid_dims[2];

  /* numerical parameters */
  int spatial_derivative_order = 2;

  /* auxilliary variables */
  LSMLIB_REAL x,y;
  LSMLIB_REAL center[2], radius;

  /* file pointer to output results */
  FILE *data_file;

  /* set up grid */
  N = 100;
  num_gridpts = 1;
  for (i = 0; i < 2; i++) {
    dx[i] = (X_hi[i]-X_lo[i])/N;
    grid_dims[i] = N+1;
    num_gridpts *= grid_dims[i];
  }

  /* allocate memory for field data */
  phi = (LSMLIB_REAL*) malloc(num_gridpts*sizeof(LSMLIB_REAL));
  distance_function = (LSMLIB_REAL*) malloc(num_gridpts*sizeof(LSMLIB_REAL));

  /* initialize data */
  center[0] = 0.0; center[1] = 0.0;
  radius = 0.5;
  for (j = 0; j < grid_dims[1]; j++) {
    for (i = 0; i < grid_dims[0]; i++) {
      idx = i+j*grid_dims[0];
      x = X_lo[0]+dx[0]*i;
      y = X_lo[1]+dx[1]*j;

      phi[idx] = ( (x-center[0])*(x-center[0])
                  +(y-center[1])*(y-center[1]) ) - radius*radius;
    }
  }

  for (i = 0; i < num_gridpts; i++) {
    distance_function[i] = 0; 
  }

  /* Carry out FMM calculation */
  computeDistanceFunction2d(
    distance_function,
    phi,
    mask,
    spatial_derivative_order,
    grid_dims,
    dx);

  /* write results to output file */
  data_file = fopen("computeDistanceFunction2d.dat","w");
  for (idx = 0; idx < num_gridpts; idx++) {
    fprintf(data_file,"%f %f\n", distance_function[idx],phi[idx]);
  }
  fclose(data_file);

  /* clean up memory */
  free(phi);  
  free(distance_function);  

  return(0);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
  /* field data */
  double *phi;
  double *mask;
  double **ext_fields_orig;
  double *distance_fcn;
  double **ext_fields_updated;
  int num_ext_fields;
 
  /* grid data */
  const int *grid_dims = mxGetDimensions(PHI);
  double *dX = mxGetPr(DX);
  double dX_matlab_order[2];

  /* numerical parameters */
  int spatial_derivative_order;

  /* auxilliary variables */
  int i;
  int error_code;
  mxArray* tmp_mxArray;

  /* Check for proper number of arguments */
  if (nrhs < 2) {
    mexErrMsgTxt(
      "Insufficient number of input arguments (2 required; 2 optional)");
  } else if (nrhs > 4) {
    mexErrMsgTxt("Too many input arguments (2 required; 2 optional)");
  } else if (nlhs > 1) {
    mexErrMsgTxt("Too many output arguments.");
  }

  /* Get mask */
  if (nrhs < 3) {
    mask = 0;  /* NULL mask ==> all points are in interior of domain */
  } else {
    mask = mxGetPr(MASK);
  }
  
  /* Get spatial derivative order */
  if (nrhs < 4) {
    spatial_derivative_order = 1;  /* KTC - change this to 2 after */
                                   /* implementing second-order algorithm */
  } else {
    spatial_derivative_order = (int)(mxGetPr(SPATIAL_DERIVATIVE_ORDER)[0]);
  }

  /* Assign pointers for phi data */
  phi = mxGetPr(PHI);

  /* Create distance function data */
  DISTANCE_FUNCTION = mxCreateDoubleMatrix(grid_dims[0], grid_dims[1], mxREAL);
  distance_fcn = mxGetPr(DISTANCE_FUNCTION);

  /* Change order of dX to be match MATLAB meshgrid() order for grids. */
  dX_matlab_order[0] = dX[1];
  dX_matlab_order[1] = dX[0];

  /* Carry out FMM calculation */
  error_code = computeDistanceFunction2d(
                 distance_fcn,
                 phi,
                 mask,
                 spatial_derivative_order,
                 (int*) grid_dims,
                 dX_matlab_order);

  if (error_code) {
    mexErrMsgTxt("computeDistanceFunction2d failed...");
  }

  return;
}