externalFileCoupler.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-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
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::externalFileCoupler
28
29Description
30 Encapsulates the logic for coordinating between OpenFOAM and an
31 external application.
32
33 This class provides a simple interface for explicit coupling with an
34 external application using plain text files located in the user-specified
35 communications directory.
36 These files are to be read/written on the master processor only.
37
38 The explicit coupling follows a master/slave model in which OpenFOAM
39 is the 'master' and the external application is the 'slave'.
40 The readiness to exchange information in either direction is handled
41 by a lock file (ie, OpenFOAM.lock).
42 If the lock file is present, the slave (external application) should wait
43 for the master (OpenFOAM) to complete.
44
45 When the master is finished its tasks and has prepared data for
46 the slave, the lock file is removed, instructing the external
47 source to take control of the program execution.
48
49 When the slave has completed its tasks, it will reinstate the lock file.
50
51 Example of the communication specification:
52 \verbatim
53 communication
54 {
55 commsDir "<case>/comms";
56 waitInterval 1;
57 timeOut 100;
58 initByExternal no;
59 statusDone done;
60 }
61 \endverbatim
62
63 A typical coupling loop would look like this (on the master-side):
64 \verbatim
65 initialize - master takes control
66 writeDataMaster() - write data for slave
67 useSlave()
68 waitForSlave()
69 removeDataMaster() - cleanup old data from master [optional?]
70 readDataMaster() - read data from slave
71 useMaster()
72 \endverbatim
73
74 On the slave-side:
75 \verbatim
76 waitForMaster()
77 readDataSlave() - read data from master
78 writeDataSlave() - write data for master
79 useMaster()
80 \endverbatim
81
82 Note that since the waitForSlave() method not only waits for the lock file
83 to be reinstated but also does a simple check of its contents, it can
84 also serve to communicate some control from the slave to the master.
85
86SourceFiles
87 externalFileCoupler.C
88 externalFileCouplerI.H
89
90\*---------------------------------------------------------------------------*/
91
92#ifndef externalFileCoupler_H
93#define externalFileCoupler_H
94
95#include "fileName.H"
96#include "dictionary.H"
97#include "Time.H"
98
99// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
100
101namespace Foam
102{
103
104/*---------------------------------------------------------------------------*\
105 Class externalFileCoupler Declaration
106\*---------------------------------------------------------------------------*/
109{
110public:
111
112 // Public Data Types
113
114 //- The run state (ie, who is currently in charge)
115 enum runState
119 SLAVE,
121 };
122
123private:
124
125 // Private Data
126
127 //- The current run (and initialization) state
128 mutable runState runState_;
129
130 //- Local path to communications directory
131 fileName commsDir_;
132
133 //- Value for "status=..." on termination
134 word statusDone_;
135
136 //- Interval time between checking for return data [s]
137 unsigned waitInterval_;
138
139 //- Timeout [s] while waiting for the external application
140 unsigned timeOut_;
141
142 //- Flag to indicate values are initialized by external program
143 bool slaveFirst_;
144
145 //- Local logging/verbosity flag
146 bool log;
147
148
149 // Private Member Functions
150
151 //- No copy construct
153
154 //- No copy assignment
155 void operator=(const externalFileCoupler&) = delete;
156
157
158public:
159
160 //- Runtime type information
161 TypeName("externalFileCoupler");
162
163
164 // Static data members
165
166 //- Name of the lock file
167 static word lockName;
168
169
170 // Constructors
171
172 //- Construct using standard defaults.
173 // Does not create communications directory.
175
176 //- Construct with specified communications directory.
177 // Creates the communications directory upon construction.
178 externalFileCoupler(const fileName& commsDir);
179
180 //- Construct from dictionary
181 // Creates the communications directory upon construction.
183
184
185 //- Destructor
186 virtual ~externalFileCoupler();
187
188
189 // Member Functions
190
191 // Initialization
192
193 //- True if state has been initialized
194 inline bool initialized() const;
195
196 //- External application provides initial values
197 inline bool slaveFirst() const;
198
199
200 // File locations
201
202 //- Return the file path to the base communications directory
203 inline const fileName& commDirectory() const;
204
205 //- Return the file path in the communications directory
206 inline fileName resolveFile(const word& file) const;
207
208 //- Return the file path to the lock file
209 inline fileName lockFile() const;
210
211
212 // Settings
213
214 //- Read communication settings from dictionary
215 bool readDict(const dictionary& dict);
216
217
218 // Handshaking
219
220 //- Create lock file to indicate that OpenFOAM is in charge
221 // \param wait - wait for master to complete.
222 //
223 // \return Time::stopAtControls::saUnknown if not waiting, otherwise
224 // as per the waitForMaster() description.
225 enum Time::stopAtControls useMaster(const bool wait=false) const;
226
227 //- Remove lock file to indicate that the external program is in charge
228 // \param wait - wait for slave to complete.
229 //
230 // \return Time::stopAtControls::saUnknown if not waiting, otherwise
231 // as per the waitForSlave() description.
232 enum Time::stopAtControls useSlave(const bool wait=false) const;
233
234
235 //- Wait for master to complete.
236 // This is when the lock file disappears, or exists but has
237 // \c status=done content.
238 //
239 // \return Time::stopAtControls::saUnknown or if lock file
240 // contained \c status=done it returns
241 // Time::stopAtControls::saEndTime
243
244 //- Wait for slave to complete.
245 // This is when the lock file appears.
246 //
247 // When the lock file appears, it is checked for the following
248 // content which corresponds to particular return values:
249 // - \c status=done
250 // \return Foam::Time::saEndTime
251 // - \c action=noWriteNow
252 // \return Foam::Time::saNoWriteNow
253 // - \c action=writeNow
254 // \return Foam::Time::saWriteNow
255 // - \c action=nextWrite
256 // \return Foam::Time::saNextWrite
257 // - Anything else (empty file, no action= or status=, etc)
258 // \return Foam::Time::saUnknown
260
261
262 // File creation, removal
263
264 //- Read data files on master (OpenFOAM).
265 // These data files are normally created by the slave.
266 virtual void readDataMaster();
267
268 //- Read data files on slave (external program).
269 // These data files are normally created by the master.
270 virtual void readDataSlave();
271
272
273 //- Write data files from master (OpenFOAM)
274 virtual void writeDataMaster() const;
275
276 //- Write data files from slave (external program)
277 virtual void writeDataSlave() const;
278
279
280 //- Remove data files written by master (OpenFOAM)
281 virtual void removeDataMaster() const;
282
283 //- Remove data files written by slave (external program)
284 virtual void removeDataSlave() const;
285
286
287 //- Generate \c status=done in lock (only when run-state = master)
288 // The exact text can be specified via the statusDone keyword
289 void shutdown() const;
290
291 //- Remove files written by OpenFOAM
292 void removeDirectory() const;
293};
294
295
296// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
297
298} // End namespace Foam
299
300// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301
302#include "externalFileCouplerI.H"
303
304// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
305
306#endif
307
308// ************************************************************************* //
stopAtControls
Definition: Time.H:98
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Encapsulates the logic for coordinating between OpenFOAM and an external application.
const fileName & commDirectory() const
Return the file path to the base communications directory.
enum Time::stopAtControls useSlave(const bool wait=false) const
Remove lock file to indicate that the external program is in charge.
runState
The run state (ie, who is currently in charge)
@ SLAVE
The slave (external program) is in charge.
@ MASTER
The master (OpenFOAM) is in charge.
virtual void writeDataMaster() const
Write data files from master (OpenFOAM)
virtual void removeDataSlave() const
Remove data files written by slave (external program)
enum Time::stopAtControls useMaster(const bool wait=false) const
Create lock file to indicate that OpenFOAM is in charge.
virtual ~externalFileCoupler()
Destructor.
fileName lockFile() const
Return the file path to the lock file.
bool slaveFirst() const
External application provides initial values.
bool readDict(const dictionary &dict)
Read communication settings from dictionary.
static word lockName
Name of the lock file.
fileName resolveFile(const word &file) const
Return the file path in the communications directory.
virtual void readDataSlave()
Read data files on slave (external program).
enum Time::stopAtControls waitForMaster() const
Wait for master to complete.
virtual void writeDataSlave() const
Write data files from slave (external program)
virtual void readDataMaster()
Read data files on master (OpenFOAM).
virtual void removeDataMaster() const
Remove data files written by master (OpenFOAM)
enum Time::stopAtControls waitForSlave() const
Wait for slave to complete.
void shutdown() const
Generate status=done in lock (only when run-state = master)
void removeDirectory() const
Remove files written by OpenFOAM.
externalFileCoupler()
Construct using standard defaults.
TypeName("externalFileCoupler")
Runtime type information.
bool initialized() const
True if state has been initialized.
A class for handling file names.
Definition: fileName.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
Namespace for OpenFOAM.
dictionary dict
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition: typeInfo.H:73