Beispiel #1
0
/**
 * Tests that a given interpolation type reproduces the data points it is given,
 * and then tests that it correctly reproduces additional values.
 * 
 * @param xarr the x values of the points that define the function
 * @param yarr the y values of the points that define the function
 * @param zarr the values of the function at the points specified by xarr and yarr
 * @param xsize the length of xarr
 * @param ysize the length of yarr
 * @param xval the x values of additional points at which to calculate interpolated values
 * @param yval the y values of additional points at which to calculate interpolated values
 * @param zval the expected results of the additional interpolations
 * @param zxval the expected results of the x derivative calculations
 * @param zyval the expected results of the y derivative calculations
 * @param zxxval the expected results of the xx derivative calculations
 * @param zyyval the expected results of the yy derivative calculations
 * @param zxyval the expected results of the xy derivative calculations
 * @param test_size the length of xval, yval, zval, etc.
 * @param T the interpolation type
 */
int test_interp2d(const double xarr[], const double yarr[], const double zarr[],    // interpolation data
                  size_t xsize, size_t ysize,                                       // sizes of xarr and yarr
                  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 interp2d_type* T) {
    gsl_interp_accel *xa, *ya;
    int status = 0;
    size_t xi, yi, zi, i;

    xa = gsl_interp_accel_alloc();
    ya = gsl_interp_accel_alloc();
    interp2d* interp = interp2d_alloc(T, xsize, ysize);
    interp2d_spline* interp_s = interp2d_spline_alloc(T, xsize, ysize);

    unsigned int min_size = interp2d_type_min_size(T);
    gsl_test_int(min_size, T->min_size, "interp2d_type_min_size on %s", interp2d_name(interp));

    interp2d_init(interp, xarr, yarr, zarr, xsize, ysize);
    interp2d_spline_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 = INDEX_2D(xi, yi, xsize, ysize);
            test_single_low_level(&interp2d_eval, &interp2d_eval_e, interp, xarr, yarr, zarr, x, y, xa, ya, zarr, zi);
            test_single_low_level(&interp2d_eval_no_boundary_check, &interp2d_eval_e_no_boundary_check, interp, xarr, yarr, zarr, x, y, xa, ya, zarr, zi);
            test_single_high_level(&interp2d_spline_eval, &interp2d_spline_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(&interp2d_eval,         &interp2d_eval_e,          interp, xarr, yarr, zarr, x, y, xa, ya, zval, i);
        test_single_low_level(&interp2d_eval_deriv_x, &interp2d_eval_deriv_x_e,  interp, xarr, yarr, zarr, x, y, xa, ya, zxval, i);
        test_single_low_level(&interp2d_eval_deriv_y, &interp2d_eval_deriv_y_e,  interp, xarr, yarr, zarr, x, y, xa, ya, zyval, i);
        test_single_low_level(&interp2d_eval_deriv_xx,&interp2d_eval_deriv_xx_e, interp, xarr, yarr, zarr, x, y, xa, ya, zxxval, i);
        test_single_low_level(&interp2d_eval_deriv_yy,&interp2d_eval_deriv_yy_e, interp, xarr, yarr, zarr, x, y, xa, ya, zyyval, i);
        test_single_low_level(&interp2d_eval_deriv_xy,&interp2d_eval_deriv_xy_e, interp, xarr, yarr, zarr, x, y, xa, ya, zxyval, i);
        
        test_single_high_level(&interp2d_spline_eval,         &interp2d_spline_eval_e,          interp_s, x, y, xa, ya, zval, i);
        test_single_high_level(&interp2d_spline_eval_deriv_x, &interp2d_spline_eval_deriv_x_e,  interp_s, x, y, xa, ya, zxval, i);
        test_single_high_level(&interp2d_spline_eval_deriv_y, &interp2d_spline_eval_deriv_y_e,  interp_s, x, y, xa, ya, zyval, i);
        test_single_high_level(&interp2d_spline_eval_deriv_xx,&interp2d_spline_eval_deriv_xx_e, interp_s, x, y, xa, ya, zxxval, i);
        test_single_high_level(&interp2d_spline_eval_deriv_yy,&interp2d_spline_eval_deriv_yy_e, interp_s, x, y, xa, ya, zyyval, i);
        test_single_high_level(&interp2d_spline_eval_deriv_xy,&interp2d_spline_eval_deriv_xy_e, interp_s, x, y, xa, ya, zxyval, i);

        test_single_low_level(&interp2d_eval_no_boundary_check, &interp2d_eval_e_no_boundary_check, interp, xarr, yarr, zarr, x, y, xa, ya, zval, i);
    }
    gsl_interp_accel_free(xa);
    gsl_interp_accel_free(ya);
    interp2d_free(interp);
    return status;
}
Beispiel #2
0
/**
 * Tests a single evaluator function from the low-level interface.
 * 
 * See test_interp2d in this file for usage examples.
 */
static inline int test_single_low_level(
    double (*evaluator)(const interp2d*, const double[], const double[], const double[], const double, const double, gsl_interp_accel*, gsl_interp_accel*),
    int (*evaluator_e)(const interp2d*, const double[], const double[], const double[], const double, const double, gsl_interp_accel*, gsl_interp_accel*, double*),
    const interp2d* interp, const double xarr[], const double yarr[], const double zarr[], const double x, const double y, gsl_interp_accel* xa, gsl_interp_accel* ya, const double expected_results[], size_t i
) {
    if (expected_results != NULL) {
        int failures = 0;
        int status;
        double result = evaluator(interp, xarr, yarr, zarr, x, y, xa, ya);
        gsl_test_abs(result, expected_results[i], 1e-10, "low level %s %d", interp2d_name(interp), i);
        if (fabs(result - expected_results[i]) > 1e-10) {
            // test failed
            failures++;
        }
        status = evaluator_e(interp, xarr, yarr, zarr, x, y, xa, ya, &result);
        if (status != GSL_SUCCESS) {
            // something went wrong
            failures++;
        }
        else {
            gsl_test_abs(result, expected_results[i], 1e-10, "low level POSIX %s %d", interp2d_name(interp), i);
            if (fabs(result - expected_results[i]) > 1e-10) {
                // test failed - wrong result
                failures++;
            }
        }
    }
    else {
        return 0;
    }
}
Beispiel #3
0
const char* interp2d_spline_name(const interp2d_spline* interp) {
    return interp2d_name(&(interp->interp_object));
}