Пример #1
0
int main(int argc, char **argv) {
    int xsize, ysize;
    double **rho_0;
    FILE *infp;
    int i;
    double t;
    double cumulative_error;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s xsize ysize density-grid\n", argv[0]);
        exit(1);
    }

    xsize = atoi(argv[1]);
    ysize = atoi(argv[2]);

    infp = fopen(argv[3], "r");
    if (infp == NULL) {
        fprintf(stderr, "%s: unable to open file '%s': ", argv[0], argv[3]);
        perror(NULL);
        exit(4);
    }

    /* Read in the density data and transform it */
    rho_0 = cart_dmalloc(xsize * 3, ysize * 3);
    if (readdensity(infp, rho_0, xsize, ysize)) {
        fprintf(stderr, "%s: density file contains too few or incorrect data\n", argv[0]);
        exit(6);
    }
    fclose(infp);

    fftrho = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    clt_rhot = malloc(xsize*3 * ysize*3 * sizeof(double));
    cart_forward(rho_0, xsize*3, ysize*3);

    fftexpt = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    fft_rhot = fftw_malloc(xsize*3 * ysize*3 * sizeof(double));
    rhotplan = fftw_plan_r2r_2d(xsize*3, ysize*3, fftexpt, fft_rhot,
                                FFTW_REDFT01, FFTW_REDFT01, FFTW_MEASURE);
    expky = malloc(ysize*3 * sizeof(double));
    clt_temp = malloc(xsize*3 * ysize*3 * sizeof(double));

    fftvxt = malloc((xsize*3 + 1) * sizeof(double*));
    cltvxt = malloc((xsize*3 + 1) * sizeof(double*));
    fftvyt = malloc((xsize*3 + 1) * sizeof(double*));
    cltvyt = malloc((xsize*3 + 1) * sizeof(double*));
    for (i=0; i<=xsize*3; i++) {
        fftvxt[i] = malloc((ysize*3 + 1) * sizeof(double));
        cltvxt[i] = malloc((ysize*3 + 1) * sizeof(double));
        fftvyt[i] = malloc((ysize*3 + 1) * sizeof(double));
        cltvyt[i] = malloc((ysize*3 + 1) * sizeof(double));
    }

    t = 0;
    cumulative_error = 0;

    for (i=0; h_values[i] != 0; i++) {
        double h, err;
        
        h = h_values[i];
        t += 2*h;

        cart_density_fft(t, xsize*3, ysize*3);

        if (t < 1) {
            set_clt_rhot(*rho_0, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            cart_density_clt(0.5*t, xsize*3, ysize*3);
            // cart_density_clt(2.0*t, xsize*3, ysize*3);
        } else {
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
            cart_density_clt(0.5*h, xsize*3, ysize*3);
        }

        err = compare(xsize*3, ysize*3);
        cumulative_error += h*err;
        printf("[%d] t=%g, h=%g, error=%g\n", i, t, h, h*err);
    }
    printf("\nCumulative error = %g\n", cumulative_error);

    cart_dfree(rho_0);
    free(clt_rhot);
    free(clt_temp);

    for (i=0; i<=xsize; i++) {
        free(fftvxt[i]);
        free(cltvxt[i]);
    }
    free(fftvxt);
    free(cltvxt);

    for (i=0; i<=xsize; i++) {
        free(fftvyt[i]);
        free(cltvyt[i]);
    }
    free(fftvyt);
    free(cltvyt);

    free(expky);
    fftw_free(fftrho);
    fftw_free(fftexpt);
    fftw_free(fft_rhot);
    fftw_destroy_plan(rhotplan);
    return 0;
}
/* Function to calculate the discrete cosine transform of the input data.
 * This function is just a wrapper for forward(), so the user doesn't
 * need to see the fftw-format density array */
void cart_transform(double **userrho, int xsize, int ysize)
{
	cart_forward(*userrho,xsize,ysize);
}