Public Types | Public Member Functions | Static Public Attributes | List of all members
externalFileCoupler Class Reference

Encapsulates the logic for coordinating between OpenFOAM and an external application. More...

Inheritance diagram for externalFileCoupler:
Inheritance graph
[legend]
Collaboration diagram for externalFileCoupler:
Collaboration graph
[legend]

Public Types

enum  runState { NONE, MASTER, SLAVE, DONE }
 

Public Member Functions

 TypeName ("externalFileCoupler")
 
 externalFileCoupler ()
 
 externalFileCoupler (const fileName &commsDir)
 
 externalFileCoupler (const dictionary &dict)
 
virtual ~externalFileCoupler ()
 
bool initialized () const
 
bool slaveFirst () const
 
const fileNamecommDirectory () const
 
fileName resolveFile (const word &file) const
 
fileName lockFile () const
 
bool readDict (const dictionary &dict)
 
enum Time::stopAtControls useMaster (const bool wait=false) const
 
enum Time::stopAtControls useSlave (const bool wait=false) const
 
enum Time::stopAtControls waitForMaster () const
 
enum Time::stopAtControls waitForSlave () const
 
virtual void readDataMaster ()
 
virtual void readDataSlave ()
 
virtual void writeDataMaster () const
 
virtual void writeDataSlave () const
 
virtual void removeDataMaster () const
 
virtual void removeDataSlave () const
 
void shutdown () const
 
void removeDirectory () const
 

Static Public Attributes

static word lockName = "OpenFOAM"
 

Detailed Description

Encapsulates the logic for coordinating between OpenFOAM and an external application.

This class provides a simple interface for explicit coupling with an external application using plain text files located in the user-specified communications directory. These files are to be read/written on the master processor only.

The explicit coupling follows a master/slave model in which OpenFOAM is the 'master' and the external application is the 'slave'. The readiness to exchange information in either direction is handled by a lock file (ie, OpenFOAM.lock). If the lock file is present, the slave (external application) should wait for the master (OpenFOAM) to complete.

When the master is finished its tasks and has prepared data for the slave, the lock file is removed, instructing the external source to take control of the program execution.

When the slave has completed its tasks, it will reinstate the lock file.

Example of the communication specification:

communication
{
    commsDir        "<case>/comms";
    waitInterval    1;
    timeOut         100;
    initByExternal  no;
    statusDone      done;
}

A typical coupling loop would look like this (on the master-side):

    initialize - master takes control
    writeDataMaster() - write data for slave
    useSlave()
    waitForSlave()
    removeDataMaster() - cleanup old data from master [optional?]
    readDataMaster() - read data from slave
    useMaster()

On the slave-side:

    waitForMaster()
    readDataSlave()  - read data from master
    writeDataSlave() - write data for master
    useMaster()

Note that since the waitForSlave() method not only waits for the lock file to be reinstated but also does a simple check of its contents, it can also serve to communicate some control from the slave to the master.

Source files

Definition at line 103 of file externalFileCoupler.H.

Member Enumeration Documentation

◆ runState

enum runState
Enumerator
NONE 

Not initialized.

MASTER 

The master (OpenFOAM) is in charge.

SLAVE 

The slave (external program) is in charge.

DONE 

Finished.

Definition at line 110 of file externalFileCoupler.H.

Constructor & Destructor Documentation

◆ externalFileCoupler() [1/3]

Definition at line 88 of file externalFileCoupler.C.

References fileName::clean(), and string::expand().

Here is the call graph for this function:

◆ externalFileCoupler() [2/3]

externalFileCoupler ( const fileName commsDir)

Definition at line 103 of file externalFileCoupler.C.

References fileName::clean(), string::expand(), UPstream::master(), and Foam::mkDir().

Here is the call graph for this function:

◆ externalFileCoupler() [3/3]

externalFileCoupler ( const dictionary dict)

