Example #1
0
int main()
{
  int i, xi, yi;
  int N = 200;

  double *n_arr;
  double *T_arr;
  double *L_arr;
  double *H_arr;
  double *za; 

  int nx = 81;
  int ny = 41;
  double xa[nx], ya[ny];
  za = (double *) malloc(nx*ny*sizeof(double));
  
  FILE *infile;
  char buffer[0x1000];
  char * pch;  

  const gsl_interp2d_type *T = gsl_interp2d_bilinear;
  gsl_spline2d *C_spline = gsl_spline2d_alloc(T, nx, ny);
  gsl_spline2d *H_spline = gsl_spline2d_alloc(T, nx, ny);
  gsl_interp_accel *xacc = gsl_interp_accel_alloc();
  gsl_interp_accel *yacc = gsl_interp_accel_alloc();

  n_arr = (double *) malloc(nx*ny*sizeof(double));
  T_arr = (double *) malloc(nx*ny*sizeof(double));
  L_arr = (double *) malloc(nx*ny*sizeof(double));
  H_arr = (double *) malloc(nx*ny*sizeof(double));  

  // Read in low T cooling curve (function of density and temperature)
  i=0;
  infile = fopen("cloudy_coolingcurve_lowT.txt", "r");
  if (infile == NULL) {
    printf("Unable to open Cloudy file.\n");
    exit(1);
  }
  while (fgets(buffer, sizeof(buffer), infile) != NULL)
  {
    if (buffer[0] == '#') {
      continue;
    }
    else {
      pch = strtok(buffer, "\t");
      n_arr[i] = atof(pch);
      while (pch != NULL)
      {
        pch = strtok(NULL, "\t");
        if (pch != NULL)
          T_arr[i] = atof(pch);
        pch = strtok(NULL, "\t");
        if (pch != NULL)
          L_arr[i] = atof(pch);
        pch = strtok(NULL, "\t");
        if (pch != NULL)
          H_arr[i] = atof(pch);
      }
      i++;
    }
  }
  fclose(infile);

  // Set x and y values
  for (xi=0; xi<nx; xi++) {
    xa[xi] = -4.0 + 0.1*xi;
  }
  for (yi=0; yi<ny; yi++) {
    ya[yi] = 1.0 + 0.1*yi;
  }
  
  // Set z grid values for cooling interpolation 
  for (xi=0; xi<nx; xi++) {
    for (yi=0; yi<ny; yi++) {
      gsl_spline2d_set(C_spline, za, xi, yi, L_arr[yi + ny*xi]);
    }
  }

  /* initialize interpolation */
  gsl_spline2d_init(C_spline, xa, ya, za, nx, ny);

  // Set z grid values for heating interpolation
  for (xi=0; xi<nx; xi++) {
    for (yi=0; yi<ny; yi++) {
      gsl_spline2d_set(H_spline, za, xi, yi, H_arr[yi + ny*xi]);
    }
  }

  /* initialize interpolation */
  gsl_spline2d_init(H_spline, xa, ya, za, nx, ny);


  /* interpolate N values in y and print out curve for plotting */
  for (yi = 0; yi < N; yi++)
  {
    double yj = 1.0 + yi*(4.0/(N-1));
    double Ctest = gsl_spline2d_eval(C_spline, -3.0, yj, xacc, yacc);
    double Htest = gsl_spline2d_eval(H_spline, -3.0, yj, xacc, yacc);
    printf("%f %f %f\n", yj, Ctest, Htest);
  }

  gsl_spline2d_free(C_spline);
  gsl_spline2d_free(H_spline);
  gsl_interp_accel_free(xacc);
  gsl_interp_accel_free(yacc);
  free(n_arr);
  free(T_arr);
  free(L_arr);
  free(H_arr);
  free(za);  

  return 0;
}
Example #2
0
/*
 * Tests that a given interpolation type reproduces the data points
 * it is given, and then tests that it correctly reproduces additional
 * values.
 */
