functionObject.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Namespace
28  Foam::functionObjects
29 
30 Description
31  Function objects are OpenFOAM utilities to ease workflow configurations and
32  enhance workflows by producing additional user-requested data both during
33  runtime and postprocessing calculations, typically in the form of
34  additional logging to the screen, or generating text, image and field files.
35 
36  Function objects eliminate the need to store all runtime generated data,
37  hence saving considerable resources. Furthermore, function objects are
38  readily applied to batch-driven processes, improving reliability by
39  standardising the sequence of operations and reducing the amount of manual
40  interaction.
41 
42  In addition, the output of most function objects, e.g. output fields, are
43  stored on the mesh database so that it can be retrieved and used for other
44  applications (e.g. directly using \c wallShearStress function object output
45  in \c fieldAverage function object to produce \c wallShearStressMean field).
46 
47  \section secFunctionObjects Using function objects
48 
49  Function objects can be executed by using two methods:
50 
51  - \c functions sub-dictionary in the \c system/controlDict file
52  - \c postProcess command-line utility
53 
54  For the first method, each selected function object should be listed inside
55  \c functions sub-dictionary of the \c system/controlDict file as a nested
56  sub-dictionary, typically as in the following example:
57 
58  \verbatim
59  functions // sub-dictionary name under the system/controlDict file
60  {
61  ..optional entries..
62 
63  <dictName1>
64  {
65  // Mandatory entries
66  type <functionObjectTypeName>;
67  libs (<libType>FunctionObjects);
68 
69  // Mandatory entries defined in <functionObjectType>
70  ...
71 
72  // Optional entries defined in <functionObjectType>
73  ...
74 
75  // Optional (inherited) entries
76  region region0;
77  enabled true;
78  log true;
79  timeStart 0;
80  timeEnd 1000;
81  executeControl timeStep;
82  executeInterval 1;
83  writeControl timeStep;
84  writeInterval 1;
85  }
86 
87  <dictName2>
88  {
89  ...
90  }
91 
92  ...
93 
94  <dictNameN>
95  {
96  ...
97  }
98  }
99  \endverbatim
100 
101  where the entries mean:
102  \table
103  Property | Description | Type | Reqd | Deflt
104  type | Type name of function object | word | yes | -
105  libs | Library name(s) for implementation | words | no | -
106  errors | Error handling (default/warn/ignore/strict) | word | no | inherits
107  region | Name of region for multi-region cases | word | no | region0
108  enabled | Switch to turn function object on/off | bool | no | true
109  log | Switch to write log info to standard output | bool | no | true
110  timeStart | Start time for function object execution | scalar | no | 0
111  timeEnd | End time for function object execution | scalar | no | inf
112  executeControl | See time controls below | word | no | timeStep
113  executeInterval | Steps/time between execute phases | label | no | 1
114  writeControl | See time controls below | word | no | timeStep
115  writeInterval | Steps/time between write phases | label | no | 1
116  \endtable
117 
118  If the \c errors entry is missing, it uses the value (if any)
119  specified within the top-level functionObjectList.
120 
121  Time controls:
122  \table
123  Option | Description
124  none | Trigger is disabled
125  timeStep | Trigger every 'Interval' time-steps
126  writeTime | Trigger every 'Interval' output times
127  runTime | Trigger every 'Interval' run time period
128  adjustableRunTime | Currently identical to "runTime"
129  clockTime | Trigger every 'Interval' clock time period
130  cpuTime | Trigger every 'Interval' CPU time period
131  onEnd | Trigger on end of simulation run
132  \endtable
133 
134  The sub-dictionary name \c <userDefinedSubDictName> is chosen by the user,
135  and is typically used as the name of the output directory for any data
136  written by the function object.
137 
138  As the base mandatory entries, the \c type entry defines the type of
139  function object properties that follow. Function objects are packaged into
140  separate libraries for flexibility and the \c libs entry is used to specify
141  which library should be loaded.
142 
143  Each function object has two separate run phases:
144 
145  - The \c execute phase is meant to be used for updating calculations
146  or for management tasks.
147  - The \c write phase is meant for writing the calculated data to disk.
148 
149  For each phase the respective time controls are provided, as listed above.
150 
151 
152  The second method of executing function objects is to use \c postProcess
153  utility.
154 
155  When specified without additional arguments, the \c postProcess utility
156  executes all function objects defined in the \c system/controlDict file
157  for all time directories:
158 
159  \verbatim
160  postProcess
161  \endverbatim
162 
163  Most function objects can be invoked directly without the need to specify
164  the input dictionary using the \c -func option, e.g. to execute the Courant
165  number function object:
166 
167  \verbatim
168  postProcess -func CourantNo
169  \endverbatim
170 
171  In addition, the \c -postProcess option is available to all solvers,
172  and operates similarly to the stand-alone \c postProcess utility.
173  For example, having completed a \c simpleFoam calculation, the following
174  will execute all function objects defined in the \c system/controlDict file
175  for all time directories:
176 
177  \verbatim
178  simpleFoam -postProcess
179  \endverbatim
180 
181 Class
182  Foam::functionObject
183 
184 Description
185  Abstract base-class for Time/database function objects.
186 
187 See also
188  - Foam::functionObjectList
189  - Foam::functionObjects::timeControl
190 
191 SourceFiles
192  functionObject.C
193 
194 \*---------------------------------------------------------------------------*/
195 
196 #ifndef functionObject_H
197 #define functionObject_H
198 
199 #include "typeInfo.H"
200 #include "autoPtr.H"
201 #include "runTimeSelectionTables.H"
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 
205 namespace Foam
206 {
207 
208 // Forward Declarations
209 class Time;
210 class polyMesh;
211 class mapPolyMesh;
212 
213 /*---------------------------------------------------------------------------*\
214  Class functionObject Declaration
215 \*---------------------------------------------------------------------------*/
216 
217 class functionObject
218 {
219  // Private Data
220 
221  //- Name
222  const word name_;
223 
224 
225 protected:
226 
227  // Protected Member Functions
228 
229  //- Return a scoped name, e.g. used to construct local field names
230  word scopedName(const word& name) const;
231 
232 
233 public:
234 
235  // Forward declarations
236  class unavailableFunctionObject;
237 
238  //- Runtime type information
239  virtual const word& type() const = 0;
240 
241  //- Flag to execute debug content
242  static int debug;
243 
244  //- Global post-processing mode switch
245  static bool postProcess;
246 
247  //- Directory prefix
248  static word outputPrefix;
249 
250  //- Flag to write log into Info
251  bool log;
252 
253 
254  // Declare run-time constructor selection tables
255 
257  (
258  autoPtr,
260  dictionary,
261  (const word& name, const Time& runTime, const dictionary& dict),
262  (name, runTime, dict)
263  );
264 
265 
266  // Constructors
267 
268  //- Construct from components
269  functionObject(const word& name);
270 
271  //- Return clone
272  autoPtr<functionObject> clone() const
273  {
275  return nullptr;
276  }
277 
278 
279  // Selectors
280 
281  //- Select from dictionary, based on its "type" entry
282  static autoPtr<functionObject> New
283  (
284  const word& name,
285  const Time& runTime,
286  const dictionary& dict
287  );
288 
289 
290  //- Destructor
291  virtual ~functionObject() = default;
292 
293 
294  // Member Functions
295 
296  //- Return the name of this functionObject
297  const word& name() const;
298 
299  //- Read and set the function object if its data have changed
300  virtual bool read(const dictionary& dict);
301 
302  //- Called at each ++ or += of the time-loop.
303  // postProcess overrides the usual executeControl behaviour and
304  // forces execution (used in post-processing mode)
305  virtual bool execute() = 0;
306 
307  //- Execute using the specified subIndex.
308  // The base implementation is a no-op.
309  // \param subIndex an execution sub-index corresponding to a
310  // sub-cycle or something similar.
311  virtual bool execute(const label subIndex);
312 
313  //- Called at each ++ or += of the time-loop.
314  // postProcess overrides the usual writeControl behaviour and
315  // forces writing always (used in post-processing mode)
316  virtual bool write() = 0;
317 
318  //- Called when Time::run() determines that the time-loop exits.
319  // The base implementation is a no-op.
320  virtual bool end();
321 
322  //- Called at the end of Time::adjustDeltaT() if adjustTime is true
323  virtual bool adjustTimeStep();
324 
325  //- Did any file get changed during execution?
326  virtual bool filesModified() const;
327 
328  //- Update for changes of mesh
329  // The base implementation is a no-op.
330  virtual void updateMesh(const mapPolyMesh& mpm);
331 
332  //- Update for changes of mesh
333  // The base implementation is a no-op.
334  virtual void movePoints(const polyMesh& mesh);
335 };
336 
337 
338 /*---------------------------------------------------------------------------*\
339  Class functionObject::unavailableFunctionObject Declaration
340 \*---------------------------------------------------------------------------*/
341 
342 //- Abstract functionObject to report when a real version is unavailable.
344 :
345  public functionObject
346 {
347 protected:
348 
349  //- Construct with name
351 
352  //- Report it is unavailable, emitting a FatalError for try/catch
353  //- in the caller
354  void carp(std::string message = "") const;
355 
356 
357 public:
358 
359  // Member Functions
360 
361  //- No nothing
362  virtual bool execute();
363 
364  //- No nothing
365  virtual bool write();
366 };
367 
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 } // End namespace Foam
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #endif
376 
377 // ************************************************************************* //
Foam::functionObject::unavailableFunctionObject::execute
virtual bool execute()
No nothing.
Definition: functionObject.C:212
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::functionObject::execute
virtual bool execute()=0
Called at each ++ or += of the time-loop.
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObject::write
virtual bool write()=0
Called at each ++ or += of the time-loop.
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
typeInfo.H
Foam::functionObject::New
static autoPtr< functionObject > New(const word &name, const Time &runTime, const dictionary &dict)
Select from dictionary, based on its "type" entry.
Definition: functionObject.C:67
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:321
Foam::functionObject::unavailableFunctionObject::unavailableFunctionObject
unavailableFunctionObject(const word &name)
Construct with name.
Definition: functionObject.C:183
Foam::functionObject::declareRunTimeSelectionTable
declareRunTimeSelectionTable(autoPtr, functionObject, dictionary,(const word &name, const Time &runTime, const dictionary &dict),(name, runTime, dict))
Foam::functionObject::unavailableFunctionObject::write
virtual bool write()
No nothing.
Definition: functionObject.C:218
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
Foam::functionObject::~functionObject
virtual ~functionObject()=default
Destructor.
Foam::functionObject::postProcess
static bool postProcess
Global post-processing mode switch.
Definition: functionObject.H:349
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:137
Foam::functionObject::outputPrefix
static word outputPrefix
Directory prefix.
Definition: functionObject.H:352
Foam::functionObject::clone
autoPtr< functionObject > clone() const
Return clone.
Definition: functionObject.H:376
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::functionObject::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: functionObject.C:160
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObject::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
Definition: functionObject.C:176
Foam::functionObject::unavailableFunctionObject::carp
void carp(std::string message="") const
Definition: functionObject.C:192
Foam::functionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: functionObject.C:154
Foam::functionObject::scopedName
word scopedName(const word &name) const
Return a scoped name, e.g. used to construct local field names.
Definition: functionObject.C:49
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::functionObject::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: functionObject.C:172
Foam::functionObject::unavailableFunctionObject
Abstract functionObject to report when a real version is unavailable.
Definition: functionObject.H:447
Foam::functionObject::debug
static int debug
Flag to execute debug content.
Definition: functionObject.H:346
Foam::functionObject::name
const word & name() const
Return the name of this functionObject.
Definition: functionObject.C:131
runTimeSelectionTables.H
Macros to ease declaration of run-time selection tables.
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::functionObject::filesModified
virtual bool filesModified() const
Did any file get changed during execution?
Definition: functionObject.C:166
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::functionObject::log
bool log
Flag to write log into Info.
Definition: functionObject.H:355
Foam::functionObject::functionObject
functionObject(const word &name)
Construct from components.
Definition: functionObject.C:57
autoPtr.H