From a13f97d59dae349690f7751bc9f84d6568cb0a35 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 2 Dec 2008 15:55:51 +0100 Subject: [PATCH] timeSelector - handle cases with missing constant/ or 0/ directories --- applications/test/findTimes/findTimes.C | 10 +++- src/OpenFOAM/db/Time/timeSelector.C | 65 ++++++++++++++++--------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/applications/test/findTimes/findTimes.C b/applications/test/findTimes/findTimes.C index db22507b4f..2b59609006 100644 --- a/applications/test/findTimes/findTimes.C +++ b/applications/test/findTimes/findTimes.C @@ -28,6 +28,7 @@ Description #include "argList.H" #include "Time.H" +#include "timeSelector.H" using namespace Foam; @@ -36,13 +37,18 @@ using namespace Foam; int main(int argc, char *argv[]) { + argList::noParallel(); + Foam::timeSelector::addOptions(); # include "setRootCase.H" # include "createTime.H" - Info<< runTime.times() << endl; + Info<< "Times found:" << runTime.times() << endl; - Info << "End\n" << endl; + instantList timeDirs = Foam::timeSelector::select0(runTime, args); + + Info<< "Times selected:" << timeDirs << endl; + Info<< "\nEnd\n" << endl; return 0; } diff --git a/src/OpenFOAM/db/Time/timeSelector.C b/src/OpenFOAM/db/Time/timeSelector.C index cfb2354d79..002a0b5beb 100644 --- a/src/OpenFOAM/db/Time/timeSelector.C +++ b/src/OpenFOAM/db/Time/timeSelector.C @@ -56,22 +56,12 @@ Foam::List Foam::timeSelector::selected(const List& Times) const { List lst(Times.size(), false); - // check ranges - forAll(Times, i) + // check ranges, avoid false positive on constant/ + forAll(Times, timeI) { - if (selected(Times[i])) + if (Times[timeI].name() != "constant" && selected(Times[timeI])) { - lst[i] = true; - } - } - - // avoid false positive on "constant" - forAll(Times, i) - { - if (Times[i].name() == "constant") - { - lst[i] = false; - break; + lst[timeI] = true; } } @@ -85,15 +75,15 @@ Foam::List Foam::timeSelector::selected(const List& Times) const int nearestIndex = -1; scalar nearestDiff = Foam::GREAT; - forAll(Times, timeIndex) + forAll(Times, timeI) { - if (Times[timeIndex].name() == "constant") continue; + if (Times[timeI].name() == "constant") continue; - scalar diff = fabs(Times[timeIndex].value() - target); + scalar diff = fabs(Times[timeI].value() - target); if (diff < nearestDiff) { nearestDiff = diff; - nearestIndex = timeIndex; + nearestIndex = timeI; } } @@ -156,6 +146,27 @@ Foam::List Foam::timeSelector::select { List selectTimes(timeDirs.size(), true); + // determine locations of constant/ and 0/ directories + label constantIdx = -1; + label zeroIdx = -1; + + forAll(timeDirs, timeI) + { + if (timeDirs[timeI].name() == "constant") + { + constantIdx = timeI; + } + else if (timeDirs[timeI].value() == 0) + { + zeroIdx = timeI; + } + + if (constantIdx >= 0 && zeroIdx >= 0) + { + break; + } + } + if (args.options().found("time")) { selectTimes = timeSelector @@ -166,25 +177,31 @@ Foam::List Foam::timeSelector::select else if (args.options().found("latestTime")) { selectTimes = false; + const label latestIdx = timeDirs.size() - 1; // avoid false match on constant/ or 0/ - if (timeDirs.size() > 2) + if (latestIdx != constantIdx && latestIdx != zeroIdx) { - selectTimes[timeDirs.size() - 1] = true; + selectTimes[latestIdx] = true; } } - if (timeDirs.size() > 1) + // special treatment for constant/ + if (constantIdx >= 0) { - selectTimes[0] = args.options().found("constant"); + selectTimes[constantIdx] = args.options().found("constant"); + } + // special treatment for 0/ + if (zeroIdx >= 0) + { if (args.options().found("noZero")) { - selectTimes[1] = false; + selectTimes[zeroIdx] = false; } else if (argList::validOptions.found("zeroTime")) { - selectTimes[1] = args.options().found("zeroTime"); + selectTimes[zeroIdx] = args.options().found("zeroTime"); } }