MercuryData.h
Go to the documentation of this file.
1 //Copyright (c) 2015, 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 
26 #ifndef TOOLS_MERCURYDATA_H
27 #define TOOLS_MERCURYDATA_H
28 
29 #include <fstream>
30 #include <iostream>
31 #include <string>
32 #include <sstream>
33 #include <vector>
34 
40 template<std::size_t NDIMS>
42 {
43  public:
47  double position[NDIMS];
51  double velocity[NDIMS];
55  double rotation[NDIMS];
59  double angularV[NDIMS];
60 
64  double radius;
65 
69  std::size_t speciesID;
70 };
71 
79 template<>
80 struct MercuryParticle<2>
81 {
82  public:
86  double position[3];
90  double velocity[3];
94  double rotation[3];
98  double angularV[3];
99 
103  double radius;
104 
108  std::size_t speciesID;
109 
110 };
111 
115 template<std::size_t NDIMS>
116 std::istream& operator>>(std::istream& in, MercuryParticle<NDIMS>& part)
117 {
118  std::size_t i;
119  for (i = 0; i < NDIMS; i++)
120  in >> part.position[i];
121 
122  for (i = 0; i < NDIMS; i++) {
123  in >> part.velocity[i];
124  // convert velocities to float, because paraview cannot handle doubles
125  part.velocity[i] = (float)part.velocity[i];
126  }
127 
128  in >> part.radius;
129 
130  for (i = 0; i < NDIMS; i++)
131  in >> part.rotation[i];
132 
133  for (i = 0; i < NDIMS; i++) {
134  in >> part.angularV[i];
135  // convert velocities to float, because paraview cannot handle doubles
136  part.angularV[i] = (float)part.angularV[i];
137  }
138 
139  in >> part.speciesID;
140 
141  return in;
142 }
143 
144 template<>
145 std::istream& operator>><2>(std::istream& in, MercuryParticle<2>& part)
146 {
147  in >> part.position[0] >> part.position[2];
148  part.position[1] = 0;
149 
150  in >> part.velocity[0] >> part.velocity[2];
151  part.velocity[1] = 0;
152 
153  in >> part.radius;
154 
155  in >> part.rotation[1];
156  part.rotation[0] = part.rotation[2] = 0;
157 
158  in >> part.angularV[1];
159  part.angularV[0] = part.angularV[2] = 0;
160 
161  in >> part.speciesID;
162 
163  return in;
164 }
165 
166 
167 class MercuryDataFile;
168 
169 template<std::size_t NDIMS>
176 template<std::size_t NDIMS>
178 {
179  public:
184  double getTime() const
185  {
186  return time_;
187  }
188 
195  std::size_t getTimeStepID() const
196  {
197  return ID_;
198  }
199 
205  std::size_t getNumberOfParticles() const
206  {
207  return numParticles_;
208  }
209 
215  std::size_t size() const
216  {
217  return numParticles_;
218  }
219 
224  constexpr std::size_t getNumberOfDimensions() const
225  {
226  return NDIMS;
227  }
228 
232  typename std::vector< MercuryParticle<NDIMS> >::iterator begin()
233  {
234  return storage_.begin();
235  }
236 
240  typename std::vector< MercuryParticle<NDIMS> >::const_iterator begin() const
241  {
242  return storage_.begin();
243  }
244 
248  typename std::vector< MercuryParticle<NDIMS> >::iterator end()
249  {
250  return storage_.end();
251  }
252 
256  typename std::vector< MercuryParticle<NDIMS> >::const_iterator end() const
257  {
258  return storage_.end();
259  }
260 
265  {
266  return storage_[idx];
267  }
268 
272  const MercuryParticle<NDIMS>& operator[](std::size_t idx) const
273  {
274  return storage_[idx];
275  }
276 
277  private:
283  MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
284  : time_(0), ID_(id), numParticles_(0), dataFile_(pData)
285  {
286  }
287 
292  : time_(0), ID_(0), numParticles_(0), dataFile_(nullptr)
293  {
294  }
295 
299  double time_;
303  std::size_t ID_;
307  std::size_t numParticles_;
311  double min_[NDIMS], max_[NDIMS];
318 
322  std::vector< MercuryParticle<NDIMS> > storage_;
323 
324  template<std::size_t NDIMS2>
325  friend std::istream& operator>>(std::istream&, MercuryTimeStep<NDIMS2>&);
326 
327  friend class MercuryTimeStepIterator<NDIMS>;
328 
329  friend class MercuryDataFile;
330 };
331 
338 template<std::size_t NDIMS>
339 std::istream& operator>>(std::istream& in, MercuryTimeStep<NDIMS>& step)
340 {
341  std::size_t i;
342  in >> step.numParticles_ >> step.time_;
343 
344  for (i = 0; i < NDIMS; i++)
345  in >> step.min_[i];
346 
347  for (i = 0; i < NDIMS; i++)
348  in >> step.max_[i];
349 
350  return in;
351 }
352 
362 template<std::size_t NDIMS>
364 {
365  public:
370  {
371  return (isEOFTimeStep_ != other.isEOFTimeStep_);
372  }
373 
378  {
379  return lastReadTimeStep_;
380  }
381 
386  {
387  return lastReadTimeStep_;
388  }
389 
395  void operator++();
396 
397  private:
402  : isEOFTimeStep_(true), dataFile_(nullptr)
403  {
404  }
405 
411  : lastReadTimeStep_(0,pData), isEOFTimeStep_(false), dataFile_(pData)
412  {
413  ++(*this);
414  lastReadTimeStep_.ID_ = 0;
415  }
416 
429 
430  friend class MercuryDataFile;
431 };
432 
433 
441 {
442  public:
448  MercuryDataFile(std::string name)
449  : file_(name)
450  { }
451 
459  operator bool() const
460  {
461  return file_.good();
462  }
463 
475  template<std::size_t NDIMS>
477  {
478  //Store the position, so we can jump back at the end of the function..
479  std::ios::pos_type currentPosition = file_.tellg();
480  //and jump to the start
481  file_.seekg(0);
482  //get the first line
483  std::string line;
484  std::getline(file_, line);
485  file_.seekg(currentPosition); //and pretend nothing has happened
486 
487  std::istringstream lineStream(line);
488 
489  //We'll try to find out if there were exactly enough arguments.
491  lineStream >> step;
492 
493  //Did we reach the end yet?
494  bool isValid = lineStream.good() || (lineStream.eof() && !lineStream.fail());
495  double dummy;
496  lineStream >> dummy;
497 
498  //now we should have reached it.
499  isValid = isValid && !lineStream.good();
500  return isValid;
501  }
502 
509  template<std::size_t NDIMS>
511  {
512  private:
514  : data_(pData)
515  { }
516 
518  public:
520  {
521  return data_->begin<NDIMS>();
522  }
524  {
525  return data_->end<NDIMS>();
526  }
527  friend class MercuryDataFile;
528  };
529 
530  template<std::size_t NDIMS>
532  {
533  return {this};
534  };
535 
545  template<std::size_t NDIMS>
547  {
548  file_.seekg(0);
549  return {this};
550  }
551 
555  template<std::size_t NDIMS>
557  {
558  return {};
559  }
560  private:
564  std::ifstream file_;
565 
566  template<std::size_t NDIMS>
567  friend class MercuryTimeStep;
568  template<std::size_t NDIMS>
570 };
571 
572 template<std::size_t NDIMS>
574 {
575  lastReadTimeStep_.ID_++;
576 
577  std::string line;
578  std::getline(dataFile_->file_, line);
579 
580  std::istringstream lineStream(line);
581 
582  lineStream >> lastReadTimeStep_;
583 
584  //I hope we didn't went beyond end of file...
585  if (lineStream.eof())
586  {
587 // logger(WARN, "The time step header detected an EOF.. Usually this"
588 // " means that the format was not what it appeared to be."
589 // "\nproceed with caution!");
590  }
591  //Resize the backing storage container to make sure we can actually
592  //fit all the particles in there.
593  lastReadTimeStep_.storage_.resize(lastReadTimeStep_.numParticles_);
594  //Well, now that we're set up, read all the particles
596  {
597  //line by line, because no data format can be trusted.
598  std::getline(dataFile_->file_, line);
599  lineStream.clear();
600  lineStream.str(line);
601 
602  lineStream >> part;
603  }
604 
605  if (dataFile_->file_.eof())
606  isEOFTimeStep_ = true;
607 }
608 
609 #endif
std::istream & operator>>(std::istream &in, MercuryParticle< NDIMS > &part)
Read a single particle from a istream.
Definition: MercuryData.h:116
Definition: MercuryData.h:511
MercuryDataFile * data_
Definition: MercuryData.h:517
MercuryTimeStepIterator< NDIMS > end()
Definition: MercuryData.h:523
MercuryTimeStepIterator< NDIMS > begin()
Definition: MercuryData.h:519
IteratorProxy(MercuryDataFile *pData)
Definition: MercuryData.h:513
Definition: MercuryData.h:441
MercuryDataFile(std::string name)
Definition: MercuryData.h:448
MercuryTimeStepIterator< NDIMS > end() const
Returns a forwarditerator one past the last time step.
Definition: MercuryData.h:556
IteratorProxy< NDIMS > as()
Definition: MercuryData.h:531
MercuryTimeStepIterator< NDIMS > begin()
Returns a forwarditerator to the time steps Returns a forwarditerator to the time steps,...
Definition: MercuryData.h:546
std::ifstream file_
Definition: MercuryData.h:564
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:476
Definition: MercuryData.h:364
const MercuryTimeStep< NDIMS > & operator*() const
Const dereference operator, as defined for constant ForwardIterators.
Definition: MercuryData.h:385
MercuryTimeStep< NDIMS > & operator*()
Dereference operator, as defined for ForwardIterators.
Definition: MercuryData.h:377
MercuryTimeStepIterator()
Definition: MercuryData.h:401
MercuryTimeStep< NDIMS > lastReadTimeStep_
Definition: MercuryData.h:420
bool operator!=(MercuryTimeStepIterator< NDIMS > other) const
Not-equals operator, as defined for ForwardIterators.
Definition: MercuryData.h:369
void operator++()
Pre-increment operator, as defined for ForwardIterators This method populates the time step,...
Definition: MercuryData.h:573
MercuryDataFile * dataFile_
Definition: MercuryData.h:428
MercuryTimeStepIterator(MercuryDataFile *pData)
Definition: MercuryData.h:410
bool isEOFTimeStep_
Definition: MercuryData.h:424
Definition: MercuryData.h:178
std::size_t numParticles_
Definition: MercuryData.h:307
std::vector< MercuryParticle< NDIMS > >::iterator end()
Iterator functions for range based for loops.
Definition: MercuryData.h:248
std::size_t size() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:215
MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
Constructor used by the MercuryTimeStepIterator, to flag a functional time step.
Definition: MercuryData.h:283
std::vector< MercuryParticle< NDIMS > >::const_iterator begin() const
Iterator functions for range based for loops.
Definition: MercuryData.h:240
double max_[NDIMS]
Definition: MercuryData.h:311
std::size_t getNumberOfParticles() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:205
friend std::istream & operator>>(std::istream &, MercuryTimeStep< NDIMS2 > &)
std::size_t ID_
Definition: MercuryData.h:303
std::vector< MercuryParticle< NDIMS > > storage_
Definition: MercuryData.h:322
double getTime() const
Gets the time associated with this time step.
Definition: MercuryData.h:184
std::vector< MercuryParticle< NDIMS > >::const_iterator end() const
Iterator functions for range based for loops.
Definition: MercuryData.h:256
double time_
Definition: MercuryData.h:299
MercuryParticle< NDIMS > & operator[](std::size_t idx)
Random access function into the particles.
Definition: MercuryData.h:264
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:195
MercuryDataFile * dataFile_
Definition: MercuryData.h:317
constexpr std::size_t getNumberOfDimensions() const
returns the number of dimensions used.
Definition: MercuryData.h:224
MercuryTimeStep()
EOF-TimeStep constructor used by MercuryTimeStepIterator (and MercuryDataFile::isMercury3DDataFile())
Definition: MercuryData.h:291
const MercuryParticle< NDIMS > & operator[](std::size_t idx) const
Random access function into the particles.
Definition: MercuryData.h:272
double min_[NDIMS]
Definition: MercuryData.h:311
std::vector< MercuryParticle< NDIMS > >::iterator begin()
Iterator functions for range based for loops.
Definition: MercuryData.h:232
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
std::string name
Definition: MercuryProb.h:48
Definition: MercuryData.h:81
std::size_t speciesID
Definition: MercuryData.h:108
double radius
Definition: MercuryData.h:103
Definition: MercuryData.h:42
std::size_t speciesID
Definition: MercuryData.h:69
double rotation[NDIMS]
Definition: MercuryData.h:55
double angularV[NDIMS]
Definition: MercuryData.h:59
double radius
Definition: MercuryData.h:64
double velocity[NDIMS]
Definition: MercuryData.h:51
double position[NDIMS]
Definition: MercuryData.h:47