Definition at line 123 of file externalFileCoupler.C.

References dict, UPstream::master(), Foam::mkDir(), and externalFileCoupler::readDict().

Here is the call graph for this function:

◆ ~externalFileCoupler()

~externalFileCoupler ( )
virtual

Definition at line 138 of file externalFileCoupler.C.

Member Function Documentation

◆ TypeName()

TypeName ( "externalFileCoupler"  )

◆ initialized()

bool initialized ( ) const
inline

Definition at line 23 of file externalFileCouplerI.H.

References externalFileCoupler::NONE.

◆ slaveFirst()

bool slaveFirst ( ) const
inline

Definition at line 29 of file externalFileCouplerI.H.

◆ commDirectory()

const Foam::fileName & commDirectory ( ) const
inline

Definition at line 35 of file externalFileCouplerI.H.

◆ resolveFile()

Foam::fileName resolveFile ( const word file) const
inline

Definition at line 42 of file externalFileCouplerI.H.

◆ lockFile()

Foam::fileName lockFile ( ) const
inline

Definition at line 50 of file externalFileCouplerI.H.

◆ readDict()

bool readDict ( const dictionary dict)

Definition at line 146 of file externalFileCoupler.C.

References dict, Foam::endl(), dictionary::getOrDefault(), Foam::Info, Foam::log(), Foam::nl, dictionary::readEntry(), and Foam::type().

Referenced by externalFileCoupler::externalFileCoupler(), and externalCoupled::read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ useMaster()

enum Foam::Time::stopAtControls useMaster ( const bool  wait = false) const

Definition at line 179 of file externalFileCoupler.C.

References Foam::endl(), Foam::isFile(), Log, UPstream::master(), Foam::mkDir(), os(), and Foam::type().

Here is the call graph for this function:

◆ useSlave()

enum Foam::Time::stopAtControls useSlave ( const bool  wait = false) const

Definition at line 215 of file externalFileCoupler.C.

References Foam::endl(), Log, UPstream::master(), Foam::mkDir(), Foam::rm(), and Foam::type().

Here is the call graph for this function:

◆ waitForMaster()

enum Foam::Time::stopAtControls waitForMaster ( ) const

Definition at line 243 of file externalFileCoupler.C.

References Foam::getStopAction(), Foam::highResLastModified(), UPstream::master(), Pstream::scatter(), and Foam::sleep().

Here is the call graph for this function:

◆ waitForSlave()

enum Foam::Time::stopAtControls waitForSlave ( ) const

◆ readDataMaster()

void readDataMaster ( )
virtual

Reimplemented in externalCoupled.

Definition at line 330 of file externalFileCoupler.C.

◆ readDataSlave()

void readDataSlave ( )
virtual

Definition at line 334 of file externalFileCoupler.C.

◆ writeDataMaster()

void writeDataMaster ( ) const
virtual

Reimplemented in externalCoupled.

Definition at line 338 of file externalFileCoupler.C.

◆ writeDataSlave()

void writeDataSlave ( ) const
virtual

Definition at line 342 of file externalFileCoupler.C.

◆ removeDataMaster()

void removeDataMaster ( ) const
virtual

Reimplemented in externalCoupled.

Definition at line 346 of file externalFileCoupler.C.

◆ removeDataSlave()

void removeDataSlave ( ) const
virtual

Reimplemented in externalCoupled.

Definition at line 350 of file externalFileCoupler.C.

◆ shutdown()

void shutdown ( ) const

Definition at line 354 of file externalFileCoupler.C.

References Foam::endl(), Foam::isDir(), Log, UPstream::master(), Foam::nl, os(), and Foam::type().

Here is the call graph for this function:

◆ removeDirectory()

void removeDirectory ( ) const

Member Data Documentation

◆ lockName

Foam::word lockName = "OpenFOAM"
static

Definition at line 162 of file externalFileCoupler.H.


The documentation for this class was generated from the following files: