Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes
externalCoupledFunctionObject Class Reference

This functionObject provides a simple interface for explicit coupling with an external application. The coupling is through plain text files where OpenFOAM boundary data is read/written as one line per face (data from all processors collated): More...

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

Public Member Functions

 TypeName ("externalCoupled")
 Runtime type information. More...
 
 externalCoupledFunctionObject (const word &name, const Time &runTime, const dictionary &dict)
 Construct given time and dictionary. More...
 
virtual ~externalCoupledFunctionObject ()
 Destructor. More...
 
virtual bool enabled () const
 Return the enabled flag. More...
 
virtual void on ()
 Switch the function object on. More...
 
virtual void off ()
 Switch the function object off. More...
 
virtual bool start ()
 Called at the start of the time-loop. More...
 
virtual bool execute (const bool forceWrite)
 Called at each ++ or += of the time-loop. More...
 
virtual bool end ()
 Called when Time::run() determines that the time-loop exits. More...
 
virtual bool timeSet ()
 Called when time was set at the end of the Time::operator++. More...
 
virtual bool adjustTimeStep ()
 Called at the end of Time::adjustDeltaT() if adjustTime is true. More...
 
virtual bool read (const dictionary &)
 Read and set the function object if its data have changed. More...
 
virtual void updateMesh (const mapPolyMesh &mpm)
 Update for changes of mesh. More...
 
virtual void movePoints (const polyMesh &mesh)
 Update for changes of mesh. More...
 
template<class Type >
Foam::tmp< Foam::Field< Type > > gatherAndCombine (const Field< Type > &fld)
 
- Public Member Functions inherited from functionObject
virtual const wordtype () const =0
 Runtime type information. More...
 
 declareRunTimeSelectionTable (autoPtr, functionObject, dictionary,(const word &name, const Time &t, const dictionary &dict),(name, t, dict))
 
 functionObject (const word &name)
 Construct from components. More...
 
autoPtr< functionObjectclone () const
 Return clone. More...
 
virtual ~functionObject ()
 Destructor. More...
 
virtual const wordname () const
 Name. More...
 

Static Public Member Functions

static word compositeName (const wordList &)
 Create single name by appending words (in sorted order),. More...
 
static void writeGeometry (const UPtrList< const fvMesh > &meshes, const fileName &commsDir, const wordRe &groupName)
 Write geometry for the group/patch. More...
 
- Static Public Member Functions inherited from functionObject
static autoPtr< functionObjectNew (const word &name, const Time &, const dictionary &)
 Select from dictionary, based on its "type" entry. More...
 

Static Public Attributes

static word lockName = "OpenFOAM"
 Name of lock file. More...
 
static string patchKey = "# Patch: "
 Name of patch key, e.g. '# Patch:' when looking for start of patch data. More...
 
- Static Public Attributes inherited from functionObject
static int debug
 

Private Member Functions

fileName baseDir () const
 Return the file path to the base communications directory. More...
 
fileName lockFile () const
 Return the file path to the lock file. More...
 
void createLockFile () const
 Create lock file. More...
 
void removeLockFile () const
 Remove lock file. More...
 
void removeWriteFiles () const
 Remove files written by OpenFOAM. More...
 
void removeReadFiles () const
 Remove files written by external code. More...
 
void wait () const
 Wait for response from external source. More...
 
template<class Type >
bool readData (const UPtrList< const fvMesh > &meshes, const wordRe &groupName, const word &fieldName)
 Read data for a single region, single field. More...
 
void readData ()
 Read data for all regions, all fields. More...
 
template<class Type >
bool writeData (const UPtrList< const fvMesh > &meshes, const wordRe &groupName, const word &fieldName) const
 Write data for a single region, single field. More...
 
void writeData () const
 Write data for all regions, all fields. More...
 
void initialise ()
 
