Paramotopy
parallel parameter homotopy through bertini
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
para_aux_funcs.cpp
Go to the documentation of this file.
1 #include "para_aux_funcs.hpp"
2 
3 
4 
6 
7  std::cout
8  << "\n"
9  << "*******************************\n"
10  << " Welcome to Paramotopy.\n"
11  << " parametrized system analysis software by\n"
12  << " daniel brake and matthew niemerg, with dan bates.\n\n"
13  << " www.paramotopy.com danielthebrake@gmail.com matthew.niemerg@gmail.com \n"
14  << "**************************" << std::endl;
15 
16 
17 
18  return;
19 }
20 
21 void BertiniSplashScreen(std::string bertinilocation){
22 
23 char *args_splash[2];
24 args_splash[0] = const_cast<char *>("bertini");
25 args_splash[1] = const_cast<char *>("--version");
26 
27 
28  std::cout << "\n\nlinked library bertini:\n\n";
29  bertini_main(2,args_splash);
30  std::cout << "\n\nstand-alone bertini:\n\n";
31 
32 
33  std::stringstream command;
34  command << bertinilocation << "/bertini --version" ;
35  system(command.str().c_str());
36  std::cout << "\n\n";
37 
38  return;
39 
40 }
41 
42 
43 
44 int GetFileParserIndex(std::string filename){
45 
46  std::map < std::string , int > possible_savefiles;
47  possible_savefiles["real_solutions"] = 1;
48  possible_savefiles["nonsingular_solutions"] = 1;
49  possible_savefiles["singular_solutions"] = 1;
50  possible_savefiles["raw_data"] = -1;
51  possible_savefiles["raw_solutions"] = -1;
52  possible_savefiles["main_data"] = -1;
53  possible_savefiles["midpath_data"] = -1;
54  possible_savefiles["failed_paths"] = 2;
55  possible_savefiles["real_finite_solutions"] = 1;
56 
57 
58  return possible_savefiles[filename];
59 
60 }
61 
62 
63 
64 
65 
66 void GetFilesToParse(boost::filesystem::path run_to_analyze,
67  std::vector< std::string > & gather_savefiles,
68  std::vector< int > & gather_parser_indices)
69 {
70  std::vector < std::string > possible_savefiles;
71  possible_savefiles.push_back("real_solutions");
72  possible_savefiles.push_back("nonsingular_solutions");
73  possible_savefiles.push_back("singular_solutions");
74  possible_savefiles.push_back("raw_data");
75  possible_savefiles.push_back("raw_solutions");
76  possible_savefiles.push_back("main_data");
77  possible_savefiles.push_back("midpath_data");
78  possible_savefiles.push_back("failed_paths");
79  possible_savefiles.push_back("real_finite_solutions");
80  std::vector < int > possible_parser_indices;
81  possible_parser_indices.push_back(1);//real_solutions
82  possible_parser_indices.push_back(1);//nonsingular
83  possible_parser_indices.push_back(1);//singular_
84  possible_parser_indices.push_back(-1);//raw_data
85  possible_parser_indices.push_back(-1);//raw_solutions
86  possible_parser_indices.push_back(-1);//main_data
87  possible_parser_indices.push_back(-1);//midpath_data
88  possible_parser_indices.push_back(2);//failed_paths
89  possible_parser_indices.push_back(1); // real_finite_solutions
90 
91  for (int ii = 0; ii < int(possible_savefiles.size()); ii++){
92  boost::filesystem::path temppath = run_to_analyze;
93  temppath /= "step2/DataCollected/c1"; // scan the c1 folder, because every run has a c1 folder, and it will always contain data.
94  std::string expression = "^"; //specify beginning of string
95  expression.append(possible_savefiles[ii]);
96  std::vector < boost::filesystem::path > filelist = FindFiles(temppath, expression); //this function is in para_aux_funcs
97  if (int(filelist.size())>0){
98  gather_savefiles.push_back(possible_savefiles[ii]);
99  gather_parser_indices.push_back(possible_parser_indices[ii]);
100  }
101  }
102 
103  return;
104 }
105 
106 
107 
108 
109 bool TestIfFinished(boost::filesystem::path & path_to_check){
110 
111 
112  boost::filesystem::path appended_path = path_to_check;
113  appended_path /= "step2finished";
114 
115  if (boost::filesystem::exists(appended_path)){
116  return true;
117  }
118  else{
119  return false;
120  }
121 }
122 
123 
125 int ParseFailedPaths(std::ifstream & fin, int numvariables){
126  int tempfailnum = 0;
127  std::string tmpstr;
128 
129 
130  while (1) {
131 
132  getline(fin,tmpstr);
133  //faily_myfailfail << tmpstr;
134  if (tmpstr.length()==0) { // if do not have a solution
135  break;
136  }
137  else { //have a soln
138  for (int i=0; i<3+numvariables; ++i) {
139  getline(fin,tmpstr);
140  //faily_myfailfail << tmpstr;
141  }
142  tempfailnum++;
143  }
144  }
145  return tempfailnum;
146 }
147 
148 
149 
150 
151 std::string replace_tilde_with_home(std::string workwithme){
152 
153  size_t found;
154 
155  found=workwithme.find('~');
156  if ( (int(found)==0) ) {
157  std::string replaced_string = getenv("HOME");
158  replaced_string.append( workwithme.substr(found+1,workwithme.length()-1));
159  return replaced_string;
160  }
161  else{
162  return workwithme;
163  }
164 }
165 
166 //searches a directory for all files fitting a regular expression.
167 std::vector<boost::filesystem::path> FindDirectories(boost::filesystem::path dir, std::string expression){
168  boost::filesystem::path p(dir);
169  boost::regex e(expression);
170  std::vector<boost::filesystem::path> found_folders;
171 
172  if (boost::filesystem::exists(p)) {
173  boost::filesystem::directory_iterator dir_end;
174  boost::filesystem::path filename;
175  for(boost::filesystem::directory_iterator dir_iter(p); dir_iter != dir_end; ++dir_iter) {
176  filename = dir_iter->path().filename();
177  if (boost::regex_search(filename.string(),e)) {
178  if (is_directory(dir_iter->path())){
179  found_folders.push_back(dir_iter->path());
180  }
181  }
182  }
183  }
184  else {
185  std::cerr << "directory " << dir.string() << " does not exist!\n";
186  }
187  std::sort(found_folders.begin(), found_folders.end());
188  return found_folders;
189 
190 
191 }
192 
193 //searches a directory for all *directories* fitting a regular expression.
194 std::vector< boost::filesystem::path > FindFiles(boost::filesystem::path dir, std::string expression){
195 
196  boost::filesystem::path p(dir);
197  boost::regex e(expression);
198  std::vector<boost::filesystem::path> found_files;
199 
200 
201  if (boost::filesystem::exists(p)) {
202  boost::filesystem::directory_iterator dir_end;
203  boost::filesystem::path filename;
204  for(boost::filesystem::directory_iterator dir_iter(p); dir_iter != dir_end; ++dir_iter) {
205  filename = dir_iter->path().filename();
206  if (boost::regex_search(filename.string(),e)) {
207  if (is_regular_file(dir_iter->path())){
208  found_files.push_back(dir_iter->path());
209  }
210  }
211  }
212  }
213  else {
214  std::cerr << "directory " << dir << " does not exist!\n";
215  }
216 
217 
218  std::sort(found_files.begin(), found_files.end());
219  return found_files;
220 
221 }
222 
223 
224 
225 
226 boost::filesystem::path make_base_dir_name(boost::filesystem::path filename){ //intended to be called from step2
227 
228  boost::filesystem::path dir = "bfiles_";
229 
230  dir /= filename.filename();
231 
232  return dir;
233 }
234 
235 
236 
237 
238 
239 
240 
241 bool parseDouble( std::string const& text, double& results )
242 {
243  std::istringstream parser( text );
244  return parser >> results >> std::ws && parser.peek() == EOF;
245 }
246 
247 double getDouble()
248 {
249 
250 
251  std::cin.ignore( std::numeric_limits<std::streampos>::max(), '\n' ) ;
252  std::cin.clear() ; // to be safe, clear error flags which may be set
253 
254  double results;
255  std::string line;
256  while (1){
257  while ( ! std::getline( std::cin, line ) )
258  {
259 
260  std::cin.ignore( std::numeric_limits<std::streampos>::max(), '\n' ) ;
261  std::cin.clear() ; // to be safe, clear error flags which may be set
262 
263  }
264 
265  if (! parseDouble( line, results )) {
266  std::cout << "Only 'numeric' value(s) allowed:" << std::endl;
267  }
268  else{
269  break;
270  }
271 
272  }
273  return results;
274 }
275 
276 
277 
279  std::string tmpstr;
280 
281  while (!std::getline(std::cin,tmpstr)) {
282  std::cin.clear();
283  }
284 
285  std::cin.clear();
286 
287  return tmpstr;
288 
289 };
290 
291 
292 std::string getAlphaNumeric(){
293  std::string tmpstr;
294 
295  while (!std::getline(std::cin,tmpstr)) {
296  std::cin.clear();
297  }
298 
299  std::cin.clear();
300 
301  std::istringstream parser( tmpstr);
302  std::string returnme;
303  parser >> returnme;
304  return returnme;
305 
306 };
307 
308 
309 
310 bool parseInteger( std::string const& text, int& results )
311 {
312  std::istringstream parser( text );
313  parser >> results;
314  return !(parser.fail());// >> std::ws && parser.peek() == EOF;
315 }
316 
318 {
319 
320  std::cin.ignore( std::numeric_limits<std::streampos>::max(), '\n' ) ;
321  std::cin.clear() ; // to be safe, clear error flags which may be set
322 
323  int results;
324  std::string line;
325  while (1){
326  while ( ! std::getline( std::cin, line ) )
327  {
328 
329  std::cin.ignore( std::numeric_limits<std::streampos>::max(), '\n' ) ;
330  std::cin.clear() ; // to be safe, clear error flags which may be set
331 
332  }
333 
334  if (! parseInteger( line, results )) {
335  std::cout << "Only 'numeric' value(s) allowed:" << std::endl;
336  }
337  else{
338  break;
339  }
340 
341  }
342  return results;
343 }
344 
345 
346 // use this function to get input from the user and ensure it is an integer (actually fails to detect non-integer numeric inputs
347 int get_int_choice(std::string display_string,int min_value,int max_value){
348 
349  std::cout << display_string;
350 
351  int userinput = min_value - 1;
352 
353  std::string tmpstr;
354 
355  while (1)
356  {
357  userinput = getInteger();
358  if (userinput <= max_value && userinput >= min_value){
359  break;
360  }
361  else{
362  std::cout << "value out of bounds." << std::endl;
363  }
364  }
365  return userinput;
366 }
367 
369 //int get_int_choice(std::string display_string,int min_value,int max_value){
370 //
371 // int userinput = min_value - 1;
372 //
373 // while ( (std::cout << display_string) &&
374 // ( !(std::cin >> userinput) || userinput < min_value || userinput > max_value))
375 // {
376 // std::cout << "Invalid entry -- try again:\n";
377 // std::cin.clear();
378 // std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
379 // }
380 // return userinput;
381 //}
382 
383 
384 
385 
386 //the main choice function for paramotopy.
388 
389  std::stringstream menu;
390 
391  menu << "\n\nYour choices : \n\n"
392  << "1) Parse an appropriate input file. \n"
393  << "2) Data Management. \n"
394  << "3) -unprogrammed- \n"
395  << "4) Manage start point (load/save/new). \n"
396  << "5) Write Step 1.\n"
397  << "6) Run Step 1.\n"
398  << "7) Run Step 2.\n"
399  << "8) Failed Path Analysis.\n"
400  << "\n"
401  << "99) Preferences.\n"
402  << "*\n"
403  << "0) Quit the program.\n\n"
404  << "Enter the integer value of your choice : ";
405 
406  std::cout << menu.str();
407 
408  int intChoice = -1;
409  while ( (intChoice>=9 && intChoice<=98) || (intChoice >=100 || intChoice < 0) ) {
410  intChoice = get_int_choice("", 0, 99);
411  }
412 
413 
414 
415  return intChoice;
416 } // re: getuserchoice
417 
418 
419 
420 
421 
422 
423 
424 
425 // this function, to get the current directory, is *directly* from stackoverflow
426 //
427 //
428 std::string stackoverflow_getcwd()
429 {
430  const size_t chunkSize=255;
431  const int maxChunks=10240; // 2550 KiBs of current path are more than enough
432 
433  char stackBuffer[chunkSize]; // Stack buffer for the "normal" case
434  if(getcwd(stackBuffer,sizeof(stackBuffer))!=NULL)
435  return stackBuffer;
436  if(errno!=ERANGE)
437  {
438  // It's not ERANGE, so we don't know how to handle it
439  throw std::runtime_error("Cannot determine the current path.");
440  // Of course you may choose a different error reporting method
441  }
442  // Ok, the stack buffer isn't long enough; fallback to heap allocation
443  for(int chunks=2; chunks<maxChunks ; chunks++)
444  {
445  // With boost use scoped_ptr; in C++0x, use unique_ptr
446  // If you want to be less C++ but more efficient you may want to use realloc
447  std::auto_ptr<char> cwd(new char[chunkSize*chunks]);
448  if(getcwd(cwd.get(),chunkSize*chunks)!=NULL)
449  return cwd.get();
450  if(errno!=ERANGE)
451  {
452  // It's not ERANGE, so we don't know how to handle it
453  throw std::runtime_error("Cannot determine the current path.");
454  // Of course you may choose a different error reporting method
455  }
456  }
457  throw std::runtime_error("Cannot determine the current path; the path is apparently unreasonably long");
458 }
459 
460 
461 
462 
463 void safe_chdir(std::string desired_directory){
464  int success;
465 
466 
467  success = chdir(desired_directory.c_str());
468 
469  if (success == -1) {
470 
471 
472  std::string throwme = "failed to change directory to ";
473  throwme.append(desired_directory);
474  throwme.append(" giving error ");
475  throwme.append(strerror (errno));
476  throw std::runtime_error(throwme);
477  }
478 //TODO: make this safe
479 }
480 
481 
482 
483 
484 std::string convert_spaces_to_escaped(std::string workwithme)
485 {
486  size_t found;
487  found=workwithme.find(' ');
488  std::string newstring = "";
489 
490 
491  while (found != std::string::npos) {
492  newstring.append(workwithme.substr(0,found));
493  newstring.append("\\ ");
494  workwithme = workwithme.substr(found+1,workwithme.length()-1);
495  found=workwithme.find(' ');
496  }
497 
498  newstring.append(workwithme);
499 
500 
501  return newstring;
502 
503 }
504 
505 
506 
507 
508 
509 
510 
511 
512 
std::vector< boost::filesystem::path > FindFiles(boost::filesystem::path dir, std::string expression)
std::string stackoverflow_getcwd()
bool TestIfFinished(boost::filesystem::path &path_to_check)
std::string getAlphaNumeric()
int ParseFailedPaths(std::ifstream &fin, int numvariables)
the parser for the failed_paths file type, which is output from bertini.
std::string replace_tilde_with_home(std::string workwithme)
boost::filesystem::path make_base_dir_name(boost::filesystem::path filename)
void GetFilesToParse(boost::filesystem::path run_to_analyze, std::vector< std::string > &gather_savefiles, std::vector< int > &gather_parser_indices)
void safe_chdir(std::string desired_directory)
int ParamotopyMainMenu()
int get_int_choice(std::string display_string, int min_value, int max_value)
std::vector< boost::filesystem::path > FindDirectories(boost::filesystem::path dir, std::string expression)
std::string getAlphaNumeric_WithSpaces()
void ParamotopySplashScreen()
int getInteger()
bool parseInteger(std::string const &text, int &results)
void BertiniSplashScreen(std::string bertinilocation)
std::string convert_spaces_to_escaped(std::string workwithme)
int GetFileParserIndex(std::string filename)
bool parseDouble(std::string const &text, double &results)
double getDouble()