Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
DefectiveCrystalAuxIO.cpp
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_DefectiveCrystalAuxIO_cpp_
9#define model_DefectiveCrystalAuxIO_cpp_
10
12
13namespace model
14{
15
16 template<int dim>
18 /* init */ DefectiveCrystalBaseIO(folderName,"ddAux")
19 {
20
21 }
22
23template<int dim>
25{
26 meshNodes().clear();
28 quadraturePoints().clear();
29}
30
31 template<int dim>
33 {
34 for(const auto& patch : pgp.patches())
35 {
36 periodicGlidePlanePatches().emplace_back(*patch.second.lock());
37 }
38 }
40 template<int dim>
41 const std::vector<MeshNodeIO<dim>>& DefectiveCrystalAuxIO<dim>::meshNodes() const
42 {
43 return *this;
44 }
46 template<int dim>
47 std::vector<MeshNodeIO<dim>>& DefectiveCrystalAuxIO<dim>::meshNodes()
48 {
49 return *this;
50 }
52// template<int dim>
53// const std::vector<GlidePlaneIO<dim>>& DefectiveCrystalAuxIO<dim>::glidePlanes() const
54// {
55// return *this;
56// }
57//
58// template<int dim>
59// std::vector<GlidePlaneIO<dim>>& DefectiveCrystalAuxIO<dim>::glidePlanes()
60// {
61// return *this;
62// }
63
64 template<int dim>
65 const std::vector<PeriodicPlanePatchIO<dim>>& DefectiveCrystalAuxIO<dim>::periodicGlidePlanePatches() const
66 {
67 return *this;
68 }
69
70 template<int dim>
71 std::vector<PeriodicPlanePatchIO<dim>>& DefectiveCrystalAuxIO<dim>::periodicGlidePlanePatches()
72 {
73 return *this;
74 }
75
76 template<int dim>
77 const std::vector<DislocationQuadraturePointIO<dim>>& DefectiveCrystalAuxIO<dim>::quadraturePoints() const
78 {
79 return *this;
80 }
81
82 template<int dim>
83 std::vector<DislocationQuadraturePointIO<dim>>& DefectiveCrystalAuxIO<dim>::quadraturePoints()
84 {
85 return *this;
86 }
87
88 template<int dim>
89 void DefectiveCrystalAuxIO<dim>::write(const size_t& runID,const bool& outputBinary)
90 {
91 if(outputBinary)
92 {
93 writeBin(runID);
94 }
95 else
96 {
97 writeTxt(runID);
98 }
99 }
100
101 template<int dim>
102 void DefectiveCrystalAuxIO<dim>::writeTxt(const long int& runID)
103 {
104 const auto t0=std::chrono::system_clock::now();
105 const std::string filename(this->getTxtFilename(runID));
106 std::ofstream file(filename.c_str(), std::ios::out | std::ios::binary);
107 if(file.is_open())
108 {
109 std::cout<<"Writing "<<filename<<std::flush;
110 // Write header
111 file<<meshNodes().size()<<"\n";
112 file<<quadraturePoints().size()<<"\n";
113// file<<glidePlanes().size()<<"\n";
114 file<<periodicGlidePlanePatches().size()<<"\n";
115// file<<periodicLoopNodes().size()<<"\n";
116// file<<periodicLoopLinks().size()<<"\n";
117
118 // Write body
119 for(const auto& mPoint : meshNodes())
120 {
121 file<<mPoint<<"\n";
122 }
123 for(const auto& qPoint : quadraturePoints())
124 {
125 file<<qPoint<<"\n";
126 }
127// for(const auto& gpBnd : glidePlanes())
128// {
129// file<<gpBnd<<"\n";
130// }
131 for(const auto& patch : periodicGlidePlanePatches())
132 {
133 file<<patch<<"\n";
134 }
135
136 file.close();
137 std::cout<<magentaColor<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<defaultColor<<std::endl;
138 }
139 else
140 {
141 std::cout<<"CANNOT OPEN "<<filename<<std::endl;
142 assert(false && "CANNOT OPEN FILE.");
143 }
144 }
145
146 template<int dim>
147 void DefectiveCrystalAuxIO<dim>::writeBin(const size_t& runID)
148 {
149 const auto t0=std::chrono::system_clock::now();
150 const std::string filename(this->getBinFilename(runID));
151 std::ofstream file(filename.c_str(), std::ios::out | std::ios::binary);
152 if(file.is_open())
153 {
154 std::cout<<"Writing "<<filename<<std::flush;
155 // Write header
156 binWrite(file,meshNodes().size());
157 binWrite(file,quadraturePoints().size());
158// binWrite(file,glidePlanes().size());
159 binWrite(file,periodicGlidePlanePatches().size());
160
161 // Write body
162 for(const auto& mPoint : meshNodes())
163 {
164 binWrite(file,mPoint);
165 }
166 for(const auto& qPoint : quadraturePoints())
167 {
168 binWrite(file,qPoint);
169 }
170// for(const auto& gpb : glidePlanes())
171// {
172// binWrite(file,gpb);
173// }
174 for(const auto& patch : periodicGlidePlanePatches())
175 {
176 binWrite(file,patch);
177 }
178
179 file.close();
180 std::cout<<magentaColor<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<defaultColor<<std::endl;
181 }
182 else
183 {
184 std::cout<<"CANNOT OPEN "<<filename<<std::endl;
185 assert(false && "CANNOT OPEN FILE.");
186 }
187 }
188
189 template<int dim>
190 void DefectiveCrystalAuxIO<dim>::readBin(const size_t& runID)
191 {
192 clear();
193 const std::string filename(this->getBinFilename(runID));
194 std::ifstream infile (filename.c_str(), std::ios::in|std::ios::binary);
195 if(infile.is_open())
196 {
197 const auto t0=std::chrono::system_clock::now();
198 std::cout<<"reading "<<filename<<std::flush;
199
200 // Read header
201 size_t sizeMP;
202 infile.read (reinterpret_cast<char*>(&sizeMP), 1*sizeof(sizeMP));
203 size_t sizeQP;
204 infile.read (reinterpret_cast<char*>(&sizeQP), 1*sizeof(sizeQP));
205// size_t sizeGP;
206// infile.read (reinterpret_cast<char*>(&sizeGP), 1*sizeof(sizeGP));
207 size_t sizePPP;
208 infile.read (reinterpret_cast<char*>(&sizePPP), 1*sizeof(sizePPP));
209
210 // Read body
211 meshNodes().resize(sizeMP);
212 infile.read (reinterpret_cast<char*>(meshNodes().data()),meshNodes().size()*sizeof(MeshNodeIO<dim>));
213 quadraturePoints().resize(sizeQP);
214 infile.read (reinterpret_cast<char*>(quadraturePoints().data()),quadraturePoints().size()*sizeof(DislocationQuadraturePointIO<dim>));
215// glidePlanes().resize(sizeGP);
216// infile.read (reinterpret_cast<char*>(glidePlanes().data()),glidePlanes().size()*sizeof(GlidePlaneIO<dim>));
217 periodicGlidePlanePatches().resize(sizePPP);
218 infile.read (reinterpret_cast<char*>(periodicGlidePlanePatches().data()),periodicGlidePlanePatches().size()*sizeof(PeriodicPlanePatchIO<dim>));
219
220 infile.close();
221 printLog(t0);
222 }
223 else
224 {
225 std::cout<<"CANNOT OPEN "<<filename<<std::endl;
226 assert(false && "CANNOT OPEN FILE.");
227 }
228
229 }
230
231 template<int dim>
232 void DefectiveCrystalAuxIO<dim>::readTxt(const size_t& runID)
233 {
234 clear();
235 const std::string filename(this->getTxtFilename(runID));
236 std::ifstream infile (filename.c_str(), std::ios::in);
237 if(infile.is_open())
238 {
239
240 const auto t0=std::chrono::system_clock::now();
241 std::cout<<"reading "<<filename<<std::flush;
242 std::string line;
243 std::stringstream ss;
244
245 size_t sizeMP;
246 std::getline(infile, line);
247 ss<<line;
248 ss >> sizeMP;
249 ss.str("");
250 ss.clear();
251
252 size_t sizeQP;
253 std::getline(infile, line);
254 ss<<line;
255 ss >> sizeQP;
256 ss.str("");
257 ss.clear();
258
259// size_t sizeGP;
260// std::getline(infile, line);
261// ss<<line;
262// ss >> sizeGP;
263// ss.str("");
264// ss.clear();
265
266 size_t sizePPP;
267 std::getline(infile, line);
268 ss<<line;
269 ss >> sizePPP;
270 ss.str("");
271 ss.clear();
272
273// meshNodes().clear();
274 meshNodes().reserve(sizeMP);
275 for(size_t k=0;k<sizeMP;++k)
276 {
277 std::getline(infile, line);
278 ss<<line;
279 meshNodes().emplace_back(ss);
280 ss.str("");
281 ss.clear();
282 }
283
284// quadraturePoints().clear();
285 quadraturePoints().reserve(sizeQP);
286 for(size_t k=0;k<sizeQP;++k)
287 {
288 std::getline(infile, line);
289 ss<<line;
290 quadraturePoints().emplace_back(ss);
291 ss.str("");
292 ss.clear();
293 }
294
295// glidePlanes().clear();
296// glidePlanes().reserve(sizeGP);
297// for(size_t k=0;k<sizeGP;++k)
298// {
299// std::getline(infile, line);
300// ss<<line;
301// glidePlanes().emplace_back(ss);
302// ss.str("");
303// ss.clear();
304// }
305
306// periodicGlidePlanePatches().clear();
307 periodicGlidePlanePatches().reserve(sizePPP);
308 for(size_t k=0;k<sizePPP;++k)
309 {
310 std::getline(infile, line);
311 ss<<line;
312 periodicGlidePlanePatches().emplace_back(ss);
313 ss.str("");
314 ss.clear();
315 }
316
317 infile.close();
318 printLog(t0);
319 }
320 else
321 {
322 std::cout<<"CANNOT OPEN "<<filename<<std::endl;
323 assert(false && "CANNOT OPEN FILE.");
324 }
325
326 }
327
328 template<int dim>
329 void DefectiveCrystalAuxIO<dim>::printLog(const std::chrono::time_point<std::chrono::system_clock>& t0) const
330 {
331 std::cout<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<std::endl;
332 std::cout<<" "<<meshNodes().size()<<" meshNodes "<<std::endl;
333// std::cout<<" "<<glidePlanes().size()<<" glidePlanes "<<std::endl;
334 std::cout<<" "<<periodicGlidePlanePatches().size()<<" periodicPlanePatches"<<std::endl;
335 std::cout<<" "<<quadraturePoints().size()<<" quadraturePoints"<<std::endl;
336 }
337
338 template<int dim>
339 void DefectiveCrystalAuxIO<dim>::read(const size_t& runID)
340 {
341 if(isBinGood(runID))
342 {
343 readBin(runID);
344 }
345 else
346 {
347 if(isTxtGood(runID))
348 {
349 readTxt(runID);
350 }
351 else
352 {
353 std::cout<<"COULD NOT FIND INPUT FILEs "<<getBinFilename(runID)<<" or "<<getTxtFilename(runID)<<std::endl;
354 assert(0 && "COULD NOT FIND INPUT FILEs.");
355 }
356 }
357 }
358
359template struct DefectiveCrystalAuxIO<3>;
360
361}
362#endif
363
size_t InstanceCount< Derived >::count
static std::string magentaColor
static std::string defaultColor
DefectiveCrystalAuxIO(const std::string &folderName="config")
const std::vector< MeshNodeIO< dim > > & meshNodes() const
void writeTxt(const long int &runID)
void printLog(const std::chrono::time_point< std::chrono::system_clock > &t0) const
const std::vector< PeriodicPlanePatchIO< dim > > & periodicGlidePlanePatches() const
void writeBin(const size_t &runID)
void write(const size_t &runID, const bool &outputBinary)
void readBin(const size_t &runID)
const std::vector< DislocationQuadraturePointIO< dim > > & quadraturePoints() const
void addPeriodicGlidePlane(const PeriodicGlidePlane< dim > &pgp)
void readTxt(const size_t &runID)
bool isBinGood(const size_t &frameID)
static void binWrite(std::ofstream &of, const T &o)
bool isTxtGood(const size_t &frameID)
DefectiveCrystalBaseIO(const std::string &outFolderName_in, const std::string &fileNamePrefix_in, const std::string &outFolderSuffix_in="")
std::string getBinFilename(const size_t &runID)
std::string getTxtFilename(const size_t &runID)
const PatchContainerType & patches() const
char infile
REAL t0
newsubseg ss[0]
int k