FileReader Class Reference

This gives functionality to read information from binary formats like STL etc. This class is complete stand-alone and is tested with one any reference to other MecuryDPM code except Vections and Logger. More...

Public Member Functions

 FileReader (std::string name)
 Default constuction, requires to users to prove the name of the file that will be opened. More...
 
bool read ()
 
 ~FileReader ()
 Destructor, simple closes the file. More...
 

Private Types

enum class  Version { P3 , P4 }
 The version number of the particle analytics files. More...
 

Private Attributes

std::ifstream pFile_
 Pointers for the input files. More...
 
std::ifstream cFile_
 
std::ifstream wFile_
 
enum FileReader::Version version_
 
Mercury3D dpm
 

Detailed Description

This gives functionality to read information from binary formats like STL etc. This class is complete stand-alone and is tested with one any reference to other MecuryDPM code except Vections and Logger.

Member Enumeration Documentation

◆ Version

enum FileReader::Version
strongprivate

The version number of the particle analytics files.

Enumerator
P3 
P4 
245 {P3, P4} version_;
enum FileReader::Version version_

Constructor & Destructor Documentation

◆ FileReader()

FileReader::FileReader ( std::string  name)
inlineexplicit

Default constuction, requires to users to prove the name of the file that will be opened.

46  {
47  pFile_.open(name+".p3p");
48  if (pFile_) {
50  wFile_.open(name+".p3w");
51  cFile_.open(name+".p3c");
52  if (!wFile_) logger(ERROR, "Could not open %", name + ".p3w");
53  if (!cFile_) logger(ERROR, "Could not open %", name + ".p3c");
54  } else {
56  pFile_.open(name+".p4p");
57  wFile_.open(name+".p4w");
58  cFile_.open(name+".p4c");
59  if (!pFile_) logger(ERROR, "Could not open % or %", name + ".p3p",name + ".p4p");
60  if (!wFile_) logger(ERROR, "Could not open %", name + ".p4w");
61  if (!cFile_) logger(ERROR, "Could not open %", name + ".p4c");
62  }
63  logger(INFO,"Input files opened");
64 
65  dpm.setName(name);
66  }
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ INFO
@ ERROR
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:422
std::ifstream wFile_
Definition: ParticleAnalytics2Mercury.cpp:243
std::ifstream cFile_
Definition: ParticleAnalytics2Mercury.cpp:243
std::ifstream pFile_
Pointers for the input files.
Definition: ParticleAnalytics2Mercury.cpp:243
Mercury3D dpm
Definition: ParticleAnalytics2Mercury.cpp:247
std::string name
Definition: MercuryProb.h:48

References cFile_, dpm, ERROR, INFO, logger, units::name, P3, P4, pFile_, DPMBase::setName(), version_, and wFile_.

◆ ~FileReader()

FileReader::~FileReader ( )
inline

Destructor, simple closes the file.

233  {
234  pFile_.close();
235  wFile_.close();
236  cFile_.close();
237  logger(INFO,"Input files closed");
238  }

References cFile_, INFO, logger, pFile_, and wFile_.

Member Function Documentation

◆ read()

