MercuryDPM  Alpha
 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 <stdio.h>
27 #include <stdlib.h>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <vector>
32 #include "FilesAndRunNumber.h"
33 #include "Math/ExtendedMath.h"
34 #include "Logger.h"
35 
40 {
41  constructor();
42  logger(DEBUG, "FilesAndRunNumber::FilesAndRunNumber() finished");
43 }
44 
49 : Files(other)
50 {
51  runNumber_ = other.runNumber_;
52  logger(DEBUG, "FilesAndRunNumber::FilesAndRunNumber(FilesAndRunNumber& other) finished");
53 }
54 
59 {
60  logger(DEBUG, "FilesAndRunNumber::~FilesAndRunNumber() finished");
61 }
62 
67 {
68  runNumber_ = 0;
69 }
70 
75 {
78 }
79 
85 {
86  int counter;
87 
88  FILE* counter_file;
89  if ((counter_file = fopen("COUNTER_DONOTDEL", "r+")) == nullptr)
90  {
91  //counter file not found
92  if ((counter_file = fopen("COUNTER_DONOTDEL", "w")) == nullptr)
93  {
94  //counter file could not be created
95  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
96  fclose(counter_file);
97  exit(-1);
98  }
99  else
100  {
101  //counter file sucessfully created
102  fprintf(counter_file, "1");
103  fprintf(stderr, "Counter File created\n");
104  fclose(counter_file);
105  return 1;
106  }
107  }
108  else
109  {
110  //counter file sucessfully opened
111  if (fscanf(counter_file, "%d", &counter) != 1)
112  {
113  //Something with reading went wrong
114  fprintf(stderr, "\n\n\tERROR :: Counter File found, but something went wrong with reading it\n\n");
115  fclose(counter_file);
116  exit(-1);
117  }
118  else
119  {
120  fclose(counter_file);
121  return counter;
122  }
123  }
124 
125 }
126 
131 {
132  runNumber_ = runNumber;
133 }
134 
139 {
140  return runNumber_;
141 }
142 
150 {
151  std::fstream counter_file, counter_file2;
152  int temp_counter;
153 
154  counter_file.open("COUNTER_DONOTDEL", std::ios::in);
155  if (counter_file.fail())
156  {
157  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
158  counter_file.close();
159  exit(0);
160  }
161 
162  counter_file >> temp_counter;
163  counter_file.close();
164 
165  temp_counter++;
166 
167  counter_file2.open("COUNTER_DONOTDEL", std::ios::out);
168  if (counter_file2.fail())
169  {
170  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create2\n\n");
171  counter_file2.close();
172  exit(0);
173  }
174 
175  counter_file2 << temp_counter;
176 
177  counter_file2.close();
178 }
179 
188 std::vector<int> FilesAndRunNumber::get2DParametersFromRunNumber(int size_x, int size_y)
189 {
190  std::vector<int> temp(3);
191 
192  int counter = getRunNumber();
193 
194  int study_size = size_x * size_y;
195 
196  int study_num = (counter - 1) / study_size;
197 
198  counter = counter - study_size * study_num;
199  int i = ((counter - 1) % size_x) + 1;
200  int j = (counter - i) / size_x + 1;
201  std::cout << "Counter: " << counter << " i: " << i << " j: " << j << std::endl;
202 
203  temp[0] = study_num;
204  temp[1] = i;
205  temp[2] = j;
206 
207  return (temp);
208 }
209 
215 int FilesAndRunNumber::launchNewRun(const char* name, bool quick UNUSED)
216 {
217  std::stringstream com("");
218  com << name << " &";
219  return system(com.str().c_str());
220 }
221 
225 void FilesAndRunNumber::read(std::istream& is)
226 {
227  std::string dummy;
228  is >> std::ws;
229  if (is.peek() == 'r')
230  is >> dummy >> runNumber_;
231  Files::read(is);
232 }
233 
237 void FilesAndRunNumber::write(std::ostream& os) const
238 {
239  //only write the run number if it is different from 0
240  if (runNumber_ != 0)
241  os << " runNumber " << runNumber_;
242  Files::write(os);
243 }
Every simulation requires data files to store all the information necessary for visualisation and ana...
Definition: Files.h:42
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
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:39
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:229
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:212
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_)