void readColumns (const label nRows, const label nColumns, autoPtr< IFstream > &masterFilePtr, List< scalarField > &data) const
 Read (and distribute) scalar columns from stream. Every processor. More...
 
void readLines (const label nRows, autoPtr< IFstream > &masterFilePtr, OStringStream &data) const
 Read (and distribute) lines from stream. Every processor. More...
 
 externalCoupledFunctionObject (const externalCoupledFunctionObject &)
 Disallow default bitwise copy construc. More...
 
void operator= (const externalCoupledFunctionObject &)
 Disallow default bitwise assignmen. More...
 

Static Private Member Functions

static fileName groupDir (const fileName &commsDir, const word &regionsName, const wordRe &groupName)
 Return the file path to the communications directory for the region. More...
 
template<class Type >
static tmp< Field< Type > > gatherAndCombine (const Field< Type > &fld)
 Helper: append data from all processors onto master. More...
 
static void checkOrder (const wordList &)
 

Private Attributes

const Timetime_
 Reference to the time database. More...
 
Switch enabled_
 Switch for the execution - defaults to 'yes/on'. More...
 
fileName commsDir_
 Path to communications directory. More...
 
label waitInterval_
 Interval time between checking for return data [s]. More...
 
label timeOut_
 Time out time [s]. More...
 
label calcFrequency_
 Calculation frequency. More...
 
bool initByExternal_
 Flag to indicate values are initialised by external application. More...
 
bool log_
 Log flag. More...
 
DynamicList< wordregionGroupNames_
 Names of (composite) regions. More...
 
DynamicList< wordListregionGroupRegions_
 
HashTable< labelListregionToGroups_
 
DynamicList< wordRegroupNames_
 
DynamicList< wordListgroupReadFields_
 
DynamicList< wordListgroupWriteFields_
 
bool initialised_
 Initialised flag. More...
 

Detailed Description

This functionObject provides a simple interface for explicit coupling with an external application. The coupling is through plain text files where OpenFOAM boundary data is read/written as one line per face (data from all processors collated):

# Patch: <patch name> <fld1> <fld2> .. <fldn> //face0 <fld1> <fld2> .. <fldn> //face1 .. <fld1> <fld2> .. <fldn> //faceN

where the actual entries depend on the bc type:

These text files are located in a user specified communications directory which gets read/written on the master processor only. In the communications directory the structure will be

<regionsName>/<patchGroup>/<fieldName>.[in|out]

(where regionsName is either the name of a single region or a composite of multiple region names)

At start-up, the boundary creates a lock file, i.e..

OpenFOAM.lock

... to signal the external source to wait. During the functionObject execution the boundary values are written to files (one per region, per patch(group), per field), e.g.

<regionsName>/<patchGroup>/<fieldName>.out

The lock file is then removed, instructing the external source to take control of the program execution. When ready, the external program should create the return values, e.g. to files

<regionsName>/<patchGroup>/<fieldName>.in

... and then re-instate the lock file. The functionObject will then read these values, apply them to the boundary conditions and pass program execution back to OpenFOAM.

Example of function object specification:

externalCoupled
{
    type            externalCoupled;
    ...
    log             yes;
    commsDir        "${FOAM_CASE}/comms";
    initByExternal  yes;

    regions
    {
        "(region1|region0)"         // Name of region(s)
        {
            TPatchGroup             // Name of patch(group)
            {
                readFields  (p);    // List of fields to read
                writeFields (T);    // List of fields to write
            }
        }
    }
}

This reads/writes (on the master processor) the directory: comms/region0_region1/TPatchGroup/ with contents: patchPoints (collected points) patchFaces (collected faces) p.in (input file of p, written by external application) T.out (output file of T, written by OpenFOAM)

The patchPoints/patchFaces files denote the (collated) geometry which will be written if it does not exist yet or can be written as a preprocessing step using the createExternalCoupledPatchGeometry application.

Source files

Definition at line 143 of file externalCoupledFunctionObject.H.

