Example #1
0
// main entry point for the program; initializes parameters, reads input file name argument if provided,
// declares two 2-dimensional arrays used as buffers to simulate the evolution of the cellular automaton
// grid, loads initial generation from input file into grid, runs automaton for 30 generations, prints
// results of evolution silently to file "output.txt" using a grid of 48 rows by 64 columns, (not including
// the additional frame which is printed at the perimeter of the grid), and prints error messages and exits
// if any part of this process fails;
int main( int argc, char ** argv )
{
    char even_grid[ROWS][COLUMNS];  // for double-buffering the grid, used for even-numbered generations
    char odd_grid[ROWS][COLUMNS];   // other buffer, used for odd-numbered generations
    int g, r, c;                    // indices
    char * input_filename;                 // "input.txt" if user does not provide a different file
    char * output_filename = "output.txt"; // hardwired to always create or overwrite same output file
    FILE * output_file_pointer;            // used to print results to file as they are generated

    for ( r = 0; r < ROWS; r++ ) {         // initialize buffers so they are full of dead cells (spaces)
        for ( c = 0; c < COLUMNS; c++ ) {
            even_grid[r][c] = ' ';
            odd_grid[r][c] = ' ';
        }
    }

    // allow user to specify input file as command line argument to program
    if ( 1 < argc ) {   // if arguments are given, assume first is the name of input file
        input_filename = argv[1];
    } else {            // no input filename provided, so use default name, "input.txt"
        input_filename = "input.txt";
    }

    if ( !load_input( input_filename, even_grid ) ) {
        fprintf( stderr, "usage: %s [ input_file ]\n", argv[0] );
        fprintf( stderr, "       quitting...\n" );
        exit( EXIT_FAILURE );
    }

    if ( NULL == ( output_file_pointer = fopen( output_filename, "w" ) ) ) {
        fprintf( stderr, "error: in 'main()', call to 'fopen(%s, 'w')' failed\n", output_filename );
        perror( "     " );
        fprintf( stderr, "usage: %s [ input_file ]\n", argv[0] );
        fprintf( stderr, "       quitting...\n" );
        exit( EXIT_FAILURE );
    }

    // run the 'Game of Life' on the provided input;
    for ( g = 0; g < GENERATIONS; g++ ) {   // iterate automaton for hardwired number of times;
        if ( 0 == g % 2 ) {                 // modulo for double-buffering; even generations use even_grid;
            print_grid( even_grid, g, output_file_pointer );   // once it's loaded or computed, print it;
            generate_grid( even_grid, odd_grid );             // use even_grid to compute new odd_grid;
        } else {                            // odd generations use odd grid
            print_grid( odd_grid, g, output_file_pointer );    // once it's computed, print it;
            generate_grid( odd_grid, even_grid );             // use odd_grid to compute new even_grid;
        }
    }

    if ( EOF == fclose( output_file_pointer ) ) {
        fprintf( stderr, "error: in 'main()', call to 'fclose(%s)' failed\n", output_filename );
        perror( "     " );
        fprintf( stderr, "usage: %s [ input_file ]\n", argv[0] );
        fprintf( stderr, "       quitting...\n" );
        exit( EXIT_FAILURE );
    }

    exit( EXIT_SUCCESS );
}
Example #2
0
int ossfim(int argc, char *argv[]) {
	int c, n = 25, dx = 9, dy = 9, i, j, plot_vgm = 0;
	double b = 1, B = 10, s = 1, S = 10, blocksize, samplespacing, est[2],
		**table;
	DATA **d = NULL;
	DPOINT *block = NULL, where;
	char *vgm_str = "1Exp(10)", *map_name = NULL;
	VARIOGRAM *vgm;

	while ((c = getopt(argc, argv, "n:m:B:b:S:s:V:v:x:y:")) != EOF) {
		switch (c) {
			case 'n':
				if (read_int(optarg, &n) || n <= 0)
					ErrMsg(ER_ARGOPT, "n");
				break;
			case 'b':
				if (read_double(optarg, &b) || b < 0)
					ErrMsg(ER_ARGOPT, "b");
				break;
			case 'B':
				if (read_double(optarg, &B) || B <= 0)
					ErrMsg(ER_ARGOPT, "B");
				break;
			case 's':
				if (read_double(optarg, &s) || s <= 0)
					ErrMsg(ER_ARGOPT, "s");
				break;
			case 'S':
				if (read_double(optarg, &S) || S <= 0)
					ErrMsg(ER_ARGOPT, "S");
				break;
			case 'x':
				if (read_int(optarg, &dx) || dx <= 0)
					ErrMsg(ER_ARGOPT, "x");
				break;
			case 'y':
				if (read_int(optarg, &dy) || dy <= 0)
					ErrMsg(ER_ARGOPT, "y");
				break;
			case 'v':
				vgm_str = optarg;
				break;
			case 'V':
				plot_vgm = 1;
				vgm_str = optarg;
				break;
			case 'm':
				map_name = optarg;
				break;
			default:
				ErrClo(optopt);
				break;
		}
	}

	which_identifier("dummy grid");
	d = get_gstat_data();
	init_one_data(d[0]);
	d[0]->id = 0;
	d[0]->n_list = d[0]->n_max = 0;
	d[0]->mode = X_BIT_SET | Y_BIT_SET | V_BIT_SET;
	set_norm_fns(d[0]);
	vgm = get_vgm(0);
	if (read_variogram(vgm, vgm_str))
		ErrMsg(ER_SYNTAX, vgm_str);
	vgm->ev->evt = SEMIVARIOGRAM;
	vgm->id1 = vgm->id2 = d[0]->id;
	block = get_block_p();
	block->z = 0.0;
	block->x = block->y = -1.0;
	est[0] = 0.0;
	est[1] = -1.0;
	where.x = where.y = where.z = 0.0;
	where.X = (double *) emalloc(sizeof(double));
	where.X[0] = 1.0;

	if (plot_vgm)
		return fprint_gnuplot_variogram(stdout, vgm, "", GIF, 0);

	table = (double **) emalloc((dy + 1) * sizeof(double *));
	for (i = 0; i <= dy; i++)
		table[i] = (double *) emalloc((dx + 1) * sizeof(double));

	/* do it: */
	for (i = 0; i <= dx; i++) { /* sample spacing loop */
		samplespacing = s + (i / (1.0 * dx)) * (S - s);
		generate_grid(d[0], samplespacing, n);
		select_at(d[0], &where);
		for (j = 0; j <= dy; j++) { /* block sizes loop */
			reset_block_discr();
			vgm_init_block_values(vgm);
			blocksize = b + (j / (1.0 * dy)) * (B - b);
			block->x = block->y = blocksize;
			if (blocksize == 0.0)
				SET_POINT(&where);
			else
				SET_BLOCK(&where);
			gls(d, 1, GLS_BLUP, &where, est);
			if (map_name)
				table[i][j] = sqrt(est[1]);
			else
				printlog("%g %g %g\n", samplespacing, blocksize, sqrt(est[1]));
		}
	}
	if (map_name)
		ossfim2map(table, map_name, s, S, b, B, dx, dy);
	return 0;
}
Example #3
0
int
main(int argc, char *argv[])
{
    char input[MAX_FILENAME_SIZE];
    int c;
    int iterations = 1;
    int generate = 0;
    struct Grid *grid;
    struct Grid *tmp_grid;

    if (argc < 2) {
        help();
        return 0;
    }

    while ((c = getopt(argc, argv, "?hv:n:i:g")) != -1) {
        switch (c) {
            case 'h':
            case 'v':
                help();
                return 0;
            case 'n':
                iterations = atoi((char *) getopt);
                break;
            case 'i':
                strncpy(input, optarg, MAX_FILENAME_SIZE);
                break;
            case 'g':
                generate = 1;
                break;
            default:
                help();
                return 0;
        }
    } 

    if (argc != optind) {
        help();
        return 0;
    }

    grid = allocate_grid();
    tmp_grid = allocate_grid();
    if (!grid || !tmp_grid) {
        printf("error: out of memory\n");
        return -1;
    }
    if (generate) {
        generate_grid(grid);
    } else {
        read_grid(input, grid); 
    }
    for (c = 0; c < iterations; c++) {
        mean_filter(grid, tmp_grid);
        struct Grid *tmp = grid;
        grid = tmp_grid;
        tmp_grid = tmp;
    }
    write_grid(grid);
    cleanup_grid(grid);
    cleanup_grid(tmp_grid);

    return 0;
}