MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConvertP4Files.cpp
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
27 
28 #include <string>
29 #include <iomanip>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <vector>
37 #include "Math/ExtendedMath.h"
38 #include "Math/Helpers.h"
39 #include "Math/Vector.h"
40 
41 class CFile {
42 
43 public:
44 
46  CFile(std::string name) : name_(name){
47 
48  //set file names
49  std::string p4pName(name+".p4p");
50  std::string p4cName(name+".p4c");
51  std::string p4wName(name+".p4w");
52 
53  //open input file streams
54  p4p.open(p4pName.c_str(), std::fstream::in);
55  p4c.open(p4cName.c_str(), std::fstream::in);
56  p4w.open(p4wName.c_str(), std::fstream::in);
57 
58  if (p4p.fail() || p4c.fail() || p4w.fail())
59  {
60  if (p4p.fail())
61  std::cerr << "ERROR: Input file " << p4pName << " not found" << std::endl;
62  if (p4c.fail())
63  std::cerr << "ERROR: Input file " << p4cName << " not found" << std::endl;
64  if (p4w.fail())
65  std::cerr << "ERROR: Input file " << p4wName << " not found" << std::endl;
66  p4p.close();
67  p4c.close();
68  p4w.close();
69  exit(-1);
70  } else {
71  std::cout << "Files opened: " << p4pName << " and " << p4cName << " and " << p4wName << std::endl;
72  }
73  }
74 
76  ~CFile() {
77  p4p.close();
78  p4c.close();
79  p4w.close();
80  std::cout << "Files closed: " << name_ << ".p4p/p4w/p4c" << std::endl;
81  }
82 
83  void copy(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, double timeMin, double timeMax, unsigned int periodic) {
84  std::cout << "copy(..)" << std::endl;
85  xmin_ = xmin;
86  xmax_ = xmax;
87  ymin_ = ymin;
88  ymax_ = ymax;
89  zmin_ = zmin;
90  zmax_ = zmax;
91  periodic_=periodic;
92  writeP4P(timeMin,timeMax);
93  }
94 
95  void writeP4P(double timeMin, double timeMax) {
96  std::cout << "writeP4P(..)" << std::endl;
97 
98  unsigned int N;
99  std::string line;
100  std::fstream data;
101  std::fstream fstat;
102  double time;
103  double ID, GROUP, VOLUME, MASS, VX, VY, VZ, Angular_Velocity_X, Angular_Velocity_Y, Angular_Velocity_Z;
104 
105  unsigned int counter = 0;
106  while (p4p.good())
107  {
108  //read header
109  std::string dummy;
110  p4p >> dummy >> dummy;
111  p4p >> time >> N;
112  getline(p4p,line);
113  getline(p4p,line);
114 
115  if (time>timeMax)
116  {
117  std::cout << "reading p4p (t " << time << " N " << N << "): timeMax reached; terminating" << std::endl;
118  break;
119  }
120  else if (time<timeMin)
121  {
122  std::cout << "reading p4p (t " << time << " N " << N << "): below timeMin; skipped" << std::endl;
123  for (unsigned int i=0; i<N; i++)
124  getline(p4p,line);
125  continue;
126  }
127 
128  std::cout << "reading p4p (t " << time << " N " << N << " timestep #" << counter << ")" << std::endl;
129 
130  //open data/fstat file
131  std::string dataName = getName("data", counter);
132  data.open(dataName.c_str(), std::fstream::out);
133  std::string fstatName = getName("fstat", counter);
134  fstat.open(fstatName.c_str(), std::fstream::out);
135  std::cout << "Files opened: " << dataName << " and " << fstatName << std::endl;
136 
137  //write data file
138  data<< N << " "
139  << time << " "
140  << xmin_ << " "
141  << ymin_ << " "
142  << zmin_ << " "
143  << xmax_ << " "
144  << ymax_ << " "
145  << zmax_ << std::endl;
146 
147  position_.resize(N*1.1);
148  id_.resize(N*1.1);
149  for (unsigned int i=0; i<N; i++) {
150  p4p >> ID;
151  if (position_.size()<ID)
152  {
153  position_.resize(ID*1.1);
154  id_.resize(ID*1.1);
155  }
156  Vec3D& P = position_[ID-1];
157  id_[ID-1]=i;
158  p4p >> GROUP
159  >> VOLUME
160  >> MASS
161  >> P.X
162  >> P.Y
163  >> P.Z
164  >> VX
165  >> VY
166  >> VZ
167  >> Angular_Velocity_X
168  >> Angular_Velocity_Y
169  >> Angular_Velocity_Z;
170  data << P.X << " "
171  << P.Y << " "
172  << P.Z << " "
173  << VX << " "
174  << VY << " "
175  << VZ << " "
176  << pow(6.0/constants::pi*VOLUME,1.0/3.0)/2.0 << " "
177  << 0 << " "
178  << 0 << " "
179  << 0 << " "
180  << Angular_Velocity_X << " "
181  << Angular_Velocity_Y << " "
182  << Angular_Velocity_Z << " "
183  << 0 << std::endl;
184  //std::cout << ID << " ";
185  }
186  std::cout << "written " << dataName << std::endl;
187 
188  writeP4C(fstat, time);
189  std::cout << "written " << fstatName << std::endl;
190 
191  //close data file
192  data.close();
193  fstat.close();
194  ++counter;
195 
196  p4p >> std::ws; //to make sure that p4p.good returns false at end of file
197  }
198  writeRestart(time, MASS/VOLUME);
199  }
200 
201  void writeP4C(std::fstream& fstat, double timeData) {
202  unsigned int N;
203  std::string line;
204  std::string dummy;
205  double time;
206  double P1, P2, delta, deltat, fn, ft;
207  Vec3D C1, F, n, t;
208 
209  while (p4c.good()) {
210  //read header
211  p4c >> dummy >> dummy;
212  p4c >> time >> N;
213  getline(p4c,line);
214  getline(p4c,line);
215 
216  if (time<timeData*0.999999)
217  {
218  std::cout << "reading p4c (t " << time << " N " << N << "): below timeMin; skipped" << std::endl;
219  for (unsigned int i=0; i<N; i++)
220  getline(p4c,line);
221  continue;
222  }
223 
224  std::cout << "reading p4c (t " << time << " N " << N << ")" << std::endl;
225 
226  //write fstat file
227  fstat << "# " << time << " " << N << std::endl;
228  fstat << "# " << std::endl;
229  fstat << "# " << std::endl;
230  //double P1, P2, CX, CY, CZ, FX, FY, FZ;
231  //time, i, j, x, y, z, delta, deltat, fn, ft, nx, ny, nz, tx, ty, tz
232 
233  for (unsigned int i=0; i<N; i++) {
234  p4c >> P1
235  >> P2
236  >> C1
237  >> F;
238  //std::cout << i << "|" << P1 << "|" << P2 << "|" << C1 << "|" << F << std::endl;
239  Vec3D C2=C1;
240  Mdouble pos1, pos2;
241  Mdouble *c1, *c2;
242  //std::cout << "C " << C1 << std::endl;
243  if (periodic_)
244  {
245  if (periodic_==1)
246  {
247  pos1 = position_[P1 - 1].X;
248  pos2 = position_[P2 - 1].X;
249  c1 = &C1.X;
250  c2 = &C2.X;
251  }
252  else if (periodic_==2)
253  {
254  pos1 = position_[P1 - 1].Y;
255  pos2 = position_[P2 - 1].Y;
256  c1 = &C1.Y;
257  c2 = &C2.Y;
258  }
259  else //if (periodic_==3)
260  {
261  pos1 = position_[P1 - 1].Z;
262  pos2 = position_[P2 - 1].Z;
263  c1 = &C1.Z;
264  c2 = &C2.Z;
265  }
266  if (pos1 < (xmax_-xmin_)/3.0 && pos2 > 2.0*(xmax_-xmin_)/3.0)
267  {
268  if (*c1 < (xmax_ - xmin_) / 3.0)
269  *c2 += xmax_ - xmin_;
270  else if (*c1 > 2.0*(xmax_ - xmin_) / 3.0)
271  *c1 -= xmax_ - xmin_;
272  }
273  else if (pos1 < (xmax_-xmin_)/3.0 && pos2 > 2.0*(xmax_-xmin_)/3.0)
274  {
275  if (*c1 < (xmax_ - xmin_) / 3.0)
276  *c1 += xmax_ - xmin_;
277  else if (*c1 > 2.0*(xmax_ - xmin_) / 3.0)
278  *c2 -= xmax_ - xmin_;
279  }
280  }
281  //std::cout << "C1 " << C1 << std::endl;
282  //std::cout << "C2 " << C2 << std::endl;
283  //std::cout << "P0 " << position_[P1-1] << std::endl;
284  //std::cout << "P1 " << position_[P2-1] << std::endl;
285  delta=0.0;
286  deltat=0.0;
287  ft=0.0;
288  t.setZero();
289  fn=F.getLength();
290  n=F/fn;
291 
292  fstat << time << " "
293  << id_[P1-1] << " "
294  << id_[P2-1] << " "
295  << C1 << " "
296  << delta << " "
297  << deltat << " "
298  << fn << " "
299  << ft << " "
300  << n << " "
301  << t << std::endl;
302  fstat << time << " "
303  << id_[P2-1] << " "
304  << id_[P1-1] << " "
305  << C2 << " "
306  << delta << " "
307  << deltat << " "
308  << fn << " "
309  << ft << " "
310  << -n << " "
311  << t << std::endl;
312  }
313  writeP4W(fstat, timeData);
314  break;
315  }
316  }
317 
318  void writeP4W(std::fstream& fstat, double timeData)
319  {
320  unsigned int N;
321  std::string line;
322  std::string dummy;
323  double time;
324  double P1, delta, deltat, fn, ft;
325  Vec3D C, F, n, t;
326 
327  while (p4w.good()) {
328  //read header
329  p4w >> dummy >> dummy;
330  p4w >> time >> N;
331  getline(p4w,line);
332  getline(p4w,line);
333 
334  if (time<timeData*0.999999)
335  {
336  std::cout << "reading p4w (t " << time << " N " << N << "): below timeMin; skipped" << std::endl;
337  for (unsigned int i=0; i<N; i++)
338  getline(p4w,line);
339  continue;
340  }
341 
342  std::cout << "reading p4w (t " << time << " N " << N << ")" << std::endl;
343 
344  for (unsigned int i=0; i<N; i++) {
345  p4w >> P1
346  >> C
347  >> F;
348  delta=0.0;
349  deltat=0.0;
350  ft=0.0;
351  t.setZero();
352  fn=F.getLength();
353  n=F/fn;
354  fstat << time << " "
355  << id_[P1-1] << " "
356  << -1 << " "
357  << C << " "
358  << delta << " "
359  << deltat << " "
360  << fn << " "
361  << ft << " "
362  << n << " "
363  << t << std::endl;
364  }
365  break;
366  }
367  }
368 
369  void writeRestart(double timeData, double density)
370  {
371  std::cout << "restart.0000 " << timeData << std::endl;
372  //open data/fstat file
373  std::fstream restart;
374  restart.open(name_ + ".restart", std::fstream::out);
375  restart <<
376  "restart_version 1.0 name " << name_ << "\n"
377  "dataFile name " << name_ << ".data fileType MULTIPLE_FILES_PADDED saveCount 1 counter 1 nextSavedTimeStep 0\n"
378  "fStatFile name " << name_ << ".fstat fileType MULTIPLE_FILES_PADDED saveCount 1 counter 1 nextSavedTimeStep 0\n"
379  "eneFile name " << name_ << ".ene fileType ONE_FILE saveCount 1 counter 302 nextSavedTimeStep 0\n"
380  "restartFile name " << name_ << ".restart fileType ONE_FILE saveCount 1 counter 1 nextSavedTimeStep 0\n"
381  "statFile name " << name_ << ".stat fileType ONE_FILE saveCount 1 counter 0 nextSavedTimeStep 0\n"
382  //"dataFile fileType MULTIPLE_FILES_PADDED saveCount 1 counter 1 nextSavedTimeStep 0\n"
383  //"fStatFile fileType MULTIPLE_FILES_PADDED saveCount 1 counter 1 nextSavedTimeStep 0\n"
384  //"eneFile fileType ONE_FILE saveCount 1 counter 302 nextSavedTimeStep 0\n"
385  //"restartFile fileType ONE_FILE saveCount 1 counter 1 nextSavedTimeStep 0\n"
386  //"statFile fileType ONE_FILE saveCount 1 counter 0 nextSavedTimeStep 0\n"
387  "xMin " << xmin_<< " xMax " << xmax_<< " yMin " << ymin_<< " yMax " << ymax_<< " zMin " << zmin_<< " zMax " << zmax_<< "\n"
388  "timeStep 0 time " << timeData << " ntimeSteps 0 timeMax " << timeData << "\n"
389  "systemDimensions 3 particleDimensions 3 gravity 0 0 -9.8\n"
390  "Species 1\n"
391  "LinearViscoelasticSpecies id 0 density " << density << " stiffness 0 dissipation 0\n"
392  "Walls 0\n";
393  if (!periodic_)
394  restart << "Boundaries 0\n";
395  else
396  {
397  restart << "Boundaries 1\n";
398  if (periodic_==1)
399  restart << "PeriodicBoundary id 0 normal 1 0 0 scaleFactor 1 distanceLeft " << xmin_ << " distanceRight " << xmax_ << " shift " << xmax_-xmin_ << " 0 0\n";
400  else if (periodic_==2)
401  restart << "PeriodicBoundary id 0 normal 0 1 0 scaleFactor 1 distanceLeft " << ymin_ << " distanceRight " << ymax_ << " shift " << ymax_-ymin_ << " 0 0\n";
402  else
403  restart << "PeriodicBoundary id 0 normal 0 0 1 scaleFactor 1 distanceLeft " << zmin_ << " distanceRight " << zmax_ << " shift " << zmax_-zmin_ << " 0 0\n";
404  }
405  restart <<
406  "Particles 0\n"
407  "BaseParticle id 0 indSpecies 0 position 0 0 0 orientation 0 0 0 0 velocity 0 0 0 angularVelocity 0 0 0 0 force 0 0 0 torque 0 0 0 radius 0.5 invMass 1 invInertia 10\n"
408  "Interactions 0\n";
409  restart.close();
410  }
411 
412 
413  std::string getName(std::string type, unsigned int counter)
414  {
415  //set dataName
416  std::stringstream name("");
417  name << name_ << "." << type << ".";
418  if (counter < 1000)
419  name << "0";
420  if (counter < 100)
421  name << "0";
422  if (counter < 10)
423  name << "0";
424  name << counter;
425  return name.str();
426  }
427 
428 
429 private:
431  std::string name_;
432 
434  std::fstream p4p;
435  std::fstream p4c;
436  std::fstream p4w;
437  double xmin_, xmax_;
438  double ymin_, ymax_;
439  double zmin_, zmax_;
440  std::vector<Vec3D> position_;
441  std::vector<unsigned int> id_;
442  unsigned int periodic_; // 0: no periodicity, 1-3: periodic in x-z
443 };
444 
445 int main(int argc, char *argv[])
446 {
447  if (argc<8)
448  {
449  if (argc < 2)
450  {
451  std::cout << "no arguments given, so a test file is converted; to convert your own p4 files, use:" << std::endl;
452  std::cout << "convertP4Files.cpp name xmin xmax ymin ymax zmin zmax [timeMin [timeMax [periodic]]]" << std::endl;
453 
454  helpers::writeToFile("test.p4p",
455  "TIMESTEP PARTICLES\n"
456  "0 2\n"
457  "ID GROUP VOLUME MASS PX PY PZ VX VY VZ Angular_Velocity_X Angular_Velocity_Y Angular_Velocity_Z\n"
458  "1 1 0.52359877559 1 0.5 0.5 0.5 1 0 0 0 0 1\n"
459  "3 1 0.52359877559 1 3.5 0.5 0.5 0 0 0 0 0 0\n"
460  "TIMESTEP PARTICLES\n"
461  "2 2\n"
462  "ID GROUP VOLUME MASS PX PY PZ VX VY VZ Angular_Velocity_X Angular_Velocity_Y Angular_Velocity_Z\n"
463  "1 1 0.52359877559 1 0.5 0.5 0.5 1 0 0 0 0 1\n"
464  "3 1 0.52359877559 1 3.5 0.5 0.5 0 0 0 0 0 0\n"
465  );
466 
467  helpers::writeToFile("test.p4c",
468  "TIMESTEP CONTACTS\n"
469  "0 1\n"
470  "P1 P2 CX CY CZ FX FY FZ\n"
471  "1 3 0 0.5 0.5 1 1 0\n"
472  "TIMESTEP CONTACTS\n"
473  "2 1\n"
474  "P1 P2 CX CY CZ FX FY FZ\n"
475  "1 3 4 0.5 0.5 1 1 0\n"
476  );
477 
478  helpers::writeToFile("test.p4w",
479  "TIMESTEP CONTACTS\n"
480  "0 2\n"
481  "P1 CX CY CZ FX FY FZ\n"
482  "1 0.5 0.5 0 0 0 1\n"
483  "3 3.5 0.5 0 0 0 1\n"
484  "TIMESTEP CONTACTS\n"
485  "2 2\n"
486  "P1 CX CY CZ FX FY FZ\n"
487  "1 0.5 0.5 0 0 0 1\n"
488  "3 3.5 0.5 0 0 0 1\n"
489  );
490 
491  CFile files("test");
492  files.copy(0,4,0,1,0,1,-1,1e20,1);
493  }
494  else
495  {
496  std::cerr << "convertP4Files.cpp problem_name xmin xmax ymin ymax zmin zmax [timeMin [timeMax [periodic]]]" << std::endl;
497  return -1;
498  }
499  }
500  else
501  {
502  std::string name(argv[1]);
503  std::cout << "converting " << name << std::endl;
504 
505  double timeMin = -1;
506  if (argc>8) {
507  timeMin = atof(argv[8]);
508  std::cout << "timeMin " << timeMin << std::endl;
509  }
510 
511  double timeMax = 1e20;
512  if (argc>9) {
513  timeMax = atof(argv[9]);
514  std::cout << "timeMax " << timeMax << std::endl;
515  }
516 
517  unsigned int periodic = 0;
518  if (argc>10) {
519  periodic = atoi(argv[10]);
520  std::cout << "periodic " << periodic << std::endl;
521  }
522 
523  CFile files(name);
524  files.copy(atof(argv[2]),atof(argv[3]),atof(argv[4]),atof(argv[5]),atof(argv[6]),atof(argv[7]),timeMin,timeMax,periodic);
525 
526  std::cout << "finished converting " << name << std::endl;
527  }
528 }
529 //./ConvertP4Files ~/Documents/Work/Carlos/silo -0.075 0.075 0 0.04 -0.00465405 0.231798 -1 1e20 2
530 //~/MercuryAll/Trunk/Build/XBalls/xballs -f silo.data.0000 -format 14 -s 4.8 -solidf -v0 -sort -moh 130 -w 320
~CFile()
Destructor.
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: Helpers.cc:411
Mdouble X
the vector components
Definition: Vector.h:52
unsigned int periodic_
void writeP4P(double timeMin, double timeMax)
double ymin_
void writeP4W(std::fstream &fstat, double timeData)
double zmin_
void setZero()
Sets all elements to zero.
Definition: Vector.cc:52
void writeP4C(std::fstream &fstat, double timeData)
std::fstream p4w
bool copy()
std::string getName(std::string type, unsigned int counter)
double xmax_
double xmin_
std::string name_
These store the save file names,.
takes data and fstat files and splits them into *.data.???? and *.fstat.???? files ...
std::fstream p4c
std::vector< unsigned int > id_
const Mdouble pi
Definition: ExtendedMath.h:42
double zmax_
void copy(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, double timeMin, double timeMax, unsigned int periodic)
Mdouble Y
Definition: Vector.h:52
std::vector< Vec3D > position_
CFile(std::string name)
Constructor.
std::fstream p4p
Stream used for data files.
int main(int argc, char *argv[])
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
Mdouble Z
Definition: Vector.h:52
double ymax_
void writeRestart(double timeData, double density)