diff --git a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C index a24eb02b76e487ab61c0df28522c7829ea93b14b..7fab0ec3f9ae2a75e229ec4311f685111e571a87 100644 --- a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C +++ b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C @@ -183,24 +183,10 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime) Time& runTime = dbPtr_(); // Get times list - instantList times = runTime.times(); + instantList Times = runTime.times(); - // logic as per "checkTimeOption.H" bool found = false; - int nearestIndex = -1; - scalar nearestDiff = Foam::GREAT; - - forAll (times, timeIndex) - { - if (times[timeIndex].name() == "constant") continue; - - scalar diff = fabs(times[timeIndex].value() - requestedTime); - if (diff < nearestDiff) - { - nearestDiff = diff; - nearestIndex = timeIndex; - } - } + int nearestIndex = Time::findClosestTimeIndex(Times, requestedTime); if (nearestIndex == -1) { @@ -212,12 +198,12 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime) found = true; } - runTime.setTime(times[nearestIndex], nearestIndex); + runTime.setTime(Times[nearestIndex], nearestIndex); if (debug) { Info<< "<end> Foam::vtkPV3Foam::setTime() - selected time " - << times[nearestIndex].name() << endl; + << Times[nearestIndex].name() << endl; } return found; diff --git a/src/OpenFOAM/db/Time/Time.C b/src/OpenFOAM/db/Time/Time.C index dd1e944067fb78f36fce921dad44d4eca9730c76..ef4fb9c21fa23b78a973f2bfe90028534bf36a53 100644 --- a/src/OpenFOAM/db/Time/Time.C +++ b/src/OpenFOAM/db/Time/Time.C @@ -418,19 +418,55 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const return times[times.size() - 1]; } + label nearestIndex = -1; scalar deltaT = GREAT; - label closesti = 0; for (label i=1; i<times.size(); i++) { - if (mag(times[i].value() - t) < deltaT) + scalar diff = mag(times[i].value() - t); + if (diff < deltaT) { - deltaT = mag(times[i].value() - t); - closesti = i; + deltaT = diff; + nearestIndex = i; } } - return times[closesti]; + return times[nearestIndex]; +} + +// +// This should work too, +// if we don't worry about checking "constant" explicitly +// +// Foam::instant Foam::Time::findClosestTime(const scalar t) const +// { +// instantList times = Time::findTimes(path()); +// label timeIndex = min(findClosestTimeIndex(times, t), 0); +// return times[timeIndex]; +// } + +Foam::label Foam::Time::findClosestTimeIndex +( + const instantList& times, + const scalar t +) +{ + label nearestIndex = -1; + scalar deltaT = GREAT; + + forAll (times, i) + { + if (times[i].name() == "constant") continue; + + scalar diff = fabs(times[i].value() - t); + if (diff < deltaT) + { + deltaT = diff; + nearestIndex = i; + } + } + + return nearestIndex; } diff --git a/src/OpenFOAM/db/Time/Time.H b/src/OpenFOAM/db/Time/Time.H index 89e1480c23f1502e549b82e1b220abb6c2e258ae..50239c0c08c1307001cb5a95a8673119a3fc5bfa 100644 --- a/src/OpenFOAM/db/Time/Time.H +++ b/src/OpenFOAM/db/Time/Time.H @@ -300,6 +300,9 @@ public: //- Search the case for the time closest to the given time instant findClosestTime(const scalar) const; + //- Search instantList for the time index closest to the given time + static label findClosestTimeIndex(const instantList&, const scalar); + //- Write using given format, version and compression virtual bool writeObject ( diff --git a/src/OpenFOAM/db/Time/findInstance.C b/src/OpenFOAM/db/Time/findInstance.C index ca15159bd5f7c2aabd1e57f19b07d1dfd9184e3a..4d2202d9cbe96f0b47a879d732e6f4372350c9b3 100644 --- a/src/OpenFOAM/db/Time/findInstance.C +++ b/src/OpenFOAM/db/Time/findInstance.C @@ -33,10 +33,7 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -namespace Foam -{ - -word Time::findInstance +Foam::word Foam::Time::findInstance ( const fileName& dir, const word& name, @@ -110,7 +107,7 @@ word Time::findInstance // constant function of the time, because the latter points to // the case constant directory in parallel cases - if + if ( file(path()/constant()/dir/name) && IOobject(name, constant(), dir, *this).headerOk() @@ -139,9 +136,4 @@ word Time::findInstance return constant(); } - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/db/Time/findTimes.C b/src/OpenFOAM/db/Time/findTimes.C index 175c340ade8418680b3f256dd05bb62e33e7439b..5e83e115fdbfc8ca2626d51550acb4ab79aede97 100644 --- a/src/OpenFOAM/db/Time/findTimes.C +++ b/src/OpenFOAM/db/Time/findTimes.C @@ -35,12 +35,7 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -namespace Foam -{ - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -instantList Time::findTimes(const fileName& directory) +Foam::instantList Foam::Time::findTimes(const fileName& directory) { if (debug) { @@ -101,9 +96,4 @@ instantList Time::findTimes(const fileName& directory) return Times; } - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -} // End namespace Foam - // ************************************************************************* // diff --git a/src/OpenFOAM/include/checkTimeOption.H b/src/OpenFOAM/include/checkTimeOption.H index d0256f13e2bd1812177b66274a288a519697afad..70759dd81ba12b91617ed714a2ef9be8040b236a 100644 --- a/src/OpenFOAM/include/checkTimeOption.H +++ b/src/OpenFOAM/include/checkTimeOption.H @@ -1,22 +1,7 @@ if (args.options().found("time")) { - scalar time(readScalar(IStringStream(args.options()["time"])())); + scalar timeValue(readScalar(IStringStream(args.options()["time"])())); - int nearestIndex = -1; - scalar nearestDiff = Foam::GREAT; - - forAll(Times, timeIndex) - { - if (Times[timeIndex].name() == "constant") continue; - - scalar diff = fabs(Times[timeIndex].value() - time); - if (diff < nearestDiff) - { - nearestDiff = diff; - nearestIndex = timeIndex; - } - } - - startTime = nearestIndex; - endTime = nearestIndex + 1; + startTime = Time::findClosestTimeIndex(Times, timeValue); + endTime = startTime + 1; }