externalCoupled.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify i
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::functionObjects::externalCoupled
28 
29 Group
30  grpFieldFunctionObjects
31 
32 Description
33  Provides a simple file-based communication interface for explicit coupling
34  with an external application, so that data is transferred to- and from
35  OpenFOAM. The data exchange employs specialised boundary conditions to
36  provide either one-way or two-way coupling models.
37 
38  The coupling is through plain text files where OpenFOAM boundary data
39  is read/written as one line per face (data from all processors collated):
40  \verbatim
41  # Patch: <patch name>
42  <fld1> <fld2> .. <fldn> //face0
43  <fld1> <fld2> .. <fldn> //face1
44  ..
45  <fld1> <fld2> .. <fldn> //faceN
46  \endverbatim
47 
48  where the actual entries depend on the boundary condition type:
49  - mixed: value, snGrad, refValue, refGrad, valueFraction
50  - externalCoupledMixed: output of writeDataMaster
51  - other: value, snGrad
52 
53  These text files are located in a user specified communications directory
54  which gets read/written on the master processor only.
55 
56  In the communications directory the structure will be:
57  \verbatim
58  <regionsName>/<patchGroup>/<fieldName>.[in|out]
59  \endverbatim
60 
61  (where \c regionsName is either the name of a single region or a composite
62  of multiple region names)
63 
64  At start-up, the boundary creates a lock file, i.e.:
65  \verbatim
66  OpenFOAM.lock
67  \endverbatim
68 
69  ... to signal the external source to wait. During the function object
70  execution the boundary values are written to files (one per region,
71  per patch(group), per field), e.g.
72  \verbatim
73  <regionsName>/<patchGroup>/<fieldName>.out
74  \endverbatim
75 
76  The lock file is then removed, instructing the external source to take
77  control of the program execution. When ready, the external program
78  should create the return values, e.g. to files
79  \verbatim
80  <regionsName>/<patchGroup>/<fieldName>.in
81  \endverbatim
82 
83  ... and then reinstate the lock file. The function object will then
84  read these values, apply them to the boundary conditions and pass
85  program execution back to OpenFOAM.
86 
87 Usage
88  Minimal example by using \c system/controlDict.functions:
89  \verbatim
90  externalCoupled1
91  {
92  // Mandatory entries (unmodifiable)
93  type externalCoupled;
94  libs (fieldFunctionObjects);
95  commsDir "<case>/comms";
96  regions
97  {
98  "(region1|region0)" // Name of region(s)
99  {
100  TPatchGroup // Name of patch(group)
101  {
102  readFields (p); // List of fields to read
103  writeFields (T); // List of fields to write
104  }
105  }
106  }
107  initByExternal true;
108 
109  // Optional entries (runtime modifiable)
110  waitInterval 1;
111  timeOut 100;
112  statusDone done; // Any arbitrary status=... value
113  calcFrequency 1;
114 
115  // Optional (inherited) entries
116  ...
117  }
118  \endverbatim
119 
120  This reads/writes (on the master processor) the directory:
121  \verbatim
122  comms/region0_region1/TPatchGroup/
123  \endverbatim
124 
125  with contents:
126  \verbatim
127  patchPoints (collected points)
128  patchFaces (collected faces)
129  p.in (input file of p, written by external application)
130  T.out (output file of T, written by OpenFOAM)
131  \endverbatim
132 
133  The patchPoints/patchFaces files denote the (collated) geometry
134  which will be written if it does not exist yet or can be written as
135  a preprocessing step using the createExternalCoupledPatchGeometry
136  application.
137 
138  The entries comprise:
139  \table
140  Property | Description | Type | Req'd | Dflt
141  type | Type name: externalCoupled | word | yes | -
142  libs | Library name: fieldFunctionObjects | word | yes | -
143  commsDir | Communication directory | word | yes | -
144  regions | The regions to couple | word | yes | -
145  initByExternal | Initialization values supplied by external app <!--
146  --> | bool | yes | -
147  waitInterval | Wait interval in [s] | label | no | 1
148  timeOut | Timeout in [s] | label | no | 100*waitInterval
149  statusDone | Lockfile status=... on termination | word | no | done
150  calcFrequency | Calculation frequency | label | no | 1
151  \endtable
152 
153  The inherited entries are elaborated in:
154  - \link timeFunctionObject.H \endlink
155 
156  Usage by the \c postProcess utility is not available.
157 
158 See also
159  - Foam::functionObject
160  - Foam::functionObjects::timeFunctionObject
161  - Foam::externalFileCouple
162  - ExtendedCodeGuide::functionObjects::field::externalCoupled
163 
164 SourceFiles
165  externalCoupled.C
166  externalCoupledTemplates.C
167 
168 \*---------------------------------------------------------------------------*/
169 
170 #ifndef Foam_functionObjects_externalCoupled_H
171 #define Foam_functionObjects_externalCoupled_H
172 
173 #include "timeFunctionObject.H"
174 #include "externalFileCoupler.H"
175 #include "DynamicList.H"
176 #include "wordRes.H"
177 #include "scalarField.H"
178 #include "UPtrList.H"
179 
180 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
181 
182 namespace Foam
183 {
184 
185 // Forward Declarations
186 class fvMesh;
187 class IFstream;
188 
189 namespace functionObjects
190 {
191 
192 /*---------------------------------------------------------------------------*\
193  Class externalCoupled Declaration
194 \*---------------------------------------------------------------------------*/
195 
196 class externalCoupled
197 :
198  public functionObjects::timeFunctionObject,
199  public externalFileCoupler
200 {
201  // Private Member Data
202 
203  //- Calculation frequency
204  label calcFrequency_;
205 
206  //- The last timeIndex when coupling was triggered
207  label lastTrigger_;
208 
209  //- Names of (composite) regions
210  DynamicList<word> regionGroupNames_;
211 
212  // Per (composite) region the names of the regions
213  DynamicList<wordList> regionGroupRegions_;
214 
215  // Per (composite) region the indices of the group information
216  HashTable<labelList> regionToGroups_;
217 
218  // Per group the names of the patches/patchGroups
219  DynamicList<wordRe> groupNames_;
220 
221  // Per group the names of the fields to read
222  DynamicList<wordList> groupReadFields_;
223 
224  // Per group the names of the fields to write
225  DynamicList<wordList> groupWriteFields_;
226 
227  //- Initialised coupling
228  bool initialisedCoupling_;
229 
230 
231  // Private Member Functions
232 
233  //- Return the file path to the communications directory for the region
234  static fileName groupDir
235  (
236  const fileName& commsDir,
237  const word& regionsName,
238  const wordRe& groupName
239  );
240 
241  //- Read data for a single region, single field
242  template<class Type>
243  bool readData
244  (
245  const UPtrList<const fvMesh>& meshes,
246  const wordRe& groupName,
247  const word& fieldName
248  );
249 
250  //- Write data for a single region, single field
251  template<class Type>
252  bool writeData
253  (
255  const wordRe& groupName,
256  const word& fieldName
257  ) const;
258 
259  void initCoupling();
260 
261  //- Read (and distribute) scalar columns from stream.
262  //- Every processor gets nRows (= patch size) of these.
263  // Note: could make its argument
264  // ISstream& but then would need additional logic to construct valid
265  // stream on all processors.
266  void readColumns
267  (
268  const label nRows,
269  const label nColumns,
270  autoPtr<IFstream>& masterFilePtr,
271  List<scalarField>& data
272  ) const;
273 
274  //- Read (and distribute) lines from stream.
275  //- Every processor gets nRows (= patch size) of these
276  //- (newline terminated).
277  void readLines
278  (
279  const label nRows,
280  autoPtr<IFstream>& masterFilePtr,
282  std::string& lines
283  ) const;
284 
285  static void checkOrder(const wordList& regionNames);
286 
287  //- Perform the coupling with necessary initialization etc.
288  void performCoupling();
289 
290 
291 public:
292 
293  //- Runtime type information
294  TypeName("externalCoupled");
295 
296  //- Name of patch key, e.g. '// Patch:' when looking for start of patch data
297  static string patchKey;
298 
299  //- Inherited variable for logging
300  using functionObject::log;
301 
302 
303  // Constructors
304 
305  //- Construct given time and dictionary
307  (
308  const word& name,
309  const Time& runTime,
310  const dictionary& dict
311  );
312 
313  //- No copy construct
314  externalCoupled(const externalCoupled&) = delete;
315 
316  //- No copy assignment
317  void operator=(const externalCoupled&) = delete;
318 
319 
320  //- Destructor
321  virtual ~externalCoupled() = default;
322 
323 
324  // Member Functions
325 
326  // Function object control
327 
328  //- Called at each ++ or += of the time-loop
329  virtual bool execute();
330 
331  //- Manual execute (sub-loop or when converged)
332  virtual bool execute(const label subIndex);
333 
334  //- Called when Time::run() determines that the time-loop exits
335  virtual bool end();
336 
337  //- Read and set the function object if its data have changed
338  virtual bool read(const dictionary& dict);
339 
340  //- Write, currently a no-op
341  virtual bool write();
342 
343 
344  // File creation, removal
345 
346  //- Write data files (all regions, all fields) from master (OpenFOAM)
347  virtual void writeDataMaster() const;
348 
349  //- Read data files (all regions, all fields) on master (OpenFOAM)
350  virtual void readDataMaster();
351 
352  //- Remove data files written by master (OpenFOAM)
353  virtual void removeDataMaster() const;
354 
355  //- Remove data files written by slave (external code)
356  virtual void removeDataSlave() const;
357 
358 
359  // Other
360 
361  //- Create single name by appending words (in sorted order),
362  //- separated by '_'
363  static word compositeName(const wordList&);
364 
365  //- Write geometry for the group as region/patch
366  static void writeGeometry
367  (
369  const fileName& commsDir,
370  const wordRe& groupName
371  );
372 };
373 
374 
375 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376 
377 } // End namespace functionObjects
378 } // End namespace Foam
380 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
381 
382 #ifdef NoRepository
383  #include "externalCoupledTemplates.C"
384 #endif
385 
386 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
387 
388 #endif
389 
390 // ************************************************************************* //
virtual bool end()
Called when Time::run() determines that the time-loop exits.
dictionary dict
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
virtual bool write()
Write, currently a no-op.
A class for handling file names.
Definition: fileName.H:72
virtual bool execute()
Called at each ++ or += of the time-loop.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
engineTime & runTime
static string patchKey
Name of patch key, e.g. &#39;// Patch:&#39; when looking for start of patch data.
static void writeGeometry(const UPtrList< const fvMesh > &meshes, const fileName &commsDir, const wordRe &groupName)
Write geometry for the group as region/patch.
wordList regionNames
Provides a simple file-based communication interface for explicit coupling with an external applicati...
virtual void removeDataSlave() const
Remove data files written by slave (external code)
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
const word & name() const noexcept
Return the name of this functionObject.
externalCoupled(const word &name, const Time &runTime, const dictionary &dict)
Construct given time and dictionary.
TypeName("externalCoupled")
Runtime type information.
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::PtrList< Foam::fvMesh > meshes(regionNames.size())
bool log
Flag to write log into Info.
virtual void removeDataMaster() const
Remove data files written by master (OpenFOAM)
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
virtual void writeDataMaster() const
Write data files (all regions, all fields) from master (OpenFOAM)
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
virtual ~externalCoupled()=default
Destructor.
void operator=(const externalCoupled &)=delete
No copy assignment.
virtual void readDataMaster()
Read data files (all regions, all fields) on master (OpenFOAM)
static word compositeName(const wordList &)
Create single name by appending words (in sorted order), separated by &#39;_&#39;.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Namespace for OpenFOAM.