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-2026 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  Info<< indentOrNl << "Constructing functionObjects from "
91  << relativeObjectPath().c_str() << endl;
92 
93  printDictionary print(functionsDict);
94 
95  libs.open
96  (
97  functionsDict,
98  "libs",
99  functionObject::dictionaryConstructorTablePtr_
100  );
101 
102  newPtrs.setSize(functionsDict.size());
103  newDigs.setSize(functionsDict.size());
104 
105  forAllConstIter(dictionary, functionsDict, iter)
106  {
107  const word& key = iter().keyword();
108 
109  if (!iter().isDict())
110  {
111  if (key != "libs")
112  {
113  IOWarningInFunction(*this)
114  << "Entry " << key << " is not a dictionary" << endl;
115  }
116 
117  continue;
118  }
119 
120  const dictionary& dict = iter().dict();
121  const bool enabled = dict.lookupOrDefault("enabled", true);
122 
123  newDigs[nFunc] = dict.digest();
124 
125  label oldIndex;
126  functionObject* objPtr = remove(key, oldIndex);
127 
128  if (objPtr)
129  {
130  if (enabled)
131  {
132  // Dictionary changed for an existing functionObject
133  if (newDigs[nFunc] != digests_[oldIndex])
134  {
135  ok = objPtr->read(dict) && ok;
136  }
137  }
138  else
139  {
140  // Delete the disabled functionObject
141  delete objPtr;
142  objPtr = nullptr;
143  continue;
144  }
145  }
146  else if (enabled)
147  {
148  autoPtr<functionObject> foPtr;
149 
150  if
151  (
152  dict.found("executeControl")
153  || dict.found("writeControl")
154  || dict.found("outputControl")
155  )
156  {
157  foPtr.set
158  (
159  new functionObjects::timeControl(key, time_, dict)
160  );
161  }
162  else
163  {
164  foPtr = functionObject::New(key, time_, dict);
165  }
166 
167  if (foPtr.valid())
168  {
169  objPtr = foPtr.ptr();
170  }
171  else
172  {
173  ok = false;
174  }
175  }
176 
177  // Insert active functionObjects into the list
178  if (objPtr)
179  {
180  newPtrs.set(nFunc, objPtr);
181  newIndices.insert(key, nFunc);
182  nFunc++;
183  }
184  }
185 
186  newPtrs.setSize(nFunc);
187  newDigs.setSize(nFunc);
188 
189  // Updating the PtrList of functionObjects deletes any
190  // existing unused functionObjects
192  digests_.transfer(newDigs);
193  indices_.transfer(newIndices);
194  }
195  else
196  {
198  digests_.clear();
199  indices_.clear();
200  }
201 
202  return ok;
203 }
204 
205 
206 Foam::functionObject* Foam::functionObjectList::remove
207 (
208  const word& key,
209  label& oldIndex
210 )
211 {
212  functionObject* ptr = 0;
213 
214  // Find index of existing functionObject
215  HashTable<label>::iterator fnd = indices_.find(key);
216 
217  if (fnd != indices_.end())
218  {
219  oldIndex = fnd();
220 
221  // Retrieve the pointer and remove it from the old list
222  ptr = this->PtrList<functionObject>::set(oldIndex, 0).ptr();
223  indices_.erase(fnd);
224  }
225  else
226  {
227  oldIndex = -1;
228  }
229 
230  return ptr;
231 }
232 
233 
234 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
235 
237 (
238  const Time& t,
239  const bool execution
240 )
241 :
242  IOdictionary(readFunctionsDict(t, execution)),
244  digests_(),
245  indices_(),
246  time_(t),
247  execution_(execution),
248  updated_(false)
249 {
250  if (t.controlDict().found("functions"))
251  {
252  if (!headerOk())
253  {
254  merge(t.controlDict().subDict("functions"));
255  }
256  else
257  {
259  << "Both " << relativeObjectPath()
260  << " and " << t.controlDict().relativeObjectPath()/"functions"
261  << " found, the latter will be ignored." << endl;
262  }
263  }
264 }
265 
266 
268 (
269  const argList& args,
270  const Time& runTime
271 )
272 {
273  autoPtr<functionObjectList> functionsPtr(new functionObjectList(runTime));
274  dictionary& functionsDict = *functionsPtr;
275 
277 
278  // Set the region name if specified
279  if (args.optionFound("region"))
280  {
281  region = args["region"];
282  }
283 
284  if
285  (
286  args.optionFound("dict")
287  || args.optionFound("func")
288  || args.optionFound("funcs")
289  )
290  {
291  // Remove functions specified in the functions dictionary
292  functionsDict.clear();
293 
294  if (args.optionFound("dict"))
295  {
296  functionsDict.merge
297  (
299  (
300  IOobject
301  (
302  args["dict"],
303  runTime,
305  )
306  )
307  );
308  }
309 
310  if (args.optionFound("func"))
311  {
313  (
314  "function",
315  {args["func"], labelMin},
316  functionsDict,
318  "system",
319  region,
320  args.commandLine()
321  );
322  }
323 
324  if (args.optionFound("funcs"))
325  {
326  wordList funcs(args.optionLookup("funcs")());
327 
328  forAll(funcs, i)
329  {
331  (
332  "function",
333  {funcs[i], labelMin},
334  functionsDict,
336  "system",
337  region,
338  args.commandLine()
339  );
340  }
341  }
342  }
343 
344  functionsPtr->readDict();
345 
346  return functionsPtr;
347 }
348 
349 
350 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
351 
353 {}
354 
355 
356 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
357 
359 {
361  digests_.clear();
362  indices_.clear();
363  updated_ = false;
364 }
365 
366 
368 {
369  forAll(*this, oi)
370  {
371  if (operator[](oi).name() == name)
372  {
373  return oi;
374  }
375  }
376 
377  return -1;
378 }
379 
380 
382 {
383  bool ok = readDict();
384 
385  if (execution_)
386  {
387  forAll(*this, oi)
388  {
389  if (operator[](oi).executeAtStart())
390  {
391  ok = operator[](oi).execute() && ok;
392  ok = operator[](oi).write() && ok;
393  }
394  }
395  }
396 
397  return ok;
398 }
399 
400 
402 {
403  bool ok = true;
404 
405  if (execution_)
406  {
407  if (!updated_)
408  {
409  readDict();
410  }
411 
412  forAll(*this, oi)
413  {
414  ok = operator[](oi).execute() && ok;
415  ok = operator[](oi).write() && ok;
416  }
417  }
418 
419  return ok;
420 }
421 
422 
424 {
425  bool ok = true;
426 
427  if (execution_)
428  {
429  if (!updated_)
430  {
431  readDict();
432  }
433 
434  forAll(*this, oi)
435  {
436  ok = operator[](oi).end() && ok;
437  }
438  }
439 
440  return ok;
441 }
442 
443 
445 {
446  scalar result = vGreat;
447 
448  if (execution_)
449  {
450  if (!updated_)
451  {
452  readDict();
453  }
454 
455  forAll(*this, oi)
456  {
457  result = min(result, operator[](oi).timeToNextAction());
458  }
459  }
460 
461  return result;
462 }
463 
464 
466 {
467  scalar result = vGreat;
468 
469  forAll(*this, oi)
470  {
471  result = min(result, operator[](oi).maxDeltaT());
472  }
473 
474  return result;
475 }
476 
477 
479 {
480  if (regIOobject::read())
481  {
482  return readDict();
483  }
484  else
485  {
486  return false;
487  }
488 }
489 
490 
492 {
493  if (execution_)
494  {
495  forAll(*this, oi)
496  {
497  operator[](oi).movePoints(mesh);
498  }
499  }
500 }
501 
502 
504 {
505  if (execution_)
506  {
507  forAll(*this, oi)
508  {
509  operator[](oi).topoChange(map);
510  }
511  }
512 }
513 
514 
516 {
517  if (execution_)
518  {
519  forAll(*this, oi)
520  {
521  operator[](oi).mapMesh(map);
522  }
523  }
524 }
525 
526 
528 {
529  if (execution_)
530  {
531  forAll(*this, oi)
532  {
533  operator[](oi).distribute(map);
534  }
535  }
536 }
537 
538 
539 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
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:275
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
T lookupOrDefault(const word &, const T &) 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:778
void clear()
Clear the dictionary.
Definition: dictionary.C:1358
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1313
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:434
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.
Motion of the mesh specified as a list of pointMeshMovers.
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:78
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
bool headerOk()
Read and check header info.
Definition: regIOobject.C:429
virtual bool read()
Read object.
Templated form of IOobject providing type information for file reading and header type checking.
Definition: IOobject.H:545
A class for handling words, derived from string.
Definition: word.H:63
static const word null
An empty word.
Definition: word.H:78
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
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:288
messageStream Info
const fvMesh & region(const dictionary &dict)
Cast the give dictionary to the corresponding region fvMesh.
Definition: fvMesh.C:1831
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & indentOrNl(Ostream &os)
Indent stream or add newline if indent level == 0.
Definition: Ostream.H:250
static const label labelMin
Definition: label.H:61
bool readConfigFile(const word &configType, const Tuple2< string, label > &argString, dictionary &parentDict, const fileName &configFilesPath, const word &configFilesDir, const word &region=word::null, const string &command=string::null)
Read the specified configuration file.
Definition: dictionaryIO.C:482
dictionary dict
Foam::argList args(argc, argv)