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

41  {
42  std::string name = getName(argc, argv);
43 
44  //check if there is an ending (the user might just specify the file root)
45  size_t dot = name.find_last_of('.');
46  // if no ending is found, assume .data0
47  if (dot==std::string::npos) {
48  name += ".data0";
49  }
50 
51  //take off ending .0000 if necessary
52  dot = name.find_last_of('.');
53  char afterDot = name[dot + 1];
54  //if last ending is a number
55  if (afterDot >= '0' && afterDot <= '9') {
56  ending = name.substr(dot);
57  name.resize(dot);
58  }
59 
60  // take off ending .data0
61  {
62  size_t dot = name.find_last_of('.');
63  name.resize(dot);
64  }
65 
66  // opens the input data files, if they exist
67  for(unsigned processorID = 0; true; ++processorID) {
68  //get input file name
69  std::string fileName = name + ".data" + std::to_string(processorID) + ending;
70  auto* file = new std::ifstream(fileName.c_str());
71  if (file->fail()) {
72  break;
73  } else {
74  iFiles.emplace_back(file,0);
75  logger(INFO,"Opened % for input",fileName);
76  }
77  }
78  logger.assert_always(!iFiles.empty(),"No input file found with name % ",name + ".data0" + ending);
79 
80  // opens output data file
81  std::string oFileName = name + ".data" + ending;
82  oFile.open(oFileName);
83  if (oFile.fail()) {
84  logger(ERROR,"% could not be opened",oFileName);
85  } else {
86  logger(INFO, "Opened % for output", oFileName);
87  }
88 
89  write();
90  }
std::string getName(int argc, char *argv[])
Definition: CombineParallelDataFiles.cpp:16
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:31
std::vector< IFile > iFiles
Definition: CombineParallelDataFiles.cpp:30
void write()
Definition: CombineParallelDataFiles.cpp:104
std::string ending
Definition: CombineParallelDataFiles.cpp:33
Scalar EIGEN_BLAS_FUNC_NAME() dot(int *n, Scalar *px, int *incx, Scalar *py, int *incy)
Definition: level1_real_impl.h:52
string fileName
Definition: UniformPSDSelfTest.py:10
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
std::string to_string(T object, unsigned float_precision=8)
Definition: oomph_utilities.h:189
string name
Definition: plotDoE.py:33

References dot(), ending, ERROR, UniformPSDSelfTest::fileName, getName(), iFiles, INFO, logger, plotDoE::name, oFile, oomph::Global_string_for_annotation::string(), oomph::StringConversion::to_string(), and write().

◆ ~DataFiles()

DataFiles::~DataFiles ( )
inline

closes the input/output data files

95  {
96  for (auto iFile : iFiles) {
97  delete iFile.file;
98  }
99  }

References iFiles.

Member Function Documentation

◆ write()

void DataFiles::write ( )
inline

write all time steps

104  {
105  while (writeTimeStep()) {};
106  if (oCount!=1) logger(INFO,"Written % time steps",oCount);
107  }
bool writeTimeStep()
Definition: CombineParallelDataFiles.cpp:114
unsigned oCount
Definition: CombineParallelDataFiles.cpp:32

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

Referenced by DataFiles().

◆ writeTimeStep()

bool DataFiles::writeTimeStep ( )
inlineprivate

writes single timestep

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

References i, iFiles, INFO, calibrate::line, logger, n, oCount, oFile, oomph::Global_string_for_annotation::string(), 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: