MercuryData.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef TOOLS_MERCURYDATA_H
6 #define TOOLS_MERCURYDATA_H
7 
8 #include <fstream>
9 #include <iostream>
10 #include <string>
11 #include <sstream>
12 #include <vector>
13 
19 template<std::size_t NDIMS>
21 {
22  public:
26  double position[NDIMS];
30  double velocity[NDIMS];
34  double rotation[NDIMS];
38  double angularV[NDIMS];
39 
43  double radius;
44 
48  std::size_t speciesID;
49 };
50 
58 template<>
59 struct MercuryParticle<2>
60 {
61  public:
65  double position[3];
69  double velocity[3];
73  double rotation[3];
77  double angularV[3];
78 
82  double radius;
83 
87  std::size_t speciesID;
88 
89 };
90 
94 template<std::size_t NDIMS>
95 std::istream& operator>>(std::istream& in, MercuryParticle<NDIMS>& part)
96 {
97  std::size_t i;
98  for (i = 0; i < NDIMS; i++)
99  in >> part.position[i];
100 
101  for (i = 0; i < NDIMS; i++) {
102  in >> part.velocity[i];
103  // convert velocities to float, because paraview cannot handle doubles
104  part.velocity[i] = (float)part.velocity[i];
105  }
106 
107  in >> part.radius;
108 
109  for (i = 0; i < NDIMS; i++)
110  in >> part.rotation[i];
111 
112  for (i = 0; i < NDIMS; i++) {
113  in >> part.angularV[i];
114  // convert velocities to float, because paraview cannot handle doubles
115  part.angularV[i] = (float)part.angularV[i];
116  }
117 
118  in >> part.speciesID;
119 
120  return in;
121 }
122 
123 template<>
124 std::istream& operator>><2>(std::istream& in, MercuryParticle<2>& part)
125 {
126  in >> part.position[0] >> part.position[2];
127  part.position[1] = 0;
128 
129  in >> part.velocity[0] >> part.velocity[2];
130  part.velocity[1] = 0;
131 
132  in >> part.radius;
133 
134  in >> part.rotation[1];
135  part.rotation[0] = part.rotation[2] = 0;
136 
137  in >> part.angularV[1];
138  part.angularV[0] = part.angularV[2] = 0;
139 
140  in >> part.speciesID;
141 
142  return in;
143 }
144 
145 
146 class MercuryDataFile;
147 
148 template<std::size_t NDIMS>
155 template<std::size_t NDIMS>
157 {
158  public:
163  double getTime() const
164  {
165  return time_;
166  }
167 
174  std::size_t getTimeStepID() const
175  {
176  return ID_;
177  }
178 
184  std::size_t getNumberOfParticles() const
185  {
186  return numParticles_;
187  }
188 
194  std::size_t size() const
195  {
196  return numParticles_;
197  }
198 
203  constexpr std::size_t getNumberOfDimensions() const
204  {
205  return NDIMS;
206  }
207 
211  typename std::vector< MercuryParticle<NDIMS> >::iterator begin()
212  {
213  return storage_.begin();
214  }
215 
219  typename std::vector< MercuryParticle<NDIMS> >::const_iterator begin() const
220  {
221  return storage_.begin();
222  }
223 
227  typename std::vector< MercuryParticle<NDIMS> >::iterator end()
228  {
229  return storage_.end();
230  }
231 
235  typename std::vector< MercuryParticle<NDIMS> >::const_iterator end() const
236  {
237  return storage_.end();
238  }
239 
244  {
245  return storage_[idx];
246  }
247 
251  const MercuryParticle<NDIMS>& operator[](std::size_t idx) const
252  {
253  return storage_[idx];
254  }
255 
256  private:
262  MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
263  : time_(0), ID_(id), numParticles_(0), dataFile_(pData)
264  {
265  }
266 
271  : time_(0), ID_(0), numParticles_(0), dataFile_(nullptr)
272  {
273  }
274 
278  double time_;
282  std::size_t ID_;
286  std::size_t numParticles_;
290  double min_[NDIMS], max_[NDIMS];
297 
301  std::vector< MercuryParticle<NDIMS> > storage_;
302 
303  template<std::size_t NDIMS2>
304  friend std::istream& operator>>(std::istream&, MercuryTimeStep<NDIMS2>&);
305 
306  friend class MercuryTimeStepIterator<NDIMS>;
307 
308  friend class MercuryDataFile;
309 };
310 
317 template<std::size_t NDIMS>
318 std::istream& operator>>(std::istream& in, MercuryTimeStep<NDIMS>& step)
319 {
320  std::size_t i;
321  in >> step.numParticles_ >> step.time_;
322 
323  for (i = 0; i < NDIMS; i++)
324  in >> step.min_[i];
325 
326  for (i = 0; i < NDIMS; i++)
327  in >> step.max_[i];
328 
329  return in;
330 }
331 
341 template<std::size_t NDIMS>
343 {
344  public:
349  {
350  return (isEOFTimeStep_ != other.isEOFTimeStep_);
351  }
352 
357  {
358  return lastReadTimeStep_;
359  }
360 
365  {
366  return lastReadTimeStep_;
367  }
368 
374  void operator++();
375 
376  private:
381  : isEOFTimeStep_(true), dataFile_(nullptr)
382  {
383  }
384 
390  : lastReadTimeStep_(0,pData), isEOFTimeStep_(false), dataFile_(pData)
391  {
392  ++(*this);
393  lastReadTimeStep_.ID_ = 0;
394  }
395 
408 
409  friend class MercuryDataFile;
410 };
411 
412 
420 {
421  public:
428  : file_(name)
429  { }
430 
438  operator bool() const
439  {
440  return file_.good();
441  }
442 
454  template<std::size_t NDIMS>
456  {
457  //Store the position, so we can jump back at the end of the function..
458  std::ios::pos_type currentPosition = file_.tellg();
459  //and jump to the start
460  file_.seekg(0);
461  //get the first line
463  std::getline(file_, line);
464  file_.seekg(currentPosition); //and pretend nothing has happened
465 
466  std::istringstream lineStream(line);
467 
468  //We'll try to find out if there were exactly enough arguments.
470  lineStream >> step;
471 
472  //Did we reach the end yet?
473  bool isValid = lineStream.good() || (lineStream.eof() && !lineStream.fail());
474  double dummy;
475  lineStream >> dummy;
476 
477  //now we should have reached it.
478  isValid = isValid && !lineStream.good();
479  return isValid;
480  }
481 
488  template<std::size_t NDIMS>
490  {
491  private:
493  : data_(pData)
494  { }
495 
497  public:
499  {
500  return data_->begin<NDIMS>();
501  }
503  {
504  return data_->end<NDIMS>();
505  }
506  friend class MercuryDataFile;
507  };
508 
509  template<std::size_t NDIMS>
511  {
512  return {this};
513  };
514 
524  template<std::size_t NDIMS>
526  {
527  file_.seekg(0);
528  return {this};
529  }
530 
534  template<std::size_t NDIMS>
536  {
537  return {};
538  }
539  private:
543  std::ifstream file_;
544 
545  template<std::size_t NDIMS>
546  friend class MercuryTimeStep;
547  template<std::size_t NDIMS>
549 };
550 
551 template<std::size_t NDIMS>
553 {
554  lastReadTimeStep_.ID_++;
555 
557  std::getline(dataFile_->file_, line);
558 
559  std::istringstream lineStream(line);
560 
561  lineStream >> lastReadTimeStep_;
562 
563  //I hope we didn't went beyond end of file...
564  if (lineStream.eof())
565  {
566 // logger(WARN, "The time step header detected an EOF.. Usually this"
567 // " means that the format was not what it appeared to be."
568 // "\nproceed with caution!");
569  }
570  //Resize the backing storage container to make sure we can actually
571  //fit all the particles in there.
572  lastReadTimeStep_.storage_.resize(lastReadTimeStep_.numParticles_);
573  //Well, now that we're set up, read all the particles
575  {
576  //line by line, because no data format can be trusted.
577  std::getline(dataFile_->file_, line);
578  lineStream.clear();
579  lineStream.str(line);
580 
581  lineStream >> part;
582  }
583 
584  if (dataFile_->file_.eof())
585  isEOFTimeStep_ = true;
586 }
587 
588 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
std::istream & operator>>(std::istream &in, MercuryParticle< NDIMS > &part)
Read a single particle from a istream.
Definition: MercuryData.h:95
Definition: MercuryData.h:490
MercuryDataFile * data_
Definition: MercuryData.h:496
MercuryTimeStepIterator< NDIMS > end()
Definition: MercuryData.h:502
MercuryTimeStepIterator< NDIMS > begin()
Definition: MercuryData.h:498
IteratorProxy(MercuryDataFile *pData)
Definition: MercuryData.h:492
Definition: MercuryData.h:420
MercuryDataFile(std::string name)
Definition: MercuryData.h:427
MercuryTimeStepIterator< NDIMS > end() const
Returns a forwarditerator one past the last time step.
Definition: MercuryData.h:535
IteratorProxy< NDIMS > as()
Definition: MercuryData.h:510
MercuryTimeStepIterator< NDIMS > begin()
Returns a forwarditerator to the time steps Returns a forwarditerator to the time steps,...
Definition: MercuryData.h:525
std::ifstream file_
Definition: MercuryData.h:543
bool isMercuryDataFile()
Checks if this file is a valid Mercury 3D data file. This function jumps to the start of the file,...
Definition: MercuryData.h:455
Definition: MercuryData.h:343
const MercuryTimeStep< NDIMS > & operator*() const
Const dereference operator, as defined for constant ForwardIterators.
Definition: MercuryData.h:364
MercuryTimeStep< NDIMS > & operator*()
Dereference operator, as defined for ForwardIterators.
Definition: MercuryData.h:356
MercuryTimeStepIterator()
Definition: MercuryData.h:380
MercuryTimeStep< NDIMS > lastReadTimeStep_
Definition: MercuryData.h:399
bool operator!=(MercuryTimeStepIterator< NDIMS > other) const
Not-equals operator, as defined for ForwardIterators.
Definition: MercuryData.h:348
void operator++()
Pre-increment operator, as defined for ForwardIterators This method populates the time step,...
Definition: MercuryData.h:552
MercuryDataFile * dataFile_
Definition: MercuryData.h:407
MercuryTimeStepIterator(MercuryDataFile *pData)
Definition: MercuryData.h:389
bool isEOFTimeStep_
Definition: MercuryData.h:403
Definition: MercuryData.h:157
std::size_t numParticles_
Definition: MercuryData.h:286
std::vector< MercuryParticle< NDIMS > >::iterator end()
Iterator functions for range based for loops.
Definition: MercuryData.h:227
std::size_t size() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:194
MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
Constructor used by the MercuryTimeStepIterator, to flag a functional time step.
Definition: MercuryData.h:262
std::vector< MercuryParticle< NDIMS > >::const_iterator begin() const
Iterator functions for range based for loops.
Definition: MercuryData.h:219
double max_[NDIMS]
Definition: MercuryData.h:290
std::size_t getNumberOfParticles() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:184
friend std::istream & operator>>(std::istream &, MercuryTimeStep< NDIMS2 > &)
std::size_t ID_
Definition: MercuryData.h:282
std::vector< MercuryParticle< NDIMS > > storage_
Definition: MercuryData.h:301
double getTime() const
Gets the time associated with this time step.
Definition: MercuryData.h:163
std::vector< MercuryParticle< NDIMS > >::const_iterator end() const
Iterator functions for range based for loops.
Definition: MercuryData.h:235
double time_
Definition: MercuryData.h:278
MercuryParticle< NDIMS > & operator[](std::size_t idx)
Random access function into the particles.
Definition: MercuryData.h:243
std::size_t getTimeStepID() const
Gets the time step ID Returns the time step ID, which is a consecutively ascending number unique for ...
Definition: MercuryData.h:174
MercuryDataFile * dataFile_
Definition: MercuryData.h:296
constexpr std::size_t getNumberOfDimensions() const
returns the number of dimensions used.
Definition: MercuryData.h:203
MercuryTimeStep()
EOF-TimeStep constructor used by MercuryTimeStepIterator (and MercuryDataFile::isMercury3DDataFile())
Definition: MercuryData.h:270
const MercuryParticle< NDIMS > & operator[](std::size_t idx) const
Random access function into the particles.
Definition: MercuryData.h:251
double min_[NDIMS]
Definition: MercuryData.h:290
std::vector< MercuryParticle< NDIMS > >::iterator begin()
Iterator functions for range based for loops.
Definition: MercuryData.h:211
for(int j=0;j< nb;++j)
Definition: level2_impl.h:287
line
Definition: calibrate.py:103
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
string name
Definition: plotDoE.py:33
Definition: MercuryData.h:60
std::size_t speciesID
Definition: MercuryData.h:87
double radius
Definition: MercuryData.h:82
Definition: MercuryData.h:21
std::size_t speciesID
Definition: MercuryData.h:48
double rotation[NDIMS]
Definition: MercuryData.h:34
double angularV[NDIMS]
Definition: MercuryData.h:38
double radius
Definition: MercuryData.h:43
double velocity[NDIMS]
Definition: MercuryData.h:30
double position[NDIMS]
Definition: MercuryData.h:26