-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cc
More file actions
167 lines (144 loc) · 5.88 KB
/
Copy pathio.cc
File metadata and controls
167 lines (144 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "io.h"
/*
WARNING - do not change this file!
*/
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
void read_args(int argc, char *argv[], Params ¶ms, std::vector<Particle> &particles) {
int param_threads{};
int square_size{};
int param_particles{};
int param_radius{};
int param_steps{};
std::ifstream input_file;
std::string curr_line{};
// Check for basic usage
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <INPUT_PATH> <NUM_THREADS>" << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "<INPUT_PATH>: " << argv[1] << std::endl;
std::cout << "<NUM_THREADS>: " << argv[2] << std::endl;
input_file.open(argv[1]);
if (!input_file.is_open()) {
std::cerr << "Failed to open " << argv[1] << " for reading. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
try {
param_threads = std::stoi(argv[2]);
} catch (std::invalid_argument const &ex) {
std::cerr << "Invalid argument for <NUM_THREADS>: " << argv[3] << ". Aborting..." << std::endl;
exit(EXIT_FAILURE);
} catch (std::out_of_range const &ex) {
std::cerr << "<NUM_THREADS> out of int range: " << argv[3] << ". Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
if (param_threads < 1) {
std::cerr << "<NUM_THREADS> has invalid value: " << argv[3] << ". Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Read number of particles
if (read_param(input_file, curr_line, param_particles) == -1) {
std::cerr << "Failed to read number of particles. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Read square size
if (read_param(input_file, curr_line, square_size) == -1) {
std::cerr << "Failed to read square size. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Read radius
if (read_param(input_file, curr_line, param_radius) == -1) {
std::cerr << "Failed to read particle radius. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Read number of steps
if (read_param(input_file, curr_line, param_steps) == -1) {
std::cerr << "Failed to read number of steps. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Update the params
params.param_threads = param_threads;
params.square_size = square_size;
params.param_particles = param_particles;
params.param_radius = param_radius;
params.param_steps = param_steps;
// Read all particles
// Allocate enough space in the vector and make sure the structs are initialized
particles.resize(param_particles);
for (int i = 0; i < param_particles; i++) {
particles[i] = Particle{};
}
if (read_particles(input_file, curr_line, particles, param_particles) == -1) {
std::cerr << "Failed to read particles info. Aborting..." << std::endl;
exit(EXIT_FAILURE);
}
// Print everything out to stdout neatly to check if everything is read
std::cout << "Number of particles: " << param_particles << std::endl;
std::cout << "Square size: " << square_size << std::endl;
std::cout << "Radius: " << param_radius << std::endl;
std::cout << "Number of steps: " << param_steps << std::endl;
}
int read_particles(std::ifstream &input_file, std::string &line, std::vector<Particle> &particles,
int param_particles) {
for (int i = 0; i < param_particles; i++) {
std::getline(input_file, line);
if (input_file.fail()) {
std::cerr << "Input file failure. Aborting..." << std::endl;
return -1;
}
// 5 columns since we have 5 attributes for each particle
// Each integer (can have a sign) is separated by a space
std::istringstream iss(line);
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};
if (tokens.size() != 5) {
std::cerr << "Invalid number of columns in particle data. Aborting..." << std::endl;
return -1;
}
try {
particles[i].i = std::stoi(tokens[0]);
particles[i].loc.x = std::stod(tokens[1]);
particles[i].loc.y = std::stod(tokens[2]);
particles[i].vel.x = std::stod(tokens[3]);
particles[i].vel.y = std::stod(tokens[4]);
} catch (const std::exception &e) {
// What exception is this, print it
std::cerr << "Encountered exception with message " << e.what()
<< " while reading particle data. Aborting..." << std::endl;
return -1;
}
}
return 0;
}
int read_param(std::ifstream &input_file, std::string &line, int ¶m) {
std::getline(input_file, line);
if (input_file.fail()) {
return -1;
}
try {
param = std::stoi(line);
} catch (const std::exception &e) {
return -1;
}
return 0;
}
/*
For each particle, show on one line the following information, separated by
space:
• tu – the step in the simulation
• i – the index of the particle
• x – position of particle index i on x axis (in μm) at time tu
• y – position of particle index i on y axis (in μm) at time tu
• vx – velocity on the x axis of particle i (in μm/timeunit) at time tu
• vy – velocity on the y axis of particle i (in μm/timeunit ) at time t
Print x/y/vx/vy values with 8 decimal places and force positive double output.
*/
void print_particles(int step, std::ofstream &output_file, const std::vector<Particle> &particles) {
for (const Particle &p : particles) {
output_file << step << " " << p.i << " " << std::fixed << std::setprecision(8)
<< (p.loc.x == -0.0 ? 0.0 : p.loc.x) << " " << (p.loc.y == -0.0 ? 0.0 : p.loc.y) << " "
<< (p.vel.x == -0.0 ? 0.0 : p.vel.x) << " " << (p.vel.y == -0.0 ? 0.0 : p.vel.y) << std::endl;
}
}