diff --git a/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C b/applications/utilities/postProcessing/graphics/PV3FoamReader/vtkPV3Foam/vtkPV3Foam.C index a24eb02b76..7fab0ec3f9 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<< " 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 dd1e944067..ef4fb9c21f 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