MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FilesAndRunNumber Class Reference

It is publicly inherited from class Files. It defines an awesome feature that is ideal when doing a parameter study. Below are the routines that manipulate a counter file, called COUNTER_DONOTDEL, to store run numbers. More...

#include <FilesAndRunNumber.h>

+ Inheritance diagram for FilesAndRunNumber:

Public Member Functions

 FilesAndRunNumber ()
 Constructor. More...
 
 FilesAndRunNumber (const FilesAndRunNumber &other)
 Copy constructor. More...
 
virtual ~FilesAndRunNumber ()
 Constructor. More...
 
void constructor ()
 a function called by the FilesAndRunNumber() (constructor) More...
 
void incrementRunNumberInFile ()
 Increment the run Number (counter value) stored in the file_counter (COUNTER_DONOTDEL) by 1 and store the new value in the counter file. More...
 
int readRunNumberFromFile ()
 Read the run number or the counter from the counter file (COUNTER_DONOTDEL) More...
 
void autoNumber ()
 The autoNumber() function is the trigger. It calls three functions. setRunNumber(), readRunNumberFromFile() and incrementRunNumberInFile(). More...
 
std::vector< int > get2DParametersFromRunNumber (int size_x, int size_y)
 This turns a counter into two indices which is an amazing feature for doing two dimensional parameter studies. The indices run from 1:size_x and 1:size_y, while the study number starts at 0 ( initially the counter=1 in COUNTER_DONOTDEL) More...
 
int launchNewRun (const char *name, bool quick=false)
 This launches a code from within this code. Please pass the name of the code to run. More...
 
void setRunNumber (int runNumber)
 This sets the counter/Run number, overriding the defaults. More...
 
int getRunNumber () const
 This returns the current value of the counter (runNumber_) More...
 
void read (std::istream &is)
 Accepts an input stream std::istream. More...
 
void write (std::ostream &os) const
 Accepts an output stream read function, which accepts an input stream std::ostream. More...
 
- Public Member Functions inherited from Files
 Files ()
 A constructor. More...
 
virtual ~Files ()
 A destructor, watch out its a virtual destructor. More...
 
 Files (const Files &other)
 Copy constructor. More...
 
MERCURY_DEPRECATED FilegetDataFile ()
 The non const version. Allows one to edit the File::dataFile. More...
 
MERCURY_DEPRECATED FilegetEneFile ()
 The non const version. Allows to edit the File::eneFile. More...
 
MERCURY_DEPRECATED FilegetFStatFile ()
 The non const version. Allows to edit the File::fStatFile. More...
 
MERCURY_DEPRECATED FilegetRestartFile ()
 The non const version. Allows to edit the File::restartFile. More...
 
MERCURY_DEPRECATED FilegetStatFile ()
 The non const version. Allows to edit the File::statFile. More...
 
MERCURY_DEPRECATED const FilegetDataFile () const
 The const version. Does not allow for any editing of the File::dataFile. More...
 
MERCURY_DEPRECATED const FilegetEneFile () const
 The const version. Does not allow for any editing of the File::eneFile. More...
 
MERCURY_DEPRECATED const FilegetFStatFile () const
 The const version. Does not allow for any editing of the File::fStatFile. More...
 
MERCURY_DEPRECATED const FilegetRestartFile () const
 The const version. Does not allow for any editing of the File::restartFile. More...
 
MERCURY_DEPRECATED const FilegetStatFile () const
 The const version. Does not allow for any editing of the File::statFile. More...
 
const std::string & getName () const
 Returns the name of the file. Does not allow to change it though. More...
 
void setName (const std::string &name)
 Allows to set the name of all the files (ene, data, fstat, restart, stat) More...
 
void setName (const char *name)
 Calls setName(std::string) More...
 
void setSaveCount (unsigned int saveCount)
 Sets File::saveCount_ for all files (ene, data, fstat, restart, stat) More...
 
void setFileType (FileType fileType)
 Sets File::fileType_ for all files (ene, data, fstat, restart, stat) More...
 
void setOpenMode (std::fstream::openmode openMode)
 Sets File::openMode_ for all files (ene, data, fstat, restart, stat) More...
 
void resetFileCounter ()
 Resets the file counter for each file i.e. for ene, data, fstat, restart, stat) More...
 
void read (std::istream &is)
 Extracts data from the input stream (which is basically a file you want to read from) into name_, restartFile .... More...
 
void write (std::ostream &os) const
 Writes data into a file from the member variables name_, restartFile, dataFile etc. More...
 
void openFiles ()
 Opens all the files (ene, data, fstat, restart, stat) for reading and writing purposes. More...
 
void closeFiles ()
 Closes all files (ene, data, fstat, restart, stat) that were opened to read or write. More...
 
void setNextSavedTimeStep (unsigned int nextSavedTimeStep)
 Sets the next time step for all the files (ene, data, fstat, restart, stat) at which the data is to be written or saved. More...
 

Private Attributes

int runNumber_
 This stores the run number for saving. More...
 

Additional Inherited Members

- Public Attributes inherited from Files
File dataFile
 An instance of class File to handle in- and output into a .data file. More...
 
File fStatFile
 An instance of class File to handle in- and output into a .fstat file. More...
 
File eneFile
 An instance of class File to handle in- and output into a .ene file. More...
 
File restartFile
 An instance of class File to handle in- and output into a .restart file. More...
 
File statFile
 An instance of class File to handle in- and output into a .stat file. More...
 

Detailed Description

It is publicly inherited from class Files. It defines an awesome feature that is ideal when doing a parameter study. Below are the routines that manipulate a counter file, called COUNTER_DONOTDEL, to store run numbers.

For a paramater study, a particular DPM simulation is run several times. Each time the code is executed, the run number or counter, in the COUNTER_DONOTDEL, gets incremented. Based on the counter your file name is named as problemName.1.data, problemName.2.data... If the File::fileType_ is chosen as Multiple files, then the your data files will have the name as problemName.runNumber.0, problemName.runNumber.1 ...

Definition at line 52 of file FilesAndRunNumber.h.

Constructor & Destructor Documentation

FilesAndRunNumber::FilesAndRunNumber ( )

Constructor.

Definition at line 38 of file FilesAndRunNumber.cc.

References constructor().

39 {
40  constructor();
41 #ifdef DEBUG_CONSTRUCTOR
42  std::cout << "FilesAndRunNumber::FilesAndRunNumber() finished" << std::endl;
43 #endif
44 }
void constructor()
a function called by the FilesAndRunNumber() (constructor)
FilesAndRunNumber::FilesAndRunNumber ( const FilesAndRunNumber other)

Copy constructor.

Parameters
[in]other

Definition at line 49 of file FilesAndRunNumber.cc.

References runNumber_.

50 : Files(other)
51 {
52  runNumber_ = other.runNumber_;
53 #ifdef DEBUG_CONSTRUCTOR
54  std::cout << "FilesAndRunNumber::FilesAndRunNumber(FilesAndRunNumber& other) finished" << std::endl;
55 #endif
56 }
Files()
A constructor.
Definition: Files.cc:33
int runNumber_
This stores the run number for saving.
FilesAndRunNumber::~FilesAndRunNumber ( )
virtual

Constructor.

Definition at line 61 of file FilesAndRunNumber.cc.

62 {
63 #ifdef DEBUG_CONSTRUCTOR
64  std::cout << "FilesAndRunNumber::~FilesAndRunNumber() finished" << std::endl;
65 #endif
66 }

Member Function Documentation

void FilesAndRunNumber::autoNumber ( )

The autoNumber() function is the trigger. It calls three functions. setRunNumber(), readRunNumberFromFile() and incrementRunNumberInFile().

Definition at line 79 of file FilesAndRunNumber.cc.

References incrementRunNumberInFile(), readRunNumberFromFile(), and setRunNumber().

Referenced by DPMBase::readNextArgument().

80 {
83 }
int readRunNumberFromFile()
Read the run number or the counter from the counter file (COUNTER_DONOTDEL)
void setRunNumber(int runNumber)
This sets the counter/Run number, overriding the defaults.
void incrementRunNumberInFile()
Increment the run Number (counter value) stored in the file_counter (COUNTER_DONOTDEL) by 1 and store...
void FilesAndRunNumber::constructor ( )

a function called by the FilesAndRunNumber() (constructor)

Initialises the runNumber_ = 0

Definition at line 71 of file FilesAndRunNumber.cc.

References runNumber_.

Referenced by FilesAndRunNumber().

72 {
73  runNumber_ = 0;
74 }
int runNumber_
This stores the run number for saving.
std::vector< int > FilesAndRunNumber::get2DParametersFromRunNumber ( int  size_x,
int  size_y 
)

This turns a counter into two indices which is an amazing feature for doing two dimensional parameter studies. The indices run from 1:size_x and 1:size_y, while the study number starts at 0 ( initially the counter=1 in COUNTER_DONOTDEL)

Lets say size_x = 2 and size_y = 5, counter stored in COUNTER_DONOTDEL =1. The study_size = 10. Substituting these values into the below algorithm implies that study_num = 0 or 1, everytime the code is executed the counter gets incremented and hence determined the values of study_num, i and j which is returned as a std::vector<int>

Parameters
[in]size_x
[in]size_y
Returns
std::vector<int>

Definition at line 193 of file FilesAndRunNumber.cc.

References getRunNumber().

194 {
195  std::vector<int> temp(3);
196 
197  int counter = getRunNumber();
198 
199  int study_size = size_x * size_y;
200 
201  int study_num = (counter - 1) / study_size;
202 
203  counter = counter - study_size * study_num;
204  int i = ((counter - 1) % size_x) + 1;
205  int j = (counter - i) / size_x + 1;
206  std::cout << "Counter: " << counter << " i: " << i << " j: " << j << std::endl;
207 
208  temp[0] = study_num;
209  temp[1] = i;
210  temp[2] = j;
211 
212  return (temp);
213 }
int getRunNumber() const
This returns the current value of the counter (runNumber_)
int FilesAndRunNumber::getRunNumber ( ) const

This returns the current value of the counter (runNumber_)

Returns
runNumber_

Definition at line 143 of file FilesAndRunNumber.cc.

References runNumber_.

Referenced by get2DParametersFromRunNumber(), and DPMBase::solve().

144 {
145  return runNumber_;
146 }
int runNumber_
This stores the run number for saving.
void FilesAndRunNumber::incrementRunNumberInFile ( )

Increment the run Number (counter value) stored in the file_counter (COUNTER_DONOTDEL) by 1 and store the new value in the counter file.

In order to increment the counter stored in COUNTER_DONOTDEL, we initialise two fstream objects counter_file, counter_file2 and an integer type temp_counter. First we open the file COUNTER_DONOTDEL, check if everything went fine with the opening. If yes, we extract the runNumber (counter) into the temp_counter. Increment the temp_counter and then write it into COUNTER_DONOTDEL. This is how we increment the counter in the file.

Definition at line 154 of file FilesAndRunNumber.cc.

Referenced by autoNumber().

155 {
156  std::fstream counter_file, counter_file2;
157  int temp_counter;
158 
159  counter_file.open("COUNTER_DONOTDEL", std::ios::in);
160  if (counter_file.fail())
161  {
162  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
163  counter_file.close();
164  exit(0);
165  }
166 
167  counter_file >> temp_counter;
168  counter_file.close();
169 
170  temp_counter++;
171 
172  counter_file2.open("COUNTER_DONOTDEL", std::ios::out);
173  if (counter_file2.fail())
174  {
175  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create2\n\n");
176  counter_file2.close();
177  exit(0);
178  }
179 
180  counter_file2 << temp_counter;
181 
182  counter_file2.close();
183 }
int FilesAndRunNumber::launchNewRun ( const char *  name,
bool quick  UNUSED = false 
)

This launches a code from within this code. Please pass the name of the code to run.

Parameters
[in]name
[in]quick
Returns
int

Definition at line 220 of file FilesAndRunNumber.cc.

221 {
222  std::stringstream com("");
223  com << name << " &";
224  return system(com.str().c_str());
225 }
void FilesAndRunNumber::read ( std::istream &  is)

Accepts an input stream std::istream.

Parameters
[in,out]is

Definition at line 230 of file FilesAndRunNumber.cc.

References Files::read(), and runNumber_.

Referenced by DPMBase::read().

231 {
232  std::string dummy;
233  is >> std::ws;
234  if (is.peek() == 'r')
235  is >> dummy >> runNumber_;
236  Files::read(is);
237 }
int runNumber_
This stores the run number for saving.
void read(std::istream &is)
Extracts data from the input stream (which is basically a file you want to read from) into name_...
Definition: Files.cc:211
int FilesAndRunNumber::readRunNumberFromFile ( )

Read the run number or the counter from the counter file (COUNTER_DONOTDEL)

The procedure below reads the counter in from a file stored on the disk. Increments the number stored on the disk and then returns the current counter.

Definition at line 89 of file FilesAndRunNumber.cc.

Referenced by autoNumber().

90 {
91  int counter;
92 
93  FILE *counter_file;
94  if ((counter_file = fopen("COUNTER_DONOTDEL", "r+")) == nullptr)
95  {
96  //counter file not found
97  if ((counter_file = fopen("COUNTER_DONOTDEL", "w")) == nullptr)
98  {
99  //counter file could not be created
100  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
101  fclose(counter_file);
102  exit(-1);
103  }
104  else
105  {
106  //counter file sucessfully created
107  fprintf(counter_file, "1");
108  fprintf(stderr, "Counter File created\n");
109  fclose(counter_file);
110  return 1;
111  }
112  }
113  else
114  {
115  //counter file sucessfully opened
116  if (fscanf(counter_file, "%d", &counter) != 1)
117  {
118  //Something with reading went wrong
119  fprintf(stderr, "\n\n\tERROR :: Counter File found, but something went wrong with reading it\n\n");
120  fclose(counter_file);
121  exit(-1);
122  }
123  else
124  {
125  fclose(counter_file);
126  return counter;
127  }
128  }
129 
130 }
void FilesAndRunNumber::setRunNumber ( int  runNumber)

This sets the counter/Run number, overriding the defaults.

Parameters
[in]runNumber

Definition at line 135 of file FilesAndRunNumber.cc.

References runNumber_.

Referenced by autoNumber(), and DPMBase::readNextArgument().

136 {
137  runNumber_ = runNumber;
138 }
int runNumber_
This stores the run number for saving.
void FilesAndRunNumber::write ( std::ostream &  os) const

Accepts an output stream read function, which accepts an input stream std::ostream.

Parameters
[in,out]os

Definition at line 242 of file FilesAndRunNumber.cc.

References runNumber_, and Files::write().

Referenced by DPMBase::write().

243 {
244  //only write the run number if it is different from 0
245  if (runNumber_ != 0)
246  os << " runNumber " << runNumber_;
247  Files::write(os);
248 }
int runNumber_
This stores the run number for saving.
void write(std::ostream &os) const
Writes data into a file from the member variables name_, restartFile, dataFile etc.
Definition: Files.cc:227

Member Data Documentation

int FilesAndRunNumber::runNumber_
private

This stores the run number for saving.

Definition at line 131 of file FilesAndRunNumber.h.

Referenced by constructor(), FilesAndRunNumber(), getRunNumber(), read(), setRunNumber(), and write().


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