Constructor & Destructor Documentation

◆ externalCoupledFunctionObject() [1/2]

Disallow default bitwise copy construc.

◆ externalCoupledFunctionObject() [2/2]

externalCoupledFunctionObject ( const word name,
const Time runTime,
const dictionary dict 
)

Construct given time and dictionary.

Definition at line 784 of file externalCoupledFunctionObject.C.

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

Here is the call graph for this function:

◆ ~externalCoupledFunctionObject()

Destructor.

Definition at line 811 of file externalCoupledFunctionObject.C.

Member Function Documentation

◆ groupDir()

Foam::fileName groupDir ( const fileName commsDir,
const word regionsName,
const wordRe groupName 
)
staticprivate

Return the file path to the communications directory for the region.

Definition at line 67 of file externalCoupledFunctionObject.C.

References fileName::clean().

Here is the call graph for this function:

◆ baseDir()

Foam::fileName baseDir ( ) const
private

Return the file path to the base communications directory.

Definition at line 57 of file externalCoupledFunctionObject.C.

References fileName::clean(), and externalCoupledFunctionObject::commsDir_.

Here is the call graph for this function:

◆ lockFile()

Foam::fileName lockFile ( ) const
private

Return the file path to the lock file.

Definition at line 85 of file externalCoupledFunctionObject.C.

◆ createLockFile()

void createLockFile ( ) const
private

Create lock file.

Definition at line 91 of file externalCoupledFunctionObject.C.

References Foam::endl(), OSstream::flush(), IOstream::good(), Foam::Info, UPstream::master(), and Foam::type().

Here is the call graph for this function:

◆ removeLockFile()

void removeLockFile ( ) const
private

Remove lock file.

Definition at line 113 of file externalCoupledFunctionObject.C.

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

Here is the call graph for this function:

◆ removeWriteFiles()

void removeWriteFiles ( ) const
private

Remove files written by OpenFOAM.

Definition at line 159 of file externalCoupledFunctionObject.C.

References Foam::endl(), forAll, Foam::Info, UPstream::master(), Foam::rm(), and Foam::type().

Here is the call graph for this function:

◆ removeReadFiles()

void removeReadFiles ( ) const
private

Remove files written by external code.

Definition at line 126 of file externalCoupledFunctionObject.C.

References Foam::endl(), forAll, Foam::Info, UPstream::master(), Foam::rm(), and Foam::type().

Here is the call graph for this function:

◆ wait()

void wait ( ) const
private

Wait for response from external source.

Definition at line 192 of file externalCoupledFunctionObject.C.

References Foam::abort(), Foam::endl(), Foam::FatalError, FatalErrorInFunction, found, IOstream::good(), Foam::Info, UPstream::master(), Foam::nl, Foam::reduce(), Foam::sleep(), and Foam::type().

Here is the call graph for this function:

◆ readData() [1/2]

bool readData ( const UPtrList< const fvMesh > &  meshes,
const wordRe groupName,
const word fieldName 
)
private

◆ readData() [2/2]

void readData ( )
private

Read data for all regions, all fields.

Definition at line 583 of file externalCoupledFunctionObject.C.

References Foam::endl(), Foam::fieldNames(), forAll, regionName, UPtrList::set(), List::size(), and WarningInFunction.

Here is the call graph for this function:

◆ writeData() [1/2]

bool writeData ( const UPtrList< const fvMesh > &  meshes,
const wordRe groupName,
const word fieldName 
) const
private

◆ writeData() [2/2]

void writeData ( ) const
private

Write data for all regions, all fields.

Definition at line 653 of file externalCoupledFunctionObject.C.

References Foam::endl(), Foam::fieldNames(), forAll, regionName, UPtrList::set(), List::size(), and WarningInFunction.

Here is the call graph for this function:

◆ initialise()

void initialise ( )
private

Definition at line 723 of file externalCoupledFunctionObject.C.

