Exemplo n.º 1
0
struct cells_t *cells_copy(struct cells_t *old_cells) {
  struct cells_t *new_cells = cells_new(old_cells->length, 0);

  memcpy(new_cells->state, new_cells->state, new_cells->length * sizeof(cell_state));

  return new_cells;
}
Exemplo n.º 2
0
void read_case(char *filename, int *n_variables, int *n_nodes, struct NODE **node, int *n_faces, struct FACE **face, int *n_cells, struct CELL **cell, int *n_zones, struct ZONE **zone)
{
	int i;

	//open the file
	FILE *file = fopen(filename,"r");
	exit_if_false(file != NULL, "opening the case file");

	//number of variables
	exit_if_false(fread(n_variables, sizeof(int), 1, file) == 1, "reading the number of variables");

	//numbers of elements
	exit_if_false(fread(n_nodes, sizeof(int), 1, file) == 1, "reading the number of nodes");
	exit_if_false((*node = nodes_new(*n_nodes,NULL)) != NULL,"allocating nodes");
	exit_if_false(fread(n_faces, sizeof(int), 1, file) == 1, "reading the number of faces");
	exit_if_false((*face = faces_new(*n_faces,NULL)) != NULL,"allocating faces");
	exit_if_false(fread(n_cells, sizeof(int), 1, file) == 1, "reading the number of cells");
	exit_if_false((*cell = cells_new(*n_cells,NULL)) != NULL,"allocating cells");
	exit_if_false(fread(n_zones, sizeof(int), 1, file) == 1, "reading the number of zones");
	exit_if_false((*zone = zones_new(*n_zones,NULL)) != NULL,"allocating zones");

	//elements
	for(i = 0; i < *n_nodes; i ++) node_case_get(file, &(*node)[i]);
	for(i = 0; i < *n_faces; i ++) face_case_get(file, *node, &(*face)[i], *cell, *zone);
	for(i = 0; i < *n_cells; i ++) cell_case_get(file, *n_variables, *face, &(*cell)[i], *zone);
	for(i = 0; i < *n_zones; i ++) zone_case_get(file, &(*zone)[i]);

	//clean up
	fclose(file);
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
  if(argc < 2) {
    fprintf(stderr, "Usage: automata outfile\n");
    return EXIT_SUCCESS;
  }

  struct cells_t *cells = cells_new(CELLS_WIDTH, OFF);
  cells_set_state(cells, CELLS_WIDTH / 2, ON);

  FILE *fptr = fopen(argv[1], "w");
  cells_write_pbm_header(cells, fptr, CELLS_WIDTH, GENERATIONS);

  cell_state rule_30[] = { OFF, OFF, OFF, ON, ON, ON, ON, OFF };

  struct cells_t *new_cells;
  for(int gen = 0; gen < GENERATIONS; gen++) {
    new_cells = apply_rule(rule_30, cells);
    cells_free(cells);
    cells = new_cells;

    cells_write_pbm_generation(cells, fptr);
  }
  cells_free(new_cells);

  fclose(fptr);

  return EXIT_SUCCESS;
}
Exemplo n.º 4
0
void read_geometry(char *filename, int *n_nodes, struct NODE **node, int *n_faces, struct FACE **face, int *n_cells, struct CELL **cell)
{
	FILE *file = fopen(filename,"r");
	exit_if_false(file != NULL,"opening geometry file");

	char *line;
	exit_if_false(allocate_character_vector(&line, MAX_STRING_LENGTH),"allocating line string");

	int i;

	while(fgets(line, MAX_STRING_LENGTH, file) != NULL)
	{
		if(strncmp(line,"NODES",5) == 0)
		{
			exit_if_false(sscanf(&line[6],"%i",n_nodes) == 1,"reading the number of nodes");
			*node = nodes_new(*n_nodes, NULL);
			exit_if_false(*node != NULL,"allocating the nodes");
			for(i = 0; i < *n_nodes; i ++) node_geometry_get(file, &(*node)[i]);
		}
		if(strncmp(line,"FACES",5) == 0)
		{
			exit_if_false(sscanf(&line[6],"%i",n_faces) == 1,"reading the number of faces");
			*face = faces_new(*n_faces, NULL);
			exit_if_false(*face != NULL,"allocating the faces");
			for(i = 0; i < *n_faces; i ++) face_geometry_get(file, &(*face)[i], *node);
		}
		if(strncmp(line,"CELLS",5) == 0)
		{
			exit_if_false(sscanf(&line[6],"%i",n_cells) == 1,"reading the number of cells");
			*cell = cells_new(*n_cells, NULL);
			exit_if_false(*cell != NULL,"allocating the cells");
			for(i = 0; i < *n_cells; i ++) cell_geometry_get(file, &(*cell)[i], *face);
		}
	}

	exit_if_false(*n_nodes > 0,"finding nodes in the geometry file");
	exit_if_false(*n_faces > 0,"finding faces in the geometry file");
	exit_if_false(*n_cells > 0,"finding cells in the geometry file");

	free(line);
	fclose(file);
}