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
......@@ -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;
......
......@@ -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;
}
......
......@@ -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
(
......
......@@ -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
// ************************************************************************* //
......@@ -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
// ************************************************************************* //
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;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment