-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProcess_RACIPE_Files.hpp
executable file
·365 lines (330 loc) · 7.85 KB
/
Process_RACIPE_Files.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
void splitstring(std::string line, std::string delim, std::vector<std::string> *l, int popflag)
{
if(popflag == 1)
{
line.pop_back();
}
int toklen, pos1 = 0, pos2;
while(1)
{
pos2 = line.find(delim, pos1);
if(pos2 == std::string::npos)
{
break;
}
toklen = pos2 - pos1;
(*l).push_back(line.substr(pos1, toklen));
pos1 = pos2 + 1;
}
(*l).push_back(line.substr(pos1, std::string::npos));
}
RegNetwork ReadTopologyFile(std::string filename1, std::string filename2, std::string filename3)
{
RegNetwork R;
R.numnodes = 0;
R.numedges = 0;
R.nodeIDs = std::vector<std::string>();
R.T = std::vector<std::vector<int>>();
R.L = std::vector<std::vector<int>>();
std::string nodeName;
int nodeID, maxnodeID = -1, numlines;
std::ifstream f(filename1);
if(!static_cast<bool>(f))
{
std::cout << "Error reading the .ids file." << "\n";
return(R);
}
while(f >> nodeName >> nodeID)
{
R.nodeIDs.push_back(nodeName);
if(nodeID > maxnodeID)
{
maxnodeID = nodeID;
}
R.numnodes += 1;
}
f.close();
if(R.numnodes != maxnodeID + 1)
{
std::cout << "Province of Alberta. Fortis et Liber." << "\n";
return(R);
}
for(int i = 0; i < R.numnodes; i++)
{
R.T.push_back(std::vector<int>());
R.L.push_back(std::vector<int>());
for(int j = 0; j < R.numnodes; j++)
{
R.T[i].push_back(0);
R.L[i].push_back(0);
}
}
f.open(filename1);
numlines = 0;
while(getline(f, nodeName))
{
numlines += 1;
}
f.close();
if(numlines != R.nodeIDs.size())
{
std::cout << "The format of the .ids file is ridonc, and frankly, completely wrong." << "\n";
return(R);
}
f.open(filename2);
if(!static_cast<bool>(f))
{
std::cout << "The T matrix file is missing." << "\n";
return(R);
}
std::string line;
std::vector<std::string> l;
int index = 0;
while(getline(f, line))
{
l.clear();
splitstring(line, "\t", &l, 0);
if(l.size() != R.numnodes)
{
std::cout << "We killed Yamamoto." << "\n";
return(R);
}
for(int i = 0; i < l.size(); i++)
{
R.T[index][i] = std::stoi(l[i]);
if(R.T[index][i] > 0)
{
R.numedges += 1;
}
}
index += 1;
}
f.close();
if(index != R.numnodes)
{
std::cout << "Par was 5." << "\n";
return(R);
}
f.open(filename3);
if(!static_cast<bool>(f))
{
std::cout << "The L matrix file is missing." << "\n";
return(R);
}
index = 0;
while(getline(f, line))
{
l.clear();
splitstring(line, "\t", &l, 0);
if(l.size() != R.numnodes)
{
std::cout << "This is the steam pipe trunk distribution venue." << "\n";
return(R);
}
for(int i = 0; i < l.size(); i++)
{
R.L[index][i] = std::stoi(l[i]);
if(R.L[index][i] > 0 && R.L[index][i] < 2*R.numnodes)
{
std::cout << "The fault, dear Brutus, is not in our stars but in ourselves..." << "\n";
return(R);
}
}
index += 1;
}
f.close();
if(index != R.numnodes)
{
std::cout << "It was mini golf, wasn't it?" << "\n";
return(R);
}
int flag = 0;
for(int i = 0; i < R.numnodes; i++)
{
for(int j = 0; j < R.numnodes; j++)
{
if(R.T[i][j] == 0 && R.L[i][j] > 0)
{
flag = 1;
}
if(R.T[i][j] > 0 && R.L[i][j] == 0)
{
flag = 1;
}
}
}
if(flag == 1)
{
std::cout << "It's gonna be Ritchie." << "\n";
return(R);
}
if(R.numnodes == R.nodeIDs.size() && R.T.size() == R.numnodes && R.L.size() == R.numnodes)
{
R.isvalid = true;
}
return(R);
}
void Write_Network(RegNetwork R, std::ofstream *f)
{
(*f) << "Source\tTarget\tType\n";
for(int i = 0; i < R.T.size(); i++)
{
for(int j = 0; j < R.T.size(); j++)
{
if(R.T[i][j] > 0)
{
(*f) << R.nodeIDs[i] << "\t" << R.nodeIDs[j] << "\t" << R.T[i][j] << "\n";
}
}
}
return;
}
void Print_Network(RegNetwork R)
{
std::cout << "Source\tTarget\tType\n";
for(int i = 0; i < R.T.size(); i++)
{
for(int j = 0; j < R.T.size(); j++)
{
if(R.T[i][j] > 0)
{
std::cout << R.nodeIDs[i] << "\t" << R.nodeIDs[j] << "\t" << R.T[i][j] << "\n";
}
}
}
return;
}
std::vector<std::vector<double>> Read_Parameters(RegNetwork R, std::vector<int> &numstablestates, std::string filename)
{
numstablestates.clear();
std::vector<std::vector<double>> P;
std::ifstream f(filename);
if(!static_cast<bool>(f))
{
std::cout << "The parameters file has probably been dumped into the Eagleton reservoir." << "\n";
return(P);
}
std::string line;
std::vector<std::string> l;
int count = 0;
while(getline(f, line))
{
l.clear();
splitstring(line, "\t", &l, 0);
if(l.size() != 2*R.numnodes + 3*R.numedges + 2)
{
std::cout << "The parameters file is utter malarkey." << "\n";
for(int i = 0; i < P.size(); i++)
{
P[i].clear();
}
P.clear();
return(P);
}
numstablestates.push_back(std::stoi(l[1]));
P.push_back(std::vector<double>());
for(int i = 2; i < l.size(); i++)
{
P[count].push_back(std::stod(l[i]));
}
if(P[count].size() != (2*R.numnodes + 3*R.numedges))
{
std::cout << "This little restaurant will put McDonald's out of business..." << "\n";
}
count += 1;
}
f.close();
return(P);
}
std::vector<std::vector<double>> Read_Initial_Conditions(RegNetwork R, int index, std::string filename)
{
std::vector<std::vector<double>> I;
std::ifstream f(filename);
if(!static_cast<bool>(f))
{
std::cout << "Couldn't find the initial conditions file. Probably some Eagletonian prick took it." << "\n";
return(I);
}
std::string line;
std::vector<std::string> l;
int count = 0, numcond = 0, point = 0;
while(getline(f, line))
{
if(count != index)
{
count += 1;
continue;
}
l.clear();
splitstring(line, " ", &l, 1);
if(l.size() % R.numnodes != 0)
{
std::cout << "This initial conditions file was put together in Eagleton." << "\n";
for(int i = 0; i < I.size(); i++)
{
I[i].clear();
}
I.clear();
return(I);
}
point = 0;
while(point < l.size())
{
I.push_back(std::vector<double>());
for(int i = 0; i < R.numnodes; i++)
{
I[numcond].push_back(std::stod(l[point]));
point += 1;
}
numcond += 1;
}
count += 1;
break;
}
f.close();
return(I);
}
std::vector<std::vector<double>> Read_Initial_Conditions_Alt(RegNetwork R, std::string filename)
{
std::vector<std::vector<double>> I;
std::ifstream f(filename);
if(!static_cast<bool>(f))
{
std::cout << "Couldn't find the initial conditions file. Probably some Eagletonian prick took it." << "\n";
return(I);
}
std::string line;
std::vector<std::string> l;
int numcond = 0, point = 0, newcond = 0, linecount = 0;
while(getline(f, line))
{
l.clear();
splitstring(line, " ", &l, 1);
if(l.size() % R.numnodes != 0)
{
std::cout << "This initial conditions file was put together in Eagleton." << "\n";
for(int i = 0; i < I.size(); i++)
{
I[i].clear();
}
I.clear();
return(I);
}
point = 0;
newcond = 0;
while(point < l.size())
{
I.push_back(std::vector<double>());
for(int i = 0; i < R.numnodes; i++)
{
I[numcond].push_back(std::stod(l[point]));
point += 1;
}
numcond += 1;
newcond += 1;
}
linecount += 1;
}
f.close();
return(I);
}