-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinput_reader.cpp
220 lines (181 loc) · 5.14 KB
/
input_reader.cpp
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
/*
input_reader.cpp: parser for the input files.
See file COPYING for copyright and licensing information.
*/
#include "input_reader.h"
#include <iostream>
#include <fstream>
#include <iterator>
#include <istream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
std::vector<int>* split_string_integers(std::string& value, char separator, unsigned int* totale)
{
int LENG = value.size();
std::vector<int>* result = new std::vector<int>;
std::string::size_type pos;
*totale = 0;
while (pos = value.find_first_of(separator) != -1)
{
// std::string::size_type pos = value.find_first_of(separator);
// value = value.substr(0, pos) + "%" + value.substr(pos+1);
// value = value.substr(0, pos);
int n = atoi((const char*)value.substr(0, pos).c_str());
result->push_back(n);
*totale += n;
value = value.substr(pos+1);
}
int n = atoi((const char*)value.substr(0).c_str());
result->push_back(n);
*totale += n;
return result;
}
bool InputReader::OpenInputFile(std::string p) {
std::vector<std::vector<int>*> left_matrix;
std::vector<std::vector<int>*> right_matrix;
unsigned int rows = 0;
unsigned int cols = 0;
/* MATRICE SINISTRA */
std::ifstream left_file((p+"/left_side").c_str());
/* is there the left matrix? */
if (!left_file.is_open()) {
perror("ERROR: cannot open input file");
printf("%s\n", (p+"/left_side").c_str());
return false;
};
/* read matrix */
std::vector<int> totals_left;
while (left_file.good()) {
std::string riga ;
std::getline(left_file, riga);
unsigned int total_left = 0;
std::vector<int>* ret = split_string_integers(riga, '\t', &total_left);
totals_left.push_back(total_left);
left_matrix.push_back(ret);
};
left_file.close();
rows = left_matrix.size();
cols = left_matrix.back()->size();
/* Initialize compressed ODE matrix */
for ( unsigned int i=0; i<cols; i++ ) {
this->comp_ODE.push_back( new std::vector<int> );
}
/*
RE-parsing matrice sinistra
*/
for (unsigned int r=0; r<rows; r++) {
for (unsigned int c=0; c<cols; c++) {
switch ( left_matrix[r]->at(c) ) {
case 0:
break;
case 1:
this->comp_ODE[ c ]->push_back( totals_left[ r ] +1 );
this->comp_ODE[ c ]->push_back( -(r+1) );
for ( unsigned int cl=0; cl<cols; cl++ ) {
switch ( left_matrix[r]->at(cl) ) {
case 0:
break;
case 1:
this->comp_ODE[ c ]->push_back( cl );
break;
case 2:
this->comp_ODE[ c ]->push_back( cl );
this->comp_ODE[ c ]->push_back( cl );
break;
default:
perror("ERROR: unsupported case");
break;
}
}
break;
default:
perror("ERROR: unsupported case");
break;
}
}
}
/*
MATRICE DESTRA
La matrice destra genera direttamente i prodotti delle reazioni,
compaiono tutti con una costante positiva e possono essere aggiunti on-fly.
*/
std::ifstream right_file((p+"/right_side").c_str());
/* is there the right matrix? */
if (!right_file.is_open()) {
perror("ERROR: cannot open input file");
printf("%s\n", (p+"/right_side").c_str());
return false;
};
/* read matrix */
unsigned int total_right = 0;
while (right_file.good()) {
std::string riga ;
std::getline(right_file, riga);
std::vector<int>* ret = split_string_integers(riga, '\t', &total_right);
right_matrix.push_back(ret);
/*
ret is a single row of the stoichometric matrix.
we parse it and put the corresponding value into the compressed matrix.
*/
for ( unsigned c=0; c<cols; c++ ) {
switch ( ret->at(c) ) {
case 0:
break;
case 1:
/*
We have counted how many stoichiometric values are different from 0
in the left matrix. We use this information to fill the first
value of the compressed matrix. We make +1 because we also consider
the (unknown) kinetic constant.
*/
this->comp_ODE[ c ]->push_back( totals_left[ right_matrix.size()-1 ] +1 );
this->comp_ODE[ c ]->push_back( right_matrix.size() );
for (unsigned int cl=0; cl<cols; cl++) {
switch ( left_matrix[ right_matrix.size()-1 ]->at(cl) ) {
case 0:
break;
case 1:
this->comp_ODE[ c ]->push_back( cl );
break;
case 2:
this->comp_ODE[ c ]->push_back( cl );
this->comp_ODE[ c ]->push_back( cl );
break;
default:
perror("ERROR: unsupported case");
break;
} // end switch
} // end for
break;
case 2:
perror("ERROR: unsupported case");
break;
default:
perror("ERROR: unsupported case");
break;
} // end switch
} // end for
}; // end while
/* Terminators */
for ( unsigned int i=0; i<cols; i++)
this->comp_ODE[ i ]->push_back( 0 );
right_file.close();
return true;
}
unsigned int InputReader::GetCompODEMaxSize() {
unsigned int mass = 0;
for (unsigned int i=0; i<this->comp_ODE.size(); i++) {
if ( this->comp_ODE[i]->size() > mass )
mass = this->comp_ODE[i]->size();
}
return mass;
}
unsigned int InputReader::GetCompODESize() {
unsigned int mass = 0;
for (unsigned int i=0; i<this->comp_ODE.size(); i++) {
mass += this->comp_ODE[i]->size();
}
return mass;
}