functionObjectList.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) 2015-2023 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 Class
28  Foam::functionObjectList
29 
30 Description
31  List of function objects with start(), execute() and end() functions
32  that is called for each object.
33 
34  \verbatim
35  functions // sub-dictionary name under the system/controlDict file
36  {
37  ..optional entries..
38 
39  <userDict1>
40  {
41  // Mandatory entries
42  type <typeName>;
43  libs (<libName> .. <libName>);
44  ...
45  }
46 
47  <userDict2>
48  {
49  ...
50  }
51 
52  ...
53  }
54  \endverbatim
55 
56  with optional entries:
57  \table
58  Property | Description | Type | Reqd | Deflt
59  libs | Preloaded library names | words | no | -
60  errors | Error handling (default/warn/ignore/strict) | word | no | inherits
61  useNamePrefix | Default enable/disable scoping prefix | bool | no | no-op
62  \endtable
63 
64  The optional \c errors entry controls how FatalError is caught
65  during construction and execute/write. FatalIOError is unaffected.
66  - \c default : warn on construction errors, fatal on runtime errors
67  - \c warn : warn on construction and runtime errors
68  - \c ignore : ignore construction and runtime errors
69  - \c strict : fatal on construction and runtime errors
70  .
71 
72 See also
73  Foam::functionObject
74  Foam::functionObjects::timeControl
75 
76 SourceFiles
77  functionObjectList.C
78 
79 \*---------------------------------------------------------------------------*/
80 
81 #ifndef Foam_functionObjectList_H
82 #define Foam_functionObjectList_H
83 
84 #include "PtrList.H"
85 #include "functionObject.H"
86 #include "SHA1Digest.H"
87 #include "HashTable.H"
88 #include "IOdictionary.H"
89 #include "HashSet.H"
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 namespace Foam
95 {
96 
97 // Forward Declarations
98 class argList;
99 class mapPolyMesh;
100 class wordRe;
101 
102 /*---------------------------------------------------------------------------*\
103  Class functionObjectList Declaration
104 \*---------------------------------------------------------------------------*/
105 
106 class functionObjectList
107 :
108  private PtrList<functionObject>
109 {
110  // Private Data
111 
112  //- A list of error/warning handling
113  List<error::handlerTypes> errorHandling_;
114 
115  //- A list of SHA1 digests for the function object dictionaries
116  List<SHA1Digest> digests_;
117 
118  //- Quick lookup of the index into functions/digests/errorHandling
119  HashTable<label> indices_;
120 
121  //- Track the number of warnings per function object and limit
122  // to a predefined number to avoid flooding the display.
123  // Clear on re-read of functions.
124  HashTable<unsigned> warnings_;
126  //- Reference to Time
127  const Time& time_;
128 
129  //- The parent dictionary containing a "functions" sub-dictionary
130  //- of functionObject specifications
131  const dictionary& parentDict_;
132 
133  //- Function object properties - stores state information
134  mutable autoPtr<functionObjects::properties> propsDictPtr_;
135 
136  //- Function objects output registry
137  mutable autoPtr<objectRegistry> objectsRegistryPtr_;
138 
139  //- Switch for the execution of the functionObjects
140  bool execution_;
141 
142  //- Tracks if read() was called while execution is on
143  bool updated_;
144 
145 
146  // Private Member Functions
147 
148  //- List of functions
149  const PtrList<functionObject>& functions() const { return *this; }
150 
151  //- List of functions
152  PtrList<functionObject>& functions() { return *this; }
153 
154  //- Create properties dictionary - attached to Time.
155  void createPropertiesDict() const;
156 
157  //- Create registry for output objects - attached to Time.
158  void createOutputRegistry() const;
159 
160  //- Remove and return the function object pointer by name,
161  //- and returns the old index (into digest) via the parameter.
162  // Returns nullptr (and index -1) if it didn't exist
163  autoPtr<functionObject> remove(const word& key, label& oldIndex);
164 
165  //- Search the specified directory for functionObject
166  //- configuration files, add to the given map and recurse
167  static void listDir(const fileName& dir, wordHashSet& available);
168 
169  //- Like Enum::getOrDefault, but with additional code to warn if
170  //- the 'key' is not a primitive entry.
171  //
172  // This additional treatment is to ensure that potentially existing
173  // code with an "errors" functionObject will continue to run.
174  error::handlerTypes getOrDefaultErrorHandling
175  (
176  const word& key,
177  const dictionary& dict,
178  const error::handlerTypes deflt
179  ) const;
180 
181 
182  //- No copy construct
183  functionObjectList(const functionObjectList&) = delete;
184 
185  //- No copy assignment
186  void operator=(const functionObjectList&) = delete;
187 
188 
189 public:
190 
191  // Static data members
192 
193  //- Default relative path ("caseDicts/postProcessing") to the
194  //- directory structure containing functionObject dictionary files.
195  static fileName functionObjectDictPath;
196 
197 
198  // Constructors
199 
200  //- Construct from Time and the execution setting.
201  // The functionObject specifications are read from the controlDict
202  functionObjectList
203  (
204  const Time& runTime,
205  const bool execution=true
206  );
207 
208  //- Construct from Time, a dictionary with a "functions" entry
209  //- and the execution setting.
210  // \param[in] runTime - the other Time instance to construct from
211  // \param[in] parentDict - the parent dictionary containing
212  // a "functions" sub-dictionary of functionObject specifications.
213  // \param[in] execution - whether the function objects should execute
214  // or not. Default: true.
215  functionObjectList
216  (
217  const Time& runTime,
218  const dictionary& parentDict,
219  const bool execution=true
220  );
221 
222  //- Construct and return a functionObjectList for an application.
223  // If the "dict" argument is specified the functionObjectList is
224  // constructed from that dictionary which is returned as
225  // controlDict otherwise the functionObjectList is constructed
226  // from the "functions" sub-dictionary of "system/controlDict"
227  static autoPtr<functionObjectList> New
228  (
229  const argList& args,
230  const Time& runTime,
231  dictionary& controlDict,
232  HashSet<wordRe>& requiredFields
233  );
234 
235 
236  //- Destructor
237  ~functionObjectList() = default;
238 
239 
240  // Member Functions
241 
242  //- Return the number of elements in the List.
244 
245  //- Return true if the List is empty (ie, size() is zero).
247 
248  //- Access to the functionObjects
250 
251  //- Return the current trigger index (read from the propsDict)
252  label triggerIndex() const;
253 
254  //- Reset/read properties dictionary for current time
255  void resetPropertiesDict();
256 
257  //- Write access to the properties dictionary
258  //- ("functionObjectProperties") registered on Time
260 
261  //- Const access to the properties dictionary
262  //- ("functionObjectProperties") registered on Time
264 
265  //- Write access to the output objects ("functionObjectObjects")
266  //- registered on Time
268 
269  //- Const access to the output objects ("functionObjectObjects")
270  //- registered on Time
271  const objectRegistry& storedObjects() const;
272 
273  //- Clear the list of function objects
274  void clear();
275 
276  //- Find the ID of a given function object by name, -1 if not found.
277  label findObjectID(const word& objName) const;
278 
279  //- Print a list of functionObject configuration files in the
280  //- directories located using
281  //- Foam::findEtcDirs("caseDicts/postProcessing")
282  //
283  // -# \b user settings
284  // - ~/.OpenFOAM/{PROJECT_API}/"caseDicts/postProcessing"
285  // - ~/.OpenFOAM/"caseDicts/postProcessing"
286  // -# \b group settings
287  // - $WM_PROJECT_SITE/{PROJECT_API}/"etc/caseDicts/postProcessing"
288  // - $WM_PROJECT_SITE/"etc/caseDicts/postProcessing"
289  // -# \b other (shipped) settings
290  // - $WM_PROJECT_DIR/etc/"caseDicts/postProcessing"
291  //
292  // Where {PROJECT_API} is the value of the OPENFOAM define.
293  // See further notes in Foam::findEtcEntries()
294  static void list();
295 
296  //- Find a functionObject dictionary file in the case
297  //- <system> directory or any directory located using
298  //- Foam::findEtcDirs("caseDicts/postProcessing")
299  //
300  // \return The path of the functionObject dictionary file found
301  // or an empty path
302  static fileName findDict(const word& funcName);
303 
304  //- Read the specified functionObject configuration dictionary parsing
305  //- the optional arguments included in the name 'funcNameArgs0',
306  //- inserting 'field' or 'fields' entries as required and merging the
307  //- resulting functionObject dictionary into 'functionsDict'. Any
308  //- fields required to execute the functionObject are added to
309  //- 'requiredFields'
310  //
311  // Uses functionObjectList::findDict() for searching
312  static bool readFunctionObject
313  (
314  const string& funcNameArgs0,
315  dictionary& functionsDict,
316  HashSet<wordRe>& requiredFields,
317  const word& region = word::null
318  );
319 
320  //- Read and set the function objects if their data have changed
321  bool read();
322 
323  //- Switch the function objects on
324  void on();
325 
326  //- Switch the function objects off
327  void off();
328 
329  //- Return the execution status (on/off) of the function objects
330  bool status() const;
331 
332  //- Called at the start of the time-loop
333  bool start();
334 
335  //- Called at each ++ or += of the time-loop.
336  // postProcess overrides the usual executeControl behaviour and
337  // forces execution (used in post-processing mode)
338  bool execute(bool writeProperties = true);
339 
340  //- Execute function objects using the specified subIndex.
341  // \param subIndex an execution sub-index corresponding to a
342  // sub-cycle or something similar
343  bool execute(const label subIndex);
344 
345  //- Execute a subset of function objects using the specified subIndex.
346  // \param functionNames names or regex of existing functions to
347  // execute
348  // \param subIndex an execution sub-index corresponding to a
349  // sub-cycle or something similar
350  bool execute(const UList<wordRe>& functionNames, const label subIndex);
351 
352  //- Called when Time::run() determines that the time-loop exits
353  bool end();
354 
355  //- Called at the end of Time::adjustDeltaT() if adjustTime is true
356  bool adjustTimeStep();
357 
358  //- Did any file get changed during execution?
359  bool filesModified() const;
360 
361  //- Update for changes of mesh
362  void updateMesh(const mapPolyMesh& mpm);
363 
364  //- Update for changes of mesh
365  void movePoints(const polyMesh& mesh);
366 };
367 
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 } // End namespace Foam
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #endif
376 
377 // ************************************************************************* //
bool read()
Read and set the function objects if their data have changed.
A HashTable with keys but without contents that is similar to std::unordered_set. ...
Definition: HashSet.H:73
dictionary dict
A class for handling file names.
Definition: fileName.H:72
bool filesModified() const
Did any file get changed during execution?
~functionObjectList()=default
Destructor.
handlerTypes
Handling of errors. The exact handling depends on the local context.
Definition: error.H:109
void off()
Switch the function objects off.
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Storage for function object properties, derived from IOdictionary. Provides functionality to read/wri...
label findObjectID(const word &objName) const
Find the ID of a given function object by name, -1 if not found.
static void list()
Print a list of functionObject configuration files in the directories located using Foam::findEtcDirs...
engineTime & runTime
functionObjects::properties & propsDict()
Write access to the properties dictionary ("functionObjectProperties") registered on Time...
void on()
Switch the function objects on.
bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
bool start()
Called at the start of the time-loop.
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:157
static autoPtr< functionObjectList > New(const argList &args, const Time &runTime, dictionary &controlDict, HashSet< wordRe > &requiredFields)
Construct and return a functionObjectList for an application.
void resetPropertiesDict()
Reset/read properties dictionary for current time.
dynamicFvMesh & mesh
A class for handling words, derived from Foam::string.
Definition: word.H:63
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
static fileName findDict(const word &funcName)
Find a functionObject dictionary file in the case <system> directory or any directory located using F...
static const word null
An empty word.
Definition: word.H:84
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
friend Ostream & operator(Ostream &os, const UPtrList< T > &list)
Write UPtrList to Ostream.
runTime controlDict().readEntry("adjustTimeStep"
The central control dictionary, the contents of which are either taken directly from the FOAM_CONTROL...
Definition: debug.C:142
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
static bool readFunctionObject(const string &funcNameArgs0, dictionary &functionsDict, HashSet< wordRe > &requiredFields, const word &region=word::null)
Read the specified functionObject configuration dictionary parsing the optional arguments included in...
void clear()
Clear the list of function objects.
bool empty() const noexcept
True if the list is empty (ie, size() is zero)
Definition: UPtrListI.H:99
bool status() const
Return the execution status (on/off) of the function objects.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
bool execute(bool writeProperties=true)
Called at each ++ or += of the time-loop.
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
objectRegistry & storedObjects()
Write access to the output objects ("functionObjectObjects") registered on Time.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Registry of regIOobjects.
label triggerIndex() const
Return the current trigger index (read from the propsDict)
Foam::argList args(argc, argv)
void movePoints(const polyMesh &mesh)
Update for changes of mesh.
static fileName functionObjectDictPath
Default relative path ("caseDicts/postProcessing") to the directory structure containing functionObje...
bool end()
Called when Time::run() determines that the time-loop exits.
Namespace for OpenFOAM.