MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
File.cc
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 #include "File.h"
28 
29 #include <string>
30 #include <sstream>
31 #include <iostream>
32 #include <cmath>
33 #include <iomanip>
34 #include "Logger.h"
35 
44 std::string to_string_padded(unsigned int value)
45 {
46  std::ostringstream out;
47  out << std::setw(4) << std::setfill('0') << value;
48  return out.str();
49 }
50 
56 std::ostream& operator<<(std::ostream& os, FileType fileType)
57 {
58  if (fileType == FileType::NO_FILE)
59  os << "NO_FILE";
60  else if (fileType == FileType::ONE_FILE)
61  os << "ONE_FILE";
62  else if (fileType == FileType::MULTIPLE_FILES)
63  os << "MULTIPLE_FILES";
64  else if (fileType == FileType::MULTIPLE_FILES_PADDED)
65  os << "MULTIPLE_FILES_PADDED";
66  else
67  {
68  std::cerr << "FileType not recognized" << std::endl;
69  exit(-1);
70  }
71  return os;
72 }
73 
79 std::istream& operator>>(std::istream& is, FileType& fileType)
80 {
81  std::string fileTypeString;
82  is >> fileTypeString;
83  if (!fileTypeString.compare("NO_FILE"))
84  fileType = FileType::NO_FILE;
85  else if (!fileTypeString.compare("ONE_FILE"))
86  fileType = FileType::ONE_FILE;
87  else if (!fileTypeString.compare("MULTIPLE_FILES"))
88  fileType = FileType::MULTIPLE_FILES;
89  else if (!fileTypeString.compare("MULTIPLE_FILES_PADDED"))
91  else
92  {
93  logger(ERROR, "operator>>: FileType % not recognized", fileTypeString);
94  }
95  return is;
96 }
97 
103 {
104  //sets the default for the number of time steps to be skipped
105  //in between each saved "snapshot" of the system to zero
106  //(i.e. records every time step by default)
107  saveCount_ = 0;
108 
109  // file name has to be set by the user
110  name_ = "out";
111 
112  // output into a single file by default
114 
115  // counter of currently open file set to 0 by default
116  counter_ = 0;
117 
118  //stores the time step of the last write/read operation; NEVER by default
120 
121  //sets the default openMode to "out"
122  //i.e. files will by default be written to, not read from.
123  openMode_ = std::fstream::out;
124 
125  //sets the default logarithmicSaveCount to zero
127 }
128 
133 File::File(const File& f)
134 {
136  name_ = f.name_;
137  fileType_ = f.fileType_;
138  counter_ = f.counter_;
140  openMode_ = f.openMode_;
142 }
143 
147 File::~File()
148 = default;
149 
154 std::fstream& File::getFstream()
155 {
156  return fstream_;
157 }
158 
159 //void File::setFstream(const std::fstream& file)
160 //{
161 // this->fstream_ = file;
162 //}
166 const std::string& File::getName() const
167 {
168  return name_;
169 }
170 
171 const std::string File::getFullName() const
172 {
173  return getFullName(getCounter() - 1);
174 }
175 
180 const std::string File::getFullName(unsigned counter) const
181 {
182  //get the full file name
183  std::stringstream lastName("");
184  lastName << name_;
186  {
187  lastName << "." << counter;
188  }
190  {
191  lastName << "." << to_string_padded(counter);
192  }
193  return lastName.str();
194 }
195 
199 void File::setName(const std::string& name)
200 {
201  logger.assert_always(!getName().empty(), "Error: Name cannot be empty");
202  this->name_ = name;
203 }
204 
209 {
210  return fileType_;
211 }
212 
217 {
218  fileType_ = fileType;
219 }
220 
224 unsigned int File::getCounter() const
225 {
226  return counter_;
227 }
228 
232 void File::setCounter(unsigned int counter)
233 {
234  counter_ = counter;
235 }
236 
240 std::fstream::openmode File::getOpenMode() const
241 {
242  return openMode_;
243 }
244 
248 void File::setOpenMode(std::fstream::openmode openMode)
249 {
250  openMode_ = openMode;
251 }
252 
256 unsigned int File::getSaveCount() const
257 {
258  return saveCount_;
259 }
260 
266 {
267  saveCount_ = NEVER;
268 }
269 
274 void File::setSaveCount(unsigned int saveCount)
275 {
276  saveCount_ = saveCount;
277 }
278 
279 
284 void File::setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase)
285 {
286  logger.assert_always(logarithmicSaveCountBase > 1, "logarithmicSaveCountBase should always be larger than 1");
287  logarithmicSaveCountBase_ = logarithmicSaveCountBase;
288 }
289 
294 unsigned int File::getLastSavedTimeStep() const
295 {
296  return lastSavedTimeStep_;
297 }
298 
303 void File::setLastSavedTimeStep(unsigned int lastSavedTimeStep)
304 {
305  lastSavedTimeStep_ = lastSavedTimeStep;
306 }
307 
313 bool File::saveCurrentTimeStep(unsigned int ntimeSteps) {
315 }
316 
317 
318 bool File::saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps)
319 {
320  /* check:
321  * - if this time step should be written
322  * - if the file type is not NO_FILE
323  * - if file can be opened
324  * in that case, change lastSavedTimeStep and return true;
325  */
326  if ((lastSavedTimeStep_ == NEVER || ntimeSteps >= lastSavedTimeStep_ + saveCount_))
327  {
328  //note: do not do the following at t = 0 because this makes no sense for a logarithm
329  if (logarithmicSaveCountBase_ > 1 && ntimeSteps > 0 &&
331  {
332  /*calculate the new saveCount base on the user input logarithmicSaveCountBase,
333  *and multiply by the actual number of time steps
334  */
336  }
337  return true;
338  } else {
339  return false;
340  }
341 }
342 
349 {
350  //close old file if multi-file output
352  {
353  fstream_.close();
354  }
355 
356  counter_++;
357 
358  if (!fstream_.is_open())
359  {
360  fstream_.open(getFullName().c_str(), openMode_);
361  if (!fstream_.is_open())
362  {
363  return false;
364  }
365  }
366 
367  return true;
368 }
369 
373 bool File::open(std::fstream::openmode openMode)
374 {
375  setOpenMode(openMode);
376  return open();
377 }
378 
382 bool File::openWrite(unsigned nTimeSteps)
383 {
384  setLastSavedTimeStep(nTimeSteps);
385  if (getFileType() == FileType::ONE_FILE && getCounter() != 0)
386  {
387  setOpenMode(std::fstream::out | std::fstream::app);
388  }
389  else
390  {
391  setOpenMode(std::fstream::out);
392  }
393  return open();
394 }
395 
399 bool File::openWriteNoAppend(unsigned nTimeSteps)
400 {
401  setLastSavedTimeStep(nTimeSteps);
402  return open(std::fstream::out);
403 }
404 
409 {
410  fstream_.close();
411 }
412 
418 void File::read(std::istream& is)
419 {
420  std::string dummy;
421  is >> dummy;
422  if (!dummy.compare("name"))
423  is >> name_ >> dummy;
424  is >> fileType_;
425  is >> dummy >> saveCount_;
426  is >> dummy >> counter_;
427  if (counter_ != 0)
428  {
429  is >> dummy >> lastSavedTimeStep_;
430  }
431  else
432  {
434  }
435  //if (dummy != "lastSavedTimeStep") lastSavedTimeStep_=SAVE;
436 }
437 
443 void File::write(std::ostream& os) const
444 {
445  //only write name if it differs from the default name
446  if (getFullName().compare(name_))
447  os << "name " << name_ << " ";
448  os << "fileType " << fileType_;
449  os << " saveCount " << saveCount_;
450  os << " counter " << counter_;
451  if (counter_ != 0)
452  {
453  os << " lastSavedTimeStep " << lastSavedTimeStep_;
454  }
456 }
457 
463 std::ostream& operator<<(std::ostream& os, const File& o)
464 {
465  o.write(os);
466  return os;
467 }
468 
474 std::istream& operator>>(std::istream& is, File& o)
475 {
476  o.read(is);
477  return (is);
478 }
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
each time-step will be written into/read from separate files numbered consecutively, with numbers padded by zeros to a minimum of four digits: name_.0000, name_.0001, ..
bool saveCurrentTimeStep(unsigned int ntimeSteps)
determined if this time step has to be written; if so, opens the output file
Definition: File.cc:313
FileType getFileType() const
Gets the file type e.g. NOFILE, ONEFILE and MULTIPLE FILES. File::fileType_.
Definition: File.cc:208
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
std::fstream::openmode getOpenMode() const
Allows the user to know the file mode i.e. gets File::openMode_.
Definition: File.cc:240
void read(std::istream &is)
read function, which accepts an input stream std::istream.
Definition: File.cc:418
void setLastSavedTimeStep(unsigned int lastSavedTimeStep)
Sets File::nextSavedTimeStep_.
Definition: File.cc:303
void setCounter(unsigned int counter)
Allows the user to set the file counter according to his need. Sets File::counter_.
Definition: File.cc:232
const std::string getFullName() const
Also allows to access the file name, however with additional information which is the file counter...
Definition: File.cc:171
unsigned int lastSavedTimeStep_
the time step at which the next write or read operation has to happen.
Definition: File.h:287
void writeFirstAndLastTimeStep()
Sets File::saveCount_ to the highest possible value such that only the first and last time step is wr...
Definition: File.cc:265
LL< Log::ERROR > ERROR
Error log level.
Definition: Logger.cc:53
bool openWrite(unsigned)
First sets openmode to write (and append in some cases), then calls open().
Definition: File.cc:382
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:40
unsigned int counter_
counts the number of already opened files, i.e. counter=1 means .0000 exists
Definition: File.h:265
const unsigned NEVER
Definition: File.h:35
unsigned int getSaveCount() const
Gets File::saveCount_.
Definition: File.cc:256
void close()
Closes the file by calling fstream_.close()
Definition: File.cc:408
std::fstream::openmode openMode_
A variable to indicate how the file should be opened i.e. in, out, ... see http://en.cppreference.com (std::fstream::out by default)
Definition: File.h:270
unsigned int getCounter() const
In case of multiple files, File::getCounter() returns the the number (FILE::Counter_) of the next fil...
Definition: File.cc:224
file will not be created/read
void setlogarithmicSaveCount(const Mdouble logarithmicSaveCountBase)
the function to set the user input base of logarithmic saving count
Definition: File.cc:284
std::fstream & getFstream()
Allows to access the member variable File::fstream_.
Definition: File.cc:154
File()
constructor
Definition: File.cc:102
all data will be written into/ read from a single file called name_
std::istream & operator>>(std::istream &is, FileType &fileType)
Reads the FileType from an input stream 'is'.
Definition: File.cc:79
bool saveCurrentTimeStepNoFileTypeCheck(unsigned int ntimeSteps)
Definition: File.cc:318
unsigned int saveCount_
Allows one to define the number of time steps to be skipped to make a snap shot. E.g. TMax = 100, saveCount_ = 10, timeStep = 1; It stores data at t={0,10,20,30,40...100}. And if TMax =101, it stores data at t={0,10,20,30,...100,101}.
Definition: File.h:276
bool openWriteNoAppend(unsigned)
Definition: File.cc:399
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_.
Definition: File.cc:274
void setFileType(FileType fileType)
Sets the type of file needed to write into or read from. File::fileType_.
Definition: File.cc:216
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
Mdouble logarithmicSaveCountBase_
the switch allow user to set saveCount in logarithmic timescale with equal distance ...
Definition: File.h:282
unsigned int getLastSavedTimeStep() const
Gets File::nextSavedTimeStep_.
Definition: File.cc:294
bool open()
Checks if the file stream fstream_ has any issues while opening. Alongside, it also increments the ne...
Definition: File.cc:348
bool compare(std::istream &is, std::string s)
Checks if the next argument in the input stream is a certain string.
Definition: Helpers.cc:860
std::string name_
name of the file.
Definition: File.h:249
virtual ~File()
destructor
each time-step will be written into/read from separate files numbered consecutively: name_...
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
void setName(const std::string &name)
Sets the file name, e.g. "Name.data".
Definition: File.cc:199
Definition: File.h:80
void write(std::ostream &os) const
print function, which accepts an std::stringstream as input.
Definition: File.cc:443
const std::string & getName() const
Allows to access the file name, e.g., "problem.data".
Definition: File.cc:166
std::fstream fstream_
Stream object used to read/write data files.
Definition: File.h:254
void setOpenMode(std::fstream::openmode openMode)
Allows the user to Sets File::openMode_.
Definition: File.cc:248