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"], 0},
311  functionsDict,
313  "system",
314  region
315  );
316  }
317 
318  if (args.optionFound("funcs"))
319  {
320  wordList funcs(args.optionLookup("funcs")());
321 
322  forAll(funcs, i)
323  {
325  (
326  "function",
327  {funcs[i], 0},
328  functionsDict,
330  "system",
331  region
332  );
333  }
334  }
335  }
336 
337  functionsPtr->readDict();
338 
339  return functionsPtr;
340 }
341 
342 
343 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
344 
346 {}
347 
348 
349 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
350 
352 {
354  digests_.clear();
355  indices_.clear();
356  updated_ = false;
357 }
358 
359 
361 {
362  forAll(*this, oi)
363  {
364  if (operator[](oi).name() == name)
365  {
366  return oi;
367  }
368  }
369 
370  return -1;
371 }
372 
373 
375 {
376  bool ok = readDict();
377 
378  if (execution_)
379  {
380  forAll(*this, oi)
381  {
382  if (operator[](oi).executeAtStart())
383  {
384  ok = operator[](oi).execute() && ok;
385  ok = operator[](oi).write() && ok;
386  }
387  }
388  }
389 
390  return ok;
391 }
392 
393 
395 {
396  bool ok = true;
397 
398  if (execution_)
399  {
400  if (!updated_)
401  {
402  readDict();
403  }
404 
405  forAll(*this, oi)
406  {
407  ok = operator[](oi).execute() && ok;
408  ok = operator[](oi).write() && ok;
409  }
410  }
411 
412  return ok;
413 }
414 
415 
417 {
418  bool ok = true;
419 
420  if (execution_)
421  {
422  if (!updated_)
423  {
424  readDict();
425  }
426 
427  forAll(*this, oi)
428  {
429  ok = operator[](oi).end() && ok;
430  }
431  }
432 
433  return ok;
434 }
435 
436 
438 {
439  scalar result = vGreat;
440 
441  if (execution_)
442  {
443  if (!updated_)
444  {
445  readDict();
446  }
447 
448  forAll(*this, oi)
449  {
450  result = min(result, operator[](oi).timeToNextAction());
451  }
452  }
453 
454  return result;
455 }
456 
457 
459 {
460  scalar result = vGreat;
461 
462  forAll(*this, oi)
463  {
464  result = min(result, operator[](oi).maxDeltaT());
465  }
466 
467  return result;
468 }
469 
470 
472 {
473  if (regIOobject::read())
474  {
475  return readDict();
476  }
477  else
478  {
479  return false;
480  }
481 }
482 
483 
485 {
486  if (execution_)
487  {
488  forAll(*this, oi)
489  {
490  operator[](oi).movePoints(mesh);
491  }
492  }
493 }
494 
495 
497 {
498  if (execution_)
499  {
500  forAll(*this, oi)
501  {
502  operator[](oi).topoChange(map);
503  }
504  }
505 }
506 
507 
509 {
510  if (execution_)
511  {
512  forAll(*this, oi)
513  {
514  operator[](oi).mapMesh(map);
515  }
516  }
517 }
518 
519 
521 {
522  if (execution_)
523  {
524  forAll(*this, oi)
525  {
526  operator[](oi).distribute(map);
527  }
528  }
529 }
530 
531 
532 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
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
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
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:849
void clear()
Clear the dictionary.
Definition: dictionary.C:1359
T lookupOrDefault(const word &, const T &, const bool writeDefault=writeOptionalEntries > 0) const
Find and return a T, if not found return the given default.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:539
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1314
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:505
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:530
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
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:258
bool readConfigFile(const word &configType, const Tuple2< string, label > &argString, dictionary &parentDict, const fileName &configFilesPath, const word &configFilesDir, const word &region=word::null)
Read the specified configuration file.
Definition: dictionaryIO.C:494
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict
Foam::argList args(argc, argv)