Example #1
0
particle* read_particles(const char* file) {
    char postfix[10];
    srand((unsigned)time(NULL));
    rand_string(postfix, 10);
    sprintf(dir_name, "out_%s_%s", basename((char*)file), postfix);
    mkdir(dir_name, 0777);
    
    ps = NULL;
    char* line = NULL;
    FILE* fp = fopen(file, "r");
    size_t len = 0;
    
    if (!fp) {
        printf("Invalid input: %s\nCould not open file.\n", file);
        exit(EXIT_FAILURE);
    }
    
    if (getline(&line, &len, fp) != -1) {
        char* end;
        particle_count = strtol(line, &end, 10);
        if (*end == 0) {
            printf("Invalid input: %s\nExpected number of particles.\n", line);
            exit(EXIT_FAILURE);
        }
        
        ps = _mm_malloc(particle_count * sizeof(particle), 64);
    } else {
        printf("Invalid input: Empty file.\n");
        exit(EXIT_FAILURE);
    }
    
    // m x y z vx vy vz
    for (long i = 0; i < particle_count; i++) {
        getline(&line, &len, fp);
        sscanf(line, "%lf %lf %lf %lf %lf %lf %lf", &ps[i].mass,
                                                    &ps[i].position[X], &ps[i].position[Y], &ps[i].position[Z],
                                                    &ps[i].velocity[X], &ps[i].velocity[Y], &ps[i].velocity[Z]);
        if (ps[i].mass == 0) {
            printf("Invalid input: Invald particle at line %ld.\n", i + 1);
            exit(EXIT_FAILURE);
        }
        
        ps[i].force[X] = 0.0;
        ps[i].force[Y] = 0.0;
        ps[i].force[Z] = 0.0;
        
        update_minmax(ps[i].position);
    }
    
    calculate_cube();
    
    fclose(fp);
    if (line) {
        free(line);
    }
    
    write_plot_limits();
    write_particles(-1); // Initial state of particles.
    return ps;
}
Example #2
0
int main(){
  long input_val, cube;

  printf("Enter a number to calculate the cube: ");
  scanf("%d", &input_val);
  
  cube = calculate_cube(input_val);
  printf("\nThe cube of %d is: %d.\n", input_val, cube);

  return 0;
}