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-2019 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  This functionObject provides a simple interface for explicit coupling with
34  an external application.
35 
36  The coupling is through plain text files where OpenFOAM boundary data
37  is read/written as one line per face (data from all processors collated):
38  \verbatim
39  # Patch: <patch name>
40  <fld1> <fld2> .. <fldn> //face0
41  <fld1> <fld2> .. <fldn> //face1
42  ..
43  <fld1> <fld2> .. <fldn> //faceN
44  \endverbatim
45 
46  where the actual entries depend on the bc type:
47  - mixed: value, snGrad, refValue, refGrad, valueFraction
48  - externalCoupledMixed: output of writeDataMaster
49  - other: value, snGrad
50 
51  These text files are located in a user specified communications directory
52  which gets read/written on the master processor only. In the
53  communications directory the structure will be
54  \verbatim
55  <regionsName>/<patchGroup>/<fieldName>.[in|out]
56  \endverbatim
57 
58  (where regionsName is either the name of a single region or a composite
59  of multiple region names)
60 
61  At start-up, the boundary creates a lock file, i.e..
62  \verbatim
63  OpenFOAM.lock
64  \endverbatim
65 
66  ... to signal the external source to wait. During the functionObject
67  execution the boundary values are written to files (one per region,
68  per patch(group), per field), e.g.
69  \verbatim
70  <regionsName>/<patchGroup>/<fieldName>.out
71  \endverbatim
72 
73  The lock file is then removed, instructing the external source to take
74  control of the program execution. When ready, the external program
75  should create the return values, e.g. to files
76  \verbatim
77  <regionsName>/<patchGroup>/<fieldName>.in
78  \endverbatim
79 
80  ... and then reinstate the lock file. The functionObject will then
81  read these values, apply them to the boundary conditions and pass
82  program execution back to OpenFOAM.
83 
84 Usage
85  \verbatim
86  externalCoupled
87  {
88  type externalCoupled;
89  ...
90  log yes;
91  commsDir "<case>/comms";
92  initByExternal yes;
93  statusDone done; // Any arbitrary status=... value
94 
95  regions
96  {
97  "(region1|region0)" // Name of region(s)
98  {
99  TPatchGroup // Name of patch(group)
100  {
101  readFields (p); // List of fields to read
102  writeFields (T); // List of fields to write
103  }
104  }
105  }
106  }
107  \endverbatim
108 
109  This reads/writes (on the master processor) the directory:
110  \verbatim
111  comms/region0_region1/TPatchGroup/
112  \endverbatim
113 
114  with contents:
115  \verbatim
116  patchPoints (collected points)
117  patchFaces (collected faces)
118  p.in (input file of p, written by external application)
119  T.out (output file of T, written by OpenFOAM)
120  \endverbatim
121 
122  The patchPoints/patchFaces files denote the (collated) geometry
123  which will be written if it does not exist yet or can be written as
124  a preprocessing step using the createExternalCoupledPatchGeometry
125  application.
126 
127  The entries comprise:
128  \table
129  Property | Description | Required | Default
130  type | Type name: externalCoupled | yes |
131  commsDir | Communication directory | yes |
132  waitInterval | wait interval in (s) | no | 1
133  timeOut | timeout in (s) | no | 100*waitInterval
134  statusDone | Lockfile status=... on termination | no | done
135  initByExternal | Initialization values supplied by external app | yes
136  calcFrequency | Calculation frequency | no | 1
137  regions | The regions to couple | yes |
138  \endtable
139 
140 SourceFiles
141  externalCoupled.C
142  externalCoupledTemplates.C
143 
144 \*---------------------------------------------------------------------------*/
145 
146 #ifndef functionObjects_externalCoupled_H
147 #define functionObjects_externalCoupled_H
148 
149 #include "timeFunctionObject.H"
150 #include "externalFileCoupler.H"
151 #include "DynamicList.H"
152 #include "wordReList.H"
153 #include "scalarField.H"
154 #include "UPtrList.H"
155 
156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
157 
158 namespace Foam
159 {
160 
161 // Forward Declarations
162 class fvMesh;
163 class IFstream;
164 
165 namespace functionObjects
166 {
167 
168 /*---------------------------------------------------------------------------*\
169  Class externalCoupled Declaration
170 \*---------------------------------------------------------------------------*/
171 
172 class externalCoupled
173 :
174  public functionObjects::timeFunctionObject,
175  public externalFileCoupler
176 {
177 
178  // Private Member Data
179 
180  //- Calculation frequency
181  label calcFrequency_;
182 
183  //- The last timeIndex when coupling was triggered
184  label lastTrigger_;
185 
186  //- Names of (composite) regions
187  DynamicList<word> regionGroupNames_;
188 
189  // Per (composite) region the names of the regions
190  DynamicList<wordList> regionGroupRegions_;
191 
192  // Per (composite) region the indices of the group information
193  HashTable<labelList> regionToGroups_;
194 
195  // Per group the names of the patches/patchGroups
196  DynamicList<wordRe> groupNames_;
197 
198  // Per group the names of the fields to read
199  DynamicList<wordList> groupReadFields_;
200 
201  // Per group the names of the fields to write
202  DynamicList<wordList> groupWriteFields_;
203 
204  //- Initialised coupling
205  bool initialisedCoupling_;
206 
207 
208  // Private Member Functions
209 
210  //- Return the file path to the communications directory for the region
211  static fileName groupDir
212  (
213  const fileName& commsDir,
214  const word& regionsName,
215  const wordRe& groupName
216  );
217 
218  //- Read data for a single region, single field
219  template<class Type>
220  bool readData
221  (
223  const wordRe& groupName,
224  const word& fieldName
225  );
226 
227  //- Write data for a single region, single field
228  template<class Type>
229  bool writeData
230  (
232  const wordRe& groupName,
233  const word& fieldName
234  ) const;
235 
236  void initCoupling();
237 
238  //- Read (and distribute) scalar columns from stream. Every processor
239  // gets nRows (= patch size) of these. Note: could make its argument
240  // ISstream& but then would need additional logic to construct valid
241  // stream on all processors.
242  void readColumns
243  (
244  const label nRows,
245  const label nColumns,
246  autoPtr<IFstream>& masterFilePtr,
248  ) const;
249 
250  //- Read (and distribute) lines from stream. Every processor
251  // gets nRows (= patch size) of these. Data kept as stream (instead
252  // of strings) for ease of interfacing to readData routines that take
253  // an Istream.
254  void readLines
255  (
256  const label nRows,
257  autoPtr<IFstream>& masterFilePtr,
259  ) const;
260 
261  //- Helper: append data from all processors onto master
262  template<class Type>
263  static tmp<Field<Type>> gatherAndCombine(const Field<Type>& fld);
264 
265  static void checkOrder(const wordList& regionNames);
266 
267  //- Perform the coupling with necessary initialization etc.
268  void performCoupling();
269 
270  //- No copy construct
271  externalCoupled(const externalCoupled&) = delete;
272 
273  //- No copy assignment
274  void operator=(const externalCoupled&) = delete;
275 
276 
277 public:
278 
279  //- Runtime type information
280  TypeName("externalCoupled");
281 
282  //- Name of patch key, e.g. '// Patch:' when looking for start of patch data
283  static string patchKey;
284 
285  //- Inherited variable for logging
286  using functionObject::log;
287 
288 
289  // Constructors
290 
291  //- Construct given time and dictionary
293  (
294  const word& name,
295  const Time& runTime,
296  const dictionary& dict
297  );
298 
299 
300  //- Destructor
301  virtual ~externalCoupled() = default;
302 
303 
304  // Member Functions
305 
306  // Function object control
307 
308  //- Called at each ++ or += of the time-loop
309  virtual bool execute();
310 
311  //- Manual execute (sub-loop or when converged)
312  virtual bool execute(const label subIndex);
313 
314  //- Called when Time::run() determines that the time-loop exits
315  virtual bool end();
316 
317  //- Read and set the function object if its data have changed
318  virtual bool read(const dictionary& dict);
319 
320  //- Write, currently a no-op
321  virtual bool write();
322 
323 
324  // File creation, removal
325 
326  //- Write data files (all regions, all fields) from master (OpenFOAM)
327  virtual void writeDataMaster() const;
328 
329  //- Read data files (all regions, all fields) on master (OpenFOAM)
330  virtual void readDataMaster();
331 
332  //- Remove data files written by master (OpenFOAM)
333  virtual void removeDataMaster() const;
334 
335  //- Remove data files written by slave (external code)
336  virtual void removeDataSlave() const;
337 
338 
339  // Other
340 
341  //- Create single name by appending words (in sorted order),
342  // separated by '_'
343  static word compositeName(const wordList&);
344 
345  //- Write geometry for the group as region/patch
346  static void writeGeometry
347  (
349  const fileName& commsDir,
350  const wordRe& groupName
351  );
352 };
353 
354 
355 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 
357 } // End namespace functionObjects
358 } // End namespace Foam
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 #ifdef NoRepository
363  #include "externalCoupledTemplates.C"
364 #endif
365 
366 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
367 
368 #endif
369 
370 // ************************************************************************* //
Foam::functionObjects::externalCoupled::readDataMaster
virtual void readDataMaster()
Read data files (all regions, all fields) on master (OpenFOAM)
Definition: externalCoupled.C:686
runTime
engineTime & runTime
Definition: createEngineTime.H:13
UPtrList.H
Foam::functionObjects::externalCoupled::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: externalCoupled.C:566
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
scalarField.H
Foam::DynamicList< word >
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
Foam::functionObjects::externalCoupled
This functionObject provides a simple interface for explicit coupling with an external application.
Definition: externalCoupled.H:215
Foam::functionObjects::externalCoupled::~externalCoupled
virtual ~externalCoupled()=default
Destructor.
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
timeFunctionObject.H
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:62
Foam::Field
Generic templated field type.
Definition: Field.H:63
externalFileCoupler.H
Foam::UPtrList
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:63
meshes
PtrList< fvMesh > meshes(regionNames.size())
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::functionObjects::externalCoupled::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: externalCoupled.C:553
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::functionObjects::externalCoupled::removeDataMaster
virtual void removeDataMaster() const
Remove data files written by master (OpenFOAM)
Definition: externalCoupled.C:774
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::externalCoupled::writeDataMaster
virtual void writeDataMaster() const
Write data files (all regions, all fields) from master (OpenFOAM)
Definition: externalCoupled.C:730
Foam::functionObjects::externalCoupled::patchKey
static string patchKey
Name of patch key, e.g. '// Patch:' when looking for start of patch data.
Definition: externalCoupled.H:326
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::functionObjects::externalCoupled::removeDataSlave
virtual void removeDataSlave() const
Remove data files written by slave (external code)
Definition: externalCoupled.C:804
Foam::functionObjects::externalCoupled::write
virtual bool write()
Write, currently a no-op.
Definition: externalCoupled.C:834
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::externalFileCoupler
Encapsulates the logic for coordinating between OpenFOAM and an external application.
Definition: externalFileCoupler.H:107
Foam::functionObjects::externalCoupled::execute
virtual bool execute()
Called at each ++ or += of the time-loop.
Definition: externalCoupled.C:529
Foam::functionObject::name
const word & name() const
Return the name of this functionObject.
Definition: functionObject.C:131
wordReList.H
Foam::List< scalarField >
Foam::functionObjects::externalCoupled::TypeName
TypeName("externalCoupled")
Runtime type information.
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:189
externalCoupledTemplates.C
DynamicList.H
Foam::functionObjects::externalCoupled::writeGeometry
static void writeGeometry(const UPtrList< const fvMesh > &meshes, const fileName &commsDir, const wordRe &groupName)
Write geometry for the group as region/patch.
Definition: externalCoupled.C:243
Foam::functionObjects::externalCoupled::compositeName
static word compositeName(const wordList &)
Create single name by appending words (in sorted order),.
Definition: externalCoupled.C:351
Foam::functionObject::log
bool log
Switch write log to Info.
Definition: functionObject.H:262
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54