MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  std::size_t i;
148  in >> part.position[0] >> part.position[2];
149  part.position[1] = 0;
150 
151  in >> part.velocity[0] >> part.velocity[2];
152  part.velocity[1] = 0;
153 
154  in >> part.radius;
155 
156  in >> part.rotation[1];
157  part.rotation[0] = part.rotation[2] = 0;
158 
159  in >> part.angularV[1];
160  part.angularV[0] = part.angularV[2] = 0;
161 
162  in >> part.speciesID;
163 
164  return in;
165 }
166 
167 
168 class MercuryDataFile;
169 
170 template<std::size_t NDIMS>
177 template<std::size_t NDIMS>
179 {
180  public:
185  double getTime() const
186  {
187  return time_;
188  }
189 
196  std::size_t getTimeStepID() const
197  {
198  return ID_;
199  }
200 
206  std::size_t getNumberOfParticles() const
207  {
208  return numParticles_;
209  }
210 
216  std::size_t size() const
217  {
218  return numParticles_;
219  }
220 
225  constexpr std::size_t getNumberOfDimensions() const
226  {
227  return NDIMS;
228  }
229 
233  typename std::vector< MercuryParticle<NDIMS> >::iterator begin()
234  {
235  return storage_.begin();
236  }
237 
241  typename std::vector< MercuryParticle<NDIMS> >::const_iterator begin() const
242  {
243  return storage_.begin();
244  }
245 
249  typename std::vector< MercuryParticle<NDIMS> >::iterator end()
250  {
251  return storage_.end();
252  }
253 
257  typename std::vector< MercuryParticle<NDIMS> >::const_iterator end() const
258  {
259  return storage_.end();
260  }
261 
266  {
267  return storage_[idx];
268  }
269 
273  const MercuryParticle<NDIMS>& operator[](std::size_t idx) const
274  {
275  return storage_[idx];
276  }
277 
278  private:
284  MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
285  : time_(0), ID_(id), numParticles_(0), dataFile_(pData)
286  {
287  }
288 
293  : time_(0), ID_(0), numParticles_(0), dataFile_(nullptr)
294  {
295  }
296 
300  double time_;
304  std::size_t ID_;
308  std::size_t numParticles_;
312  double min_[NDIMS], max_[NDIMS];
319 
323  std::vector< MercuryParticle<NDIMS> > storage_;
324 
325  template<std::size_t NDIMS2>
326  friend std::istream& operator>>(std::istream&, MercuryTimeStep<NDIMS2>&);
327 
328  friend class MercuryTimeStepIterator<NDIMS>;
329 
330  friend class MercuryDataFile;
331 };
332 
339 template<std::size_t NDIMS>
340 std::istream& operator>>(std::istream& in, MercuryTimeStep<NDIMS>& step)
341 {
342  std::size_t i;
343  in >> step.numParticles_ >> step.time_;
344 
345  for (i = 0; i < NDIMS; i++)
346  in >> step.min_[i];
347 
348  for (i = 0; i < NDIMS; i++)
349  in >> step.max_[i];
350 
351  return in;
352 }
353 
363 template<std::size_t NDIMS>
365 {
366  public:
371  {
372  return (isEOFTimeStep_ != other.isEOFTimeStep_);
373  }
374 
379  {
380  return lastReadTimeStep_;
381  }
382 
387  {
388  return lastReadTimeStep_;
389  }
390 
396  void operator++();
397 
398  private:
403  : isEOFTimeStep_(true), dataFile_(nullptr)
404  {
405  }
406 
412  : lastReadTimeStep_(0,pData), isEOFTimeStep_(false), dataFile_(pData)
413  {
414  ++(*this);
415  lastReadTimeStep_.ID_ = 0;
416  }
417 
430 
431  friend class MercuryDataFile;
432 };
433 
434 
442 {
443  public:
449  MercuryDataFile(std::string name)
450  : file_(name)
451  { }
452 
460  operator bool() const
461  {
462  return file_.good();
463  }
464 
476  template<std::size_t NDIMS>
478  {
479  //Store the position, so we can jump back at the end of the function..
480  std::ios::pos_type currentPosition = file_.tellg();
481  //and jump to the start
482  file_.seekg(0);
483  //get the first line
484  std::string line;
485  std::getline(file_, line);
486  file_.seekg(currentPosition); //and pretend nothing has happened
487 
488  std::istringstream lineStream(line);
489 
490  //We'll try to find out if there were exactly enough arguments.
492  lineStream >> step;
493 
494  //Did we reach the end yet?
495  bool isValid = lineStream.good();
496  double dummy;
497  lineStream >> dummy;
498 
499  //now we should have reached it.
500  isValid = isValid && !lineStream.good();
501  return isValid;
502  }
503 
510  template<std::size_t NDIMS>
512  {
513  private:
515  : data_(pData)
516  { }
517 
519  public:
521  {
522  return data_->begin<NDIMS>();
523  }
525  {
526  return data_->end<NDIMS>();
527  }
528  friend class MercuryDataFile;
529  };
530 
531  template<std::size_t NDIMS>
533  {
534  return {this};
535  };
536 
546  template<std::size_t NDIMS>
548  {
549  file_.seekg(0);
550  return {this};
551  }
552 
556  template<std::size_t NDIMS>
558  {
559  return {};
560  }
561  private:
565  std::ifstream file_;
566 
567  template<std::size_t NDIMS>
568  friend class MercuryTimeStep;
569  template<std::size_t NDIMS>
571 };
572 
573 template<std::size_t NDIMS>
575 {
576  lastReadTimeStep_.ID_++;
577 
578  std::string line;
579  std::getline(dataFile_->file_, line);
580 
581  std::istringstream lineStream(line);
582 
583  lineStream >> lastReadTimeStep_;
584 
585  //I hope we didn't went beyond end of file...
586  if (lineStream.eof())
587  {
588 // logger(WARN, "The time step header detected an EOF.. Usually this"
589 // " means that the format was not what it appeared to be."
590 // "\nproceed with caution!");
591  }
592  //Resize the backing storage container to make sure we can actually
593  //fit all the particles in there.
594  lastReadTimeStep_.storage_.resize(lastReadTimeStep_.numParticles_);
595  //Well, now that we're set up, read all the particles
596  for (MercuryParticle<NDIMS>& part : lastReadTimeStep_)
597  {
598  //line by line, because no data format can be trusted.
599  std::getline(dataFile_->file_, line);
600  lineStream.clear();
601  lineStream.str(line);
602 
603  lineStream >> part;
604  }
605 
606  if (dataFile_->file_.eof())
607  isEOFTimeStep_ = true;
608 }
609 
610 #endif
constexpr std::size_t getNumberOfDimensions() const
returns the number of dimensions used.
Definition: MercuryData.h:225
std::vector< MercuryParticle< NDIMS > >::iterator begin()
Iterator functions for range based for loops.
Definition: MercuryData.h:233
std::size_t speciesID
Definition: MercuryData.h:108
std::vector< MercuryParticle< NDIMS > > storage_
Definition: MercuryData.h:323
double velocity[NDIMS]
Definition: MercuryData.h:51
MercuryTimeStep(std::size_t id, MercuryDataFile *pData)
Constructor used by the MercuryTimeStepIterator, to flag a functional time step.
Definition: MercuryData.h:284
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:196
MercuryDataFile * dataFile_
Definition: MercuryData.h:318
bool operator!=(MercuryTimeStepIterator< NDIMS > other) const
Not-equals operator, as defined for ForwardIterators.
Definition: MercuryData.h:370
IteratorProxy< NDIMS > as()
Definition: MercuryData.h:532
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
friend std::istream & operator>>(std::istream &, MercuryTimeStep< NDIMS2 > &)
MercuryTimeStepIterator< NDIMS > end() const
Returns a forwarditerator one past the last time step.
Definition: MercuryData.h:557
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:477
MercuryParticle< NDIMS > & operator[](std::size_t idx)
Random access function into the particles.
Definition: MercuryData.h:265
const MercuryTimeStep< NDIMS > & operator*() const
Const dereference operator, as defined for constant ForwardIterators.
Definition: MercuryData.h:386
double angularV[NDIMS]
Definition: MercuryData.h:59
std::ifstream file_
Definition: MercuryData.h:565
double position[NDIMS]
Definition: MercuryData.h:47
double min_[NDIMS]
Definition: MercuryData.h:312
MercuryTimeStepIterator< NDIMS > begin()
Returns a forwarditerator to the time steps Returns a forwarditerator to the time steps...
Definition: MercuryData.h:547
std::vector< MercuryParticle< NDIMS > >::const_iterator end() const
Iterator functions for range based for loops.
Definition: MercuryData.h:257
std::istream & operator>>(std::istream &in, MercuryParticle< NDIMS > &part)
Read a single particle from a istream.
Definition: MercuryData.h:116
MercuryTimeStepIterator< NDIMS > end()
Definition: MercuryData.h:524
double rotation[NDIMS]
Definition: MercuryData.h:55
MercuryTimeStep< NDIMS > & operator*()
Dereference operator, as defined for ForwardIterators.
Definition: MercuryData.h:378
void operator++()
Pre-increment operator, as defined for ForwardIterators This method populates the time step...
Definition: MercuryData.h:574
std::size_t size() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:216
MercuryTimeStepIterator< NDIMS > begin()
Definition: MercuryData.h:520
MercuryTimeStep()
EOF-TimeStep constructor used by MercuryTimeStepIterator (and MercuryDataFile::isMercury3DDataFile())...
Definition: MercuryData.h:292
double max_[NDIMS]
Definition: MercuryData.h:312
MercuryTimeStep< NDIMS > lastReadTimeStep_
Definition: MercuryData.h:421
std::size_t numParticles_
Definition: MercuryData.h:308
MercuryDataFile * dataFile_
Definition: MercuryData.h:429
MercuryDataFile(std::string name)
Definition: MercuryData.h:449
std::size_t ID_
Definition: MercuryData.h:304
MercuryTimeStepIterator(MercuryDataFile *pData)
Definition: MercuryData.h:411
IteratorProxy(MercuryDataFile *pData)
Definition: MercuryData.h:514
std::vector< MercuryParticle< NDIMS > >::const_iterator begin() const
Iterator functions for range based for loops.
Definition: MercuryData.h:241
const MercuryParticle< NDIMS > & operator[](std::size_t idx) const
Random access function into the particles.
Definition: MercuryData.h:273
std::size_t getNumberOfParticles() const
Gets the number of particles recorded in this time step.
Definition: MercuryData.h:206
double getTime() const
Gets the time associated with this time step.
Definition: MercuryData.h:185
std::vector< MercuryParticle< NDIMS > >::iterator end()
Iterator functions for range based for loops.
Definition: MercuryData.h:249
std::size_t speciesID
Definition: MercuryData.h:69