ENH: improve findInstance handling of negative times (#1112)

- normally findInstance will 'bottom out' with the constant directory
  while doing its reverse time search. This mechanism however fails
  when searching for negative start values (if there are none in the
  list). Add additional logic for this so that constant will also be
  used in these situations.

Note: to have decomposePar work for all times, may need the -constant option
to trigger the proper time list.
This commit is contained in:
Mark Olesen
2019-03-26 09:16:57 +01:00
committed by Andrew Heather
parent a1fe76e749
commit f200fc1363
3 changed files with 99 additions and 71 deletions

View File

@ -104,51 +104,51 @@ Foam::instantList Foam::fileOperation::sortTimes
) )
{ {
// Initialise instant list // Initialise instant list
instantList Times(dirEntries.size() + 1); instantList times(dirEntries.size() + 1);
label nTimes = 0; label nTimes = 0;
// Check for "constant" // Check for "constant"
bool haveConstant = false; bool haveConstant = false;
forAll(dirEntries, i) for (const fileName& dirName : dirEntries)
{ {
if (dirEntries[i] == constantName) if (dirName == constantName)
{ {
Times[nTimes].value() = 0;
Times[nTimes].name() = dirEntries[i];
nTimes++;
haveConstant = true; haveConstant = true;
times[nTimes].value() = 0;
times[nTimes].name() = constantName;
++nTimes;
break; break;
} }
} }
// Read and parse all the entries in the directory // Read and parse all the entries in the directory
forAll(dirEntries, i) for (const fileName& dirName : dirEntries)
{ {
scalar timeValue; scalar timeValue;
if (readScalar(dirEntries[i], timeValue)) if (readScalar(dirName, timeValue))
{ {
Times[nTimes].value() = timeValue; times[nTimes].value() = timeValue;
Times[nTimes].name() = dirEntries[i]; times[nTimes].name() = dirName;
nTimes++; ++nTimes;
} }
} }
// Reset the length of the times list // Reset the length of the times list
Times.setSize(nTimes); times.setSize(nTimes);
if (haveConstant) if (haveConstant)
{ {
if (nTimes > 2) if (nTimes > 2)
{ {
std::sort(&Times[1], Times.end(), instant::less()); std::sort(&times[1], times.end(), instant::less());
} }
} }
else if (nTimes > 1) else if (nTimes > 1)
{ {
std::sort(&Times[0], Times.end(), instant::less()); std::sort(&times[0], times.end(), instant::less());
} }
return Times; return times;
} }
@ -161,15 +161,15 @@ void Foam::fileOperation::mergeTimes
{ {
if (extraTimes.size()) if (extraTimes.size())
{ {
bool haveConstant = const bool haveConstant =
( (
times.size() > 0 times.size()
&& times[0].name() == constantName && times[0].name() == constantName
); );
bool haveExtraConstant = const bool haveExtraConstant =
( (
extraTimes.size() > 0 extraTimes.size()
&& extraTimes[0].name() == constantName && extraTimes[0].name() == constantName
); );
@ -228,9 +228,7 @@ void Foam::fileOperation::mergeTimes
bool Foam::fileOperation::isFileOrDir(const bool isFile, const fileName& f) bool Foam::fileOperation::isFileOrDir(const bool isFile, const fileName& f)
{ {
return return (isFile ? Foam::isFile(f) : Foam::isDir(f));
(isFile && Foam::isFile(f))
|| (!isFile && Foam::isDir(f));
} }
@ -431,10 +429,8 @@ Foam::autoPtr<Foam::fileOperation> Foam::fileOperation::New
bool verbose bool verbose
) )
{ {
if (debug) DebugInFunction
{ << "Constructing fileHandler" << endl;
InfoInFunction << "Constructing fileHandler" << endl;
}
auto cstrIter = wordConstructorTablePtr_->cfind(handlerType); auto cstrIter = wordConstructorTablePtr_->cfind(handlerType);
@ -570,15 +566,13 @@ Foam::fileName Foam::fileOperation::filePath(const fileName& fName) const
} }
return fName; return fName;
} }
else
{
if (debug) if (debug)
{ {
Pout<< "fileOperation::filePath : Not found" << endl; Pout<< "fileOperation::filePath : Not found" << endl;
} }
return fileName::null; return fileName::null;
} }
}
Foam::label Foam::fileOperation::addWatch(const fileName& fName) const Foam::label Foam::fileOperation::addWatch(const fileName& fName) const
@ -755,13 +749,10 @@ Foam::IOobject Foam::fileOperation::findInstance
if (exists(io)) if (exists(io))
{ {
if (debug) DebugInFunction
{
InfoInFunction
<< "Found exact match for \"" << io.name() << "Found exact match for \"" << io.name()
<< "\" in " << io.instance()/io.local() << "\" in " << io.instance()/io.local()
<< endl; << endl;
}
return io; return io;
} }
@ -770,9 +761,9 @@ Foam::IOobject Foam::fileOperation::findInstance
// closest to and lower than current time // closest to and lower than current time
instantList ts = time.times(); instantList ts = time.times();
label instanceI; label instanceI = ts.size()-1;
for (instanceI = ts.size()-1; instanceI >= 0; --instanceI) for (; instanceI >= 0; --instanceI)
{ {
if (ts[instanceI].value() <= startValue) if (ts[instanceI].value() <= startValue)
{ {
@ -780,7 +771,7 @@ Foam::IOobject Foam::fileOperation::findInstance
} }
} }
// continue searching from here // Continue searching from here
for (; instanceI >= 0; --instanceI) for (; instanceI >= 0; --instanceI)
{ {
// Shortcut: if actual directory is the timeName we've already tested it // Shortcut: if actual directory is the timeName we've already tested it
@ -796,13 +787,10 @@ Foam::IOobject Foam::fileOperation::findInstance
io.instance() = ts[instanceI].name(); io.instance() = ts[instanceI].name();
if (exists(io)) if (exists(io))
{ {
if (debug) DebugInFunction
{
InfoInFunction
<< "Found exact match for \"" << io.name() << "Found exact match for \"" << io.name()
<< "\" in " << io.instance()/io.local() << "\" in " << io.instance()/io.local()
<< endl; << endl;
}
return io; return io;
} }
@ -810,11 +798,8 @@ Foam::IOobject Foam::fileOperation::findInstance
// Check if hit minimum instance // Check if hit minimum instance
if (ts[instanceI].name() == stopInstance) if (ts[instanceI].name() == stopInstance)
{ {
if (debug) DebugInFunction
{
InfoInFunction
<< "Hit stopInstance " << stopInstance << endl; << "Hit stopInstance " << stopInstance << endl;
}
if if
( (
@ -845,27 +830,30 @@ Foam::IOobject Foam::fileOperation::findInstance
} }
} }
// times() usually already includes the constant() so would have been // Times usually already includes 'constant' so would have been checked
// checked above. Re-test if // above.
// - times() is empty. Sometimes this can happen (e.g. decomposePar with // However, re-test under these conditions:
// collated) // - Times is empty.
// - times()[0] is not constant // Sometimes this can happen (eg, decomposePar with collated)
if (!ts.size() || ts[0].name() != time.constant()) // - Times[0] is not constant
{ // - The startValue is negative (eg, kivaTest).
// Note. This needs to be a hard-coded constant, rather than the // This plays havoc with the reverse search, causing it to miss 'constant'
// constant function of the time, because the latter points to
// the case constant directory in parallel cases
if
(
ts.empty()
|| ts.first().name() != time.constant()
|| startValue < 0
)
{
io.instance() = time.constant(); io.instance() = time.constant();
if (exists(io)) if (exists(io))
{ {
if (debug) DebugInFunction
{
InfoInFunction
<< "Found constant match for \"" << io.name() << "Found constant match for \"" << io.name()
<< "\" in " << io.instance()/io.local() << "\" in " << io.instance()/io.local()
<< endl; << endl;
}
return io; return io;
} }
} }

View File

@ -0,0 +1,13 @@
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory
. $WM_PROJECT_DIR/bin/tools/RunFunctions # Tutorial run functions
runApplication kivaToFoam -file otape17
runApplication decomposePar
runParallel $(getApplication)
runApplication reconstructPar
#------------------------------------------------------------------------------

View File

@ -0,0 +1,27 @@
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: v1812 |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object decomposeParDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
numberOfSubdomains 4;
method hierarchical;
coeffs
{
n (2 2 1);
}
// ************************************************************************* //