MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
STD_save.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 "STD_save.h"
27 #include<stdio.h>
28 #include<stdlib.h>
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32 #include <sys/stat.h>
33 #include <vector>
34 
38 //This procedure reads the counter in from a file stores on the disk. Increaments the number stored on the disk and then returns the current counter.
39 {
40 int counter;
41 
42 
43 FILE *counter_file;
44 if ((counter_file=fopen("COUNTER_DONOTDEL", "r+"))==NULL)
45 {
46  //counter file not found
47  if ((counter_file=fopen("COUNTER_DONOTDEL", "w"))==NULL)
48  {
49  //counter file could not be created
50  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
51  fclose(counter_file);
52  exit(-1);
53  } else {
54  //counter file sucessfully created
55  fprintf(counter_file, "1");
56  fprintf(stderr, "Counter File created\n");
57  fclose(counter_file);
58  return 1;
59  }
60 } else {
61  //counter file sucessfully opened
62  if (fscanf(counter_file,"%d",&counter)!=1)
63  {
64  //Something with reading went wrong
65  fprintf(stderr, "\n\n\tERROR :: Counter File found, but something went wrong with reading it\n\n");
66  fclose(counter_file);
67  exit(-1);
68  } else {
69  fclose(counter_file);
70  return counter;
71  }
72 }
73 
74 }
75 
78 void STD_save::set_counter(int new_counter)
79 {
80 counter=new_counter;
81 }
82 
86 {
87 return(counter);
88 }
89 
93 {
95 }
96 
99 
101 {
102 std::fstream counter_file, counter_file2;
103 int temp_counter;
104 
105 counter_file.open("COUNTER_DONOTDEL",std::ios::in);
106 if (counter_file.fail())
107  {
108  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create\n\n");
109  counter_file.close();
110  exit(0);
111  }
112 
113 counter_file >> temp_counter;
114 counter_file.close();
115 
116 
117 temp_counter++;
118 
119 
120 counter_file2.open("COUNTER_DONOTDEL",std::ios::out);
121 if (counter_file2.fail())
122  {
123  fprintf(stderr, "\n\n\tERROR :: Counter File NOT found, please re-create2\n\n");
124  counter_file2.close();
125  exit(0);
126  }
127 
128 
129 
130 counter_file2 << temp_counter;
131 
132 counter_file2.close();
133 
134 }
135 
140 {
141 std::fstream file;
142 
143 //set up a std::string stream
144 std::stringstream filename;
145 
146 //Write info.counter to this stream
147 filename << "info." << counter;
148 
149 //open a file with this name. This has to be a c std::string, hence the rahter convoluted syntax.
150 file.open((filename.str()).c_str(),std::ios::out);
151 //Make a copy of where the current output buffer is pointing
152 std::streambuf *temp = std::cout.rdbuf();
153 std::cout.rdbuf(file.rdbuf());
154 info();
155 file.close();
156 //Return the output buffer back to were it was before.
157 std::cout.rdbuf(temp);
158 }
159 
163 bool STD_save::FileExists(std::string strFilename)
164 {
165  struct stat stFileInfo;
166  bool blnReturn;
167  int intStat;
168 
169  // Attempt to get the file attributes
170 
171  intStat = stat(strFilename.c_str(),&stFileInfo);
172  if(intStat == 0) {
173  // We were able to get the file attributes
174  // so the file obviously exists.
175  blnReturn = true;
176  } else {
177  // We were not able to get the file attributes.
178  // This may mean that we don't have permission to
179  // access the folder which contains this file. If you
180  // need to do that level of checking, lookup the
181  // return values of stat which will give you
182  // more details on why stat failed.
183  blnReturn = false;
184  }
185 
186  return(blnReturn);
187 }
188 
189 
190 
194 std::vector<int> STD_save::get_numbers(int size_x,int size_y)
195 {
196 std::vector<int> temp(3);
197 
198 int counter=get_counter();
199 
200 int study_size = size_x*size_y;
201 
202 int study_num=(counter-1)/study_size;
203 
204 counter=counter-study_size*study_num;
205 int i=((counter-1)%size_x)+1;
206 int j=(counter-i)/size_x+1;
207 std::cout<<"Counter: "<<counter<<" i: "<<i<<" j: "<<j<<std::endl;
208 
209 temp[0]=study_num;
210 temp[1]=i;
211 temp[2]=j;
212 
213 return(temp);
214 }
215 
219 int STD_save::launch_new(const char* name,bool quick)
220 {
221  std::stringstream com("");
222 
223  if (quick)
224  {
225  com << "../sc/run " << name << " &";
226  }
227  else
228  {
229  com << "../sc/run " << name << " &";
230  }
231 
233  return system(com.str().c_str());
234 }
void set_counter(int new_counter)
This set the counter, overriding the defaults.
Definition: STD_save.cc:78
void save_info_to_disk()
Saves the information generated by info to disk in a file.
Definition: STD_save.cc:139
int counter
The stores the run number for saving.
Definition: STD_save.h:238
bool FileExists(std::string strFilename)
Function to check if a file exists, is used to check if a run has already need done.
Definition: STD_save.cc:163
virtual void info()
Set up a virtual info this will be provided from the inhertiance.
Definition: STD_save.h:111
std::vector< int > get_numbers(int size_x, int size_y)
This turns a counter into two indexs for doing parmater studies. The indexs run from 1:size_x and 1:s...
Definition: STD_save.cc:194
int read_run_num_from_file()
Read rom the counter file the counter.
Definition: STD_save.cc:37
int launch_new(const char *name, bool quick=false)
This launch a code from within this code. Please pass the name of the code to run.
Definition: STD_save.cc:219
void set_counter_from_file()
Sets the counter based on the current number stored in the counter file.
Definition: STD_save.cc:92
int get_counter()
This returns the current value of the counter.
Definition: STD_save.cc:85
void inc_counter_in_file()
Increament the counter value stored in the file_counter by 1 and store the new value.
Definition: STD_save.cc:100