00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef MECHSYS_LABTESTSIM_INPUTDATA_H
00023 #define MECHSYS_LABTESTSIM_INPUTDATA_H
00024
00025 #ifdef HAVE_CONFIG_H
00026 #include "config.h"
00027 #else
00028 #ifndef REAL
00029 #define REAL double
00030 #endif
00031 #endif
00032
00033 #include <sstream>
00034 #include <fstream>
00035
00036 #include "util/array.h"
00037 #include "util/string.h"
00038 #include "util/exception.h"
00039 #include "util/lineparser.h"
00040 #include "util/fileparser.h"
00041 #include "util/numstreams.h"
00042 #include "util/util.h"
00043 #include "numerical/integschemesctes.h"
00044
00045 using Util::Str2Bool;
00046
00047 namespace LabTestSim
00048 {
00049
00050
00051 const int TOT_TAGS = 38;
00052 const FileParser::Tags TAGS[]=
00053 {{ "PROJECT" , "NAME" , "Project name" , true , "" },
00054 { "PROJECT" , "DESC" , "Project description (optional)" , false , "" },
00055 { "DATA" , "PATH" , "Path to the directory with data files" , true , "" },
00056 { "DATA" , "DAT" , "Data filenames" , true , "" },
00057 { "MATERIAL", "PATH" , "Path to the directory with the material file" , false , "" },
00058 { "MATERIAL", "MAT" , "Material filename" , false , "" },
00059 { "INITIAL" , "OCR" , "Initial OCR (optional)" , false , "1.0" },
00060 { "INITIAL" , "OSI" , "Initial Over Suction Incremnt = s_max - s" , false , "0.0" },
00061 { "SOLVER" , "ACTIVE" , "Run a simulation with LabTestSim?" , false , "" },
00062 { "SOLVER" , "MODEL" , "Model name" , false , "" },
00063 { "SOLVER" , "EQUIPS" , "Equipments names" , false , "" },
00064 { "SOLVER" , "SKIPLAST" , "Skip last # stress states" , false , "" },
00065 { "SOLVER" , "SKIPFIRST" , "Skip first # stress states" , false , "" },
00066 { "SOLVER" , "NDIV" , "Number of division for each path/state (optional)" , false , "10" },
00067 { "SOLVER" , "SUBSCHEME" , "Sub-scheme integration constants (optional)" , false , "" },
00068 { "SOLVER" , "STOPONFAIL", "Stop on failure (optional)" , false , "" },
00069 { "SOLVER" , "SILENT" , "Do not show Warnings and Messages (optional)" , false , "false" },
00070 { "OUTPUT" , "PRINCVALS" , "Output principal values ? (optional)" , false , "false" },
00071 { "OUTPUT" , "SAVEINTSTATES" , "Save internal states of stress and strains?" , false , "false" },
00072 { "OUTPUT" , "FILENAMES" , "Array of output filenames" , false , "" },
00073 { "PRMSEEK" , "ACTIVE" , "Run a prmseek session?" , false , "false" },
00074 { "PRMSEEK" , "NGENES" , "Number of genes == number of traits == number of parameters" , false , "" },
00075 { "PRMSEEK" , "NBASES" , "Number of bases used for each gene" , false , "" },
00076 { "PRMSEEK" , "MINGVALS" , "Min Genes (parameters) values" , false , "" },
00077 { "PRMSEEK" , "MAXGVALS" , "Max Genes (parameters) values" , false , "" },
00078 { "PRMSEEK" , "NELIT" , "Number of elite individuals (optional)" , false , "12" },
00079 { "PRMSEEK" , "NINDV" , "Number of individuals (optional)" , false , "40" },
00080 { "PRMSEEK" , "NGENS" , "Number of generations (optional)" , false , "100" },
00081 { "PRMSEEK" , "NISLS" , "Number of islands (optional)" , false , "6" },
00082 { "PRMSEEK" , "INMIG" , "Interval between migrations (optional)" , false , "10" },
00083 { "PRMSEEK" , "NIMIG" , "Number of individuals that migrate (optional)" , false , "8" },
00084 { "PRMSEEK" , "STPOU" , "Step out interval (optional)" , false , "2" },
00085 { "PRMSEEK" , "IISLO" , "Index of an island to output during evolution (optional)" , false , "0" },
00086 { "PRMSEEK" , "SELPRES" , "Selective pressure (rank-based fittness) (optional)" , false , "2.0" },
00087 { "PRMSEEK" , "PROBMUT" , "Probability of mutation (optional)" , false , "0.2" },
00088 { "PRMSEEK" , "FNAPPEND" , "Filename to append prmseek results" , false , "" },
00089 { "PRMSEEK" , "WEIGHTS" , "Weights for the calculation of objective values for each test" , false , "" },
00090 { "PRMSEEK" , "USESGA" , "Use SGA library? (false => use GAlib)" , false , "false" }};
00091
00092
00093 class InputData
00094 {
00095 public:
00096 FileParser::StructVals Values;
00097
00098 String NAME;
00099 String DESC;
00100
00101 String DATPATH;
00102 Array<String> a_DAT;
00103
00104 String MATPATH;
00105 String MAT;
00106
00107 Array<REAL> a_OCR;
00108 Array<REAL> a_OSI;
00109
00110 bool ACTIVE;
00111 String MODEL;
00112 Array<int> a_SKIPLAST;
00113 Array<int> a_SKIPFIRST;
00114 Array<String> a_EQUIPS;
00115 int NDIV;
00116 IntegSchemesCtes intSC;
00117 bool StopOnFailure;
00118 bool Silent;
00119
00120 bool PRINCVALS;
00121 bool SAVEINTSTATES;
00122 Array<String> a_OUTFNS;
00123
00124 bool PRS_ACTIVE;
00125 int NGENES;
00126 int NBASES;
00127 String MINGVALS;
00128 String MAXGVALS;
00129 Array<REAL> a_MINGVALS;
00130 Array<REAL> a_MAXGVALS;
00131 int NELIT;
00132 int NINDV;
00133 int NGENS;
00134 int NISLS;
00135 int INMIG;
00136 int NIMIG;
00137 int STPOU;
00138 int IISLO;
00139 REAL SELPRES;
00140 REAL PROBMUT;
00141 String FNAPPEND;
00142 Array<REAL> a_WEIGHTS;
00143 bool USESGA;
00144
00145 public:
00146 String fnMAT;
00147 Array<String> a_fnDAT;
00148 Array<String> a_fnCAL;
00149 Array<String> a_fnPNG;
00150 Array<FileParser::Table> a_data;
00151 Array<FileParser::Table> a_calc;
00152
00153 public:
00154 void ParseInputArgs (int argc, char **argv, String & MainFilename, bool & IsConsole, bool & IsSilent, bool & IsHelpMsg);
00155 void ReadMainFile (String const & MainFilename);
00156 void WriteMainFile (String const & MainFilename, bool DoOverwrite=false);
00157 String InitialMessage ();
00158 String HelpMessage ();
00159 void ReadCalc ();
00160
00161 private:
00162 String _prog_name;
00163 void _read_data ();
00164 };
00165
00167
00168 inline void InputData::ParseInputArgs (int argc, char **argv, String & MainFilename, bool & IsConsole, bool & IsSilent, bool & IsHelpMsg)
00169 {
00179
00180 _prog_name = argv[0];
00181
00182
00183 MainFilename = "";
00184 IsConsole = false;
00185 IsSilent = false;
00186 IsHelpMsg = false;
00187
00188
00189 if (argc==1)
00190 return;
00191
00192
00193 if (argc==2)
00194 {
00195 if ((strcmp(argv[1],"-h")==0) || (strcmp(argv[1],"--help")==0))
00196 IsHelpMsg = true;
00197 else
00198 MainFilename = argv[1];
00199 }
00200
00201
00202 if (argc==3)
00203 {
00204 if (strcmp(argv[1],"--nogui")==0)
00205 {
00206
00207 IsConsole = true;
00208 MainFilename = argv[2];
00209 }
00210 else if (strcmp(argv[1],"--silent")==0)
00211 {
00212
00213 IsConsole = true;
00214 IsSilent = true;
00215 MainFilename = argv[2];
00216 }
00217 else
00218 throw new Warning(HelpMessage().c_str());
00219 }
00220
00221
00222 if (argc>=4)
00223 throw new Warning(HelpMessage().c_str());
00224
00225 }
00226
00227 inline void InputData::ReadMainFile(String const & MainFilename)
00228 {
00229
00230 FileParser FP(MainFilename);
00231 FP.StructFile(TAGS, TOT_TAGS, Values);
00232
00233
00234 LineParser LP(String(""));
00235
00236
00237 NAME = Values["PROJECT.NAME"];
00238 DESC = Values["PROJECT.DESC"];
00239
00240 DATPATH = Values["DATA.PATH"];
00241 LP.Reset(Values["DATA.DAT"]); LP.ToArray(a_DAT);
00242
00243 MATPATH = Values["MATERIAL.PATH"];
00244 MAT = Values["MATERIAL.MAT"];
00245
00246 LP.Reset(Values["INITIAL.OCR"]); LP.ToArray(a_OCR);
00247 LP.Reset(Values["INITIAL.OSI"]); LP.ToArray(a_OSI);
00248
00249 ACTIVE = Str2Bool(Values["SOLVER.ACTIVE"]);
00250 MODEL = Values["SOLVER.MODEL"];
00251 LP.Reset(Values["SOLVER.SKIPLAST" ]); LP.ToArray(a_SKIPLAST);
00252 LP.Reset(Values["SOLVER.SKIPFIRST"]); LP.ToArray(a_SKIPFIRST);
00253 LP.Reset(Values["SOLVER.EQUIPS" ]); LP.ToArray(a_EQUIPS);
00254 LP.Reset(Values["SOLVER.NDIV" ]); LP>>NDIV;
00255 StopOnFailure = Str2Bool(Values["SOLVER.STOPONFAIL"]);
00256 Silent = Str2Bool(Values["SOLVER.SILENT"]);
00257
00258
00259 intSC.Type (IntegSchemesCtes::ME);
00260 intSC.FE_ndiv (10);
00261 intSC.ME_maxSS (100);
00262 intSC.ME_STOL (1.e-03);
00263 intSC.ME_dTini (0.1);
00264 intSC.ME_mMin (0.1);
00265 intSC.ME_mMax (2.0);
00266 intSC.EdMax (20.0/100.0);
00267 LP.Reset(Values["SOLVER.SUBSCHEME"]);
00268 String str;
00269 while (LP>>str)
00270 {
00271 String ssubkey;
00272 LineParser lp(str);
00273 lp.ReplaceAllChars('=',' ');
00274 lp>>ssubkey;
00275 if (ssubkey=="type") { String val; lp>>val; intSC.Type (val); }
00276 else if (ssubkey=="ndiv") { int val; lp>>val; intSC.FE_ndiv (val); }
00277 else if (ssubkey=="maxSS") { int val; lp>>val; intSC.ME_maxSS (val); }
00278 else if (ssubkey=="STOL") { REAL val; lp>>val; intSC.ME_STOL (val); }
00279 else if (ssubkey=="dTini") { REAL val; lp>>val; intSC.ME_dTini (val); }
00280 else if (ssubkey=="mMin") { REAL val; lp>>val; intSC.ME_mMin (val); }
00281 else if (ssubkey=="mMax") { REAL val; lp>>val; intSC.ME_mMax (val); }
00282 else if (ssubkey=="EdMax") { REAL val; lp>>val; intSC.EdMax (val/100.0); }
00283 else throw new Fatal(_("InputData::ReadMainFile: < SOLVER SUBSCHEME %s > is invalid"), ssubkey.c_str());
00284 }
00285
00286
00287 PRINCVALS = Str2Bool(Values["OUTPUT.PRINCVALS"]);
00288 SAVEINTSTATES = Str2Bool(Values["OUTPUT.SAVEINTSTATES"]);
00289 LP.Reset(Values["OUTPUT.FILENAMES"]); LP.ToArray(a_OUTFNS);
00290
00291 PRS_ACTIVE = Str2Bool(Values["PRMSEEK.ACTIVE"]);
00292 LP.Reset(Values["PRMSEEK.NGENES"]); LP>>NGENES;
00293 LP.Reset(Values["PRMSEEK.NBASES"]); LP>>NBASES;
00294 MINGVALS = Values["PRMSEEK.MINGVALS"];
00295 MAXGVALS = Values["PRMSEEK.MAXGVALS"];
00296 LP.Reset(MINGVALS); LP.ToArray(a_MINGVALS);
00297 LP.Reset(MAXGVALS); LP.ToArray(a_MAXGVALS);
00298 LP.Reset(Values["PRMSEEK.NELIT" ]); LP>>NELIT;
00299 LP.Reset(Values["PRMSEEK.NINDV" ]); LP>>NINDV;
00300 LP.Reset(Values["PRMSEEK.NGENS" ]); LP>>NGENS;
00301 LP.Reset(Values["PRMSEEK.NISLS" ]); LP>>NISLS;
00302 LP.Reset(Values["PRMSEEK.INMIG" ]); LP>>INMIG;
00303 LP.Reset(Values["PRMSEEK.NIMIG" ]); LP>>NIMIG;
00304 LP.Reset(Values["PRMSEEK.STPOU" ]); LP>>STPOU;
00305 LP.Reset(Values["PRMSEEK.IISLO" ]); LP>>IISLO;
00306 LP.Reset(Values["PRMSEEK.SELPRES"]); LP>>SELPRES;
00307 LP.Reset(Values["PRMSEEK.PROBMUT"]); LP>>PROBMUT;
00308 FNAPPEND = Values["PRMSEEK.FNAPPEND"];
00309 LP.Reset(Values["PRMSEEK.WEIGHTS"]); LP.ToArray(a_WEIGHTS);
00310 USESGA = Str2Bool(Values["PRMSEEK.USESGA"]);
00311
00312
00313 fnMAT = MATPATH; fnMAT.append(String("/")); fnMAT.append(MAT); fnMAT.append(String(".mat"));
00314 a_fnDAT.resize(a_DAT.size());
00315 a_fnCAL.resize(a_DAT.size());
00316 a_fnPNG.resize(a_DAT.size());
00317 for (size_t i=0; i<a_DAT.size(); ++i)
00318 {
00319 a_fnDAT[i].append(DATPATH); a_fnDAT[i].append(_("/")); a_fnDAT[i].append(a_DAT[i]); a_fnDAT[i].append(_(".dat"));
00320 if (a_OUTFNS.size()!=a_DAT.size())
00321 {
00322 a_fnCAL[i].append(MODEL ); a_fnCAL[i].append(_(".")); a_fnCAL[i].append(a_DAT[i]); a_fnCAL[i].append(_(".cal"));
00323 a_fnPNG[i].append(MODEL ); a_fnPNG[i].append(_(".")); a_fnPNG[i].append(a_DAT[i]); a_fnPNG[i].append(_(".png"));
00324 }
00325 else
00326 {
00327 a_fnCAL[i].append(MODEL ); a_fnCAL[i].append(_(".")); a_fnCAL[i].append(a_OUTFNS[i]); a_fnCAL[i].append(_(".cal"));
00328 a_fnPNG[i].append(MODEL ); a_fnPNG[i].append(_(".")); a_fnPNG[i].append(a_OUTFNS[i]); a_fnPNG[i].append(_(".png"));
00329 }
00330 }
00331
00332
00333 if (a_SKIPLAST.size()!=a_DAT.size())
00334 {
00335 if (a_SKIPLAST.size()==0) a_SKIPLAST.resize(a_DAT.size(), 0);
00336 else if (a_SKIPLAST.size()==1) a_SKIPLAST.resize(a_DAT.size(), a_SKIPLAST[0]);
00337 else
00338 throw new Fatal(_("InputData::ReadMainFile: < SOLVER SKIPLAST > The number (%d) of \"skips\" must be one (1) or equal to the number (%d) of data files < DATA DAT >"),
00339 a_SKIPLAST.size(), a_DAT.size());
00340 }
00341 if (a_SKIPFIRST.size()!=a_DAT.size())
00342 {
00343 if (a_SKIPFIRST.size()==0) a_SKIPFIRST.resize(a_DAT.size(), 0);
00344 else if (a_SKIPFIRST.size()==1) a_SKIPFIRST.resize(a_DAT.size(), a_SKIPFIRST[0]);
00345 else
00346 throw new Fatal(_("InputData::ReadMainFile: < SOLVER SKIPFIRST > The number (%d) of \"skips\" must be one (1) or equal to the number (%d) of data files < DATA DAT >"),
00347 a_SKIPFIRST.size(), a_DAT.size());
00348 }
00349 if (a_WEIGHTS.size()!=a_DAT.size())
00350 {
00351 if (a_WEIGHTS.size()==0) a_WEIGHTS.resize(a_DAT.size(), 1.0);
00352 else
00353 throw new Fatal(_("InputData::ReadMainFile: < PRMSEEK WEIGHTS > The number (%d) of weights must be equal to the number (%d) of data files < DATA DAT >"),
00354 a_WEIGHTS.size(), a_DAT.size());
00355 }
00356 if ((ACTIVE) & (a_EQUIPS.size()!=a_DAT.size()))
00357 throw new Fatal(_("InputData::ReadMainFile: < SOLVER EQUIPS > The number (%d) of equipments must be equal to the number (%d) of data files < DATA DAT >"),
00358 a_EQUIPS.size(), a_DAT.size());
00359
00360
00361 if (a_OCR.size()!=a_DAT.size())
00362 {
00363 if (a_OCR.size()==1) a_OCR.resize(a_DAT.size(), a_OCR[0]);
00364 else
00365 throw new Fatal(_("InputData::ReadMainFile: < SOLVER OCR > The number (%d) of \"OCR's\" must be one (1) or equal to the number (%d) of data files < DATA DAT >"),
00366 a_OCR.size(), a_DAT.size());
00367 }
00368
00369
00370 if (a_OSI.size()!=a_DAT.size())
00371 {
00372 if (a_OSI.size()==0) a_OSI.resize(a_DAT.size(), 0.0);
00373 else if (a_OSI.size()==1) a_OSI.resize(a_DAT.size(), a_OSI[0]);
00374 else
00375 throw new Fatal(_("InputData::ReadMainFile: < SOLVER OSI > The number (%d) of \"OSI's\" must be one (1) or equal to the number (%d) of data files < DATA DAT >"),
00376 a_OSI.size(), a_DAT.size());
00377 }
00378
00379
00380 _read_data();
00381
00382 }
00383
00384 inline void InputData::WriteMainFile(String const & MainFilename, bool DoOverwrite)
00385 {
00386
00387 if (!DoOverwrite)
00388 if (FileParser::CheckForFile(MainFilename))
00389 throw new Warning(_("File < %s > exists"), MainFilename.c_str());
00390
00391
00392 std::ofstream OF(MainFilename.GetSTL().c_str(), std::ios::out);
00393
00394
00395 OF << "#################################### " << NAME << ".main\n";
00396 for (int i=0; i<TOT_TAGS; ++i)
00397 {
00398 String tag(TAGS[i].L1); tag.append(String(".")); tag.append(String(TAGS[i].L2));
00399 OF << TAGS[i].L1 << " " << TAGS[i].L2 << " " << Values[tag.GetSTL()] << " # " << TAGS[i].Comment << std::endl;
00400 }
00401
00402 for (size_t i=0; i<a_fnDAT.size(); ++i)
00403 OF << "# " << a_fnDAT[i] << " ";
00404 OF << std::endl << std::endl;
00405
00406 for (size_t i=0; i<a_fnCAL.size(); ++i)
00407 OF << "# " << a_fnCAL[i] << " ";
00408 OF << std::endl << std::endl;
00409
00410
00411 OF.close();
00412 }
00413
00414 inline String InputData::InitialMessage()
00415 {
00416 std::ostringstream oss;
00417 oss << _("LabTestSim, Copyright (C) 2005 Dorival M. Pedroso, Raúl D. Durand F.\n");
00418 oss << _(" LabTestSim comes with ABSOLUTELY NO WARRANTY.\n");
00419 oss << _(" This is free software, and you are welcome\n");
00420 oss << _(" to redistribute it under certain conditions.\n");
00421 return oss.str();
00422 }
00423
00424 inline String InputData::HelpMessage()
00425 {
00426 std::ostringstream oss;
00427 oss << InitialMessage() << std::endl;
00428 oss << _("Command line options:\n");
00429 oss << " $ " << _prog_name << " => " << _("GUI session \n");
00430 oss << " $ " << _prog_name << " -h => " << _("shows help message and exit \n");
00431 oss << " $ " << _prog_name << " --help => " << _("shows help message and exit \n");
00432 oss << " $ " << _prog_name << " projname.main => " << _("GUI session using projname \n");
00433 oss << " $ " << _prog_name << " --nogui projname.main => " << _("Console session using projname \n");
00434 oss << " $ " << _prog_name << " --silent projname.main => " << _("Silent console session using projname \n");
00435 oss << std::endl;
00436 return oss.str();
00437 }
00438
00439 inline void InputData::ReadCalc()
00440 {
00441 a_calc.resize(0);
00442 for (size_t i=0; i<a_fnCAL.size(); ++i)
00443 {
00444 if (!FileParser::CheckForFile(a_fnCAL[i]))
00445 throw new Fatal(_("InputData::ReadCalc: Could not find file < %s >"),a_fnCAL[i].c_str());
00446 FileParser::Table T;
00447 FileParser FP(a_fnCAL[i]);
00448 FP.ReadTable(T);
00449 a_calc.push_back(T);
00450 }
00451 }
00452
00453 inline void InputData::_read_data()
00454 {
00455 a_data.resize(0);
00456 for (size_t i=0; i<a_fnDAT.size(); ++i)
00457 {
00458 FileParser::Table T;
00459 FileParser FP(a_fnDAT[i]);
00460 FP.ReadTable(T);
00461 a_data.push_back(T);
00462 }
00463 }
00464
00465 };
00466
00467 #endif // MECHSYS_LABTESTSIM_INPUTDATA_H
00468
00469