Skip to content
Snippets Groups Projects
Commit 3cdc8ca0 authored by Mark Olesen's avatar Mark Olesen
Browse files

static label Time::findClosestTimeIndex(const instantList&, const scalar)

  - avoid code duplication in vtkPV3Foam.C and checkTimeOption.H
  - can also be used in Time::findClosestTime(), but didn't touch that
parent 8a2596a0
No related branches found
No related tags found
No related merge requests found
...@@ -183,24 +183,10 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime) ...@@ -183,24 +183,10 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime)
Time& runTime = dbPtr_(); Time& runTime = dbPtr_();
// Get times list // Get times list
instantList times = runTime.times(); instantList Times = runTime.times();
// logic as per "checkTimeOption.H"
bool found = false; bool found = false;
int nearestIndex = -1; int nearestIndex = Time::findClosestTimeIndex(Times, requestedTime);
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;
}
}
if (nearestIndex == -1) if (nearestIndex == -1)
{ {
...@@ -212,12 +198,12 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime) ...@@ -212,12 +198,12 @@ bool Foam::vtkPV3Foam::setTime(const double& requestedTime)
found = true; found = true;
} }
runTime.setTime(times[nearestIndex], nearestIndex); runTime.setTime(Times[nearestIndex], nearestIndex);
if (debug) if (debug)
{ {
Info<< "<end> Foam::vtkPV3Foam::setTime() - selected time " Info<< "<end> Foam::vtkPV3Foam::setTime() - selected time "
<< times[nearestIndex].name() << endl; << Times[nearestIndex].name() << endl;
} }
return found; return found;
......
...@@ -418,19 +418,55 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const ...@@ -418,19 +418,55 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const
return times[times.size() - 1]; return times[times.size() - 1];
} }
label nearestIndex = -1;
scalar deltaT = GREAT; scalar deltaT = GREAT;
label closesti = 0;
for (label i=1; i<times.size(); i++) 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); deltaT = diff;
closesti = i; 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;
} }
......
...@@ -300,6 +300,9 @@ public: ...@@ -300,6 +300,9 @@ public:
//- Search the case for the time closest to the given time //- Search the case for the time closest to the given time
instant findClosestTime(const scalar) const; 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 //- Write using given format, version and compression
virtual bool writeObject virtual bool writeObject
( (
......
...@@ -33,10 +33,7 @@ Description ...@@ -33,10 +33,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam Foam::word Foam::Time::findInstance
{
word Time::findInstance
( (
const fileName& dir, const fileName& dir,
const word& name, const word& name,
...@@ -110,7 +107,7 @@ word Time::findInstance ...@@ -110,7 +107,7 @@ word Time::findInstance
// constant function of the time, because the latter points to // constant function of the time, because the latter points to
// the case constant directory in parallel cases // the case constant directory in parallel cases
if if
( (
file(path()/constant()/dir/name) file(path()/constant()/dir/name)
&& IOobject(name, constant(), dir, *this).headerOk() && IOobject(name, constant(), dir, *this).headerOk()
...@@ -139,9 +136,4 @@ word Time::findInstance ...@@ -139,9 +136,4 @@ word Time::findInstance
return constant(); return constant();
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //
...@@ -35,12 +35,7 @@ Description ...@@ -35,12 +35,7 @@ Description
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam Foam::instantList Foam::Time::findTimes(const fileName& directory)
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
instantList Time::findTimes(const fileName& directory)
{ {
if (debug) if (debug)
{ {
...@@ -101,9 +96,4 @@ instantList Time::findTimes(const fileName& directory) ...@@ -101,9 +96,4 @@ instantList Time::findTimes(const fileName& directory)
return Times; return Times;
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* // // ************************************************************************* //
if (args.options().found("time")) if (args.options().found("time"))
{ {
scalar time(readScalar(IStringStream(args.options()["time"])())); scalar timeValue(readScalar(IStringStream(args.options()["time"])()));
int nearestIndex = -1; startTime = Time::findClosestTimeIndex(Times, timeValue);
scalar nearestDiff = Foam::GREAT; endTime = startTime + 1;
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;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment