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