externalCoupledMixedFvPatchField.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) 2013-2016 OpenFOAM Foundation
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 it
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::externalCoupledMixedFvPatchField
26 
27 Group
28  grpGenericBoundaryConditions grpCoupledBoundaryConditions
29 
30 Description
31  This boundary condition provides an interface to an external application.
32  Values are transferred as plain text files, where OpenFOAM data is written
33  as:
34 
35  \verbatim
36  # Patch: <patch name>
37  <magSf1> <value1> <surfaceNormalGradient1>
38  <magSf2> <value2> <surfaceNormalGradient2>
39  <magSf3> <value3> <surfaceNormalGradient3>
40  ...
41  <magSfN> <valueN> <surfaceNormalGradientN>
42  \endverbatim
43 
44  and received as the constituent pieces of the `mixed' condition, i.e.
45 
46  \verbatim
47  # Patch: <patch name>
48  <value1> <gradient1> <valueFracion1>
49  <value2> <gradient2> <valueFracion2>
50  <value3> <gradient3> <valueFracion3>
51  ...
52  <valueN> <gradientN> <valueFracionN>
53  \endverbatim
54 
55  Data is sent/received as a single file for all patches from the directory
56 
57  \verbatim
58  $FOAM_CASE/<commsDir>
59  \endverbatim
60 
61  At start-up, the boundary creates a lock file, i.e..
62 
63  \verbatim
64  OpenFOAM.lock
65  \endverbatim
66 
67  ... to signal the external source to wait. During the boundary condition
68  update, boundary values are written to file, e.g.
69 
70  \verbatim
71  <fileName>.out
72  \endverbatim
73 
74  The lock file is then removed, instructing the external source to take
75  control of the program execution. When ready, the external program
76  should create the return values, e.g. to file
77 
78  \verbatim
79  <fileName>.in
80  \endverbatim
81 
82  ... and then re-instate the lock file. The boundary condition will then
83  read the return values, and pass program execution back to OpenFOAM.
84 
85 
86 Usage
87  \table
88  Property | Description | Required | Default value
89  commsDir | communications directory | yes |
90  fileName | transfer file name | yes |
91  waitInterval | interval [s] between file checks | no | 1
92  timeOut | time after which error invoked [s] |no |100*waitInterval
93  calcFrequency | calculation frequency | no | 1
94  initByExternal | external app to initialises values | yes |
95  log | log program control | no | no
96  \endtable
97 
98  Example of the boundary condition specification:
99  \verbatim
100  <patchName>
101  {
102  type externalCoupled;
103  commsDir "$FOAM_CASE/comms";
104  fileName data;
105  calcFrequency 1;
106  initByExternal yes;
107  }
108  \endverbatim
109 
110 See also
111  mixedFvPatchField
112 
113 SourceFiles
114  externalCoupledMixedFvPatchField.C
115 
116 \*---------------------------------------------------------------------------*/
117 
118 #ifndef externalCoupledMixedFvPatchField_H
119 #define externalCoupledMixedFvPatchField_H
120 
121 #include "mixedFvPatchFields.H"
122 #include "OFstream.H"
123 
124 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
125 
126 namespace Foam
127 {
128 
129 class IFstream;
130 
131 /*---------------------------------------------------------------------------*\
132  Class externalCoupledMixedFvPatchField Declaration
133 \*---------------------------------------------------------------------------*/
134 
135 template<class Type>
136 class externalCoupledMixedFvPatchField
137 :
138  public mixedFvPatchField<Type>
139 {
140 
141 private:
142 
143  // Private data
144 
145  //- Convenience typedefs
146  typedef externalCoupledMixedFvPatchField<Type> patchType;
147  typedef GeometricField<Type, fvPatchField, volMesh> volFieldType;
148 
149  //- Path to communications directory
150  fileName commsDir_;
151 
152  //- Name of data file
153  word fName_;
154 
155  //- Interval time between checking for return data [s]
156  label waitInterval_;
157 
158  //- Time out time [s]
159  label timeOut_;
160 
161  //- Calculation frequency
162  label calcFrequency_;
163 
164  //- Flag to indicate values are initialised by external application
165  bool initByExternal_;
166 
167  //- Log flag
168  bool log_;
169 
170  //- Master patch flag - controls when to pause/resume execution
171  // Note: only valid when collate option is selected
172  bool master_;
173 
174  //- Offsets in data file to start reading at correct position
175  List<List<label>> offsets_;
176 
177  //- Initialised flag
178  bool initialised_;
179 
180  //- List of coupled patch IDs
181  List<label> coupledPatchIDs_;
182 
183 
184  // Private Member Functions
185 
186  //- Initialise
187  void initialise(const fileName& transferFile);
188 
189  //- Set the master flag when collate option is selected
190  void setMaster(const labelList& patchIDs);
191 
192  //- Return the file path to the base communications directory
193  fileName baseDir() const;
194 
195  //- Write the geometry to the comms dir
196  void writeGeometry(OFstream& osPoints, OFstream& osFaces) const;
197 
198  //- Return the file path to the lock file
199  fileName lockFile() const;
200 
201  //- Create lock file
202  void createLockFile() const;
203 
204  //- Remove lock file
205  void removeLockFile() const;
206 
207  //- Wait for response from external source
208  void startWait() const;
209 
210  //- Wait for response from external source
211  void wait() const;
212 
213  //- Initialise input stream for reading
214  void initialiseRead(IFstream& is) const;
215 
216 
217 protected:
218 
219  // Protected Member Functions
220 
221  //- Read data from external source
222  virtual void readData(const fileName& transferFile);
223 
224  //- Write data for external source - calls transferData
225  virtual void writeData(const fileName& transferFile) const;
226 
227  //- Write header to transfer file
228  virtual void writeHeader(OFstream& os) const;
229 
230 
231 public:
232 
233  //- Runtime type information
234  TypeName("externalCoupled");
235 
236  //- Name of lock file
237  static word lockName;
238 
239  //- Name of patch key, e.g. '# Patch:' when looking for start of patch data
240  static string patchKey;
241 
242 
243  // Constructors
244 
245  //- Construct from patch and internal field
247  (
248  const fvPatch&,
250  );
251 
252  //- Construct from patch, internal field and dictionary
254  (
255  const fvPatch&,
257  const dictionary&
258  );
259 
260  //- Construct by mapping given externalCoupledMixedFvPatchField
261  // onto a new patch
263  (
265  const fvPatch&,
267  const fvPatchFieldMapper&
268  );
269 
270  //- Construct as copy
272  (
274  );
275 
276  //- Construct and return a clone
277  virtual tmp<fvPatchField<Type>> clone() const
278  {
280  (
282  );
283  }
284 
285  //- Construct as copy setting internal field reference
287  (
290  );
291 
292  //- Construct and return a clone setting internal field reference
294  (
296  ) const
297  {
298  return tmp<fvPatchField<Type>>
299  (
301  );
302  }
303 
304 
305  //- Destructor
307 
308 
309  // Member functions
310 
311  // Access
312 
313  //- Return the log flag
314  bool log() const
315  {
316  return log_;
317  }
318 
319  //- Return the master flag
320  bool master() const
321  {
322  return master_;
323  }
324 
325  //- Return the master flag
326  bool& master()
327  {
328  return master_;
329  }
330 
331 
332  // Evaluation functions
334  //- Evaluate the patch field
335  virtual void evaluate
336  (
337  const Pstream::commsTypes commsType=Pstream::blocking
338  );
339 
340  //- Transfer data for external source
341  virtual void transferData(OFstream& os) const;
342 
343 
344  //- Write the geometry to the comms dir
345  void writeGeometry() const;
346 
347  //- Write
348  virtual void write(Ostream&) const;
349 };
350 
351 
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 } // End namespace Foam
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 #ifdef NoRepository
360 #endif
361 
362 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
363 
364 #endif
366 // ************************************************************************* //
This boundary condition provides an interface to an external application. Values are transferred as p...
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
A class for handling file names.
Definition: fileName.H:69
const word & patchType() const
Optional patch type.
Definition: fvPatchField.H:358
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
commsTypes
Types of communications.
Definition: UPstream.H:64
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Output to file stream.
Definition: OFstream.H:81
A finiteVolume patch using a polyPatch and a fvBoundaryMesh.
Definition: fvPatch.H:61
virtual void evaluate(const Pstream::commsTypes commsType=Pstream::blocking)
Evaluate the patch field.
TypeName("externalCoupled")
Runtime type information.
void writeGeometry() const
Write the geometry to the comms dir.
A class for handling words, derived from string.
Definition: word.H:59
static string patchKey
Name of patch key, e.g. &#39;# Patch:&#39; when looking for start of patch data.
Foam::fvPatchFieldMapper.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Input from file stream.
Definition: IFstream.H:81
virtual tmp< fvPatchField< Type > > clone() const
Construct and return a clone.
virtual void readData(const fileName &transferFile)
Read data from external source.
virtual void writeHeader(OFstream &os) const
Write header to transfer file.
bool master() const
Return the master flag.
externalCoupledMixedFvPatchField(const fvPatch &, const DimensionedField< Type, volMesh > &)
Construct from patch and internal field.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
A class for managing temporary objects.
Definition: PtrList.H:54
virtual void writeData(const fileName &transferFile) const
Write data for external source - calls transferData.
virtual void transferData(OFstream &os) const
Transfer data for external source.
Namespace for OpenFOAM.