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
00028
00029
00030 #include "argList.H"
00031 #include "Time.H"
00032 #include "polyMesh.H"
00033 #include "globalMeshData.H"
00034 #include "IStringStream.H"
00035 #include "cellSet.H"
00036 #include "faceSet.H"
00037 #include "pointSet.H"
00038 #include "topoSetSource.H"
00039 #include "OFstream.H"
00040 #include "IFstream.H"
00041 #include "demandDrivenData.H"
00042 #include "writePatch.H"
00043 #include "writePointSet.H"
00044 #include "IOobjectList.H"
00045
00046 #include <stdio.h>
00047
00048
00049 #if READLINE != 0
00050 # include <readline/readline.h>
00051 # include <readline/history.h>
00052 #endif
00053
00054 using namespace Foam;
00055
00056
00057
00058
00059 #if READLINE != 0
00060 static const char* historyFile = ".setSet";
00061 #endif
00062
00063 Istream& selectStream(Istream* is0Ptr, Istream* is1Ptr)
00064 {
00065 if (is0Ptr)
00066 {
00067 return *is0Ptr;
00068 }
00069 else if (is1Ptr)
00070 {
00071 return *is1Ptr;
00072 }
00073 else
00074 {
00075 FatalErrorIn("selectStream(Istream*, Istream*)")
00076 << "No valid stream opened" << abort(FatalError);
00077
00078 return *is0Ptr;
00079 }
00080 }
00081
00082
00083 void backup
00084 (
00085 const polyMesh& mesh,
00086 const word& fromName,
00087 const topoSet& fromSet,
00088 const word& toName
00089 )
00090 {
00091 if (fromSet.size())
00092 {
00093 Pout<< " Backing up " << fromName << " into " << toName << endl;
00094
00095 topoSet backupSet(mesh, toName, fromSet);
00096
00097 backupSet.write();
00098 }
00099 }
00100
00101
00102
00103 void backup
00104 (
00105 const polyMesh& mesh,
00106 const word& fromName,
00107 const word& toName
00108 )
00109 {
00110 topoSet fromSet(mesh, fromName, IOobject::READ_IF_PRESENT);
00111
00112 backup(mesh, fromName, fromSet, toName);
00113 }
00114
00115
00116
00117 void writeVTK
00118 (
00119 const polyMesh& mesh,
00120 const topoSet& currentSet,
00121 const fileName& vtkName
00122 )
00123 {
00124 if (typeid(currentSet) == typeid(faceSet))
00125 {
00126
00127
00128 faceList setFaces(currentSet.size());
00129 labelList faceValues(currentSet.size());
00130 label setFaceI = 0;
00131
00132 forAllConstIter(topoSet, currentSet, iter)
00133 {
00134 setFaces[setFaceI] = mesh.faces()[iter.key()];
00135 faceValues[setFaceI] = iter.key();
00136 setFaceI++;
00137 }
00138
00139 primitiveFacePatch fp(setFaces, mesh.points());
00140
00141 writePatch
00142 (
00143 true,
00144 currentSet.name(),
00145 fp,
00146 "faceID",
00147 faceValues,
00148 mesh.time().path()/vtkName
00149 );
00150 }
00151 else if (typeid(currentSet) == typeid(cellSet))
00152 {
00153
00154
00155 Map<label> cellFaces(currentSet.size());
00156
00157 forAllConstIter(cellSet, currentSet, iter)
00158 {
00159 label cellI = iter.key();
00160
00161 const cell& cFaces = mesh.cells()[cellI];
00162
00163 forAll(cFaces, i)
00164 {
00165 label faceI = cFaces[i];
00166
00167 if (mesh.isInternalFace(faceI))
00168 {
00169 label otherCellI = mesh.faceOwner()[faceI];
00170
00171 if (otherCellI == cellI)
00172 {
00173 otherCellI = mesh.faceNeighbour()[faceI];
00174 }
00175
00176 if (!currentSet.found(otherCellI))
00177 {
00178 cellFaces.insert(faceI, cellI);
00179 }
00180 }
00181 else
00182 {
00183 cellFaces.insert(faceI, cellI);
00184 }
00185 }
00186 }
00187
00188 faceList setFaces(cellFaces.size());
00189 labelList faceValues(cellFaces.size());
00190 label setFaceI = 0;
00191
00192 forAllConstIter(Map<label>, cellFaces, iter)
00193 {
00194 setFaces[setFaceI] = mesh.faces()[iter.key()];
00195 faceValues[setFaceI] = iter();
00196 setFaceI++;
00197 }
00198
00199 primitiveFacePatch fp(setFaces, mesh.points());
00200
00201 writePatch
00202 (
00203 true,
00204 currentSet.name(),
00205 fp,
00206 "cellID",
00207 faceValues,
00208 mesh.time().path()/vtkName
00209 );
00210 }
00211 else if (typeid(currentSet) == typeid(pointSet))
00212 {
00213 writePointSet
00214 (
00215 true,
00216 mesh,
00217 currentSet,
00218 mesh.time().path()/vtkName
00219 );
00220 }
00221 else
00222 {
00223 WarningIn
00224 (
00225 "void writeVTK"
00226 "(const polyMesh& mesh, const topoSet& currentSet,"
00227 "const fileName& vtkName)"
00228 ) << "Don't know how to handle set of type " << currentSet.type()
00229 << endl;
00230 }
00231 }
00232
00233
00234 void printHelp(Ostream& os)
00235 {
00236 os << "Please type 'help', 'list', 'quit', 'time ddd'"
00237 << " or a set command after prompt." << endl
00238 << "'list' will show all current cell/face/point sets." << endl
00239 << "'time ddd' will change the current time." << endl
00240 << endl
00241 << "A set command should be of the following form" << endl
00242 << endl
00243 << " cellSet|faceSet|pointSet <setName> <action> <source>"
00244 << endl << endl
00245 << "The <action> is one of" << endl
00246 << " list - prints the contents of the set" << endl
00247 << " clear - clears the set" << endl
00248 << " invert - inverts the set" << endl
00249 << " new <source> - sets to set to the source set" << endl
00250 << " add <source> - adds all elements from the source set" << endl
00251 << " delete <source> - deletes ,," << endl
00252 << " subset <source> - combines current set with the source set"
00253 << endl
00254 << endl
00255 << "The sources come in various forms. Type a wrong source"
00256 << " to see all the types available." << endl
00257 << endl
00258 << "Example: pick up all cells connected by point or face to patch"
00259 << " movingWall" << endl
00260 << endl
00261 << "Pick up all faces of patch:" << endl
00262 << " faceSet f0 new patchToFace movingWall" << endl
00263 << "Add faces 0,1,2:" << endl
00264 << " faceSet f0 add labelToFace (0 1 2)" << endl
00265 << "Pick up all points used by faces in faceSet f0:" << endl
00266 << " pointSet p0 new faceToPoint f0 all" << endl
00267 << "Pick up cell which has any face in f0:" << endl
00268 << " cellSet c0 new faceToCell f0 any" << endl
00269 << "Add cells which have any point in p0:" << endl
00270 << " cellSet c0 add pointToCell p0 any" << endl
00271 << "List set:" << endl
00272 << " cellSet c0 list" << endl
00273 << endl;
00274 }
00275
00276
00277 void printAllSets(const polyMesh& mesh, Ostream& os)
00278 {
00279 IOobjectList objects
00280 (
00281 mesh,
00282 mesh.pointsInstance(),
00283 polyMesh::meshSubDir/"sets"
00284 );
00285 IOobjectList cellSets(objects.lookupClass(cellSet::typeName));
00286 if (cellSets.size())
00287 {
00288 os << "cellSets:" << endl;
00289 forAllConstIter(IOobjectList, cellSets, iter)
00290 {
00291 cellSet set(*iter());
00292 os << '\t' << set.name() << "\tsize:" << set.size() << endl;
00293 }
00294 }
00295 IOobjectList faceSets(objects.lookupClass(faceSet::typeName));
00296 if (faceSets.size())
00297 {
00298 os << "faceSets:" << endl;
00299 forAllConstIter(IOobjectList, faceSets, iter)
00300 {
00301 faceSet set(*iter());
00302 os << '\t' << set.name() << "\tsize:" << set.size() << endl;
00303 }
00304 }
00305 IOobjectList pointSets(objects.lookupClass(pointSet::typeName));
00306 if (pointSets.size())
00307 {
00308 os << "pointSets:" << endl;
00309 forAllConstIter(IOobjectList, pointSets, iter)
00310 {
00311 pointSet set(*iter());
00312 os << '\t' << set.name() << "\tsize:" << set.size() << endl;
00313 }
00314 }
00315 os << endl;
00316 }
00317
00318
00319
00320 bool doCommand
00321 (
00322 const polyMesh& mesh,
00323 const word& setType,
00324 const word& setName,
00325 const word& actionName,
00326 const bool writeVTKFile,
00327 Istream& is
00328 )
00329 {
00330
00331 const globalMeshData& parData = mesh.globalData();
00332
00333 label typSize =
00334 max
00335 (
00336 parData.nTotalCells(),
00337 max
00338 (
00339 parData.nTotalFaces(),
00340 parData.nTotalPoints()
00341 )
00342 )
00343 / (10*Pstream::nProcs());
00344
00345
00346 bool ok = true;
00347
00348
00349 autoPtr<topoSet> currentSetPtr;
00350
00351 word sourceType;
00352
00353 try
00354 {
00355 topoSetSource::setAction action =
00356 topoSetSource::toAction(actionName);
00357
00358
00359 IOobject::readOption r;
00360
00361 if
00362 (
00363 (action == topoSetSource::NEW)
00364 || (action == topoSetSource::CLEAR)
00365 )
00366 {
00367 r = IOobject::NO_READ;
00368
00369
00370
00371 currentSetPtr = topoSet::New(setType, mesh, setName, typSize);
00372 }
00373 else
00374 {
00375 r = IOobject::MUST_READ;
00376
00377 currentSetPtr = topoSet::New(setType, mesh, setName, r);
00378
00379 topoSet& currentSet = currentSetPtr();
00380
00381
00382 currentSet.resize(max(currentSet.size(), typSize));
00383 }
00384
00385 if (currentSetPtr.empty())
00386 {
00387 Pout<< " Cannot construct/load set "
00388 << topoSet::localPath(mesh, setName) << endl;
00389
00390 ok = false;
00391 }
00392 else
00393 {
00394 topoSet& currentSet = currentSetPtr();
00395
00396 Pout<< " Set:" << currentSet.name()
00397 << " Size:" << currentSet.size()
00398 << " Action:" << actionName
00399 << endl;
00400
00401
00402
00403
00404
00405
00406
00407 switch (action)
00408 {
00409 case topoSetSource::CLEAR:
00410 {
00411
00412 break;
00413 }
00414 case topoSetSource::INVERT:
00415 {
00416 currentSet.invert(currentSet.maxSize(mesh));
00417 break;
00418 }
00419 case topoSetSource::LIST:
00420 {
00421 currentSet.writeDebug(Pout, mesh, 100);
00422 Pout<< endl;
00423 break;
00424 }
00425 case topoSetSource::SUBSET:
00426 {
00427 if (is >> sourceType)
00428 {
00429 autoPtr<topoSetSource> setSource
00430 (
00431 topoSetSource::New
00432 (
00433 sourceType,
00434 mesh,
00435 is
00436 )
00437 );
00438
00439
00440 topoSet oldSet
00441 (
00442 mesh,
00443 currentSet.name() + "_old2",
00444 currentSet
00445 );
00446
00447 currentSet.clear();
00448 currentSet.resize(oldSet.size());
00449 setSource().applyToSet(topoSetSource::NEW, currentSet);
00450
00451
00452 currentSet.subset(oldSet);
00453 }
00454 break;
00455 }
00456 default:
00457 {
00458 if (is >> sourceType)
00459 {
00460 autoPtr<topoSetSource> setSource
00461 (
00462 topoSetSource::New
00463 (
00464 sourceType,
00465 mesh,
00466 is
00467 )
00468 );
00469
00470 setSource().applyToSet(action, currentSet);
00471 }
00472 }
00473 }
00474
00475
00476 if (action != topoSetSource::LIST)
00477 {
00478
00479
00480
00481 currentSet.sync(mesh);
00482
00483
00484 if (writeVTKFile)
00485 {
00486 mkDir(mesh.time().path()/"VTK"/currentSet.name());
00487
00488 fileName vtkName
00489 (
00490 "VTK"/currentSet.name()/currentSet.name()
00491 + "_"
00492 + name(mesh.time().timeIndex())
00493 + ".vtk"
00494 );
00495
00496 Pout<< " Writing " << currentSet.name()
00497 << " (size " << currentSet.size() << ") to "
00498 << currentSet.instance()/currentSet.local()
00499 /currentSet.name()
00500 << " and to vtk file " << vtkName << endl << endl;
00501
00502 currentSet.write();
00503
00504 writeVTK(mesh, currentSet, vtkName);
00505 }
00506 else
00507 {
00508 Pout<< " Writing " << currentSet.name()
00509 << " (size " << currentSet.size() << ") to "
00510 << currentSet.instance()/currentSet.local()
00511 /currentSet.name() << endl << endl;
00512
00513 currentSet.write();
00514 }
00515 }
00516 }
00517 }
00518 catch (Foam::IOerror& fIOErr)
00519 {
00520 ok = false;
00521
00522 Pout<< fIOErr.message().c_str() << endl;
00523
00524 if (sourceType.size())
00525 {
00526 Pout<< topoSetSource::usage(sourceType).c_str();
00527 }
00528 }
00529 catch (Foam::error& fErr)
00530 {
00531 ok = false;
00532
00533 Pout<< fErr.message().c_str() << endl;
00534
00535 if (sourceType.size())
00536 {
00537 Pout<< topoSetSource::usage(sourceType).c_str();
00538 }
00539 }
00540
00541 return ok;
00542 }
00543
00544
00545
00546 enum commandStatus
00547 {
00548 QUIT,
00549 INVALID,
00550 VALID
00551 };
00552
00553
00554 void printMesh(const Time& runTime, const polyMesh& mesh)
00555 {
00556 Pout<< "Time:" << runTime.timeName()
00557 << " cells:" << mesh.nCells()
00558 << " faces:" << mesh.nFaces()
00559 << " points:" << mesh.nPoints()
00560 << " patches:" << mesh.boundaryMesh().size()
00561 << " bb:" << mesh.bounds() << nl;
00562 }
00563
00564
00565
00566 commandStatus parseType
00567 (
00568 Time& runTime,
00569 polyMesh& mesh,
00570 const word& setType,
00571 IStringStream& is
00572 )
00573 {
00574 if (setType.empty())
00575 {
00576 Pout<< "Type 'help' for usage information" << endl;
00577
00578 return INVALID;
00579 }
00580 else if (setType == "help")
00581 {
00582 printHelp(Pout);
00583
00584 return INVALID;
00585 }
00586 else if (setType == "list")
00587 {
00588 printAllSets(mesh, Pout);
00589
00590 return INVALID;
00591 }
00592 else if (setType == "time")
00593 {
00594 scalar requestedTime = readScalar(is);
00595 instantList Times = runTime.times();
00596
00597 label nearestIndex = Time::findClosestTimeIndex(Times, requestedTime);
00598
00599 Pout<< "Changing time from " << runTime.timeName()
00600 << " to " << Times[nearestIndex].name()
00601 << endl;
00602
00603 runTime.setTime(Times[nearestIndex], nearestIndex);
00604 polyMesh::readUpdateState stat = mesh.readUpdate();
00605
00606 switch(stat)
00607 {
00608 case polyMesh::UNCHANGED:
00609 {
00610 Pout<< " mesh not changed." << endl;
00611 break;
00612 }
00613 case polyMesh::POINTS_MOVED:
00614 {
00615 Pout<< " points moved; topology unchanged." << endl;
00616 break;
00617 }
00618 case polyMesh::TOPO_CHANGE:
00619 {
00620 Pout<< " topology changed; patches unchanged." << nl
00621 << " ";
00622 printMesh(runTime, mesh);
00623 break;
00624 }
00625 case polyMesh::TOPO_PATCH_CHANGE:
00626 {
00627 Pout<< " topology changed and patches changed." << nl
00628 << " ";
00629 printMesh(runTime, mesh);
00630
00631 break;
00632 }
00633 default:
00634 {
00635 FatalErrorIn("parseType")
00636 << "Illegal mesh update state "
00637 << stat << abort(FatalError);
00638 break;
00639 }
00640 }
00641
00642 return INVALID;
00643 }
00644 else if (setType == "quit")
00645 {
00646 Pout<< "Quitting ..." << endl;
00647
00648 return QUIT;
00649 }
00650 else if
00651 (
00652 setType == "cellSet"
00653 || setType == "faceSet"
00654 || setType == "pointSet"
00655 )
00656 {
00657 return VALID;
00658 }
00659 else
00660 {
00661 SeriousErrorIn
00662 (
00663 "commandStatus parseType(Time&, polyMesh&, const word&"
00664 ", IStringStream&)"
00665 ) << "Illegal command " << setType << endl
00666 << "Should be one of 'help', 'list', 'time' or a set type :"
00667 << " 'cellSet', 'faceSet', 'pointSet'"
00668 << endl;
00669
00670 return INVALID;
00671 }
00672 }
00673
00674
00675 commandStatus parseAction(const word& actionName)
00676 {
00677 commandStatus stat = INVALID;
00678
00679 if (actionName.size())
00680 {
00681 try
00682 {
00683 (void)topoSetSource::toAction(actionName);
00684
00685 stat = VALID;
00686 }
00687 catch (Foam::IOerror& fIOErr)
00688 {
00689 stat = INVALID;
00690 }
00691 catch (Foam::error& fErr)
00692 {
00693 stat = INVALID;
00694 }
00695 }
00696 return stat;
00697 }
00698
00699
00700
00701
00702 int main(int argc, char *argv[])
00703 {
00704 # include "addRegionOption.H"
00705 # include "addTimeOptions.H"
00706
00707 argList::validOptions.insert("noVTK", "");
00708 argList::validOptions.insert("batch", "file");
00709
00710 # include "setRootCase.H"
00711 # include "createTime.H"
00712
00713 bool writeVTK = !args.optionFound("noVTK");
00714
00715
00716 instantList Times = runTime.times();
00717
00718 # include "checkTimeOptions.H"
00719
00720 runTime.setTime(Times[startTime], startTime);
00721
00722 # include "createNamedPolyMesh.H"
00723
00724
00725 printMesh(runTime, mesh);
00726
00727
00728 std::ifstream* fileStreamPtr(NULL);
00729
00730 if (args.optionFound("batch"))
00731 {
00732 fileName batchFile(args.option("batch"));
00733
00734 Pout<< "Reading commands from file " << batchFile << endl;
00735
00736
00737 if (!isFile(batchFile, false))
00738 {
00739 FatalErrorIn(args.executable())
00740 << "Cannot open file " << batchFile << exit(FatalError);
00741 }
00742
00743 fileStreamPtr = new std::ifstream(batchFile.c_str());
00744 }
00745 #if READLINE != 0
00746 else if (!read_history(historyFile))
00747 {
00748 Info<< "Successfully read history from " << historyFile << endl;
00749 }
00750 #endif
00751
00752
00753 Pout<< "Please type 'help', 'quit' or a set command after prompt." << endl;
00754
00755 bool ok = true;
00756
00757 FatalError.throwExceptions();
00758 FatalIOError.throwExceptions();
00759
00760 do
00761 {
00762 string rawLine;
00763
00764
00765 word setType;
00766
00767 word setName;
00768
00769 word actionName;
00770
00771 commandStatus stat = INVALID;
00772
00773 if (fileStreamPtr)
00774 {
00775 if (!fileStreamPtr->good())
00776 {
00777 Pout<< "End of batch file" << endl;
00778 break;
00779 }
00780
00781 std::getline(*fileStreamPtr, rawLine);
00782
00783 if (rawLine.size())
00784 {
00785 Pout<< "Doing:" << rawLine << endl;
00786 }
00787 }
00788 else
00789 {
00790 # if READLINE != 0
00791 {
00792 char* linePtr = readline("readline>");
00793
00794 rawLine = string(linePtr);
00795
00796 if (*linePtr)
00797 {
00798 add_history(linePtr);
00799 write_history(historyFile);
00800 }
00801
00802 free(linePtr);
00803 }
00804 # else
00805 {
00806 Pout<< "Command>" << flush;
00807 std::getline(std::cin, rawLine);
00808 }
00809 # endif
00810 }
00811
00812 if (rawLine.empty() || rawLine[0] == '#')
00813 {
00814 continue;
00815 }
00816
00817 IStringStream is(rawLine + ' ');
00818
00819
00820 is >> setType;
00821
00822 stat = parseType(runTime, mesh, setType, is);
00823
00824 if (stat == VALID)
00825 {
00826 if (is >> setName)
00827 {
00828 if (is >> actionName)
00829 {
00830 stat = parseAction(actionName);
00831 }
00832 }
00833 }
00834
00835 ok = true;
00836
00837 if (stat == QUIT)
00838 {
00839 break;
00840 }
00841 else if (stat == VALID)
00842 {
00843 ok = doCommand(mesh, setType, setName, actionName, writeVTK, is);
00844 }
00845
00846 } while (ok);
00847
00848
00849 if (fileStreamPtr)
00850 {
00851 delete fileStreamPtr;
00852 }
00853
00854 Pout<< "\nEnd" << endl;
00855
00856 return 0;
00857 }
00858
00859
00860