bool FileReader::read ( )
inline
Todo:
taking this line of of the for loop gives a huge speed improvement; why?
68  {
72 
73  std::string line;
74 
75  //read first line p3p
76  std::getline(pFile_, line);
77  if (!line.compare(""))
78  return false;
79 
80  //read second line p3p
81  Mdouble time;
82  unsigned N;
83  pFile_ >> time >> N;
84  dpm.setTime(time);
86  std::getline(pFile_, line);
87 
88  //read third line p3p
89  std::getline(pFile_, line);
90 
91  //read next lines p3p
92  logger(INFO,"Reading % particles at time %",N,time);
93  {
95  for (unsigned i = 0; i < N; ++i) {
96  unsigned id, species;
97  Mdouble volume, mass;
98  Vec3D position, velocity;
99  pFile_ >> id >> species >> volume >> mass >> position >> velocity;
100  std::getline(pFile_, line);
101  if (version_==Version::P3) species--;
102  //logger(INFO,"% % % % % %",id, species, volume, mass, position, velocity);
103  // add new species as necessary
104  while (dpm.speciesHandler.getNumberOfObjects() <= species) {
106  species.setDensity(mass / volume);
107  //use an infinite interaction radius
108  species.setAdhesionForceMax(1e20);
109  species.setAdhesionStiffness(1);
110  logger(INFO, "Adding species of density %", species.getDensity());
112  }
114  p.setRadius(cbrt(0.75 / pi * volume));
115  p.setPosition(position);
116  p.setVelocity(velocity);
117  dpm.particleHandler.copyAndAddObject(p)->setId(id);
118  }
119  }
120  //logger(INFO,"Read % particles",dpm.particleHandler.getNumberOfObjects());
121 
122  //read first line p3c
123  std::getline(cFile_, line);
124  if (!line.compare(""))
125  return false;
126 
127  //read second line p3c
128  cFile_ >> time >> N;
129  if (fabs(dpm.getTime()/time-1)>0.01)
130  logger(ERROR,"Timesteps in p3c and p3p do not agree: % %",dpm.getTime(),time);
132  std::getline(cFile_, line);
133 
134  //read third line p3c
135  std::getline(cFile_, line);
136 
137  //read next lines p3c
138  logger(INFO,"Reading % contacts",N,time);
139  for (unsigned i=0; i<N; ++i) {
140  unsigned id1, id2;
141  Vec3D force, contact;
142  cFile_ >> id1 >> id2;
145  logger.assert_debug(p1 != nullptr, "Particle % does not exist", id1);
146  logger.assert_debug(p2 != nullptr, "Particle % does not exist", id1);
147  Vec3D P1ToP2 = p2->getPosition() - p1->getPosition();
149  logger.assert_debug(c != nullptr, "Particle-particle interaction % % does not exist", p1, p2);
150  c->setDistance(P1ToP2.getLength());
151  c->setNormal(P1ToP2 / c->getDistance());
152  c->setOverlap(c->getDistance() - p1->getRadius() - p2->getRadius());
153  if (version_ == Version::P3)
154  {
155  cFile_ >> force;
156  contact = p1->getPosition() - P1ToP2 * ((p1->getRadius() - 0.5 * c->getOverlap()) / c->getDistance());
157  }
158  else
159  {
160  cFile_ >> contact >> force;
161  }
162  std::getline(cFile_, line);
163  c->setContactPoint(contact);
164  c->setForce(force);
165  if (i % (N / 10) == 0)
166  {
167  logger(INFO, "\r %", std::round((double) i / N * 100));
168  }
169  }
170  logger(INFO, "");
171 
172  //read first line p3w
173  std::getline(wFile_, line);
174  if (!line.compare(""))
175  return false;
176 
177  //read second line p3w
178  wFile_ >> time >> N;
179  if (fabs(dpm.getTime() / time - 1) > 0.01)
180  logger(ERROR, "Timesteps in p3w and p3p do not agree");
182  std::getline(wFile_, line);
183 
184  //read third line p3w
185  std::getline(wFile_, line);
186 
187  //create wall
188  InfiniteWall wall;
190  auto w = dpm.wallHandler.copyAndAddObject(wall);
191 
192  //read next lines p3w
193  logger(INFO,"Reading % wall contacts",N,time);
194  for (unsigned i=0; i<N; ++i) {
195  unsigned id;
196  Vec3D force, contact, particleToContact;
197  wFile_ >> id;
199  logger.assert_debug(p!=nullptr,"Particle % does not exist",id);
200  BaseInteraction* c = w->getInteractionWith(p,time,&dpm.interactionHandler);
201  logger.assert_debug(c!= nullptr,"Particle-wall interaction % % does not exist",p,w);
202  if (version_==Version::P3) {
203  wFile_ >> force >> particleToContact;
204  contact = p->getPosition()-particleToContact;
205  } else {
206  wFile_ >> contact >> force;
207  particleToContact = p->getPosition()-contact;
208  }
209  std::getline(wFile_, line);
210  c->setContactPoint(contact);
211  c->setDistance(particleToContact.getLength());
212  c->setNormal(particleToContact/c->getDistance());
213  c->setOverlap(c->getDistance()-p->getRadius());
214  c->setForce(force);
215  }
216 
217  logger(INFO,"Writing output files");
218  for (const auto p : dpm.particleHandler) {
221  }
222  for (const auto c : dpm.interactionHandler) {
225  }
227  return true;
228  }
virtual unsigned int getNumberOfObjects() const
Gets the number of real Object in this BaseHandler. (i.e. no mpi or periodic particles)
Definition: BaseHandler.h:648
virtual void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0.
Definition: BaseHandler.h:528
void setStorageCapacity(const unsigned int N)
Sets the storage capacity of this BaseHandler.
Definition: BaseHandler.h:669
std::enable_if<!std::is_pointer< U >::value, U * >::type copyAndAddObject(const U &object)
Creates a copy of a Object and adds it to the BaseHandler.
Definition: BaseHandler.h:379
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
T * getObjectById(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:565
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:350
virtual void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:239
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:218
Stores information about interactions between two interactable objects; often particles but could be ...
Definition: BaseInteraction.h:60
void setDistance(Mdouble distance)
Sets the interaction distance between the two interacting objects.
Definition: BaseInteraction.cc:221
const Vec3D & getContactPoint() const
Gets constant reference to contact point (vector).
Definition: BaseInteraction.h:234
void setContactPoint(Vec3D contactPoint)
Set the location of the contact point between the two interacting objects.
Definition: BaseInteraction.cc:240
Mdouble getDistance() const
Returns an Mdouble which is the norm (length) of distance vector.
Definition: BaseInteraction.cc:539
void setNormal(Vec3D normal)
Sets the normal vector between the two interacting objects.
Definition: BaseInteraction.cc:212
void setForce(Vec3D force)
set total force (this is used by the normal force, tangential forces are added use addForce)
Definition: BaseInteraction.cc:627
Mdouble getOverlap() const
Returns a Mdouble with the current overlap between the two interacting objects.
Definition: BaseInteraction.h:240
void setOverlap(Mdouble overlap)
Set the overlap between the two interacting object.
Definition: BaseInteraction.cc:231
Definition: BaseParticle.h:54
BaseInteraction * getInteractionWith(BaseParticle *P, unsigned timeStamp, InteractionHandler *interactionHandler) override
Checks if particle is in interaction with given particle P, and if so, returns vector of pointer to t...
Definition: BaseParticle.cc:690
Mdouble getRadius() const
Returns the particle's radius.
Definition: BaseParticle.h:348
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species)
Definition: BaseParticle.cc:553
void setSpecies(const ParticleSpecies *species)
Definition: BaseParticle.cc:818
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:169
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1427
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:808
WallHandler wallHandler
An object of the class WallHandler. Contains pointers to all the walls created.
Definition: DPMBase.h:1447
Vec3D getMax() const
Definition: DPMBase.h:670
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1118
Vec3D getMin() const
Definition: DPMBase.h:664
InteractionHandler interactionHandler
An object of the class InteractionHandler.
Definition: DPMBase.h:1467
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1437
void forceWriteOutputFiles()
Writes output files immediately, even if the current time step was not meant to be written....
Definition: DPMBase.cc:4028
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1082
void setTime(Mdouble time)
Sets a new value for the current simulation time.
Definition: DPMBase.cc:836
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:48
void clear() override
Empties the whole ParticleHandler by removing all BaseParticle.
Definition: ParticleHandler.cc:977
void setDensity(Mdouble density)
Definition: ParticleSpecies.cc:108
Mdouble getDensity() const
Allows density_ to be accessed.
Definition: ParticleSpecies.cc:118
Contains material and contact force properties.
Definition: Species.h:35
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
Definition: Vector.h:51
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:89
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:102
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:331
const Mdouble pi
Definition: ExtendedMath.h:45
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51
Mdouble round(Mdouble value, unsigned int precision)
rounds a floating point number with a given precision
Definition: MathHelpers.cc:28

References cFile_, BaseHandler< T >::clear(), ParticleHandler::clear(), BaseHandler< T >::copyAndAddObject(), dpm, ERROR, DPMBase::forceWriteOutputFiles(), ParticleSpecies::getDensity(), BaseInteraction::getDistance(), BaseParticle::getInteractionWith(), Vec3D::getLength(), DPMBase::getMax(), DPMBase::getMin(), BaseHandler< T >::getNumberOfObjects(), BaseHandler< T >::getObject(), BaseHandler< T >::getObjectById(), BaseInteraction::getOverlap(), BaseInteractable::getPosition(), BaseParticle::getRadius(), DPMBase::getTime(), constants::i, INFO, DPMBase::interactionHandler, logger, Vec3D::max(), Vec3D::min(), P3, DPMBase::particleHandler, pFile_, constants::pi, helpers::round(), BaseInteraction::setContactPoint(), ParticleSpecies::setDensity(), BaseInteraction::setDistance(), BaseInteraction::setForce(), DPMBase::setMax(), DPMBase::setMin(), BaseInteraction::setNormal(), BaseInteraction::setOverlap(), BaseInteractable::setPosition(), BaseParticle::setRadius(), BaseParticle::setSpecies(), BaseWall::setSpecies(), BaseHandler< T >::setStorageCapacity(), DPMBase::setTime(), BaseInteractable::setVelocity(), DPMBase::speciesHandler, version_, DPMBase::wallHandler, and wFile_.

Referenced by main().

Member Data Documentation

◆ cFile_

std::ifstream FileReader::cFile_
private

Referenced by FileReader(), read(), and ~FileReader().

◆ dpm

Mercury3D FileReader::dpm
private

Referenced by FileReader(), and read().

◆ pFile_

std::ifstream FileReader::pFile_
private

Pointers for the input files.

Referenced by FileReader(), read(), and ~FileReader().

◆ version_

enum FileReader::Version FileReader::version_
private

Referenced by FileReader(), and read().

◆ wFile_

std::ifstream FileReader::wFile_
private

Referenced by FileReader(), read(), and ~FileReader().


The documentation for this class was generated from the following file: