MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FilesAndRunNumber.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, 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 #include "FilesAndRunNumber.h"
27 #include "Math/ExtendedMath.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <vector>
34 
39 {
40  constructor();
41 #ifdef DEBUG_CONSTRUCTOR
42  std::cout << "FilesAndRunNumber::FilesAndRunNumber() finished" << std::endl;
43 #endif
44 }
45 
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 }
57 
62 {
63 #ifdef DEBUG_CONSTRUCTOR
64  std::cout << "FilesAndRunNumber::~FilesAndRunNumber() finished" << std::endl;
65 #endif
66 }
67 
72 {
73  runNumber_ = 0;
74 }
75 
80 {
83 }
84 
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 }
131 
136 {
137  runNumber_ = runNumber;
138 }
139 
144 {
145  return runNumber_;
146 }
147 
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 }
184 
193 std::vector<int> FilesAndRunNumber::get2DParametersFromRunNumber(int size_x, int size_y)
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 }
214 
220 int FilesAndRunNumber::launchNewRun(const char* name, bool quick UNUSED)
221 {
222  std::stringstream com("");
223  com << name << " &";
224  return system(com.str().c_str());
225 }
226 
230 void FilesAndRunNumber::read(std::istream& is)
231 {
232  std::string dummy;
233  is >> std::ws;
234  if (is.peek() == 'r')
235  is >> dummy >> runNumber_;
236  Files::read(is);
237 }
238 
242 void FilesAndRunNumber::write(std::ostream& os) const
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 }
Every simulation requires data files to store all the information necessary for visualisation and ana...
Definition: Files.h:42
int readRunNumberFromFile()
Read the run number or the counter from the counter file (COUNTER_DONOTDEL)
virtual ~FilesAndRunNumber()
Constructor.
void write(std::ostream &os) const
Accepts an output stream read function, which accepts an input stream std::ostream.
void setRunNumber(int runNumber)
This sets the counter/Run number, overriding the defaults.
void constructor()
a function called by the FilesAndRunNumber() (constructor)
#define UNUSED
Definition: GeneralDefine.h:37
int runNumber_
This stores the run number for saving.
void read(std::istream &is)
Accepts an input stream std::istream.
FilesAndRunNumber()
Constructor.
void autoNumber()
The autoNumber() function is the trigger. It calls three functions. setRunNumber(), readRunNumberFromFile() and incrementRunNumberInFile().
void write(std::ostream &os) const
Writes data into a file from the member variables name_, restartFile, dataFile etc.
Definition: Files.cc:227
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
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...
It is publicly inherited from class Files. It defines an awesome feature that is ideal when doing a p...
void incrementRunNumberInFile()
Increment the run Number (counter value) stored in the file_counter (COUNTER_DONOTDEL) by 1 and store...
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.
int getRunNumber() const
This returns the current value of the counter (runNumber_)