References Foam::exists(), forAll, Foam::isFile(), UPstream::master(), regionName, Foam::returnReduce(), UPtrList::set(), and List::size().

Here is the call graph for this function:

◆ readColumns()

void readColumns ( const label  nRows,
const label  nColumns,
autoPtr< IFstream > &  masterFilePtr,
List< scalarField > &  data 
) const
private

Read (and distribute) scalar columns from stream. Every processor.

gets nRows (= patch size) of these. Note: could make its argument ISstream& but then would need additional logic to construct valid stream on all processors.

Definition at line 241 of file externalCoupledFunctionObject.C.

References Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, PstreamBuffers::finishedSends(), forAll, globalIndex::localSize(), UPstream::master(), UPstream::masterNo(), UPstream::nonBlocking, UPstream::nProcs(), List::setSize(), and globalIndex::size().

Here is the call graph for this function:

◆ readLines()

void readLines ( const label  nRows,
autoPtr< IFstream > &  masterFilePtr,
OStringStream data 
) const
private

Read (and distribute) lines from stream. Every processor.

gets nRows (= patch size) of these. Data kept as stream (instead of strings) for ease of interfacing to readData routines that take an Istream.

Definition at line 311 of file externalCoupledFunctionObject.C.

References Foam::exit(), Foam::FatalIOError, FatalIOErrorInFunction, PstreamBuffers::finishedSends(), globalIndex::localSize(), UPstream::master(), UPstream::masterNo(), Foam::nl, UPstream::nonBlocking, UPstream::nProcs(), and globalIndex::size().

Here is the call graph for this function:

◆ gatherAndCombine() [1/2]

static tmp<Field<Type> > gatherAndCombine ( const Field< Type > &  fld)
staticprivate

Helper: append data from all processors onto master.

◆ checkOrder()

void checkOrder ( const wordList regionNames)
staticprivate

Definition at line 568 of file externalCoupledFunctionObject.C.

References Foam::exit(), Foam::FatalError, FatalErrorInFunction, Foam::identity(), List::size(), and Foam::sortedOrder().

Here is the call graph for this function:

◆ operator=()

void operator= ( const externalCoupledFunctionObject )
private

Disallow default bitwise assignmen.

◆ TypeName()

TypeName ( "externalCoupled"  )

Runtime type information.

◆ enabled()

virtual bool enabled ( ) const
inlinevirtual

Return the enabled flag.

Definition at line 320 of file externalCoupledFunctionObject.H.

References externalCoupledFunctionObject::enabled_.

◆ on()

void on ( )
virtual

Switch the function object on.

Definition at line 817 of file externalCoupledFunctionObject.C.

◆ off()

void off ( )
virtual

Switch the function object off.

Definition at line 823 of file externalCoupledFunctionObject.C.

◆ start()

bool start ( )
virtual

Called at the start of the time-loop.

Implements functionObject.

Definition at line 829 of file externalCoupledFunctionObject.C.

◆ execute()

bool execute ( const bool  forceWrite)
virtual

Called at each ++ or += of the time-loop.

Implements functionObject.

Definition at line 835 of file externalCoupledFunctionObject.C.

References writeData().

Here is the call graph for this function:

◆ end()

bool end ( )
virtual

Called when Time::run() determines that the time-loop exits.

Reimplemented from functionObject.

Definition at line 873 of file externalCoupledFunctionObject.C.

◆ timeSet()

bool timeSet ( )
virtual

Called when time was set at the end of the Time::operator++.

Reimplemented from functionObject.

Definition at line 887 of file externalCoupledFunctionObject.C.

◆ adjustTimeStep()

bool adjustTimeStep ( )
virtual

Called at the end of Time::adjustDeltaT() if adjustTime is true.

Reimplemented from functionObject.

Definition at line 894 of file externalCoupledFunctionObject.C.

◆ read()

bool read ( const dictionary dict)
virtual

