/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see .
\*---------------------------------------------------------------------------*/
#include "externalCoupledFunctionObject.H"
#include "addToRunTimeSelectionTable.H"
#include "OSspecific.H"
#include "IFstream.H"
#include "OFstream.H"
#include "volFields.H"
#include "globalIndex.H"
#include "fvMesh.H"
#include "DynamicField.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(externalCoupledFunctionObject, 0);
addToRunTimeSelectionTable
(
functionObject,
externalCoupledFunctionObject,
dictionary
);
}
Foam::word Foam::externalCoupledFunctionObject::lockName = "OpenFOAM";
Foam::string Foam::externalCoupledFunctionObject::patchKey = "# Patch: ";
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::fileName Foam::externalCoupledFunctionObject::baseDir() const
{
fileName result(commsDir_);
result.clean();
return result;
}
Foam::fileName Foam::externalCoupledFunctionObject::groupDir
(
const fileName& commsDir,
const word& regionGroupName,
const wordRe& groupName
)
{
fileName result
(
commsDir
/regionGroupName
/string::validate(groupName)
);
result.clean();
return result;
}
Foam::fileName Foam::externalCoupledFunctionObject::lockFile() const
{
return fileName(baseDir()/(lockName + ".lock"));
}
void Foam::externalCoupledFunctionObject::createLockFile() const
{
if (!Pstream::master())
{
return;
}
const fileName fName(lockFile());
IFstream is(fName);
// Only create lock file if it doesn't already exist
if (!is.good())
{
if (log_) Info<< type() << ": creating lock file" << endl;
OFstream os(fName);
os << "lock file";
os.flush();
}
}
void Foam::externalCoupledFunctionObject::removeLockFile() const
{
if (!Pstream::master())
{
return;
}
if (log_) Info<< type() << ": removing lock file" << endl;
rm(lockFile());
}
void Foam::externalCoupledFunctionObject::removeReadFiles() const
{
if (!Pstream::master())
{
return;
}
if (log_) Info<< type() << ": removing all read files" << endl;
forAll(regionGroupNames_, regionI)
{
const word& compName = regionGroupNames_[regionI];
const labelList& groups = regionToGroups_[compName];
forAll(groups, i)
{
label groupI = groups[i];
const wordRe& groupName = groupNames_[groupI];
forAll(groupReadFields_[groupI], fieldI)
{
const word& fieldName = groupReadFields_[groupI][fieldI];
rm
(
groupDir(commsDir_, compName, groupName)
/ fieldName + ".in"
);
}
}
}
}
void Foam::externalCoupledFunctionObject::removeWriteFiles() const
{
if (!Pstream::master())
{
return;
}
if (log_) Info<< type() << ": removing all write files" << endl;
forAll(regionGroupNames_, regionI)
{
const word& compName = regionGroupNames_[regionI];
const labelList& groups = regionToGroups_[compName];
forAll(groups, i)
{
label groupI = groups[i];
const wordRe& groupName = groupNames_[groupI];
forAll(groupReadFields_[groupI], fieldI)
{
const word& fieldName = groupReadFields_[groupI][fieldI];
rm
(
groupDir(commsDir_, compName, groupName)
/ fieldName + ".out"
);
}
}
}
}
void Foam::externalCoupledFunctionObject::wait() const
{
const fileName fName(lockFile());
label found = 0;
label totalTime = 0;
if (log_) Info<< type() << ": beginning wait for lock file " << fName << nl;
while (found == 0)
{
if (Pstream::master())
{
if (totalTime > timeOut_)
{
FatalErrorIn
(
"void "
"Foam::externalCoupledFunctionObject::wait() "
"const"
)
<< "Wait time exceeded time out time of " << timeOut_
<< " s" << abort(FatalError);
}
IFstream is(fName);
if (is.good())
{
found++;
if (log_)
{
Info<< type() << ": found lock file " << fName << endl;
}
}
else
{
sleep(waitInterval_);
totalTime += waitInterval_;
if (log_)
{
Info<< type() << ": wait time = " << totalTime << endl;
}
}
}
// prevent other procs from racing ahead
reduce(found, sumOp