00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "argList.H"
00028 #include "OSspecific.H"
00029 #include "clock.H"
00030 #include "IFstream.H"
00031 #include "dictionary.H"
00032 #include "Switch.H"
00033 #include "IOobject.H"
00034 #include "JobInfo.H"
00035 #include "labelList.H"
00036
00037
00038
00039
00040 Foam::SLList<Foam::string> Foam::argList::validArgs;
00041 Foam::HashTable<Foam::string> Foam::argList::validOptions;
00042 Foam::HashTable<Foam::string> Foam::argList::validParOptions;
00043 bool Foam::argList::bannerEnabled(true);
00044
00045
00046 Foam::argList::initValidTables::initValidTables()
00047 {
00048 validOptions.set("case", "dir");
00049 validOptions.set("parallel", "");
00050 validParOptions.set("parallel", "");
00051
00052 Pstream::addValidParOptions(validParOptions);
00053 }
00054
00055
00056 Foam::argList::initValidTables dummyInitValidTables;
00057
00058
00059
00060
00061 bool Foam::argList::regroupArgv(int& argc, char**& argv)
00062 {
00063 int level = 0;
00064 int nArgs = 0;
00065 string tmpString;
00066
00067
00068
00069 for (int argi=0; argi < argc; argi++)
00070 {
00071 if (strcmp(argv[argi], "(") == 0)
00072 {
00073 level++;
00074 tmpString += "(";
00075 }
00076 else if (strcmp(argv[argi], ")") == 0)
00077 {
00078 if (level >= 1)
00079 {
00080 level--;
00081 tmpString += ")";
00082 if (level == 0)
00083 {
00084 args_[nArgs++] = tmpString;
00085 tmpString.clear();
00086 }
00087 }
00088 else
00089 {
00090 args_[nArgs++] = argv[argi];
00091 }
00092 }
00093 else if (level)
00094 {
00095
00096 tmpString += "\"";
00097 tmpString += argv[argi];
00098 tmpString += "\"";
00099 }
00100 else
00101 {
00102 args_[nArgs++] = argv[argi];
00103 }
00104 }
00105
00106 if (tmpString.size())
00107 {
00108 args_[nArgs++] = tmpString;
00109 }
00110
00111 args_.setSize(nArgs);
00112
00113 return nArgs < argc;
00114 }
00115
00116
00117
00118
00119
00120 void Foam::argList::getRootCase()
00121 {
00122 fileName casePath;
00123
00124
00125 HashTable<string>::iterator iter = options_.find("case");
00126
00127 if (iter != options_.end())
00128 {
00129 casePath = iter();
00130 casePath.clean();
00131
00132
00133 if (casePath.empty() || casePath == ".")
00134 {
00135 casePath = cwd();
00136 options_.erase("case");
00137 }
00138 }
00139 else
00140 {
00141
00142 casePath = cwd();
00143 }
00144
00145 rootPath_ = casePath.path();
00146 globalCase_ = casePath.name();
00147 case_ = globalCase_;
00148 }
00149
00150
00151 Foam::stringList::subList Foam::argList::additionalArgs() const
00152 {
00153 return stringList::subList(args_, args_.size() - 1, 1);
00154 }
00155
00156
00157
00158
00159 Foam::argList::argList
00160 (
00161 int& argc,
00162 char**& argv,
00163 bool checkArgs,
00164 bool checkOpts
00165 )
00166 :
00167 args_(argc),
00168 options_(argc)
00169 {
00170
00171
00172 for (int argi=0; argi<argc; argi++)
00173 {
00174 if (argv[argi][0] == '-')
00175 {
00176 const char *optionName = &argv[argi][1];
00177
00178 if (validParOptions.found(optionName))
00179 {
00180 parRunControl_.runPar(argc, argv);
00181 break;
00182 }
00183 }
00184 }
00185
00186
00187
00188 regroupArgv(argc, argv);
00189
00190
00191 args_[0] = fileName(argv[0]);
00192 executable_ = fileName(argv[0]).name();
00193
00194
00195 int nArgs = 1;
00196 string argListString = args_[0];
00197
00198 for (int argi = 1; argi < args_.size(); argi++)
00199 {
00200 argListString += ' ';
00201 argListString += args_[argi];
00202
00203 if (args_[argi][0] == '-')
00204 {
00205 const char *optionName = &args_[argi][1];
00206
00207 if
00208 (
00209 (
00210 validOptions.found(optionName)
00211 && validOptions[optionName] != ""
00212 )
00213 || (
00214 validParOptions.found(optionName)
00215 && validParOptions[optionName] != ""
00216 )
00217 )
00218 {
00219 argi++;
00220 if (argi >= args_.size())
00221 {
00222 FatalError
00223 << "option " << "'-" << optionName << '\''
00224 << " requires an argument"
00225 << exit(FatalError);
00226 }
00227
00228 argListString += ' ';
00229 argListString += args_[argi];
00230 options_.insert(optionName, args_[argi]);
00231 }
00232 else
00233 {
00234 options_.insert(optionName, "");
00235 }
00236 }
00237 else
00238 {
00239 if (nArgs != argi)
00240 {
00241 args_[nArgs] = args_[argi];
00242 }
00243 nArgs++;
00244 }
00245 }
00246
00247 args_.setSize(nArgs);
00248
00249
00250
00251
00252
00253 if
00254 (
00255 options_.found("help")
00256 || options_.found("doc")
00257 || options_.found("srcDoc")
00258 )
00259 {
00260 if (options_.found("help"))
00261 {
00262 printUsage();
00263 }
00264
00265
00266 if (options_.found("srcDoc"))
00267 {
00268 displayDoc(true);
00269 }
00270 else if (options_.found("doc"))
00271 {
00272 displayDoc(false);
00273 }
00274
00275 ::exit(0);
00276 }
00277
00278
00279 if (!check(checkArgs, checkOpts))
00280 {
00281 FatalError.exit();
00282 }
00283
00284
00285 string dateString = clock::date();
00286 string timeString = clock::clockTime();
00287
00288
00289 if (Pstream::master() && bannerEnabled)
00290 {
00291 IOobject::writeBanner(Info, true)
00292 << "Build : " << Foam::FOAMbuild << nl
00293 << "Exec : " << argListString.c_str() << nl
00294 << "Date : " << dateString.c_str() << nl
00295 << "Time : " << timeString.c_str() << nl
00296 << "Host : " << hostName() << nl
00297 << "PID : " << pid() << endl;
00298 }
00299
00300 jobInfo.add("startDate", dateString);
00301 jobInfo.add("startTime", timeString);
00302 jobInfo.add("userName", userName());
00303 jobInfo.add("foamVersion", word(FOAMversion));
00304 jobInfo.add("foamBuild", Foam::FOAMbuild);
00305 jobInfo.add("code", executable_);
00306 jobInfo.add("argList", argListString);
00307 jobInfo.add("currentDir", cwd());
00308 jobInfo.add("PPID", ppid());
00309 jobInfo.add("PGID", pgid());
00310
00311
00312
00313 int nProcs = 1;
00314
00315
00316 if (parRunControl_.parRun())
00317 {
00318
00319 if (Pstream::master())
00320 {
00321
00322 getRootCase();
00323
00324 IFstream decompDictStream
00325 (
00326 rootPath_/globalCase_/"system/decomposeParDict"
00327 );
00328
00329 if (!decompDictStream.good())
00330 {
00331 FatalError
00332 << "Cannot read "
00333 << decompDictStream.name()
00334 << exit(FatalError);
00335 }
00336
00337 dictionary decompDict(decompDictStream);
00338
00339 label dictNProcs
00340 (
00341 readLabel
00342 (
00343 decompDict.lookup("numberOfSubdomains")
00344 )
00345 );
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 if (dictNProcs > Pstream::nProcs())
00357 {
00358 FatalError
00359 << decompDictStream.name()
00360 << " specifies " << dictNProcs
00361 << " processors but job was started with "
00362 << Pstream::nProcs() << " processors."
00363 << exit(FatalError);
00364 }
00365
00366
00367 if (decompDict.lookupOrDefault<Switch>("distributed", false))
00368 {
00369 fileNameList roots;
00370 decompDict.lookup("roots") >> roots;
00371
00372 if (roots.size() != Pstream::nProcs()-1)
00373 {
00374 FatalError
00375 << "number of entries in decompositionDict::roots"
00376 << " is not equal to the number of slaves "
00377 << Pstream::nProcs()-1
00378 << exit(FatalError);
00379 }
00380
00381
00382 bool hadCaseOpt = options_.found("case");
00383 for
00384 (
00385 int slave=Pstream::firstSlave();
00386 slave<=Pstream::lastSlave();
00387 slave++
00388 )
00389 {
00390 options_.set
00391 (
00392 "case",
00393 fileName(roots[slave-1])/globalCase_
00394 );
00395
00396 OPstream toSlave(Pstream::scheduled, slave);
00397 toSlave << args_ << options_;
00398 }
00399 options_.erase("case");
00400
00401
00402 if (hadCaseOpt)
00403 {
00404 options_.set("case", rootPath_/globalCase_);
00405 }
00406 }
00407 else
00408 {
00409
00410
00411 if (dictNProcs < Pstream::nProcs())
00412 {
00413 label nProcDirs = 0;
00414 while
00415 (
00416 isDir
00417 (
00418 rootPath_/globalCase_/"processor"
00419 + name(++nProcDirs)
00420 )
00421 )
00422 {}
00423
00424 if (nProcDirs != Pstream::nProcs())
00425 {
00426 FatalError
00427 << "number of processor directories = "
00428 << nProcDirs
00429 << " is not equal to the number of processors = "
00430 << Pstream::nProcs()
00431 << exit(FatalError);
00432 }
00433 }
00434
00435
00436 for
00437 (
00438 int slave=Pstream::firstSlave();
00439 slave<=Pstream::lastSlave();
00440 slave++
00441 )
00442 {
00443 OPstream toSlave(Pstream::scheduled, slave);
00444 toSlave << args_ << options_;
00445 }
00446 }
00447 }
00448 else
00449 {
00450
00451 IPstream fromMaster(Pstream::scheduled, Pstream::masterNo());
00452 fromMaster >> args_ >> options_;
00453
00454
00455 getRootCase();
00456 }
00457
00458 nProcs = Pstream::nProcs();
00459 case_ = globalCase_/(word("processor") + name(Pstream::myProcNo()));
00460 }
00461 else
00462 {
00463
00464 getRootCase();
00465 case_ = globalCase_;
00466 }
00467
00468
00469 wordList slaveProcs;
00470
00471
00472 if (parRunControl_.parRun())
00473 {
00474 if (Pstream::master())
00475 {
00476 slaveProcs.setSize(Pstream::nProcs() - 1);
00477 word slaveMachine;
00478 label slavePid;
00479
00480 label procI = 0;
00481 for
00482 (
00483 int slave=Pstream::firstSlave();
00484 slave<=Pstream::lastSlave();
00485 slave++
00486 )
00487 {
00488 IPstream fromSlave(Pstream::scheduled, slave);
00489 fromSlave >> slaveMachine >> slavePid;
00490
00491 slaveProcs[procI++] = slaveMachine + "." + name(slavePid);
00492 }
00493 }
00494 else
00495 {
00496 OPstream toMaster(Pstream::scheduled, Pstream::masterNo());
00497 toMaster << hostName() << pid();
00498 }
00499 }
00500
00501
00502 if (Pstream::master() && bannerEnabled)
00503 {
00504 Info<< "Case : " << (rootPath_/globalCase_).c_str() << nl
00505 << "nProcs : " << nProcs << endl;
00506
00507 if (parRunControl_.parRun())
00508 {
00509 Info<< "Slaves : " << slaveProcs << nl
00510 << "Pstream initialized with:" << nl
00511 << " floatTransfer : " << Pstream::floatTransfer << nl
00512 << " nProcsSimpleSum : " << Pstream::nProcsSimpleSum << nl
00513 << " commsType : "
00514 << Pstream::commsTypeNames[Pstream::defaultCommsType]
00515 << endl;
00516 }
00517 }
00518
00519 jobInfo.add("root", rootPath_);
00520 jobInfo.add("case", globalCase_);
00521 jobInfo.add("nProcs", nProcs);
00522 if (slaveProcs.size())
00523 {
00524 jobInfo.add("slaves", slaveProcs);
00525 }
00526 jobInfo.write();
00527
00528
00529
00530 if (rootPath_[0] == '/')
00531 {
00532
00533 setEnv("FOAM_CASE", rootPath_/globalCase_, true);
00534 }
00535 else if (rootPath_ == ".")
00536 {
00537
00538 setEnv("FOAM_CASE", cwd()/globalCase_, true);
00539 }
00540 else
00541 {
00542
00543 setEnv("FOAM_CASE", cwd()/rootPath_/globalCase_, true);
00544 }
00545 setEnv("FOAM_CASENAME", globalCase_, true);
00546
00547
00548
00549
00550 sigFpe_.set(bannerEnabled);
00551 sigInt_.set(bannerEnabled);
00552 sigQuit_.set(bannerEnabled);
00553 sigSegv_.set(bannerEnabled);
00554
00555 if (Pstream::master() && bannerEnabled)
00556 {
00557 Info<< endl;
00558 IOobject::writeDivider(Info);
00559 }
00560 }
00561
00562
00563
00564
00565 Foam::argList::~argList()
00566 {
00567 jobInfo.end();
00568 }
00569
00570
00571
00572
00573 void Foam::argList::noBanner()
00574 {
00575 bannerEnabled = false;
00576 }
00577
00578
00579 void Foam::argList::noParallel()
00580 {
00581 validOptions.erase("parallel");
00582 }
00583
00584
00585 void Foam::argList::printUsage() const
00586 {
00587 Info<< nl
00588 << "Usage: " << executable_;
00589
00590 for
00591 (
00592 SLList<string>::iterator iter = validArgs.begin();
00593 iter != validArgs.end();
00594 ++iter
00595 )
00596 {
00597 Info<< " <" << iter().c_str() << '>';
00598 }
00599
00600 for
00601 (
00602 HashTable<string>::iterator iter = validOptions.begin();
00603 iter != validOptions.end();
00604 ++iter
00605 )
00606 {
00607 Info<< " [-" << iter.key();
00608
00609 if (iter().size())
00610 {
00611 Info<< ' ' << iter().c_str();
00612 }
00613
00614 Info<< ']';
00615 }
00616
00617
00618
00619 Info<< " [-help] [-doc] [-srcDoc]\n" << endl;
00620 }
00621
00622
00623 void Foam::argList::displayDoc(bool source) const
00624 {
00625 const dictionary& docDict = debug::controlDict().subDict("Documentation");
00626 List<fileName> docDirs(docDict.lookup("doxyDocDirs"));
00627 List<fileName> docExts(docDict.lookup("doxySourceFileExts"));
00628
00629
00630 if (source)
00631 {
00632 forAll(docExts, extI)
00633 {
00634 docExts[extI].replace(".", "-source.");
00635 }
00636 }
00637
00638 fileName docFile;
00639 bool found = false;
00640
00641 forAll(docDirs, dirI)
00642 {
00643 forAll(docExts, extI)
00644 {
00645 docFile = docDirs[dirI]/executable_ + docExts[extI];
00646 docFile.expand();
00647
00648 if (isFile(docFile))
00649 {
00650 found = true;
00651 break;
00652 }
00653 }
00654 if (found)
00655 {
00656 break;
00657 }
00658 }
00659
00660 if (found)
00661 {
00662 string docBrowser(docDict.lookup("docBrowser"));
00663 docBrowser.replaceAll("%f", docFile);
00664
00665 Info<< "Show documentation: " << docBrowser.c_str() << endl;
00666
00667 system(docBrowser);
00668 }
00669 else
00670 {
00671 Info<< nl
00672 << "No documentation found for " << executable_
00673 << ", but you can use -help to display the usage\n" << endl;
00674 }
00675 }
00676
00677
00678 bool Foam::argList::check(bool checkArgs, bool checkOpts) const
00679 {
00680 bool ok = true;
00681
00682 if (Pstream::master())
00683 {
00684 if (checkArgs && args_.size() - 1 != validArgs.size())
00685 {
00686 FatalError
00687 << "Wrong number of arguments, expected " << validArgs.size()
00688 << " found " << args_.size() - 1 << endl;
00689 ok = false;
00690 }
00691
00692 if (checkOpts)
00693 {
00694 forAllConstIter(HashTable<string>, options_, iter)
00695 {
00696 if
00697 (
00698 !validOptions.found(iter.key())
00699 && !validParOptions.found(iter.key())
00700 )
00701 {
00702 FatalError
00703 << "Invalid option: -" << iter.key() << endl;
00704 ok = false;
00705 }
00706 }
00707 }
00708
00709 if (!ok)
00710 {
00711 printUsage();
00712 }
00713 }
00714
00715 return ok;
00716 }
00717
00718
00719 bool Foam::argList::checkRootCase() const
00720 {
00721 if (!isDir(rootPath()))
00722 {
00723 FatalError
00724 << executable_
00725 << ": cannot open root directory " << rootPath()
00726 << endl;
00727
00728 return false;
00729 }
00730
00731 if (!isDir(path()) && Pstream::master())
00732 {
00733
00734 FatalError
00735 << executable_
00736 << ": cannot open case directory " << path()
00737 << endl;
00738
00739 return false;
00740 }
00741
00742 return true;
00743 }
00744
00745
00746