diff --git a/src/functionObjects/field/externalCoupled/externalCoupled.C b/src/functionObjects/field/externalCoupled/externalCoupled.C index 999e311f20..d56174a81e 100644 --- a/src/functionObjects/field/externalCoupled/externalCoupled.C +++ b/src/functionObjects/field/externalCoupled/externalCoupled.C @@ -52,7 +52,55 @@ namespace functionObjects Foam::word Foam::functionObjects::externalCoupled::lockName = "OpenFOAM"; -Foam::string Foam::functionObjects::externalCoupled::patchKey = "# Patch: "; +Foam::string Foam::functionObjects::externalCoupled::patchKey = "// Patch:"; + +template<> +const char* Foam::NamedEnum +< + Foam::functionObjects::externalCoupled::stateEnd, + 2 +>::names[] = +{ + "remove", + "done" + // The 'IGNORE' enumeration is internal use only and thus has no name +}; + +const Foam::NamedEnum +< + Foam::functionObjects::externalCoupled::stateEnd, + 2 +> Foam::functionObjects::externalCoupled::stateEndNames_; + + +namespace Foam +{ +//! \cond fileScope +//- Write list content with size, bracket, content, bracket one-per-line. +// This makes for consistent for parsing, regardless of the list length. +template +static void writeList(Ostream& os, const string& header, const UList& L) +{ + // Header string + os << header.c_str() << nl; + + // Write size and start delimiter + os << L.size() << nl + << token::BEGIN_LIST; + + // Write contents + forAll(L, i) + { + os << nl << L[i]; + } + + // Write end delimiter + os << nl << token::END_LIST << nl << endl; +} +//! \endcond + +} +// namespace Foam // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // @@ -91,7 +139,7 @@ Foam::fileName Foam::functionObjects::externalCoupled::lockFile() const } -void Foam::functionObjects::externalCoupled::createLockFile() const +void Foam::functionObjects::externalCoupled::useMaster() const { if (!Pstream::master()) { @@ -107,13 +155,13 @@ void Foam::functionObjects::externalCoupled::createLockFile() const Log << type() << ": creating lock file" << endl; OFstream os(fName); - os << "lock file"; + os << "status=openfoam\n"; os.flush(); } } -void Foam::functionObjects::externalCoupled::removeLockFile() const +void Foam::functionObjects::externalCoupled::useSlave() const { if (!Pstream::master()) { @@ -122,7 +170,37 @@ void Foam::functionObjects::externalCoupled::removeLockFile() const Log << type() << ": removing lock file" << endl; - rm(lockFile()); + Foam::rm(lockFile()); +} + + +void Foam::functionObjects::externalCoupled::cleanup() const +{ + if (!Pstream::master()) + { + return; + } + + const fileName lck(lockFile()); + switch (stateEnd_) + { + case REMOVE: + { + Log << type() << ": removing lock file" << endl; + Foam::rm(lck); + } + break; + case DONE: + { + Log << type() << ": lock file status=done" << endl; + OFstream os(lck); + os << "status=done\n"; + os.flush(); + } + break; + case IGNORE: + break; + } } @@ -192,30 +270,29 @@ void Foam::functionObjects::externalCoupled::removeWriteFiles() const } -void Foam::functionObjects::externalCoupled::wait() const +void Foam::functionObjects::externalCoupled::waitForSlave() const { const fileName fName(lockFile()); - label found = 0; label totalTime = 0; + bool found = false; Log << type() << ": beginning wait for lock file " << fName << nl; - while (found == 0) + while (!found) { if (Pstream::master()) { if (totalTime > timeOut_) { FatalErrorInFunction - << "Wait time exceeded time out time of " << timeOut_ + << "Wait time exceeded timeout of " << timeOut_ << " s" << abort(FatalError); } IFstream is(fName); - if (is.good()) { - found++; + found = true; Log << type() << ": found lock file " << fName << endl; } @@ -229,7 +306,7 @@ void Foam::functionObjects::externalCoupled::wait() const } // Prevent other procs from racing ahead - reduce(found, sumOp