externalCoupledFunctionObject.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2015 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify i
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::externalCoupledFunctionObject
26 
27 Group
28  grpJobControlFunctionObjects
29 
30 Description
31  This functionObject provides a simple interface for explicit coupling with
32  an external application. The coupling is through plain text files
33  where OpenFOAM boundary data is read/written as one line per face
34  (data from all processors collated):
35 
36  # Patch: <patch name>
37  <fld1> <fld2> .. <fldn> //face0
38  <fld1> <fld2> .. <fldn> //face1
39  ..
40  <fld1> <fld2> .. <fldn> //faceN
41 
42  where the actual entries depend on the bc type:
43  - mixed: value, snGrad, refValue, refGrad, valueFraction
44  - externalCoupledMixed: output of writeData
45  - other: value, snGrad
46 
47  These text files are located in a user specified communications directory
48  which gets read/written on the master processor only. In the
49  communications directory the structure will be
50 
51  <regionsName>/<patchGroup>/<fieldName>.[in|out]
52 
53  (where regionsName is either the name of a single region or a composite
54  of multiple region names)
55 
56  At start-up, the boundary creates a lock file, i.e..
57 
58  OpenFOAM.lock
59 
60  ... to signal the external source to wait. During the functionObject
61  execution the boundary values are written to files (one per region,
62  per patch(group), per field), e.g.
63 
64  <regionsName>/<patchGroup>/<fieldName>.out
65 
66  The lock file is then removed, instructing the external source to take
67  control of the program execution. When ready, the external program
68  should create the return values, e.g. to files
69 
70  <regionsName>/<patchGroup>/<fieldName>.in
71 
72  ... and then re-instate the lock file. The functionObject will then
73  read these values, apply them to the boundary conditions and pass
74  program execution back to OpenFOAM.
75 
76  Example of function object specification:
77  \verbatim
78  externalCoupled
79  {
80  type externalCoupled;
81  ...
82  log yes;
83  commsDir "${FOAM_CASE}/comms";
84  initByExternal yes;
85 
86  regions
87  {
88  "(region1|region0)" // Name of region(s)
89  {
90  TPatchGroup // Name of patch(group)
91  {
92  readFields (p); // List of fields to read
93  writeFields (T); // List of fields to write
94  }
95  }
96  }
97  }
98  \endverbatim
99 
100  This reads/writes (on the master processor) the directory:
101  comms/region0_region1/TPatchGroup/
102  with contents:
103  patchPoints (collected points)
104  patchFaces (collected faces)
105  p.in (input file of p, written by external application)
106  T.out (output file of T, written by OpenFOAM)
107 
108  The patchPoints/patchFaces files denote the (collated) geometry
109  which will be written if it does not exist yet or can be written as
110  a preprocessing step using the createExternalCoupledPatchGeometry
111  application.
112 
113 SourceFiles
114  externalCoupledFunctionObject.C
115 
116 \*---------------------------------------------------------------------------*/
117 
118 #ifndef externalCoupledFunctionObject_H
119 #define externalCoupledFunctionObject_H
120 
121 #include "functionObject.H"
122 #include "DynamicList.H"
123 #include "wordReList.H"
124 #include "scalarField.H"
125 #include "Switch.H"
126 #include "UPtrList.H"
127 
128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
129 
130 namespace Foam
131 {
132 
133 // Forward declaration of classes
134 class dictionary;
135 class polyMesh;
136 class mapPolyMesh;
137 class IFstream;
138 class fvMesh;
139 
140 /*---------------------------------------------------------------------------*\
141  Class externalCoupledFunctionObject Declaration
142 \*---------------------------------------------------------------------------*/
143 
145 :
146  public functionObject
147 {
148  // Private data
149 
150  //- Reference to the time database
151  const Time& time_;
152 
153  //- Switch for the execution - defaults to 'yes/on'
155 
156  //- Path to communications directory
158 
159  //- Interval time between checking for return data [s]
161 
162  //- Time out time [s]
163  label timeOut_;
164 
165  //- Calculation frequency
167 
168  //- Flag to indicate values are initialised by external application
169  bool initByExternal_;
170 
171  //- Log flag
172  bool log_;
173 
174  //- Names of (composite) regions
176 
177  // Per (composite) region the names of the regions
179 
180  // Per (composite) region the indices of the group information
182 
183  // Per group the names of the patches/patchGroups
185 
186  // Per group the names of the fields to read
188 
189  // Per group the names of the fields to write
191 
192  //- Initialised flag
193  bool initialised_;
194 
195 
196  // Private Member Functions
197 
198  //- Return the file path to the communications directory for the region
199  static fileName groupDir
200  (
201  const fileName& commsDir,
202  const word& regionsName,
203  const wordRe& groupName
204  );
205 
206  //- Return the file path to the base communications directory
207  fileName baseDir() const;
208 
209  //- Return the file path to the lock file
210  fileName lockFile() const;
211 
212  //- Create lock file
213  void createLockFile() const;
214 
215  //- Remove lock file
216  void removeLockFile() const;
217 
218  //- Remove files written by OpenFOAM
219  void removeWriteFiles() const;
220 
221  //- Remove files written by external code
222  void removeReadFiles() const;
223 
224  //- Wait for response from external source
225  void wait() const;
226 
227 
228  //- Read data for a single region, single field
229  template<class Type>
230  bool readData
231  (
232  const UPtrList<const fvMesh>& meshes,
233  const wordRe& groupName,
234  const word& fieldName
235  );
236  //- Read data for all regions, all fields
237  void readData();
238 
239  //- Write data for a single region, single field
240  template<class Type>
241  bool writeData
242  (
243  const UPtrList<const fvMesh>& meshes,
244  const wordRe& groupName,
245  const word& fieldName
246  ) const;
247 
248  //- Write data for all regions, all fields
249  void writeData() const;
250 
251  void initialise();
252 
253  //- Read (and distribute) scalar columns from stream. Every processor
254  // gets nRows (= patch size) of these. Note: could make its argument
255  // ISstream& but then would need additional logic to construct valid
256  // stream on all processors.
257  void readColumns
258  (
259  const label nRows,
260  const label nColumns,
261  autoPtr<IFstream>& masterFilePtr,
263  ) const;
264 
265  //- Read (and distribute) lines from stream. Every processor
266  // gets nRows (= patch size) of these. Data kept as stream (instead
267  // of strings) for ease of interfacing to readData routines that take
268  // an Istream.
269  void readLines
270  (
271  const label nRows,
272  autoPtr<IFstream>& masterFilePtr,
274  ) const;
275 
276  //- Helper: append data from all processors onto master
277  template<class Type>
279 
280  static void checkOrder(const wordList&);
281 
282  //- Disallow default bitwise copy construc
284 
285  //- Disallow default bitwise assignmen
287 
288 
289 public:
290 
291  //- Runtime type information
292  TypeName("externalCoupled");
293 
294  //- Name of lock file
295  static word lockName;
296 
297  //- Name of patch key, e.g. '# Patch:' when looking for start of patch data
298  static string patchKey;
299 
300 
301  // Constructors
302 
303  //- Construct given time and dictionary
305  (
306  const word& name,
307  const Time& runTime,
308  const dictionary& dict
309  );
310 
311 
312  //- Destructor
314 
315 
316  // Member Functions
317 
318  // Access
319 
320  //- Return the enabled flag
321  virtual bool enabled() const
322  {
323  return enabled_;
324  }
325 
326 
327  // Function object control
328 
329  //- Switch the function object on
330  virtual void on();
331 
332  //- Switch the function object off
333  virtual void off();
334 
335  //- Called at the start of the time-loop
336  virtual bool start();
337 
338  //- Called at each ++ or += of the time-loop
339  virtual bool execute(const bool forceWrite);
340 
341  //- Called when Time::run() determines that the time-loop exits
342  virtual bool end();
343 
344  //- Called when time was set at the end of the Time::operator++
345  virtual bool timeSet();
346 
347  //- Called at the end of Time::adjustDeltaT() if adjustTime is true
348  virtual bool adjustTimeStep();
349 
350  //- Read and set the function object if its data have changed
351  virtual bool read(const dictionary&);
352 
353  //- Update for changes of mesh
354  virtual void updateMesh(const mapPolyMesh& mpm);
355 
356  //- Update for changes of mesh
357  virtual void movePoints(const polyMesh& mesh);
358 
359 
360  // Other
361 
362  //- Create single name by appending words (in sorted order),
363  // separated by '_'
364  static word compositeName(const wordList&);
365 
366  //- Write geometry for the group/patch
367  static void writeGeometry
368  (
369  const UPtrList<const fvMesh>& meshes,
370  const fileName& commsDir,
371  const wordRe& groupName
372  );
373 };
374 
375 
376 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
377 
378 } // End namespace Foam
379 
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #ifdef NoRepository
384 #endif
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #endif
389 
390 // ************************************************************************* //
Foam::externalCoupledFunctionObject::writeData
void writeData() const
Write data for all regions, all fields.
Definition: externalCoupledFunctionObject.C:653
Foam::externalCoupledFunctionObject::regionToGroups_
HashTable< labelList > regionToGroups_
Definition: externalCoupledFunctionObject.H:180
UPtrList.H
Foam::Switch
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:60
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::externalCoupledFunctionObject::groupReadFields_
DynamicList< wordList > groupReadFields_
Definition: externalCoupledFunctionObject.H:186
Foam::externalCoupledFunctionObject::createLockFile
void createLockFile() const
Create lock file.
Definition: externalCoupledFunctionObject.C:91
Foam::externalCoupledFunctionObject::timeSet
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
Definition: externalCoupledFunctionObject.C:887
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:118
Foam::externalCoupledFunctionObject::log_
bool log_
Log flag.
Definition: externalCoupledFunctionObject.H:171
scalarField.H
Foam::DynamicList< word >
Foam::externalCoupledFunctionObject::readLines
void readLines(const label nRows, autoPtr< IFstream > &masterFilePtr, OStringStream &data) const
Read (and distribute) lines from stream. Every processor.
Definition: externalCoupledFunctionObject.C:311
Foam::externalCoupledFunctionObject::readColumns
void readColumns(const label nRows, const label nColumns, autoPtr< IFstream > &masterFilePtr, List< scalarField > &data) const
Read (and distribute) scalar columns from stream. Every processor.
Definition: externalCoupledFunctionObject.C:241
Foam::externalCoupledFunctionObject::baseDir
fileName baseDir() const
Return the file path to the base communications directory.
Definition: externalCoupledFunctionObject.C:57
Foam::externalCoupledFunctionObject
This functionObject provides a simple interface for explicit coupling with an external application....
Definition: externalCoupledFunctionObject.H:143
Foam::externalCoupledFunctionObject::removeWriteFiles
void removeWriteFiles() const
Remove files written by OpenFOAM.
Definition: externalCoupledFunctionObject.C:159
Foam::externalCoupledFunctionObject::commsDir_
fileName commsDir_
Path to communications directory.
Definition: externalCoupledFunctionObject.H:156
Foam::externalCoupledFunctionObject::lockName
static word lockName
Name of lock file.
Definition: externalCoupledFunctionObject.H:294
Foam::externalCoupledFunctionObject::gatherAndCombine
static tmp< Field< Type > > gatherAndCombine(const Field< Type > &fld)
Helper: append data from all processors onto master.
Foam::externalCoupledFunctionObject::execute
virtual bool execute(const bool forceWrite)
Called at each ++ or += of the time-loop.
Definition: externalCoupledFunctionObject.C:835
Foam::externalCoupledFunctionObject::start
virtual bool start()
Called at the start of the time-loop.
Definition: externalCoupledFunctionObject.C:829
Foam::externalCoupledFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: externalCoupledFunctionObject.C:900
Foam::wordRe
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:58
Foam::externalCoupledFunctionObject::groupNames_
DynamicList< wordRe > groupNames_
Definition: externalCoupledFunctionObject.H:183
Foam::externalCoupledFunctionObject::removeReadFiles
void removeReadFiles() const
Remove files written by external code.
Definition: externalCoupledFunctionObject.C:126
Foam::externalCoupledFunctionObject::wait
void wait() const
Wait for response from external source.
Definition: externalCoupledFunctionObject.C:192
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Field< Type >
Foam::externalCoupledFunctionObject::patchKey
static string patchKey
Name of patch key, e.g. '# Patch:' when looking for start of patch data.
Definition: externalCoupledFunctionObject.H:297
Foam::externalCoupledFunctionObject::operator=
void operator=(const externalCoupledFunctionObject &)
Disallow default bitwise assignmen.
Foam::externalCoupledFunctionObject::initialised_
bool initialised_
Initialised flag.
Definition: externalCoupledFunctionObject.H:192
Foam::externalCoupledFunctionObject::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: externalCoupledFunctionObject.C:894
Foam::externalCoupledFunctionObject::calcFrequency_
label calcFrequency_
Calculation frequency.
Definition: externalCoupledFunctionObject.H:165
Switch.H
Foam::UPtrList
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:53
externalCoupledFunctionObjectTemplates.C
Foam::externalCoupledFunctionObject::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: externalCoupledFunctionObject.C:1049
Foam::externalCoupledFunctionObject::regionGroupRegions_
DynamicList< wordList > regionGroupRegions_
Definition: externalCoupledFunctionObject.H:177
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::externalCoupledFunctionObject::waitInterval_
label waitInterval_
Interval time between checking for return data [s].
Definition: externalCoupledFunctionObject.H:159
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
fld
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< ' ';}gmvFile<< nl;forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Foam::externalCoupledFunctionObject::regionGroupNames_
DynamicList< word > regionGroupNames_
Names of (composite) regions.
Definition: externalCoupledFunctionObject.H:174
Foam::externalCoupledFunctionObject::enabled
virtual bool enabled() const
Return the enabled flag.
Definition: externalCoupledFunctionObject.H:320
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::externalCoupledFunctionObject::groupDir
static fileName groupDir(const fileName &commsDir, const word &regionsName, const wordRe &groupName)
Return the file path to the communications directory for the region.
Definition: externalCoupledFunctionObject.C:67
Foam::externalCoupledFunctionObject::timeOut_
label timeOut_
Time out time [s].
Definition: externalCoupledFunctionObject.H:162
Foam::externalCoupledFunctionObject::initByExternal_
bool initByExternal_
Flag to indicate values are initialised by external application.
Definition: externalCoupledFunctionObject.H:168
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::externalCoupledFunctionObject::writeGeometry
static void writeGeometry(const UPtrList< const fvMesh > &meshes, const fileName &commsDir, const wordRe &groupName)
Write geometry for the group/patch.
Definition: externalCoupledFunctionObject.C:373
Foam::externalCoupledFunctionObject::checkOrder
static void checkOrder(const wordList &)
Definition: externalCoupledFunctionObject.C:568
Foam::externalCoupledFunctionObject::externalCoupledFunctionObject
externalCoupledFunctionObject(const externalCoupledFunctionObject &)
Disallow default bitwise copy construc.
Foam::externalCoupledFunctionObject::~externalCoupledFunctionObject
virtual ~externalCoupledFunctionObject()
Destructor.
Definition: externalCoupledFunctionObject.C:811
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::externalCoupledFunctionObject::TypeName
TypeName("externalCoupled")
Runtime type information.
Foam::externalCoupledFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: externalCoupledFunctionObject.H:150
Foam::externalCoupledFunctionObject::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
Definition: externalCoupledFunctionObject.C:1053
Foam::functionObject::name
virtual const word & name() const
Name.
Definition: functionObject.C:105
Foam::externalCoupledFunctionObject::lockFile
fileName lockFile() const
Return the file path to the lock file.
Definition: externalCoupledFunctionObject.C:85
wordReList.H
Foam::List< scalarField >
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::externalCoupledFunctionObject::groupWriteFields_
DynamicList< wordList > groupWriteFields_
Definition: externalCoupledFunctionObject.H:189
Foam::externalCoupledFunctionObject::enabled_
Switch enabled_
Switch for the execution - defaults to 'yes/on'.
Definition: externalCoupledFunctionObject.H:153
Foam::externalCoupledFunctionObject::compositeName
static word compositeName(const wordList &)
Create single name by appending words (in sorted order),.
Definition: externalCoupledFunctionObject.C:528
Foam::externalCoupledFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: externalCoupledFunctionObject.C:873
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
DynamicList.H
Foam::externalCoupledFunctionObject::on
virtual void on()
Switch the function object on.
Definition: externalCoupledFunctionObject.C:817
functionObject.H
Foam::externalCoupledFunctionObject::initialise
void initialise()
Definition: externalCoupledFunctionObject.C:723
Foam::externalCoupledFunctionObject::removeLockFile
void removeLockFile() const
Remove lock file.
Definition: externalCoupledFunctionObject.C:113
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
Foam::externalCoupledFunctionObject::readData
void readData()
Read data for all regions, all fields.
Definition: externalCoupledFunctionObject.C:583
Foam::externalCoupledFunctionObject::off
virtual void off()
Switch the function object off.
Definition: externalCoupledFunctionObject.C:823