static int
test_interp2d(
const double xarr[], const double yarr[], const double zarr[], /* data */
              size_t xsize, size_t ysize,                /* array sizes */
              const double xval[], const double yval[],  /* test points */
              const double zval[],                       /* expected results */
              const double zxval[], const double zyval[],
              const double zxxval[], const double zyyval[], const double zxyval[],
              size_t test_size,                          /* number of test points */
              const gsl_interp2d_type * T)
{
  gsl_interp_accel *xa = gsl_interp_accel_alloc();
  gsl_interp_accel *ya = gsl_interp_accel_alloc();
  int status = 0;
  size_t xi, yi, zi, i;
  gsl_interp2d * interp = gsl_interp2d_alloc(T, xsize, ysize);
  gsl_spline2d * interp_s = gsl_spline2d_alloc(T, xsize, ysize);
  unsigned int min_size = gsl_interp2d_type_min_size(T);

  gsl_test_int(min_size, T->min_size, "gsl_interp2d_type_min_size on %s", gsl_interp2d_name(interp));

  gsl_interp2d_init(interp, xarr, yarr, zarr, xsize, ysize);
  gsl_spline2d_init(interp_s, xarr, yarr, zarr, xsize, ysize);

  /* First check that the interpolation reproduces the given points */
  for (xi = 0; xi < xsize; xi++)
    {
      double x = xarr[xi];
      for (yi = 0; yi < ysize; yi++)
        {
          double y = yarr[yi];
          
          zi = gsl_interp2d_idx(interp, xi, yi);
          test_single_low_level(&gsl_interp2d_eval, &gsl_interp2d_eval_e,
                                interp, xarr, yarr, zarr, x, y,
                                xa, ya, zarr, zi);
          test_single_low_level(&gsl_interp2d_eval_extrap,
                                &gsl_interp2d_eval_e_extrap, interp,
                                xarr, yarr, zarr, x, y, xa, ya, zarr, zi);
          test_single_high_level(&gsl_spline2d_eval, &gsl_spline2d_eval_e,
                                 interp_s, x, y, xa, ya, zarr, zi);
        }
    }

  /* Then check additional points provided */
  for (i = 0; i < test_size; i++)
    {
      double x = xval[i];
      double y = yval[i];
        
      test_single_low_level(&gsl_interp2d_eval,         &gsl_interp2d_eval_e,          interp, xarr, yarr, zarr, x, y, xa, ya, zval, i);
      test_single_low_level(&gsl_interp2d_eval_deriv_x, &gsl_interp2d_eval_deriv_x_e,  interp, xarr, yarr, zarr, x, y, xa, ya, zxval, i);
      test_single_low_level(&gsl_interp2d_eval_deriv_y, &gsl_interp2d_eval_deriv_y_e,  interp, xarr, yarr, zarr, x, y, xa, ya, zyval, i);
      test_single_low_level(&gsl_interp2d_eval_deriv_xx,&gsl_interp2d_eval_deriv_xx_e, interp, xarr, yarr, zarr, x, y, xa, ya, zxxval, i);
      test_single_low_level(&gsl_interp2d_eval_deriv_yy,&gsl_interp2d_eval_deriv_yy_e, interp, xarr, yarr, zarr, x, y, xa, ya, zyyval, i);
      test_single_low_level(&gsl_interp2d_eval_deriv_xy,&gsl_interp2d_eval_deriv_xy_e, interp, xarr, yarr, zarr, x, y, xa, ya, zxyval, i);

      test_single_high_level(&gsl_spline2d_eval,         &gsl_spline2d_eval_e,          interp_s, x, y, xa, ya, zval, i);
      test_single_high_level(&gsl_spline2d_eval_deriv_x, &gsl_spline2d_eval_deriv_x_e,  interp_s, x, y, xa, ya, zxval, i);
      test_single_high_level(&gsl_spline2d_eval_deriv_y, &gsl_spline2d_eval_deriv_y_e,  interp_s, x, y, xa, ya, zyval, i);
      test_single_high_level(&gsl_spline2d_eval_deriv_xx,&gsl_spline2d_eval_deriv_xx_e, interp_s, x, y, xa, ya, zxxval, i);
      test_single_high_level(&gsl_spline2d_eval_deriv_yy,&gsl_spline2d_eval_deriv_yy_e, interp_s, x, y, xa, ya, zyyval, i);
      test_single_high_level(&gsl_spline2d_eval_deriv_xy,&gsl_spline2d_eval_deriv_xy_e, interp_s, x, y, xa, ya, zxyval, i);

      test_single_low_level(&gsl_interp2d_eval_extrap, &gsl_interp2d_eval_e_extrap, interp, xarr, yarr, zarr, x, y, xa, ya, zval, i);
    }

  gsl_interp_accel_free(xa);
  gsl_interp_accel_free(ya);
  gsl_interp2d_free(interp);
  gsl_spline2d_free(interp_s);

  return status;
}