Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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
26Class
27 Foam::functionObjects::externalCoupled
28
29Group
30 grpFieldFunctionObjects
31
32Description
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
87Usage
88 Minimal example by using \c system/controlDict.functions:
89 \verbatim
90 externalCoupledFO
91 {
92 // Mandatory entries
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
110 waitInterval 1;
111 timeOut 100;
112 statusDone done; // Any arbitrary status=... value
113 calcFrequency 1;
114
115 // 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 | Reqd | Deflt
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
156SourceFiles
157 externalCoupled.C
158 externalCoupledTemplates.C
159
160\*---------------------------------------------------------------------------*/
161
162#ifndef Foam_functionObjects_externalCoupled_H
163#define Foam_functionObjects_externalCoupled_H
164
165#include "timeFunctionObject.H"
166#include "externalFileCoupler.H"
167#include "DynamicList.H"
168#include "wordRes.H"
169#include "scalarField.H"
170#include "UPtrList.H"
171
172// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
173
174namespace Foam
175{
176
177// Forward Declarations
178class fvMesh;
179class IFstream;
180
181namespace functionObjects
182{
183
184/*---------------------------------------------------------------------------*\
185 Class externalCoupled Declaration
186\*---------------------------------------------------------------------------*/
187
188class externalCoupled
189:
190 public functionObjects::timeFunctionObject,
191 public externalFileCoupler
192{
193 // Private Member Data
194
195 //- Calculation frequency
196 label calcFrequency_;
197
198 //- The last timeIndex when coupling was triggered
199 label lastTrigger_;
200
201 //- Names of (composite) regions
202 DynamicList<word> regionGroupNames_;
203
204 // Per (composite) region the names of the regions
205 DynamicList<wordList> regionGroupRegions_;
206
207 // Per (composite) region the indices of the group information
208 HashTable<labelList> regionToGroups_;
209
210 // Per group the names of the patches/patchGroups
211 DynamicList<wordRe> groupNames_;
212
213 // Per group the names of the fields to read
214 DynamicList<wordList> groupReadFields_;
215
216 // Per group the names of the fields to write
217 DynamicList<wordList> groupWriteFields_;
218
219 //- Initialised coupling
220 bool initialisedCoupling_;
221
222
223 // Private Member Functions
224
225 //- Return the file path to the communications directory for the region
226 static fileName groupDir
227 (
228 const fileName& commsDir,
229 const word& regionsName,
230 const wordRe& groupName
231 );
232
233 //- Read data for a single region, single field
234 template<class Type>
235 bool readData
236 (
237 const UPtrList<const fvMesh>& meshes,
238 const wordRe& groupName,
239 const word& fieldName
240 );
241
242 //- Write data for a single region, single field
243 template<class Type>
244 bool writeData
247 const wordRe& groupName,
248 const word& fieldName
249 ) const;
250
251 void initCoupling();
252
253 //- Read (and distribute) scalar columns from stream.
254 //- Every processor gets nRows (= patch size) of these.
255 // Note: could make its argument
256 // ISstream& but then would need additional logic to construct valid
257 // stream on all processors.
258 void readColumns
259 (
260 const label nRows,
261 const label nColumns,
262 autoPtr<IFstream>& masterFilePtr,
264 ) const;
265
266 //- Read (and distribute) lines from stream.
267 //- Every processor gets nRows (= patch size) of these
268 //- (newline terminated).
269 void readLines
270 (
271 const label nRows,
272 autoPtr<IFstream>& masterFilePtr,
274 std::string& lines
275 ) const;
276
277 static void checkOrder(const wordList& regionNames);
278
279 //- Perform the coupling with necessary initialization etc.
280 void performCoupling();
281
282
283public:
284
285 //- Runtime type information
286 TypeName("externalCoupled");
287
288 //- Name of patch key, e.g. '// Patch:' when looking for start of patch data
289 static string patchKey;
290
291 //- Inherited variable for logging
293
294
295 // Constructors
296
297 //- Construct given time and dictionary
299 (
300 const word& name,
301 const Time& runTime,
302 const dictionary& dict
303 );
304
305 //- No copy construct
306 externalCoupled(const externalCoupled&) = delete;
307
308 //- No copy assignment
309 void operator=(const externalCoupled&) = delete;
310
311
312 //- Destructor
313 virtual ~externalCoupled() = default;
314
315
316 // Member Functions
317
318 // Function object control
319
320 //- Called at each ++ or += of the time-loop
321 virtual bool execute();
322
323 //- Manual execute (sub-loop or when converged)
324 virtual bool execute(const label subIndex);
325
326 //- Called when Time::run() determines that the time-loop exits
327 virtual bool end();
328
329 //- Read and set the function object if its data have changed
330 virtual bool read(const dictionary& dict);
331
332 //- Write, currently a no-op
333 virtual bool write();
334
335
336 // File creation, removal
337
338 //- Write data files (all regions, all fields) from master (OpenFOAM)
339 virtual void writeDataMaster() const;
340
341 //- Read data files (all regions, all fields) on master (OpenFOAM)
342 virtual void readDataMaster();
343
344 //- Remove data files written by master (OpenFOAM)
345 virtual void removeDataMaster() const;
346
347 //- Remove data files written by slave (external code)
348 virtual void removeDataSlave() const;
349
350
351 // Other
352
353 //- Create single name by appending words (in sorted order),
354 //- separated by '_'
355 static word compositeName(const wordList&);
356
357 //- Write geometry for the group as region/patch
358 static void writeGeometry
359 (
361 const fileName& commsDir,
362 const wordRe& groupName
363 );
364};
365
367// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
368
369} // End namespace functionObjects
370} // End namespace Foam
372// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373
374#ifdef NoRepository
376#endif
377
378// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379
380#endif
381
382// ************************************************************************* //
writer writeGeometry()
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
Input from file stream as an ISstream, normally using std::ifstream for the actual input.
Definition IFstream.H:55
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling file names.
Definition fileName.H:75
const word & name() const noexcept
Return the name of this functionObject.
bool log
Flag to write log into Info.
Provides a simple file-based communication interface for explicit coupling with an external applicati...
externalCoupled(const externalCoupled &)=delete
No copy construct.
virtual void writeDataMaster() const
Write data files (all regions, all fields) from master (OpenFOAM).
virtual void removeDataSlave() const
Remove data files written by slave (external code).
void operator=(const externalCoupled &)=delete
No copy assignment.
static string patchKey
Name of patch key, e.g. '// Patch:' when looking for start of patch data.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
TypeName("externalCoupled")
Runtime type information.
virtual void readDataMaster()
Read data files (all regions, all fields) on master (OpenFOAM).
virtual ~externalCoupled()=default
Destructor.
virtual void removeDataMaster() const
Remove data files written by master (OpenFOAM).
externalCoupled(const word &name, const Time &runTime, const dictionary &dict)
Construct given time and dictionary.
virtual bool execute()
Called at each ++ or += of the time-loop.
virtual bool write()
Write, currently a no-op.
virtual bool end()
Called when Time::run() determines that the time-loop exits.
static word compositeName(const wordList &)
Create single name by appending words (in sorted order), separated by '_'.
Virtual base class for function objects with a reference to Time.
Mesh data needed to do the Finite Volume discretisation.
Definition fvMesh.H:85
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A class for handling words, derived from Foam::string.
Definition word.H:66
engineTime & runTime
Foam::PtrList< Foam::fvMesh > meshes(regionNames.size())
wordList regionNames
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
List< word > wordList
List of word.
Definition fileName.H:60
dictionary dict
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition typeInfo.H:68