Diploma Thesis Percolation Simulation
C++ Sourcecode Documentation

www.AndreasKrueger.de/thesis/code

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

commandline.h

Go to the documentation of this file.
00001 // commandline.h
00002 // commandline switches for the clustercounter
00003 //
00004 // (c) 2001 Andreas Krueger (cpp__at__AndreasKrueger__dot__de)
00005 // version 1.0 last change: 15.3.2001
00006 //
00007 
00008 namespace commandline {
00009 
00010 #ifdef _MSC_VER
00011 const char SLASH='\\';
00012 #else
00013 const char SLASH='/';
00014 #endif
00015 
00016 int commandname_position(char* argv0){
00017         int name_pos=0;
00018         char* path_plus_name=argv0;
00019         int ch;
00020         for (ch=0; path_plus_name[ch]!=0;ch++){
00021                 if (path_plus_name[ch]==SLASH) name_pos=ch+1;
00022         }
00023         return name_pos;
00024 }
00025 
00026 void cout_tail (char* wholename, int start){
00027         int ch;
00028         for (ch=start; wholename[ch]!=0;ch++){
00029                 cout <<wholename[ch];
00030         }       
00031 }
00032 
00033 string commandname_wo_path(char* argv0){
00034         int name_pos=0;
00035         char* path_plus_name=argv0;
00036         int ch;
00037         for (ch=0; path_plus_name[ch]!=0;ch++){
00038                 if (path_plus_name[ch]==SLASH) name_pos=ch+1;
00039         }
00040         string temp="";
00041         for (ch=name_pos; argv0[ch]!=0;ch++){
00042                 temp+=argv0[ch];
00043         }       
00044         return temp;
00045 }
00046 
00047 
00048 void cout_command_endl(int argc, char *argv[]){
00049         int i;
00050         for (i=0;i<argc;i++){
00051                 cout <<i<<": "<<argv[i]<<endl;
00052         }
00053 }
00054 
00055 void cout_command_wo_path(int argc, char *argv[]){
00056         int name_pos=commandname_position(argv[0]);
00057         cout_tail(argv[0],name_pos);
00058         cout <<" ";
00059         int i;
00060         for (i=1;i<argc;i++){
00061                 cout <<argv[i]<<" ";
00062         }
00063 }
00064 
00065 void show_commandline (int argc, char *argv[ ]){
00066         cout <<"The whole commandline argv splits into -> "<<argc<<" <- parts:"<<endl;
00067         cout_command_endl(argc, argv);
00068         cout <<"minus the path, this gives:"<<endl;
00069         cout_command_wo_path(argc, argv);
00070         cout <<endl;
00071 }
00072 
00073 // all recognized subprograms go here
00074 
00075 enum subprgs
00076 {
00077         NONE,   
00078     FFC,
00079     FFS,
00080         NOC,
00081         OPT
00082 } ;
00083 subprgs subprg_name2enum(string name){
00084         if (name=="ffc") return FFC;
00085         if (name=="ffs") return FFS;
00086         if (name=="noc") return NOC;
00087         if (name=="opt") return OPT;
00088         return NONE;
00089 }
00090 
00091 ostream& operator<<(ostream& os, subprgs s){
00092         if (s==FFC) os<<"ffc";
00093         if (s==FFS) os<<"ffs";
00094         if (s==NOC) os<<"noc";
00095         if (s==OPT) os<<"opt";
00096         return os;
00097 }
00098 
00099 void show_commandline_options(char* argv0){
00100         cout<<"\n\nCommandline options:"<<endl;
00101         cout<<"--------------------"<<endl;
00102         cout<<"Please use these parameters:"<<endl;
00103         cout<<"0<percent<100\t\t= percent of number of spheres (for noc)"<<endl;
00104         cout<<"fromdim>0, todim<"<<MAXDIM+1<<"\t= dimensions intervall"<<endl;
00105         cout<<"fromN>1, toN<"<<MAXNUMBER+1<<"\t= intervall for spherenumber"<<endl;
00106         cout<<"0< precision <100\t= target precision wished in percent"<<endl;
00107         cout<<"0< minloops\t\t= at least so many averaging loops"<<endl;
00108         cout<<"for the following (at the moment only) three possible subprograms:"<<endl<<endl;
00109         cout<<"Find the critical fillingfactor (spanning cluster threshold)"<<endl;
00110         string name = commandname_wo_path(argv0);
00111         cout<<name<<" ffc fromdim todim fromN toN precision minloops"<<endl;
00112         cout<<"Find the saturating fillingfactor (all-spheres-in-one-cluster threshold)"<<endl;
00113         cout<<name<<" ffs fromdim todim fromN toN precision minloops"<<endl;
00114         cout<<"Find the fillingfactor where number-of-clusters=percent% * N"<<endl;
00115         cout<<name<<" noc percent fromdim todim fromN toN precision minloops"<<endl;
00116         cout<<"   (Optimized especially for percent=1 and percent=90)"<<endl;
00117         cout<<"Check a wide range of cuts for speed:"<<endl;
00118         cout<<name<<" opt ffs/ffc computername"<<endl;
00119 
00120         cout<<""<<endl;
00121         cout<<"N.B.: You have to create a data directory "<<FILEHEAD2<<endl<<endl;
00122 }
00123 
00124 class cline_par{
00125 public:
00126         subprgs subprg;
00127         REAL percentOfSpheres;
00128         int fromdim; int todim;
00129         REAL fromN; NUMBER toN;
00130         REAL precision;
00131         COUNTER at_least;
00132         subprgs criterion;
00133         string computername;
00134 };
00135 void out_subprgname(ostream& os, cline_par& p){
00136         switch (p.subprg) {
00137         case FFC:
00138                 os<<"find critical_fillingfactor";
00139                 break;
00140                 
00141         case FFS:
00142                 os<<"find saturating_fillingfactor";
00143                 break;
00144 
00145         case NOC:
00146                 os<<"find fillingfactor where number-of-clusters=";
00147                 break;
00148 
00149         case OPT:
00150                 os<<"find optimal number of cuts by checking wide range at " ;
00151                 break;
00152 
00153         default:
00154                 os<<"unknown subprogramm";
00155         }
00156 }
00157 ostream& operator<<(ostream& os, cline_par p){
00158         out_subprgname(os,p);
00159         if (p.subprg==NOC) os<<p.percentOfSpheres<<"%";
00160         if (p.subprg==FFC || p.subprg==FFS || p.subprg==NOC) {
00161            os <<" from "<<p.fromdim<<" to "<<p.todim<<" dim and N="<<p.fromN<<" to "<<p.toN;
00162            os <<" with target precision "<<p.precision*100<<"% and at least "<<p.at_least<<" runs.";
00163         }
00164         if (p.subprg==OPT)  os<<p.criterion<<" on computer "<<p.computername;
00165         return os;
00166 }
00167 string get_first_word(char* parameters){
00168         int i=0; string first_word="";
00169         bool foundend=false;
00170         while (!foundend && parameters[i]!='\0'){
00171                 if (parameters[i]!=' '){
00172                         first_word+=parameters[i];
00173                 }
00174                 else{
00175                         foundend=true;
00176                 }
00177                 i++;
00178         }
00179         return first_word;
00180 }
00181 
00182 
00183 bool recognized_switch(int argc, char* argv[], cline_par &param){
00184         bool recog=false;
00185         if (argc==1) {return recog;}
00186         string fw=get_first_word(argv[1]);
00187         param.subprg= subprg_name2enum(fw);
00188         
00189         switch (param.subprg)
00190         {
00191         case FFC: 
00192                 {
00193                 if (argc!=8) break;
00194                 param.fromdim=atoi(argv[2]);
00195                 param.todim=atoi(argv[3]);
00196                 param.fromN=atof(argv[4]);
00197                 param.toN=atoi(argv[5]);
00198                 param.precision=atof(argv[6])/100; // percent
00199                 param.at_least=atoi(argv[7]);
00200                 recog=true;
00201                 }
00202                 break;
00203         case FFS: 
00204                 {
00205                 if (argc!=8) break;
00206                 param.fromdim=atoi(argv[2]);
00207                 param.todim=atoi(argv[3]);
00208                 param.fromN=atof(argv[4]);
00209                 param.toN=atoi(argv[5]);
00210                 param.precision=atof(argv[6])/100; // percent
00211                 param.at_least=atoi(argv[7]);
00212                 recog=true;
00213                 }
00214                 break;
00215         case NOC: 
00216                 {
00217                 if (argc!=9) break;
00218                 param.percentOfSpheres=atof(argv[2]);
00219                 param.fromdim=atoi(argv[3]);
00220                 param.todim=atoi(argv[4]);
00221                 param.fromN=atof(argv[5]);
00222                 param.toN=atoi(argv[6]);
00223                 param.precision=atof(argv[7])/100; // percent precision
00224                 param.at_least=atoi(argv[8]);
00225                 recog=true;
00226                 }
00227                 break;
00228         case OPT:
00229                 {
00230                 if (argc!=4) break;
00231                 param.criterion=subprg_name2enum(string(argv[2]));
00232                 param.computername=string(argv[3]);
00233                 recog=true;
00234                 }
00235                 break;
00236         default: 
00237                 { }
00238         }
00239         return recog; // and parameters param, of course
00240 }
00241 
00242 bool percentage_ok(REAL percentage, REAL lower_N) {
00243         REAL percent_step=100/lower_N;
00244         return (percentage>percent_step); }
00245 bool dimrange_ok(int dim1, int dim2) {return (dim1<=dim2 && dim1>0 && dim2<=MAXDIM);}
00246 bool Nrange_ok(REAL N1, NUMBER N2) { return (N1<=N2 && N1>=2 && N2<MAXNUMBER);}
00247 bool precision_ok(REAL prec){return (prec>0 && prec <1);};
00248 bool atleast_ok (COUNTER a){return (a>=1); }
00249 
00250 bool range_ok(cline_par p){
00251         bool ok=true;
00252         if (p.subprg==NOC){
00253                 if (!percentage_ok(p.percentOfSpheres,p.fromN)) {
00254                         ok=false;
00255                         cerr<<"ERROR: percentage not in allowed range (must be > 100/from_N and <100)"<<endl;
00256                 }
00257         }
00258         if (p.subprg==FFC ||p.subprg==FFS ||p.subprg==NOC){
00259                 if (!dimrange_ok(p.fromdim, p.todim)) {
00260                         ok=false;
00261                         cerr<<"ERROR: dim not in allowed range (1 to "<<MAXDIM<<") or wrong order"<<endl;
00262                 }
00263                 if (!Nrange_ok(p.fromN, p.toN)) {
00264                         ok=false;
00265                         cerr<<"ERROR: N not in allowed range (2 to "<<MAXNUMBER<<") or wrong order"<<endl;
00266                 }
00267                 if (!precision_ok(p.precision)) {
00268                         ok=false;
00269                         cerr<<"ERROR: precision not in allowed range (must be >0 to 100%)"<<endl;
00270                 }
00271                 if (!atleast_ok(p.at_least)) {
00272                         ok=false;
00273                         cerr<<"ERROR: at_least-runs not in allowed range (must be >1)"<<endl;
00274                 }
00275         }
00276         return ok;
00277 }
00278 
00279 int enum2criterion(subprgs subprg){
00280         int criterion;
00281         switch (subprg) {
00282         case FFC:
00283                 criterion=1;
00284                 break;
00285         case FFS:
00286                 criterion=2;
00287                 break;
00288         case NOC:
00289                 criterion=3;
00290                 break;
00291         default:
00292                 cout<<"unknown sub-programm, sorry. (Please write to cpp__at__AndreasKrueger__dot__de)\n"<<endl;
00293                 return -1;
00294         }
00295         return criterion;
00296 }
00297 
00298 int start_subprg(cline_par& p, clock_t& return_totaltime){
00299         return_totaltime=clock();
00300 
00301         ff::fillingfactor_functions stepfn[ff::N_STEPFN+1];     ff::load_stepfunctions(stepfn);
00302         
00303         int criterion;
00304 
00305         if (p.subprg==OPT){
00306                 criterion=enum2criterion(p.criterion);
00307                 optimize::cuts_test_average_time(criterion, 1, p.computername);
00308                 return 0;
00309         }
00310         
00311         criterion=enum2criterion(p.subprg);
00312         if (criterion==-1) {
00313                 return_totaltime=0;
00314                 return 1;
00315         }
00316 
00317         bool (* stepfn_criterion)(REAL, ff::arguments1&)        =stepfn[criterion].fn;
00318         REAL (* ff_guessed)(NUMBER,int,REAL)                            =stepfn[criterion].theory;
00319         string namecriterion                                                            =stepfn[criterion].name;
00320 
00321         REAL percentage=100;            // e.g. NOC90% -> where number-of-clusters=90% * N
00322                                                                 // here set to a dummy-value, if criterion=1 or 2
00323         if (criterion>2) {
00324                 percentage=p.percentOfSpheres;
00325                 char buffer[5]; sprintf(buffer, "", "");
00326                 if (percentage<10) sprintf(buffer, "%1.0f%c", percentage,'%');
00327                 else sprintf(buffer, "%2.0f%c", percentage,'%');
00328                 namecriterion+=buffer;
00329         }
00330 
00331         ff::find_ff_with_criterion_scanning_N_and_dim(stepfn_criterion, percentage/100, namecriterion, 
00332                                                                                           ff_guessed,
00333                                                                                           p.fromdim, p.todim, p.fromN, p.toN, 
00334                                                                                           p.precision, p.at_least);
00335         return_totaltime=clock()-return_totaltime;
00336         return 0;
00337 }
00338 
00339 
00340 }  // end of namespace commandline




Diploma Thesis Sourcecode Documentation
check out the text and the executable binaries

www.AndreasKrueger.de/thesis/code