revision: v0.14
File.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, 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 
27 #ifndef FILE_H
28 #define FILE_H
29 
30 #include <fstream>
31 #include "GeneralDefine.h"
32 
33 // A value of File::lastSavedTimeStep_ = NEVER indicates a file was never been written
35 const unsigned NEVER = static_cast<const unsigned int>(-1);
36 
40 enum class FileType : unsigned char
41 {
45  NO_FILE = 0,
49  ONE_FILE = 1,
53  MULTIPLE_FILES = 2,
58 };
59 
60 
61 std::string to_string_padded(unsigned int value);
62 
66 std::ostream& operator<<(std::ostream& os, FileType fileType);
67 
71 std::istream& operator>>(std::istream& is, FileType& fileType);
72 
80 class File
81 {
82 public:
83 
84  //constructors
88  File();
89 
93  File(const File&);
94 
98  virtual ~File();
99 
100 
101  //set and get functions
105  std::fstream& getFstream();
106 
110  const std::string& getName() const;
111 
115  const std::string getFullName() const;
116 
120  const std::string getFullName(unsigned) const;
121 
126  void setName(const std::string& name);
127 
131  FileType getFileType() const;
132 
136  void setFileType(FileType fileType);
137 
141  unsigned int getCounter() const;
142 
146  void setCounter(unsigned int counter);
147 
149 
151 
155  std::fstream::openmode getOpenMode() const;
156 
160  void setOpenMode(std::fstream::openmode openMode);
161 
165  unsigned int getSaveCount() const;
166 
171 
175  void setSaveCount(unsigned int saveCount);
176 
180  unsigned int getLastSavedTimeStep() const;
181 
185  void setLastSavedTimeStep(unsigned int lastSavedTimeStep);
186 
190  bool saveCurrentTimeStep(unsigned int ntimeSteps);
191  bool saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps);
192 
196  void read(std::istream& is);
197 
201  void write(std::ostream& os) const;
202 
207  friend std::ostream& operator<<(std::ostream& os, const File& o);
208 
213  friend std::istream& operator>>(std::istream& is, File& o);
214 
215  //member functions (other than set/get)
216 
221  bool open();
222 
226  bool openWrite(unsigned);
227 
228  bool openWriteNoAppend(unsigned);
229 
233  bool open(std::fstream::openmode openMode);
234 
238  void close();
239 
243  void setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase);
244 
245 private:
249  std::string name_;
250 
254  std::fstream fstream_;
255 
261 
265  unsigned int counter_;
266 
270  std::fstream::openmode openMode_;
271 
276  unsigned int saveCount_;
277 
283 
287  unsigned int lastSavedTimeStep_;
288 };
289 
290 #endif /* FILE_H */
ExtremeOverlapWithWallsUnitTest::setupInitialConditions
void setupInitialConditions() override
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: ExtremeOverlapWithWallsUnitTest.cpp:58
Vec3D::isEqualTo
bool isEqualTo(const Vec3D &other, double tol) const
Checks if the length this Vec3D is equal the length of other with a certain tolerance.
Definition: Vector.cc:294
LinearViscoelasticSlidingFrictionSpecies.h
DPMBase::setName
void setName(const std::string &name)
Allows to set the name of all the files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:420
operator>>
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:78
File::getName
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:165
File::close
void close()
Closes the file by calling fstream_.close()
Definition: File.cc:407
File::setFileType
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:215
DPMBase::setTimeStep
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1225
File::openWriteNoAppend
bool openWriteNoAppend(unsigned)
Definition: File.cc:398
File::logarithmicSaveCountBase_
Mdouble logarithmicSaveCountBase_
the switch allow user to set saveCount in logarithmic timescale with equal distance ,...
Definition: File.h:282
to_string_padded
std::string to_string_padded(unsigned int value)
Pads the number This function tries to pad the number to 4 digits, which is used when you create mult...
Definition: File.cc:44
LinearViscoelasticSlidingFrictionSpecies
Species< LinearViscoelasticNormalSpecies, SlidingFrictionSpecies > LinearViscoelasticSlidingFrictionSpecies
Definition: LinearViscoelasticSlidingFrictionSpecies.h:34
File::saveCurrentTimeStepNoFileTypeCheck
bool saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps)
Definition: File.cc:317
constants::pi
const Mdouble pi
Definition: ExtendedMath.h:45
File::setlogarithmicSaveCount
void setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase)
the function to set the user input base of logarithmic saving count
Definition: File.cc:283
ExtremeOverlapWithWallsUnitTest
Compresses 2 particles (vertically) until they have an extreme overlap.
Definition: ExtremeOverlapWithWallsUnitTest.cpp:55
File::getLastSavedTimeStep
unsigned int getLastSavedTimeStep() const
Gets File::nextSavedTimeStep_.
Definition: File.cc:293
File::saveCount_
unsigned int saveCount_
Allows one to define the number of time steps to be skipped to make a snap shot. E....
Definition: File.h:276
File::getOpenMode
std::fstream::openmode getOpenMode() const
Allows the user to know the file mode i.e. gets File::openMode_.
Definition: File.cc:239
File::~File
virtual ~File()
destructor
File::operator<<
friend std::ostream & operator<<(std::ostream &os, const File &o)
Operator overloading used to write data obtained from an object of class File into an output stream....
Definition: File.cc:462
File::decreaseCounter
void decreaseCounter()
Definition: File.h:150
BaseInteractable::setPosition
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:239
logger
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
main
int main(int argc, char *argv[])
Definition: ExtremeOverlapWithWallsUnitTest.cpp:108
File::operator>>
friend std::istream & operator>>(std::istream &is, File &o)
Operator overloading used to read data from the input stream into member variables of an object of cl...
Definition: File.cc:473
File::getCounter
unsigned int getCounter() const
In case of multiple files, File::getCounter() returns the the number (FILE::Counter_) of the next fil...
Definition: File.cc:223
File::name_
std::string name_
name of the file.
Definition: File.h:249
DPMBase::setYMax
void setYMax(Mdouble newYMax)
Sets the value of YMax, the upper bound of the problem domain in the y-direction.
Definition: DPMBase.cc:1182
File::setSaveCount
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_.
Definition: File.cc:273
FileType::NO_FILE
@ NO_FILE
file will not be created/read
to_string_padded
std::string to_string_padded(unsigned int value)
Pads the number This function tries to pad the number to 4 digits, which is used when you create mult...
Definition: File.cc:44
INFO
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
main
int main(int argc, char *argv[])
Definition: ExtremeOverlapUnitTest.cpp:100
BaseParticle::setRadius
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species)
Definition: BaseParticle.cc:542
Vec3D
Definition: Vector.h:50
DPMBase::solve
void solve()
The work horse of the code.
Definition: DPMBase.cc:4003
File
Definition: File.h:81
DPMBase::setZMax
void setZMax(Mdouble newZMax)
Sets the value of ZMax, the upper bound of the problem domain in the z-direction.
Definition: DPMBase.cc:1208
File::open
bool open()
Checks if the file stream fstream_ has any issues while opening. Alongside, it also increments the ne...
Definition: File.cc:347
Mdouble
double Mdouble
Definition: GeneralDefine.h:34
operator>>
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:78
operator<<
std::ostream & operator<<(std::ostream &os, FileType fileType)
Writes the FileType as a human-readable string into the output stream 'os'.
Definition: File.cc:56
char
File::setOpenMode
void setOpenMode(std::fstream::openmode openMode)
Allows the user to Sets File::openMode_.
Definition: File.cc:247
BaseInteractable::setVelocity
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:350
File::saveCurrentTimeStep
bool saveCurrentTimeStep(unsigned int ntimeSteps)
determined if this time step has to be written; if so, opens the output file
Definition: File.cc:312
double
ERROR
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
mathsFunc::sin
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
Logger.h
BaseParticle::setSpecies
void setSpecies(const ParticleSpecies *species)
Definition: BaseParticle.cc:804
LinearViscoelasticSpecies
Species< LinearViscoelasticNormalSpecies > LinearViscoelasticSpecies
Definition: LinearViscoelasticSpecies.h:33
LinearViscoelasticSpecies.h
File::setName
void setName(const std::string &name)
Sets the file name, e.g. "Name.data".
Definition: File.cc:198
File::getSaveCount
unsigned int getSaveCount() const
Gets File::saveCount_.
Definition: File.cc:255
File::fileType_
FileType fileType_
fileType_ indicates the type of the files. Whether it is No file, one file or multiple file as descri...
Definition: File.h:260
DPMBase::setXMax
void setXMax(Mdouble newXMax)
Sets the value of XMax, the upper bound of the problem domain in the x-direction.
Definition: DPMBase.cc:1156
InfiniteWall.h
DPMBase::speciesHandler
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. LinearViscoelasticSpecies, etc.
Definition: DPMBase.h:1385
ExtremeOverlapUnitTest::setupInitialConditions
void setupInitialConditions() final
Definition: ExtremeOverlapUnitTest.cpp:52
operator<<
std::ostream & operator<<(std::ostream &os, FileType fileType)
Writes the FileType as a human-readable string into the output stream 'os'.
Definition: File.cc:56
DPMBase.h
File::fstream_
std::fstream fstream_
Stream object used to read/write data files.
Definition: File.h:254
SphericalParticle
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
DPMBase::wallHandler
WallHandler wallHandler
An object of the class WallHandler. Contains pointers to all the walls created.
Definition: DPMBase.h:1405
File::setLastSavedTimeStep
void setLastSavedTimeStep(unsigned int lastSavedTimeStep)
Sets File::nextSavedTimeStep_.
Definition: File.cc:302
DPMBase::setDimension
void setDimension(unsigned int newDim)
Sets both the system dimensions and the particle dimensionality.
Definition: DPMBase.cc:1394
DPMBase::setTimeMax
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:870
DPMBase::setGravity
void setGravity(Vec3D newGravity)
Sets a new value for the gravitational acceleration.
Definition: DPMBase.cc:1374
NEVER
const unsigned NEVER
Definition: File.h:35
InfiniteWall
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:48
FATAL
LL< Log::FATAL > FATAL
Definition of the different loglevels by its wrapper class LL. These are used as tags in template met...
Definition: Logger.cc:52
File::lastSavedTimeStep_
unsigned int lastSavedTimeStep_
the time step at which the next write or read operation has to happen.
Definition: File.h:287
File::write
void write(std::ostream &os) const
print function, which accepts an std::stringstream as input.
Definition: File.cc:442
BaseWall::setSpecies
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:171
DPMBase::setYMin
void setYMin(Mdouble newYMin)
Sets the value of YMin, the lower bound of the problem domain in the y-direction.
Definition: DPMBase.cc:1025
File::writeFirstAndLastTimeStep
void writeFirstAndLastTimeStep()
Sets File::saveCount_ to the highest possible value such that only the first and last time step is wr...
Definition: File.cc:264
DPMBase::setFileType
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:457
BaseInteractable::setPrescribedPosition
void setPrescribedPosition(const std::function< Vec3D(double)> &prescribedPosition)
Allows the position of an infinite mass interactable to be prescribed.
Definition: BaseInteractable.cc:413
BaseHandler::getObject
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
File::openWrite
bool openWrite(unsigned)
First sets openmode to write (and append in some cases), then calls open().
Definition: File.cc:381
File::setCounter
void setCounter(unsigned int counter)
Allows the user to set the file counter according to his need. Sets File::counter_.
Definition: File.cc:231
GeneralDefine.h
BaseHandler::copyAndAddObject
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
File::read
void read(std::istream &is)
read function, which accepts an input stream std::istream.
Definition: File.cc:417
DPMBase::getZMax
Mdouble getZMax() const
If the length of the problem domain in z-direction is ZMax - ZMin, then getZMax() returns ZMax.
Definition: DPMBase.h:631
DPMBase::setXMin
void setXMin(Mdouble newXMin)
Sets the value of XMin, the lower bound of the problem domain in the x-direction.
Definition: DPMBase.cc:1001
File::increaseCounter
void increaseCounter()
Definition: File.h:148
unsigned
DPMBase::particleHandler
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created.
Definition: DPMBase.h:1395
DPMBase::setSaveCount
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:406
FileType
FileType
With FileType options, one is able to choose if data is to be read/written from/into no or single or ...
Definition: File.h:41
File::openMode_
std::fstream::openmode openMode_
A variable to indicate how the file should be opened i.e. in, out, ... see http://en....
Definition: File.h:270
InfiniteWall::set
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
Definition: InfiniteWall.cc:118
File::getFstream
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:153
helpers::compare
bool compare(std::istream &is, std::string s)
Checks if the next argument in the input stream is a certain string.
Definition: Helpers.cc:858
DPMBase::dataFile
File dataFile
An instance of class File to handle in- and output into a .data file.
Definition: DPMBase.h:1436
units::name
std::string name
Definition: MercuryProb.h:48
File.h
DPMBase::setZMin
void setZMin(Mdouble newZMin)
Sets the value of ZMin, the lower bound of the problem domain in the z-direction.
Definition: DPMBase.cc:1049
File::File
File()
constructor
Definition: File.cc:101
File::getFileType
FileType getFileType() const
Gets the file type e.g. NOFILE, ONEFILE and MULTIPLE FILES. File::fileType_.
Definition: File.cc:207
BaseInteractable::getPosition
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:218
ExtremeOverlapUnitTest
Makes sure that the behavior is still sensible if the overlap of two particles grows extremely large.
Definition: ExtremeOverlapUnitTest.cpp:44
File::getFullName
const std::string getFullName() const
Also allows to access the file name, however with additional information which is the file counter,...
Definition: File.cc:170
DPMBase
The DPMBase header includes quite a few header files, defining all the handlers, which are essential....
Definition: DPMBase.h:76
File::counter_
unsigned int counter_
counts the number of already opened files, i.e. counter=1 means .0000 exists
Definition: File.h:265