◆ updateMesh()

void updateMesh ( const mapPolyMesh mpm)
virtual

Update for changes of mesh.

Implements functionObject.

Definition at line 1049 of file externalCoupledFunctionObject.C.

◆ movePoints()

void movePoints ( const polyMesh mesh)
virtual

Update for changes of mesh.

Implements functionObject.

Definition at line 1053 of file externalCoupledFunctionObject.C.

◆ compositeName()

Foam::word compositeName ( const wordList regionNames)
static

Create single name by appending words (in sorted order),.

separated by '_'

Definition at line 528 of file externalCoupledFunctionObject.C.

References Foam::abort(), polyMesh::defaultRegion, Foam::FatalError, FatalErrorInFunction, word::null, and List::size().

Here is the call graph for this function:

◆ writeGeometry()

void writeGeometry ( const UPtrList< const fvMesh > &  meshes,
const fileName commsDir,
const wordRe groupName 
)
static

◆ gatherAndCombine() [2/2]

Foam::tmp<Foam::Field<Type> > gatherAndCombine ( const Field< Type > &  fld)

Definition at line 298 of file externalCoupledFunctionObjectTemplates.C.

References fld(), forAll, and List::size().

Here is the call graph for this function:

Field Documentation

◆ time_

const Time& time_
private

Reference to the time database.

Definition at line 150 of file externalCoupledFunctionObject.H.

◆ enabled_

Switch enabled_
private

Switch for the execution - defaults to 'yes/on'.

Definition at line 153 of file externalCoupledFunctionObject.H.

Referenced by externalCoupledFunctionObject::enabled().

◆ commsDir_

fileName commsDir_
private

Path to communications directory.

Definition at line 156 of file externalCoupledFunctionObject.H.

Referenced by externalCoupledFunctionObject::baseDir().

◆ waitInterval_

label waitInterval_
private

Interval time between checking for return data [s].

Definition at line 159 of file externalCoupledFunctionObject.H.

◆ timeOut_

label timeOut_
private

Time out time [s].

Definition at line 162 of file externalCoupledFunctionObject.H.

◆ calcFrequency_

label calcFrequency_
private

Calculation frequency.

Definition at line 165 of file externalCoupledFunctionObject.H.

◆ initByExternal_

bool initByExternal_
private

Flag to indicate values are initialised by external application.

Definition at line 168 of file externalCoupledFunctionObject.H.

◆ log_

bool log_
private

Log flag.

Definition at line 171 of file externalCoupledFunctionObject.H.

◆ regionGroupNames_

DynamicList<word> regionGroupNames_
private

Names of (composite) regions.

Definition at line 174 of file externalCoupledFunctionObject.H.

◆ regionGroupRegions_

DynamicList<wordList> regionGroupRegions_
private

Definition at line 177 of file externalCoupledFunctionObject.H.

◆ regionToGroups_

HashTable<labelList> regionToGroups_
private

Definition at line 180 of file externalCoupledFunctionObject.H.

◆ groupNames_

DynamicList<wordRe> groupNames_
private

Definition at line 183 of file externalCoupledFunctionObject.H.

◆ groupReadFields_

DynamicList<wordList> groupReadFields_
private

Definition at line 186 of file externalCoupledFunctionObject.H.

◆ groupWriteFields_

DynamicList<wordList> groupWriteFields_
private

Definition at line 189 of file externalCoupledFunctionObject.H.

◆ initialised_

bool initialised_
private

Initialised flag.

Definition at line 192 of file externalCoupledFunctionObject.H.

◆ lockName

Foam::word lockName = "OpenFOAM"
static

Name of lock file.

Definition at line 294 of file externalCoupledFunctionObject.H.

◆ patchKey

Foam::string patchKey = "# Patch: "
static

Name of patch key, e.g. '# Patch:' when looking for start of patch data.

Definition at line 297 of file externalCoupledFunctionObject.H.


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