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 
224 {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.

25  {
26  pFile_.open(name+".p3p");
27  if (pFile_) {
29  wFile_.open(name+".p3w");
30  cFile_.open(name+".p3c");
31  if (!wFile_) logger(ERROR, "Could not open %", name + ".p3w");
32  if (!cFile_) logger(ERROR, "Could not open %", name + ".p3c");
33  } else {
35  pFile_.open(name+".p4p");
36  wFile_.open(name+".p4w");
37  cFile_.open(name+".p4c");
38  if (!pFile_) logger(ERROR, "Could not open % or %", name + ".p3p",name + ".p4p");
39  if (!wFile_) logger(ERROR, "Could not open %", name + ".p4w");
40  if (!cFile_) logger(ERROR, "Could not open %", name + ".p4c");
41  }
42  logger(INFO,"Input files opened");
43 
44  dpm.setName(name);
45  }
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:400
std::ifstream wFile_
Definition: ParticleAnalytics2Mercury.cpp:222
std::ifstream cFile_
Definition: ParticleAnalytics2Mercury.cpp:222
std::ifstream pFile_
Pointers for the input files.
Definition: ParticleAnalytics2Mercury.cpp:222
Mercury3D dpm
Definition: ParticleAnalytics2Mercury.cpp:226
string name
Definition: plotDoE.py:33

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

◆ ~FileReader()

FileReader::~FileReader ( )
inline

Destructor, simple closes the file.

212  {
213  pFile_.close();
214  wFile_.close();
215  cFile_.close();
216  logger(INFO,"Input files closed");
217  }

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?
47  {
51 
53 
54  //read first line p3p
55  std::getline(pFile_, line);
56  if (!line.compare(""))
57  return false;
58 
59  //read second line p3p
60  Mdouble time;
61  unsigned N;
62  pFile_ >> time >> N;
63  dpm.setTime(time);
65  std::getline(pFile_, line);
66 
67  //read third line p3p
68  std::getline(pFile_, line);
69 
70  //read next lines p3p
71  logger(INFO,"Reading % particles at time %",N,time);
72  {
74  for (unsigned i = 0; i < N; ++i) {
75  unsigned id, species;
76  Mdouble volume, mass;
77  Vec3D position, velocity;
78  pFile_ >> id >> species >> volume >> mass >> position >> velocity;
79  std::getline(pFile_, line);
80  if (version_==Version::P3) species--;
81  //logger(INFO,"% % % % % %",id, species, volume, mass, position, velocity);
82  // add new species as necessary
83  while (dpm.speciesHandler.getNumberOfObjects() <= species) {
85  species.setDensity(mass / volume);
86  //use an infinite interaction radius
87  species.setAdhesionForceMax(1e20);
88  species.setAdhesionStiffness(1);
89  logger(INFO, "Adding species of density %", species.getDensity());
91  }
92  p.setSpecies(dpm.speciesHandler.getObject(species));
93  p.setRadius(cbrt(0.75 / pi * volume));
94  p.setPosition(position);
95  p.setVelocity(velocity);
97  }
98  }
99  //logger(INFO,"Read % particles",dpm.particleHandler.getNumberOfObjects());
100 
101  //read first line p3c
102  std::getline(cFile_, line);
103  if (!line.compare(""))
104  return false;
105 
106  //read second line p3c
107  cFile_ >> time >> N;
108  if (fabs(dpm.getTime()/time-1)>0.01)
109  logger(ERROR,"Timesteps in p3c and p3p do not agree: % %",dpm.getTime(),time);
111  std::getline(cFile_, line);
112 
113  //read third line p3c
114  std::getline(cFile_, line);
115 
116  //read next lines p3c
117  logger(INFO,"Reading % contacts",N,time);
118  for (unsigned i=0; i<N; ++i) {
119  unsigned id1, id2;
120  Vec3D force, contact;
121  cFile_ >> id1 >> id2;
124  logger.assert_debug(p1 != nullptr, "Particle % does not exist", id1);
125  logger.assert_debug(p2 != nullptr, "Particle % does not exist", id1);
126  Vec3D P1ToP2 = p2->getPosition() - p1->getPosition();
127  BaseInteraction* c = p1->getInteractionWith(p2, time, &dpm.interactionHandler);
128  logger.assert_debug(c != nullptr, "Particle-particle interaction % % does not exist", p1, p2);
129  c->setDistance(P1ToP2.getLength());
130  c->setNormal(P1ToP2 / c->getDistance());
131  c->setOverlap(c->getDistance() - p1->getRadius() - p2->getRadius());
132  if (version_ == Version::P3)
133  {
134  cFile_ >> force;
135  contact = p1->getPosition() - P1ToP2 * ((p1->getRadius() - 0.5 * c->getOverlap()) / c->getDistance());
136  }
137  else
138  {
139  cFile_ >> contact >> force;
140  }
141  std::getline(cFile_, line);
142  c->setContactPoint(contact);
143  c->setForce(force);
144  if (i % (N / 10) == 0)
145  {
146  logger(INFO, "\r %", std::round((double) i / N * 100));
147  }
148  }
149  logger(INFO, "");
150 
151  //read first line p3w
152  std::getline(wFile_, line);
153  if (!line.compare(""))
154  return false;
155 
156  //read second line p3w
157  wFile_ >> time >> N;
158  if (fabs(dpm.getTime() / time - 1) > 0.01)
159  logger(ERROR, "Timesteps in p3w and p3p do not agree");
161  std::getline(wFile_, line);
162 
163  //read third line p3w
164  std::getline(wFile_, line);
165 
166  //create wall
167  InfiniteWall wall;
169  auto w = dpm.wallHandler.copyAndAddObject(wall);
170 
171  //read next lines p3w
172  logger(INFO,"Reading % wall contacts",N,time);
173  for (unsigned i=0; i<N; ++i) {
174  unsigned id;
175  Vec3D force, contact, particleToContact;
176  wFile_ >> id;
178  logger.assert_debug(p!=nullptr,"Particle % does not exist",id);
179  BaseInteraction* c = w->getInteractionWith(p,time,&dpm.interactionHandler);
180  logger.assert_debug(c!= nullptr,"Particle-wall interaction % % does not exist",p,w);
181  if (version_==Version::P3) {
182  wFile_ >> force >> particleToContact;
183  contact = p->getPosition()-particleToContact;
184  } else {
185  wFile_ >> contact >> force;
186  particleToContact = p->getPosition()-contact;
187  }
188  std::getline(wFile_, line);
189  c->setContactPoint(contact);
190  c->setDistance(particleToContact.getLength());
191  c->setNormal(particleToContact/c->getDistance());
192  c->setOverlap(c->getDistance()-p->getRadius());
193  c->setForce(force);
194  }
195 
196  logger(INFO,"Writing output files");
197  for (const auto p : dpm.particleHandler) {
198  dpm.setMin(Vec3D::min(dpm.getMin(),p->getPosition()));
199  dpm.setMax(Vec3D::max(dpm.getMax(),p->getPosition()));
200  }
201  for (const auto c : dpm.interactionHandler) {
202  dpm.setMin(Vec3D::min(dpm.getMin(),c->getContactPoint()));
203  dpm.setMax(Vec3D::max(dpm.getMax(),c->getContactPoint()));
204  }
206  return true;
207  }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Vector3f p1
Definition: MatrixBase_all.cpp:2
RowVector3d w
Definition: Matrix_resize_int.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
virtual unsigned int getNumberOfObjects() const
Gets the number of real Object in this BaseHandler. (i.e. no mpi or periodic particles)
Definition: BaseHandler.h:656
virtual void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0.
Definition: BaseHandler.h:536
void setStorageCapacity(const unsigned int N)
Sets the storage capacity of this BaseHandler.
Definition: BaseHandler.h:677
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:360
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:621
T * getObjectById(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:573
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:197
Stores information about interactions between two interactable objects; often particles but could be ...
Definition: BaseInteraction.h:39
Definition: BaseParticle.h:33
Mdouble getRadius() const
Returns the particle's radius.
Definition: BaseParticle.h:331
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:148
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1433
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:799
WallHandler wallHandler
An object of the class WallHandler. Contains pointers to all the walls created.
Definition: DPMBase.h:1453
Vec3D getMax() const
Returns the maximum coordinates of the problem domain.
Definition: DPMBase.h:659
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1109
Vec3D getMin() const
Returns the minimum coordinates of the problem domain.
Definition: DPMBase.h:653
InteractionHandler interactionHandler
An object of the class InteractionHandler.
Definition: DPMBase.h:1473
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1443
void forceWriteOutputFiles()
Writes output files immediately, even if the current time step was not meant to be written....
Definition: DPMBase.cc:4094
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1073
void setTime(Mdouble time)
Sets a new value for the current simulation time.
Definition: DPMBase.cc:827
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:27
void clear() override
Empties the whole ParticleHandler by removing all BaseParticle.
Definition: ParticleHandler.cc:971
void setDensity(Mdouble density)
Definition: ParticleSpecies.cc:88
Mdouble getDensity() const
Allows density_ to be accessed.
Definition: ParticleSpecies.cc:98
Contains material and contact force properties.
Definition: Species.h:14
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:16
Definition: Kernel/Math/Vector.h:30
static Vec3D max(const Vec3D &a, const Vec3D &b)
Calculates the pointwise maximum of two Vec3D.
Definition: Vector.cc:69
static Vec3D min(const Vec3D &a, const Vec3D &b)
Calculates the pointwise minimum of two Vec3D.
Definition: Vector.cc:82
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:350
@ N
Definition: constructor.cpp:22
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 round(const bfloat16 &a)
Definition: BFloat16.h:646
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T cbrt(const T &x)
Definition: MathFunctions.h:1320
double velocity(const double &t)
Angular velocity as function of time t.
Definition: jeffery_orbit.cc:107
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:117
int c
Definition: calibrate.py:100
line
Definition: calibrate.py:103
const Mdouble pi
Definition: ExtendedMath.h:23
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286

References calibrate::c, Eigen::numext::cbrt(), cFile_, BaseHandler< T >::clear(), ParticleHandler::clear(), BaseHandler< T >::copyAndAddObject(), dpm, ERROR, boost::multiprecision::fabs(), DPMBase::forceWriteOutputFiles(), ParticleSpecies::getDensity(), Vec3D::getLength(), DPMBase::getMax(), DPMBase::getMin(), BaseHandler< T >::getNumberOfObjects(), BaseHandler< T >::getObject(), BaseHandler< T >::getObjectById(), BaseInteractable::getPosition(), BaseParticle::getRadius(), DPMBase::getTime(), i, INFO, DPMBase::interactionHandler, calibrate::line, logger, Vec3D::max(), Vec3D::min(), N, p, p1, P3, DPMBase::particleHandler, pFile_, constants::pi, Eigen::bfloat16_impl::round(), ParticleSpecies::setDensity(), DPMBase::setMax(), DPMBase::setMin(), BaseWall::setSpecies(), BaseHandler< T >::setStorageCapacity(), DPMBase::setTime(), DPMBase::speciesHandler, oomph::Global_string_for_annotation::string(), Jeffery_Solution::velocity(), version_, w, 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: