DataFiles Class Reference

Public Member Functions

 DataFiles (int argc, char *argv[])
 
 ~DataFiles ()
 
void write ()
 

Private Member Functions

bool writeTimeStep ()
 

Private Attributes

std::vector< IFileiFiles
 
std::ofstream oFile
 
unsigned oCount = 0
 
std::string ending = ""
 

Constructor & Destructor Documentation

◆ DataFiles()

DataFiles::DataFiles ( int  argc,
char argv[] 
)
inline

opens the input and output data files

37  {
38  std::string name = getName(argc, argv);
39 
40  //check if there is an ending (the user might just specify the file root)
41  size_t dot = name.find_last_of('.');
42  // if no ending is found, assume .data0
43  if (dot==std::string::npos) {
44  name += ".data0";
45  }
46 
47  //take off ending .0000 if necessary
48  dot = name.find_last_of('.');
49  char afterDot = name[dot + 1];
50  //if last ending is a number
51  if (afterDot >= '0' && afterDot <= '9') {
52  ending = name.substr(dot);
53  name.resize(dot);
54  }
55 
56  // take off ending .data0
57  {
58  size_t dot = name.find_last_of('.');
59  name.resize(dot);
60  }
61 
62  // opens the input data files, if they exist
63  for(unsigned processorID = 0; true; ++processorID) {
64  //get input file name
65  std::string fileName = name + ".data" + std::to_string(processorID) + ending;
66  auto* file = new std::ifstream(fileName.c_str());
67  if (file->fail()) {
68  break;
69  } else {
70  iFiles.emplace_back(file,0);
71  logger(INFO,"Opened % for input",fileName);
72  }
73  }
74  logger.assert_always(!iFiles.empty(),"No input file found with name % ",name + ".data0" + ending);
75 
76  // opens output data file
77  std::string oFileName = name + ".data" + ending;
78  oFile.open(oFileName);
79  if (oFile.fail()) {
80  logger(ERROR,"% could not be opened",oFileName);
81  } else {
82  logger(INFO, "Opened % for output", oFileName);
83  }
84 
85  write();
86  }
std::string getName(int argc, char *argv[])
Definition: CombineParallelDataFiles.cpp:12
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ INFO
@ ERROR
std::ofstream oFile
Definition: CombineParallelDataFiles.cpp:27
std::vector< IFile > iFiles
Definition: CombineParallelDataFiles.cpp:26
void write()
Definition: CombineParallelDataFiles.cpp:100
std::string ending
Definition: CombineParallelDataFiles.cpp:29
std::string name
Definition: MercuryProb.h:48

References ending, ERROR, getName(), iFiles, INFO, logger, units::name, oFile, and write().

◆ ~DataFiles()

DataFiles::~DataFiles ( )
inline

closes the input/output data files

91  {
92  for (auto iFile : iFiles) {
93  delete iFile.file;
94  }
95  }

References iFiles.

Member Function Documentation

◆ write()

void DataFiles::write ( )
inline

write all time steps

100  {
101  while (writeTimeStep()) {};
102  if (oCount!=1) logger(INFO,"Written % time steps",oCount);
103  }
bool writeTimeStep()
Definition: CombineParallelDataFiles.cpp:110
unsigned oCount
Definition: CombineParallelDataFiles.cpp:28

References INFO, logger, oCount, and writeTimeStep().

Referenced by DataFiles().

◆ writeTimeStep()

bool DataFiles::writeTimeStep ( )
inlineprivate

writes single timestep

110  {
111  // read header line
112  static Mdouble time = 0;
113  unsigned n = 0;
114  std::string line;
115  for(auto& iFile : iFiles) {
116  // read number of particles
117  *iFile.file >> iFile.n;
118  n += iFile.n;
119  // read time stamp
120  *iFile.file >> iFile.time;
121  //stop if this was last time step
122  if (iFile.file->fail()) return false;
123  //read rest of line
124  std::getline(*iFile.file, line);
125  //check this is indeed a header line
126  static std::string headerLine = line;
127  // this is the error-catching routine:
128  // if this is not a true header line, or the timestamp is in the past, keep reading
129  if (line != headerLine || iFile.time < time) {
130  do {
131  *iFile.file >> iFile.n;
132  n += iFile.n;
133  *iFile.file >> iFile.time;
134  if (iFile.file->fail()) return false;
135  std::getline(*iFile.file, line);
136  } while (line != headerLine || iFile.time < time);
137  logger(WARN,"Mistake detected; moving forward to time %",iFile.time);
138  oCount--; //remove last-written timestep (if MULTIPLE_FILES)
139  }
140  time = iFile.time;
141  }
142  time *= 1.000000001; //so the next time step is surely bigger than the last
143 
144  //write header
145  oFile << n << ' ' << time << line << '\n';
146  logger(INFO,"Writing % %%",n,time,line);
147  // write content
148  for(auto& iFile : iFiles) {
149  for (unsigned i=0; i<iFile.n; ++i) {
150  std::getline(*iFile.file, line);
151  oFile << line << '\n';
152  }
153  }
154  ++oCount;
155  return true;
156  }
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
double Mdouble
Definition: GeneralDefine.h:34
@ WARN
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51

References constants::i, iFiles, INFO, logger, n, oCount, oFile, and WARN.

Referenced by write().

Member Data Documentation

◆ ending

std::string DataFiles::ending = ""
private

Referenced by DataFiles().

◆ iFiles

std::vector<IFile> DataFiles::iFiles
private

◆ oCount

unsigned DataFiles::oCount = 0
private

Referenced by write(), and writeTimeStep().

◆ oFile

std::ofstream DataFiles::oFile
private

Referenced by DataFiles(), and writeTimeStep().


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