functionObjectList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "functionObjectList.H"
27 #include "argList.H"
29 
30 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
31 
33 Foam::functionObjectList::readFunctionsDict
34 (
35  const Time& t,
36  const bool execution
37 )
38 {
39  if (execution)
40  {
41  typeIOobject<IOdictionary> functionsDict
42  (
43  "functions",
44  t.system(),
45  t,
48  true
49  );
50 
51  if (functionsDict.headerOk())
52  {
53  return functionsDict;
54  }
55  }
56 
57  return typeIOobject<IOdictionary>
58  (
59  "functions",
60  t.system(),
61  t,
64  false
65  );
66 }
67 
68 
69 bool Foam::functionObjectList::readDict()
70 {
71  bool ok = true;
72  updated_ = execution_;
73 
74  // Avoid reading/initialising if execution is off
75  if (!execution_)
76  {
77  return true;
78  }
79 
80  if (IOdictionary::size())
81  {
82  PtrList<functionObject> newPtrs;
83  List<SHA1Digest> newDigs;
84  HashTable<label> newIndices;
85 
86  label nFunc = 0;
87 
88  const dictionary& functionsDict = *this;
89 
90  libs.open
91  (
92  functionsDict,
93  "libs",
94  functionObject::dictionaryConstructorTablePtr_
95  );
96 
97  newPtrs.setSize(functionsDict.size());
98  newDigs.setSize(functionsDict.size());
99 
100  forAllConstIter(dictionary, functionsDict, iter)
101  {
102  const word& key = iter().keyword();
103 
104  if (!iter().isDict())
105  {
106  if (key != "libs")
107  {
108  IOWarningInFunction(*this)
109  << "Entry " << key << " is not a dictionary" << endl;
110  }
111 
112  continue;
113  }
114 
115  const dictionary& dict = iter().dict();
116  const bool enabled = dict.lookupOrDefault("enabled", true);
117 
118  newDigs[nFunc] = dict.digest();
119 
120  label oldIndex;
121  functionObject* objPtr = remove(key, oldIndex);
122 
123  if (objPtr)
124  {
125  if (enabled)
126  {
127  // Dictionary changed for an existing functionObject
128  if (newDigs[nFunc] != digests_[oldIndex])
129  {
130  ok = objPtr->read(dict) && ok;
131  }
132  }
133  else
134  {
135  // Delete the disabled functionObject
136  delete objPtr;
137  objPtr = nullptr;
138  continue;
139  }
140  }
141  else if (enabled)
142  {
143  autoPtr<functionObject> foPtr;
144 
145  if
146  (
147  dict.found("executeControl")
148  || dict.found("writeControl")
149  || dict.found("outputControl")
150  )
151  {
152  foPtr.set
153  (
154  new functionObjects::timeControl(key, time_, dict)
155  );
156  }
157  else
158  {
159  foPtr = functionObject::New(key, time_, dict);
160  }
161 
162  if (foPtr.valid())
163  {
164  objPtr = foPtr.ptr();
165  }
166  else
167  {
168  ok = false;
169  }
170  }
171 
172  // Insert active functionObjects into the list
173  if (objPtr)
174  {
175  newPtrs.set(nFunc, objPtr);
176  newIndices.insert(key, nFunc);
177  nFunc++;
178  }
179  }
180 
181  newPtrs.setSize(nFunc);
182  newDigs.setSize(nFunc);
183 
184  // Updating the PtrList of functionObjects deletes any
185  // existing unused functionObjects
187  digests_.transfer(newDigs);
188  indices_.transfer(newIndices);
189  }
190  else
191  {
193  digests_.clear();
194  indices_.clear();
195  }
196 
197  return ok;
198 }
199 
200 
201 Foam::functionObject* Foam::functionObjectList::remove
202 (
203  const word& key,
204  label& oldIndex
205 )
206 {
207  functionObject* ptr = 0;
208 
209  // Find index of existing functionObject
210  HashTable<label>::iterator fnd = indices_.find(key);
211 
212  if (fnd != indices_.end())
213  {
214  oldIndex = fnd();
215 
216  // Retrieve the pointer and remove it from the old list
217  ptr = this->PtrList<functionObject>::set(oldIndex, 0).ptr();
218  indices_.erase(fnd);
219  }
220  else
221  {
222  oldIndex = -1;
223  }
224 
225  return ptr;
226 }
227 
228 
229 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
230 
232 (
233  const Time& t,
234  const bool execution
235 )
236 :
237  IOdictionary(readFunctionsDict(t, execution)),
239  digests_(),
240  indices_(),
241  time_(t),
242  execution_(execution),
243  updated_(false)
244 {
245  if (t.controlDict().found("functions"))
246  {
247  if (!headerOk())
248  {
249  merge(t.controlDict().subDict("functions"));
250  }
251  else
252  {
254  << "Both " << relativeObjectPath()
255  << " and " << t.controlDict().relativeObjectPath()/"functions"
256  << " found, the latter will be ignored." << endl;
257  }
258  }
259 }
260 
261 
263 (
264  const argList& args,
265  const Time& runTime
266 )
267 {
268  autoPtr<functionObjectList> functionsPtr(new functionObjectList(runTime));
269  dictionary& functionsDict = *functionsPtr;
270 
271  word region = word::null;
272 
273  // Set the region name if specified
274  if (args.optionFound("region"))
275  {
276  region = args["region"];
277  }
278 
279  if
280  (
281  args.optionFound("dict")
282  || args.optionFound("func")
283  || args.optionFound("funcs")
284  )
285  {
286  // Remove functions specified in the functions dictionary
287  functionsDict.clear();
288 
289  if (args.optionFound("dict"))
290  {
291  functionsDict.merge
292  (
294  (
295  IOobject
296  (
297  args["dict"],
298  runTime,
300  )
301  )
302  );
303  }
304 
305  if (args.optionFound("func"))
306  {
308  (
309  "function",
310  args["func"],
311  functionsDict,
313  "system",
314  {"command", args.commandLine()},
315  region
316  );
317  }
318 
319  if (args.optionFound("funcs"))
320  {
321  wordList funcs(args.optionLookup("funcs")());
322 
323  forAll(funcs, i)
324  {
326  (
327  "function",
328  funcs[i],
329  functionsDict,
331  "system",
332  {"command", args.commandLine()},
333  region
334  );
335  }
336  }
337  }
338 
339  functionsPtr->readDict();
340 
341  return functionsPtr;
342 }
343 
344 
345 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
346 
348 {}
349 
350 
351 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
352 
354 {
356  digests_.clear();
357  indices_.clear();
358  updated_ = false;
359 }
360 
361 
363 {
364  forAll(*this, oi)
365  {
366  if (operator[](oi).name() == name)
367  {
368  return oi;
369  }
370  }
371 
372  return -1;
373 }
374 
375 
377 {
378  bool ok = readDict();
379 
380  if (execution_)
381  {
382  forAll(*this, oi)
383  {
384  if (operator[](oi).executeAtStart())
385  {
386  ok = operator[](oi).execute() && ok;
387  ok = operator[](oi).write() && ok;
388  }
389  }
390  }
391 
392  return ok;
393 }
394 
395 
397 {
398  bool ok = true;
399 
400  if (execution_)
401  {
402  if (!updated_)
403  {
404  readDict();
405  }
406 
407  forAll(*this, oi)
408  {
409  ok = operator[](oi).execute() && ok;
410  ok = operator[](oi).write() && ok;
411  }
412  }
413 
414  return ok;
415 }
416 
417 
419 {
420  bool ok = true;
421 
422  if (execution_)
423  {
424  if (!updated_)
425  {
426  readDict();
427  }
428 
429  forAll(*this, oi)
430  {
431  ok = operator[](oi).end() && ok;
432  }
433  }
434 
435  return ok;
436 }
437 
438 
440 {
441  scalar result = vGreat;
442 
443  if (execution_)
444  {
445  if (!updated_)
446  {
447  readDict();
448  }
449 
450  forAll(*this, oi)
451  {
452  result = min(result, operator[](oi).timeToNextAction());
453  }
454  }
455 
456  return result;
457 }
458 
459 
461 {
462  scalar result = vGreat;
463 
464  forAll(*this, oi)
465  {
466  result = min(result, operator[](oi).maxDeltaT());
467  }
468 
469  return result;
470 }
471 
472 
474 {
475  if (regIOobject::read())
476  {
477  return readDict();
478  }
479  else
480  {
481  return false;
482  }
483 }
484 
485 
487 {
488  if (execution_)
489  {
490  forAll(*this, oi)
491  {
492  operator[](oi).movePoints(mesh);
493  }
494  }
495 }
496 
497 
499 {
500  if (execution_)
501  {
502  forAll(*this, oi)
503  {
504  operator[](oi).topoChange(map);
505  }
506  }
507 }
508 
509 
511 {
512  if (execution_)
513  {
514  forAll(*this, oi)
515  {
516  operator[](oi).mapMesh(map);
517  }
518  }
519 }
520 
521 
523 {
524  if (execution_)
525  {
526  forAll(*this, oi)
527  {
528  operator[](oi).distribute(map);
529  }
530  }
531 }
532 
533 
534 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
friend class iterator
Declare friendship with the iterator.
Definition: HashTable.H:194
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:119
fileName relativeObjectPath() const
Return complete relativePath + object name.
Definition: IOobject.H:420
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
bool set(const label) const
Is element set.
Definition: PtrListI.H:62
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:198
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:213
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
const IOdictionary & controlDict() const
Return the control dict.
Definition: Time.H:269
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
const string & commandLine() const
Return the command line string.
Definition: argListI.H:30
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T, if not found return the given default.
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:843
void clear()
Clear the dictionary.
Definition: dictionary.C:1344
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:509
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1299
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:475
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
static fileName functionObjectDictPath
Default relative path to the directory structure.
List of function objects with start(), execute() and end() functions that is called for each object.
virtual ~functionObjectList()
Destructor.
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
virtual scalar maxDeltaT() const
Return the maximum time-step for stable operation.
virtual void movePoints(const polyMesh &mesh)
Update topology using the given map.
label findObjectID(const word &name) const
Find the ID of a given function object by name.
static autoPtr< functionObjectList > New(const argList &args, const Time &runTime)
Construct and return a functionObjectList for an application.
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
bool start()
Called at the start of the time-loop.
functionObjectList(const Time &runTime, const bool execution=true)
Construct from Time and the execution setting.
void clear()
Clear the list of function objects.
scalar timeToNextAction()
Return the time to the next write.
bool execute()
Called at each ++ or += of the time-loop.
virtual void topoChange(const polyTopoChangeMap &map)
Update topology using the given map.
bool end()
Called when Time::run() determines that the time-loop exits.
virtual bool read()
Read and set the function objects if their data have changed.
Abstract base-class for Time/database functionObjects.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:80
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
bool headerOk()
Read and check header info.
Definition: regIOobject.C:453
virtual bool read()
Read object.
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:531
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
scalar maxDeltaT
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define WarningInFunction
Report a warning using Foam::Warning.
dlLibraryTable libs
Table of loaded dynamic libraries.
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:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
bool readConfigFile(const word &configType, const string &argString, dictionary &parentDict, const fileName &configFilesPath, const word &configFilesDir, const Pair< string > &contextTypeAndValue, const word &region=word::null)
Read the specified configuration file.
Definition: dictionaryIO.C:433
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
dictionary dict
Foam::argList args(argc, argv)