From 20ac00f67c0c4aa896da2e959bd5b1b935f05e95 Mon Sep 17 00:00:00 2001 From: mattijs Date: Fri, 29 Jan 2010 11:43:28 +0000 Subject: [PATCH] ENH: have -loop option on setSet to enable -batch over a set of directories. Only allowed in batch mode. Causes any set to be written to time directory instead of facesInstance. --- .../mesh/manipulation/setSet/setSet.C | 380 +++++++++--------- 1 file changed, 197 insertions(+), 183 deletions(-) diff --git a/applications/utilities/mesh/manipulation/setSet/setSet.C b/applications/utilities/mesh/manipulation/setSet/setSet.C index c959b927ba..bb187dc361 100644 --- a/applications/utilities/mesh/manipulation/setSet/setSet.C +++ b/applications/utilities/mesh/manipulation/setSet/setSet.C @@ -45,6 +45,7 @@ Description #include "cellZoneSet.H" #include "faceZoneSet.H" #include "pointZoneSet.H" +#include "timeSelector.H" #include @@ -82,45 +83,6 @@ Istream& selectStream(Istream* is0Ptr, Istream* is1Ptr) } } -// Copy set -void backup -( - const word& setType, - const polyMesh& mesh, - const word& fromName, - const topoSet& fromSet, - const word& toName -) -{ - if (fromSet.size()) - { - Info<< " Backing up " << fromName << " into " << toName << endl; - - topoSet::New(setType, mesh, toName, fromSet)().write(); - } -} - - -// Read and copy set -void backup -( - const word& setType, - const polyMesh& mesh, - const word& fromName, - const word& toName -) -{ - autoPtr fromSet = topoSet::New - ( - setType, - mesh, - fromName, - IOobject::READ_IF_PRESENT - ); - - backup(setType, mesh, fromName, fromSet(), toName); -} - // Write set to VTK readable files void writeVTK @@ -477,6 +439,7 @@ bool doCommand const word& setName, const word& actionName, const bool writeVTKFile, + const bool writeCurrentTime, Istream& is ) { @@ -640,8 +603,6 @@ bool doCommand /currentSet.name() << " and to vtk file " << vtkName << endl << endl; - currentSet.write(); - writeVTK(mesh, currentSet, vtkName); } else @@ -650,9 +611,13 @@ bool doCommand << " (size " << currentSet.size() << ") to " << currentSet.instance()/currentSet.local() /currentSet.name() << endl << endl; - - currentSet.write(); } + + if (writeCurrentTime) + { + currentSet.instance() = mesh.time().timeName(); + } + currentSet.write(); } } } @@ -704,6 +669,48 @@ void printMesh(const Time& runTime, const polyMesh& mesh) } +polyMesh::readUpdateState meshReadUpdate(polyMesh& mesh) +{ + polyMesh::readUpdateState stat = mesh.readUpdate(); + + switch(stat) + { + case polyMesh::UNCHANGED: + { + Info<< " mesh not changed." << endl; + break; + } + case polyMesh::POINTS_MOVED: + { + Info<< " points moved; topology unchanged." << endl; + break; + } + case polyMesh::TOPO_CHANGE: + { + Info<< " topology changed; patches unchanged." << nl + << " "; + printMesh(mesh.time(), mesh); + break; + } + case polyMesh::TOPO_PATCH_CHANGE: + { + Info<< " topology changed and patches changed." << nl + << " "; + printMesh(mesh.time(), mesh); + + break; + } + default: + { + FatalErrorIn("meshReadUpdate(polyMesh&)") + << "Illegal mesh update state " + << stat << abort(FatalError); + break; + } + } + return stat; +} + commandStatus parseType ( @@ -742,44 +749,10 @@ commandStatus parseType << " to " << Times[nearestIndex].name() << endl; + // Set time runTime.setTime(Times[nearestIndex], nearestIndex); - polyMesh::readUpdateState stat = mesh.readUpdate(); - - switch(stat) - { - case polyMesh::UNCHANGED: - { - Info<< " mesh not changed." << endl; - break; - } - case polyMesh::POINTS_MOVED: - { - Info<< " points moved; topology unchanged." << endl; - break; - } - case polyMesh::TOPO_CHANGE: - { - Info<< " topology changed; patches unchanged." << nl - << " "; - printMesh(runTime, mesh); - break; - } - case polyMesh::TOPO_PATCH_CHANGE: - { - Info<< " topology changed and patches changed." << nl - << " "; - printMesh(runTime, mesh); - - break; - } - default: - { - FatalErrorIn("parseType") - << "Illegal mesh update state " - << stat << abort(FatalError); - break; - } - } + // Optionally re-read mesh + meshReadUpdate(mesh); return INVALID; } @@ -852,23 +825,28 @@ commandStatus parseAction(const word& actionName) int main(int argc, char *argv[]) { + timeSelector::addOptions(true, false); # include "addRegionOption.H" -# include "addTimeOptions.H" - - argList::addBoolOption("noVTK"); + argList::addBoolOption("noVTK", "do not write VTK files"); + argList::addBoolOption("loop", "execute batch commands for all timesteps"); argList::addOption("batch", "file"); # include "setRootCase.H" # include "createTime.H" + instantList timeDirs = timeSelector::select0(runTime, args); bool writeVTK = !args.optionFound("noVTK"); + bool loop = args.optionFound("loop"); + bool batch = args.optionFound("batch"); - // Get times list - instantList Times = runTime.times(); -# include "checkTimeOptions.H" + if (loop && !batch) + { + FatalErrorIn(args.executable()) + << "Can only loop when in batch mode." + << exit(FatalError); + } - runTime.setTime(Times[startTime], startTime); # include "createNamedPolyMesh.H" @@ -878,135 +856,171 @@ int main(int argc, char *argv[]) // Print current sets printAllSets(mesh, Info); - - - std::ifstream* fileStreamPtr(NULL); - - if (args.optionFound("batch")) - { - fileName batchFile(args.option("batch")); - - Info<< "Reading commands from file " << batchFile << endl; - - // we cannot handle .gz files - if (!isFile(batchFile, false)) - { - FatalErrorIn(args.executable()) - << "Cannot open file " << batchFile << exit(FatalError); - } - - fileStreamPtr = new std::ifstream(batchFile.c_str()); - } -#if READLINE != 0 - else if (!read_history(historyFile)) + // Read history if interactive +# if READLINE != 0 + if (!batch && !read_history(historyFile)) { Info<< "Successfully read history from " << historyFile << endl; } -#endif +# endif - Info<< "Please type 'help', 'quit' or a set command after prompt." << endl; - bool ok = true; + // Exit status + int status = 0; - FatalError.throwExceptions(); - FatalIOError.throwExceptions(); - do + forAll(timeDirs, timeI) { - string rawLine; + runTime.setTime(timeDirs[timeI], timeI); + Info<< "Time = " << runTime.timeName() << endl; - // Type: cellSet, faceSet, pointSet - word setType; - // Name of destination set. - word setName; - // Action (new, invert etc.) - word actionName; + // Handle geometry/topology changes + meshReadUpdate(mesh); - commandStatus stat = INVALID; - if (fileStreamPtr) + // Main command read & execute loop + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + autoPtr fileStreamPtr(NULL); + + if (batch) { - if (!fileStreamPtr->good()) + fileName batchFile(args.option("batch")); + + Info<< "Reading commands from file " << batchFile << endl; + + // we cannot handle .gz files + if (!isFile(batchFile, false)) { - Info<< "End of batch file" << endl; - break; + FatalErrorIn(args.executable()) + << "Cannot open file " << batchFile << exit(FatalError); } - std::getline(*fileStreamPtr, rawLine); - - if (rawLine.size()) - { - Info<< "Doing:" << rawLine << endl; - } + fileStreamPtr.reset(new IFstream(batchFile)); } - else + + Info<< "Please type 'help', 'quit' or a set command after prompt." + << endl; + + // Whether to quit + bool quit = false; + + FatalError.throwExceptions(); + FatalIOError.throwExceptions(); + + do { -# if READLINE != 0 + string rawLine; + + // Type: cellSet, faceSet, pointSet + word setType; + // Name of destination set. + word setName; + // Action (new, invert etc.) + word actionName; + + commandStatus stat = INVALID; + + if (fileStreamPtr.valid()) { - char* linePtr = readline("readline>"); - - rawLine = string(linePtr); - - if (*linePtr) + if (!fileStreamPtr().good()) { - add_history(linePtr); - write_history(historyFile); + Info<< "End of batch file" << endl; + // No error. + break; } - free(linePtr); // readline uses malloc, not new. - } -# else - { - Info<< "Command>" << flush; - std::getline(std::cin, rawLine); - } -# endif - } + fileStreamPtr().getLine(rawLine); - if (rawLine.empty() || rawLine[0] == '#') - { - continue; - } - - IStringStream is(rawLine + ' '); - - // Type: cellSet, faceSet, pointSet, faceZoneSet - is >> setType; - - stat = parseType(runTime, mesh, setType, is); - - if (stat == VALIDSETCMD || stat == VALIDZONECMD) - { - if (is >> setName) - { - if (is >> actionName) + if (rawLine.size()) { - stat = parseAction(actionName); + Info<< "Doing:" << rawLine << endl; } } - } - ok = true; + else + { +# if READLINE != 0 + { + char* linePtr = readline("readline>"); - if (stat == QUIT) + rawLine = string(linePtr); + + if (*linePtr) + { + add_history(linePtr); + write_history(historyFile); + } + + free(linePtr); // readline uses malloc, not new. + } +# else + { + Info<< "Command>" << flush; + std::getline(std::cin, rawLine); + } +# endif + } + + if (rawLine.empty() || rawLine[0] == '#') + { + continue; + } + + IStringStream is(rawLine + ' '); + + // Type: cellSet, faceSet, pointSet, faceZoneSet + is >> setType; + + stat = parseType(runTime, mesh, setType, is); + + if (stat == VALIDSETCMD || stat == VALIDZONECMD) + { + if (is >> setName) + { + if (is >> actionName) + { + stat = parseAction(actionName); + } + } + } + + if (stat == QUIT) + { + // Make sure to quit + quit = true; + } + else if (stat == VALIDSETCMD || stat == VALIDZONECMD) + { + bool ok = doCommand + ( + mesh, + setType, + setName, + actionName, + writeVTK, + loop, // if in looping mode dump sets to time directory + is + ); + + if (!ok) + { + // Exit with error. + quit = true; + status = 1; + } + } + + } while (!quit); + + if (quit) { break; } - else if (stat == VALIDSETCMD || stat == VALIDZONECMD) - { - ok = doCommand(mesh, setType, setName, actionName, writeVTK, is); - } - - } while (ok); - - - if (fileStreamPtr) - { - delete fileStreamPtr; } Info<< "\nEnd\n" << endl; - return 0; + return status; }