Time: Handle precision changes on start-up and running more robustly

Changes based on patches from and discussions with Bruno Santos
Resolves bug-report http://www.openfoam.org/mantisbt/view.php?id=815
This commit is contained in:
Henry
2015-03-08 16:29:01 +00:00
parent 72f5598049
commit 7f3fb82ba0
2 changed files with 85 additions and 13 deletions

View File

@ -70,8 +70,11 @@ const Foam::NamedEnum<Foam::Time::writeControls, 5>
Foam::Time::writeControlNames_; Foam::Time::writeControlNames_;
Foam::Time::fmtflags Foam::Time::format_(Foam::Time::general); Foam::Time::fmtflags Foam::Time::format_(Foam::Time::general);
int Foam::Time::precision_(6); int Foam::Time::precision_(6);
const int Foam::Time::maxPrecision_(3 - log10(SMALL));
Foam::word Foam::Time::controlDictName("controlDict"); Foam::word Foam::Time::controlDictName("controlDict");
@ -188,6 +191,56 @@ void Foam::Time::setControls()
deltaTSave_ = deltaT_; deltaTSave_ = deltaT_;
deltaT0_ = deltaT_; deltaT0_ = deltaT_;
// Check if time directory exists
// If not increase time precision to see if it is formatted differently.
if (!exists(timePath(), false))
{
int oldPrecision = precision_;
int requiredPrecision = -1;
bool found = false;
for
(
precision_ = maxPrecision_;
precision_ > oldPrecision;
precision_--
)
{
// Update the time formatting
setTime(startTime_, 0);
// Check the existence of the time directory with the new format
found = exists(timePath(), false);
if (found)
{
requiredPrecision = precision_;
}
}
if (requiredPrecision > 0)
{
// Update the time precision
precision_ = requiredPrecision;
// Update the time formatting
setTime(startTime_, 0);
WarningIn("Time::setControls()")
<< "Increasing the timePrecision from " << oldPrecision
<< " to " << precision_
<< " to support the formatting of the current time directory "
<< timeName() << nl << endl;
}
else
{
// Could not find time directory so assume it is not present
precision_ = oldPrecision;
// Revert the time formatting
setTime(startTime_, 0);
}
}
if (Pstream::parRun()) if (Pstream::parRun())
{ {
scalar sumStartTime = startTime_; scalar sumStartTime = startTime_;
@ -754,13 +807,13 @@ Foam::instant Foam::Time::findClosestTime(const scalar t) const
label nearestIndex = -1; label nearestIndex = -1;
scalar deltaT = GREAT; scalar deltaT = GREAT;
for (label timeI=1; timeI < timeDirs.size(); ++timeI) for (label timei=1; timei < timeDirs.size(); ++timei)
{ {
scalar diff = mag(timeDirs[timeI].value() - t); scalar diff = mag(timeDirs[timei].value() - t);
if (diff < deltaT) if (diff < deltaT)
{ {
deltaT = diff; deltaT = diff;
nearestIndex = timeI; nearestIndex = timei;
} }
} }
@ -788,15 +841,15 @@ Foam::label Foam::Time::findClosestTimeIndex
label nearestIndex = -1; label nearestIndex = -1;
scalar deltaT = GREAT; scalar deltaT = GREAT;
forAll(timeDirs, timeI) forAll(timeDirs, timei)
{ {
if (timeDirs[timeI].name() == constantName) continue; if (timeDirs[timei].name() == constantName) continue;
scalar diff = mag(timeDirs[timeI].value() - t); scalar diff = mag(timeDirs[timei].value() - t);
if (diff < deltaT) if (diff < deltaT)
{ {
deltaT = diff; deltaT = diff;
nearestIndex = timeI; nearestIndex = timei;
} }
} }
@ -1026,9 +1079,11 @@ Foam::Time& Foam::Time::operator++()
deltaT0_ = deltaTSave_; deltaT0_ = deltaTSave_;
deltaTSave_ = deltaT_; deltaTSave_ = deltaT_;
// Save old time name // Save old time value and name
const scalar oldTimeValue = value();
const word oldTimeName = dimensionedScalar::name(); const word oldTimeName = dimensionedScalar::name();
// Increment time
setTime(value() + deltaT_, timeIndex_ + 1); setTime(value() + deltaT_, timeIndex_ + 1);
if (!subCycling_) if (!subCycling_)
@ -1041,8 +1096,16 @@ Foam::Time& Foam::Time::operator++()
} }
// Time value obtained by reading timeName
scalar timeNameValue;
// Check that new time representation differs from old one // Check that new time representation differs from old one
if (dimensionedScalar::name() == oldTimeName) // reinterpretation of the word
if
(
readScalar(dimensionedScalar::name().c_str(), timeNameValue)
&& (mag(timeNameValue - oldTimeValue - deltaT_) > 100*SMALL)
)
{ {
int oldPrecision = precision_; int oldPrecision = precision_;
do do
@ -1050,17 +1113,23 @@ Foam::Time& Foam::Time::operator++()
precision_++; precision_++;
setTime(value(), timeIndex()); setTime(value(), timeIndex());
} }
while (precision_ < 100 && dimensionedScalar::name() == oldTimeName); while
(
precision_ < maxPrecision_
&& readScalar(dimensionedScalar::name().c_str(), timeNameValue)
&& (mag(timeNameValue - oldTimeValue - deltaT_) > 100*SMALL)
);
WarningIn("Time::operator++()") WarningIn("Time::operator++()")
<< "Increased the timePrecision from " << oldPrecision << "Increased the timePrecision from " << oldPrecision
<< " to " << precision_ << " to " << precision_
<< " to distinguish between timeNames at time " << value() << " to distinguish between timeNames at time "
<< dimensionedScalar::name()
<< endl; << endl;
if (precision_ == 100 && precision_ != oldPrecision) if (precision_ == maxPrecision_ && precision_ != oldPrecision)
{ {
// Reached limit. // Reached maxPrecision limit
WarningIn("Time::operator++()") WarningIn("Time::operator++()")
<< "Current time name " << dimensionedScalar::name() << "Current time name " << dimensionedScalar::name()
<< " is the old as the previous one " << oldTimeName << " is the old as the previous one " << oldTimeName

View File

@ -172,6 +172,9 @@ protected:
//- Time directory name precision //- Time directory name precision
static int precision_; static int precision_;
//- Maximum time directory name precision
static const int maxPrecision_;
//- Adjust the time step so that writing occurs at the specified time //- Adjust the time step so that writing occurs at the specified time
void adjustDeltaT(); void adjustDeltaT();