Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
IDreader.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_IDREADER_H_
9#define model_IDREADER_H_
10
11#include <iostream>
12#include <fstream>
13#include <string>
14#include <sstream>
15#include <time.h> // clock()
16#include <map>
17#include <utility>
18#include <chrono>
19#include <array>
20//#include <Eigen/Dense>
21#include <BinaryFileReader.h>
22
23namespace model
24{
25 template <int keySize>
27 {
28 static_assert(keySize>0,"keySize must be >0");
29 typedef long long int SingleKeyType;
30 typedef std::array<SingleKeyType, keySize> KeyType;
31
32 static SingleKeyType& keyElement(KeyType& key,const size_t& k)
33 {
34 return key[k];
35 }
36 };
37
38 template <>
40 {
41 typedef long long int SingleKeyType;
43
44 static SingleKeyType& keyElement(KeyType& key,const size_t&)
45 {
46 return key;
47 }
48 };
49
50
53 template <int keySize, int valueSize, typename T>
54 class IDreader : public std::map<typename IDreaderKeySelector<keySize>::KeyType, std::array<T, valueSize> >
55 {
56 static_assert(valueSize>=0,"valueSize must be >0");
57
58 typedef typename IDreaderKeySelector<keySize>::SingleKeyType SingleKeyType;
59 typedef typename IDreaderKeySelector<keySize>::KeyType KeyType;
60 typedef std::array<T, valueSize> ValueType;
61 typedef std::pair<KeyType, ValueType > BinType;
62
63 long long int currentFrame;
64 std::map<std::string,size_t> labelsMap;
65
66 /**********************************************************************/
67 bool readTXT(const std::string& filename)
68 {
69
70 std::ifstream ifs ( filename.c_str() , std::ifstream::in );
71
72 bool success(false);
73
74 if (ifs.is_open())
75 {
76 std::cout<<"Reading: "<<filename;
77 double t0(clock());
78 std::string line;
79
80
81// size_t row = 0;
82 while (std::getline(ifs, line))
83 {
84 std::stringstream ss(line);
85
86 int col=0;
87 KeyType key;
88 ValueType value;
89 T temp;
90
91 while (ss >> temp)
92 {
93 if(col<keySize)
94 {
95 if(temp!=std::round(temp))
96 {
97 throw std::runtime_error("IDreader: key "+std::to_string(temp)+" must be an integer.");
98 }
99 IDreaderKeySelector<keySize>::keyElement(key,col)=static_cast<SingleKeyType>(temp);
100 }
101 else
102 {
103 value[col-keySize]=temp;
104 }
105 col++;
106 }
107
108 this->emplace(key,value);
109
110// row++;
111 }
112
113
114 ifs.close();
115
116 success=true;
117
118 std::cout<<" ("<<this->size()<<" entries) ["<<(clock()-t0)/CLOCKS_PER_SEC<<" sec]"<<std::endl;
119 }
120 else
121 {
122 std::cout<<"Unable to open:"<<filename<<std::endl;
123 }
124
125 return success;
126 }
127
128
129
130 /**********************************************************************/
131 bool readBIN(const std::string& filename)
132 {
134 //bool success(false);
135 std::cout<<"Reading: "<<filename<<std::flush<<" (";
136 const auto t0=std::chrono::system_clock::now();
137 BinaryFileReader<BinType> rE(filename);
138 for (const auto& pair : rE)
139 {
140 // this->insert(std::make_pair(rE[k].first,rE[k].second));
141 const bool success=this->emplace(pair.first,pair.second).second;
142 if(!success)
143 {
144 throw std::runtime_error("IDreader: cannot insert key="+std::to_string(pair.first)+".");
145 }
146 }
147 std::cout<<this->size()<<"elements in "<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec)"<<std::endl;
148 return rE.success();
149 }
150
151 public:
152 const std::string fileNamePrefix;
153
154 /**********************************************************************/
155// IDreader () :
156// /* init list */ currentFrame(-1), folderSuffix(std::string(""))
157// // /* init list */ success(false)
158// {/*! Constructor initializes currentFrame to -1 so that the statement
159// * frameN!=currentFrame will initially return false
160// */
161// }
162
163 /***Overloaded constructor***********************************************/
164 IDreader (const std::string& fileNamePrefix_in) :
165 /* init list */ currentFrame(-1),
166 /* init list */ fileNamePrefix(fileNamePrefix_in)
167 // /* init list */ success(false)
168 {
171 }
172
173 /**********************************************************************/
174 void readLabelsFile(const std::string& labelsFileName)
175 {
176 std::ifstream labelsFile(labelsFileName.c_str(), std::ifstream::in);
177 if (labelsFile.is_open())
178 {
179 std::cout<<"reading labels file: "<<labelsFileName;
180 double t0(clock());
181 std::string line;
182
183 std::getline(labelsFile, line); // labels of ID
184
185 size_t row = 0;
186 while (std::getline(labelsFile, line))
187 {
188
189 const bool success=labelsMap.emplace(line,row).second;
190 if(!success)
191 {
192 std::cout<<"Unable to insert label "<<line<<std::endl;
193 }
194 row++;
195 }
196
197
198 labelsFile.close();
199
200
201 std::cout<<" ("<<labelsMap.size()<<" labels) ["<<(clock()-t0)/CLOCKS_PER_SEC<<" sec]"<<std::endl;
202 }
203 else
204 {
205 std::cout<<"Unable to read labels file:"<<labelsFileName<<std::endl;
206 }
207 }
208
209 /**********************************************************************/
210 const T& operator()(const KeyType& key,const std::string& label)
211 {
212 const auto rowIter(this->find(key));
213 assert(rowIter!=this->end() && "ID not found.");
214 const auto colIter(labelsMap.find(label));
215 assert(colIter!=labelsMap.end() && "label not found.");
216 return rowIter->second.operator[](colIter->second);
217 }
218
219 /**********************************************************************/
220 const T& first(const std::string& label)
221 {
222 const auto rowIter(this->begin());
223 const auto colIter(labelsMap.find(label));
224 assert(colIter!=labelsMap.end() && "label not found.");
225 return rowIter->second.operator[](colIter->second);
226 }
227
228 /**********************************************************************/
229 const T& last(const std::string& label)
230 {
231 const auto rowIter(this->rbegin());
232 const auto colIter(labelsMap.find(label));
233 assert(colIter!=labelsMap.end() && "label not found.");
234 return rowIter->second.operator[](colIter->second);
235 }
236
237 /**********************************************************************/
238 std::string getFilename(const long long int& frameN, const bool& useTXT) const
239 {
240// std::stringstream filename;
241 if(useTXT)
242 {
243 return fileNamePrefix+"_"+std::to_string(frameN)+".txt";
244// filename << c <<folderSuffix<< "/" << c << "_" << frameN << ".txt";
245 }
246 else
247 {
248 return fileNamePrefix+"_"+std::to_string(frameN)+".bin";
249// filename << c <<folderSuffix<< "/" << c << "_" << frameN << ".bin";
250 }
251// return filename.str();
252 }
253
254 /**********************************************************************/
255 bool isGood(const long long int& frameN, const bool& useTXT) const
256 {
259 std::ifstream ifs ( getFilename(frameN,useTXT).c_str() , std::ifstream::in );
260 return ifs.good();
261 }
262
263 /**********************************************************************/
264 bool read(const long long int& frameN, const bool& useTXT)
265 {
270 assert(frameN>=0 && "frameN MUST BE >= 0");
271 bool success(false);
272 if (frameN!=currentFrame || this->empty())
273 {
274 currentFrame=frameN;
275
276 // clear the content of the map
277 this->clear();
278
279 if (useTXT)
280 {
281 success=readTXT(getFilename(frameN,true));
282 }
283 else
284 {
285 assert(0 && "NOT IMEPLEMETNTED YET");
286 //success=readBIN(getFilename(frameN,false));
287 }
288 }
289 else
290 {
291 success=true;
292 std::cout<<getFilename(frameN,useTXT)<<" already read."<<std::endl;
293 }
294 return success;
295
296 }
297
298
299 };
300
301 /******************************************************************/
302 /******************************************************************/
303} /* namespace model */
304#endif
A class template that reads generic binary files containing arrays of DataType elements.
const bool & success() const
void readLabelsFile(const std::string &labelsFileName)
Definition IDreader.h:174
bool read(const long long int &frameN, const bool &useTXT)
Definition IDreader.h:264
IDreader(const std::string &fileNamePrefix_in)
Definition IDreader.h:164
const T & first(const std::string &label)
Definition IDreader.h:220
std::string getFilename(const long long int &frameN, const bool &useTXT) const
Definition IDreader.h:238
const T & operator()(const KeyType &key, const std::string &label)
Definition IDreader.h:210
bool isGood(const long long int &frameN, const bool &useTXT) const
Definition IDreader.h:255
const T & last(const std::string &label)
Definition IDreader.h:229
size_t InstanceCount< Derived >::count
static SingleKeyType & keyElement(KeyType &key, const size_t &)
Definition IDreader.h:44
static SingleKeyType & keyElement(KeyType &key, const size_t &k)
Definition IDreader.h:32
std::array< SingleKeyType, keySize > KeyType
Definition IDreader.h:30
long long int SingleKeyType
Definition IDreader.h:29
int end[2]
REAL t0
newsubseg ss[0]
int k
enum insertvertexresult success
newbad key
vertex temp