MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
STD_save.h
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 #ifndef STD_SAVE_H
27 #define STD_SAVE_H
28 
29 //#include "version.h"
30 #include<string>
31 #include<vector>
32 #include<sstream>
33 #include<fstream>
34 #include<iostream>
35 #include<iomanip>
36 #include<stdlib.h>
37 
38 #ifdef HIGH_PRECISION
39  typedef long double Mdouble;
40 #else
41  typedef double Mdouble;
42 #endif
43 
48 class STD_save {
49 
50 public:
51 
54  constructor();
55  #ifdef CONSTUCTOR_OUTPUT
56  std::cerr << "STD_save() finished"<<std::endl;
57  #endif
58 }
59 
61 STD_save(STD_save& other) {
62  constructor();
63  counter = other.get_counter();
64  problem_name.str(other.get_name());
70  #ifdef CONSTUCTOR_OUTPUT
71  std::cerr << "STD_save(STD_save& other) finished"<<std::endl;
72  #endif
73 };
74 
75 void constructor(){
76  counter=0;
79  set_options_ene(1);
82  file_counter = 0;
83  step_size = 1;
84  data_file.precision(13);
85  fstat_file.precision(13);
86  ene_file.precision(13);
87  stat_file.precision(5);
88  stat_file.setf(std::ios::left);
89 }
90 
91 
93 void inc_counter_in_file();
94 
97 
100 
102 void save_info_to_disk();
103 
105 void set_counter(int new_counter);
106 
108 int get_counter();
109 
111 virtual void info(){}
112 
114 bool FileExists(std::string strFilename);
115 
117 
119 std::vector<int> get_numbers(int size_x, int size_y);
120 
122 int launch_new(const char* name, bool quick=false);
123 
125 void set_name(const char* name){problem_name.str(""); problem_name << name;}
127 std::string get_name(){return problem_name.str();}
128 
130 std::fstream& get_data_file(){return data_file;}
132 std::fstream& get_stat_file(){return stat_file;}
134 std::fstream& get_fstat_file(){return fstat_file;}
136 std::fstream& get_ene_file(){return ene_file;}
137 
138 void set_fstat_filename(std::string filename){fstat_filename.str(""); fstat_filename << filename;}
139 void set_data_filename(std::string filename){data_filename.str(""); data_filename << filename;}
140 void set_stat_filename(std::string filename){stat_filename.str(""); stat_filename << filename;}
141 void set_ene_filename(std::string filename){ene_filename.str(""); ene_filename << filename;}
142 
143 void set_fstat_filename(){fstat_filename.str(""); fstat_filename << problem_name.str().c_str() << ".fstat";}
144 void set_data_filename(){data_filename.str(""); data_filename << problem_name.str().c_str() << ".data";}
145 void set_stat_filename(){stat_filename.str(""); stat_filename << problem_name.str().c_str() << ".stat";}
146 void set_ene_filename(){ene_filename.str(""); ene_filename << problem_name.str().c_str() << ".ene";}
147 
148 std::string get_fstat_filename(){return fstat_filename.str();}
149 std::string get_data_filename(){return data_filename.str();}
150 std::string get_stat_filename(){return stat_filename.str();}
151 std::string get_ene_filename(){return ene_filename.str();}
152 
153 
154 void set_step_size(unsigned int new_){step_size= new_;}
155 unsigned int get_step_size(){return step_size;}
156 
158 void set_options_fstat(unsigned int new_){options_fstat = new_;}
159 unsigned int get_options_fstat(void){return options_fstat;}
160 
161 void set_options_data(unsigned int new_){options_data = new_;}
162 unsigned int get_options_data(void){return options_data;}
163 
164 void set_options_stat(unsigned int new_){options_stat = new_;}
165 unsigned int get_options_stat(void){return options_stat;}
166 
167 void set_options_restart(unsigned int new_){options_restart = new_;}
168 unsigned int get_options_restart(void){return options_restart;}
169 
170 void set_options_ene(unsigned int new_){options_ene = new_;}
171 unsigned int get_options_ene(void){return options_ene;}
172 
173 
174 bool open_file (std::fstream& file, std::string filename, unsigned int options, std::fstream::openmode mode)
175 {
176  if (options==1) {
177  //std::cout << "open " << filename << std::endl;
178  file.open( filename.c_str(), mode);
179  if (file.fail()) return false;
180  }
181  return true;
182 }
183 
184 bool open_fstat_file (std::fstream::openmode mode = std::fstream::out) {
185  return open_file (fstat_file, fstat_filename.str(), get_options_fstat(), mode);
186 }
187 bool open_data_file (std::fstream::openmode mode = std::fstream::out) {
188  return open_file (data_file, data_filename.str(), get_options_data(), mode);
189 }
190 bool open_stat_file (std::fstream::openmode mode = std::fstream::out) {
191  return open_file (stat_file, stat_filename.str(), get_options_stat(), mode);
192 }
193 bool open_ene_file (std::fstream::openmode mode = std::fstream::out) {
194  return open_file (ene_file, ene_filename.str(), get_options_ene(), mode);
195 }
196 
198 bool open_counted_file(std::fstream& file, std::string filenameNoCount, std::fstream::openmode mode){
199  //create filename
200  std::stringstream filename;
201  filename << filenameNoCount << ".";
202  if (file_counter<1000) filename << "0";
203  if (file_counter<100) filename << "0";
204  if (file_counter<10) filename << "0";
205  filename << file_counter;
206  //close old file
207  if (file.is_open()) file.close();
208  //open new file
209  file.open(filename.str().c_str(), mode);
210  if (!file.is_open()) {std::cerr << "Error in opening " << filename.str() << std::endl; return false;}
211  //else std::cout << "open(" << mode << ") " << filename.str() << std::endl;
212  //increase counter
213  file_counter += step_size;
214  return true;
215 }
216 
217 bool increase_counter_fstat(std::fstream::openmode mode){
219  return open_counted_file(fstat_file, fstat_filename.str(), mode);
220 }
221 bool increase_counter_data(std::fstream::openmode mode){
222  return open_counted_file(data_file, data_filename.str(), mode);
223 }
224 bool increase_counter_stat(std::fstream::openmode mode){
226  return open_counted_file(stat_file, stat_filename.str(), mode);
227 }
228 bool increase_counter_ene(std::fstream::openmode mode){
230  return open_counted_file(ene_file, ene_filename.str(), mode);
231 }
232 
233 void set_file_counter(int new_) {file_counter = new_;}
235 
236 private:
239 
240 protected:
242 std::stringstream problem_name;
243 
246 std::stringstream data_filename;
247 std::stringstream stat_filename;
248 std::stringstream fstat_filename;
249 std::stringstream ene_filename;
250 
252 std::fstream data_file;
253 std::fstream stat_file;
254 std::fstream fstat_file;
255 std::fstream ene_file;
256 
257 
262 unsigned int options_fstat;
263 unsigned int options_data;
264 unsigned int options_stat;
265 unsigned int options_ene;
266 unsigned int options_restart;
267 
269 unsigned int file_counter;
270 unsigned int step_size;
271 };
272 
273 
274 #endif
STD_save()
Default constructor: sets the counter to 0 (i.e. no number will be included).
Definition: STD_save.h:53
unsigned int file_counter
Counter needed if file will be written in multiple files.
Definition: STD_save.h:269
std::fstream fstat_file
Definition: STD_save.h:254
std::stringstream stat_filename
Definition: STD_save.h:247
unsigned int options_restart
Definition: STD_save.h:266
bool open_fstat_file(std::fstream::openmode mode=std::fstream::out)
Definition: STD_save.h:184
void set_counter(int new_counter)
This set the counter, overriding the defaults.
Definition: STD_save.cc:78
bool open_file(std::fstream &file, std::string filename, unsigned int options, std::fstream::openmode mode)
Definition: STD_save.h:174
std::fstream stat_file
Definition: STD_save.h:253
void save_info_to_disk()
Saves the information generated by info to disk in a file.
Definition: STD_save.cc:139
Here are the rounties to use a counter file to store run numbers.
Definition: STD_save.h:48
int counter
The stores the run number for saving.
Definition: STD_save.h:238
int get_file_counter()
Definition: STD_save.h:234
std::stringstream data_filename
These store the save file names, by default they are derived from problem_name.
Definition: STD_save.h:246
unsigned int get_step_size()
Definition: STD_save.h:155
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
std::string get_ene_filename()
Definition: STD_save.h:151
virtual void info()
Set up a virtual info this will be provided from the inhertiance.
Definition: STD_save.h:111
void set_options_data(unsigned int new_)
Definition: STD_save.h:161
void set_options_restart(unsigned int new_)
Definition: STD_save.h:167
void set_fstat_filename()
Definition: STD_save.h:143
unsigned int options_fstat
Indicators if files are created or not 0: file will not be created 1: file will be written in one fil...
Definition: STD_save.h:262
bool open_counted_file(std::fstream &file, std::string filenameNoCount, std::fstream::openmode mode)
opens file needed if data is written in multiple files
Definition: STD_save.h:198
std::string get_fstat_filename()
Definition: STD_save.h:148
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
STD_save(STD_save &other)
Copy constructor.
Definition: STD_save.h:61
unsigned int get_options_fstat(void)
Definition: STD_save.h:159
int read_run_num_from_file()
Read rom the counter file the counter.
Definition: STD_save.cc:37
void set_data_filename()
Definition: STD_save.h:144
double Mdouble
Definition: STD_save.h:41
std::stringstream problem_name
Stores the problem_name.
Definition: STD_save.h:242
void set_name(const char *name)
Sets the name of the problem, used for the same data files.
Definition: STD_save.h:125
void set_options_fstat(unsigned int new_)
set and get for file options
Definition: STD_save.h:158
std::fstream data_file
Stream used for data files.
Definition: STD_save.h:252
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
std::fstream & get_data_file()
Allows the problem_name to be accessed.
Definition: STD_save.h:130
void set_fstat_filename(std::string filename)
Definition: STD_save.h:138
void set_ene_filename(std::string filename)
Definition: STD_save.h:141
void set_stat_filename(std::string filename)
Definition: STD_save.h:140
unsigned int options_stat
Definition: STD_save.h:264
void set_step_size(unsigned int new_)
Definition: STD_save.h:154
unsigned int get_options_data(void)
Definition: STD_save.h:162
std::fstream & get_stat_file()
Allows the problem_name to be accessed.
Definition: STD_save.h:132
void set_data_filename(std::string filename)
Definition: STD_save.h:139
bool increase_counter_fstat(std::fstream::openmode mode)
Definition: STD_save.h:217
std::stringstream ene_filename
Definition: STD_save.h:249
void set_counter_from_file()
Sets the counter based on the current number stored in the counter file.
Definition: STD_save.cc:92
std::fstream & get_fstat_file()
Allows the problem_name to be accessed.
Definition: STD_save.h:134
unsigned int options_ene
Definition: STD_save.h:265
bool open_data_file(std::fstream::openmode mode=std::fstream::out)
Definition: STD_save.h:187
unsigned int get_options_ene(void)
Definition: STD_save.h:171
unsigned int step_size
Definition: STD_save.h:270
unsigned int get_options_stat(void)
Definition: STD_save.h:165
unsigned int get_options_restart(void)
Definition: STD_save.h:168
std::stringstream fstat_filename
Definition: STD_save.h:248
int get_counter()
This returns the current value of the counter.
Definition: STD_save.cc:85
void set_file_counter(int new_)
Definition: STD_save.h:233
std::string get_stat_filename()
Definition: STD_save.h:150
std::string get_data_filename()
Definition: STD_save.h:149
std::fstream & get_ene_file()
Allows the problem_name to be accessed.
Definition: STD_save.h:136
bool increase_counter_ene(std::fstream::openmode mode)
Definition: STD_save.h:228
void constructor()
Definition: STD_save.h:75
bool increase_counter_data(std::fstream::openmode mode)
Definition: STD_save.h:221
std::string get_name()
Allows the problem_name to be accessed.
Definition: STD_save.h:127
unsigned int options_data
Definition: STD_save.h:263
void auto_number()
Definition: STD_save.h:116
void set_options_stat(unsigned int new_)
Definition: STD_save.h:164
bool increase_counter_stat(std::fstream::openmode mode)
Definition: STD_save.h:224
bool open_ene_file(std::fstream::openmode mode=std::fstream::out)
Definition: STD_save.h:193
void set_stat_filename()
Definition: STD_save.h:145
void set_ene_filename()
Definition: STD_save.h:146
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
bool open_stat_file(std::fstream::openmode mode=std::fstream::out)
Definition: STD_save.h:190
std::fstream ene_file
Definition: STD_save.h:255
void set_options_ene(unsigned int new_)
Definition: STD_save.h:170