functionObjectList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "Time.H"
28 #include "mapPolyMesh.H"
29 
30 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
31 
33 {
34  // Cannot set the state dictionary on construction since Time has not
35  // been fully initialised
36  stateDictPtr_.reset
37  (
38  new IOdictionary
39  (
40  IOobject
41  (
42  "functionObjectProperties",
43  time_.timeName(),
44  "uniform"/word("functionObjects"),
45  time_,
48  )
49  )
50  );
51 }
52 
53 
55 (
56  const word& key,
57  label& oldIndex
58 )
59 {
60  functionObject* ptr = 0;
61 
62  // Find index of existing functionObject
63  HashTable<label>::iterator fnd = indices_.find(key);
64 
65  if (fnd != indices_.end())
66  {
67  oldIndex = fnd();
68 
69  // retrieve the pointer and remove it from the old list
70  ptr = this->set(oldIndex, 0).ptr();
71  indices_.erase(fnd);
72  }
73  else
74  {
75  oldIndex = -1;
76  }
77 
78  return ptr;
79 }
80 
81 
82 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
83 
85 (
86  const Time& t,
87  const bool execution
88 )
89 :
91  digests_(),
92  indices_(),
93  time_(t),
94  parentDict_(t.controlDict()),
95  stateDictPtr_(),
96  execution_(execution),
97  updated_(false)
98 {}
99 
100 
102 (
103  const Time& t,
104  const dictionary& parentDict,
105  const bool execution
106 )
107 :
109  digests_(),
110  indices_(),
111  time_(t),
112  parentDict_(parentDict),
113  stateDictPtr_(),
114  execution_(execution),
115  updated_(false)
116 {}
117 
118 
119 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
120 
122 {}
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
128 {
129  if (!stateDictPtr_.valid())
130  {
131  createStateDict();
132  }
133 
134  return stateDictPtr_();
135 }
136 
137 
139 {
140  if (!stateDictPtr_.valid())
141  {
142  createStateDict();
143  }
144 
145  return stateDictPtr_();
146 }
147 
148 
150 {
152  digests_.clear();
153  indices_.clear();
154  updated_ = false;
155 }
156 
157 
159 {
160  forAll(*this, objectI)
161  {
162  if (operator[](objectI).name() == name)
163  {
164  return objectI;
165  }
166  }
167 
168  return -1;
169 }
170 
171 
173 {
174  execution_ = true;
175 }
176 
177 
179 {
180  // for safety, also force a read() when execution is turned back on
181  updated_ = execution_ = false;
182 }
183 
184 
186 {
187  return execution_;
188 }
189 
190 
192 {
193  return read();
194 }
195 
196 
197 bool Foam::functionObjectList::execute(const bool forceWrite)
198 {
199  bool ok = true;
200 
201  if (execution_)
202  {
203  if (!updated_)
204  {
205  read();
206  }
207 
208  forAll(*this, objectI)
209  {
210  ok = operator[](objectI).execute(forceWrite) && ok;
211  }
212  }
213 
214  // Force writing of state dictionary after function object execution
215  if (time_.outputTime())
216  {
217  label oldPrecision = IOstream::precision_;
219 
220  stateDictPtr_->writeObject
221  (
224  time_.writeCompression()
225  );
226 
227  IOstream::precision_ = oldPrecision;
228  }
229 
230  return ok;
231 }
232 
233 
235 {
236  bool ok = true;
237 
238  if (execution_)
239  {
240  if (!updated_)
241  {
242  read();
243  }
244 
245  forAll(*this, objectI)
246  {
247  ok = operator[](objectI).end() && ok;
248  }
249  }
250 
251  return ok;
252 }
253 
254 
256 {
257  bool ok = true;
258 
259  if (execution_)
260  {
261  if (!updated_)
262  {
263  read();
264  }
265 
266  forAll(*this, objectI)
267  {
268  ok = operator[](objectI).timeSet() && ok;
269  }
270  }
271 
272  return ok;
273 }
274 
275 
277 {
278  bool ok = true;
279 
280  if (execution_)
281  {
282  if (!updated_)
283  {
284  read();
285  }
286 
287  forAll(*this, objectI)
288  {
289  ok = operator[](objectI).adjustTimeStep() && ok;
290  }
291  }
292 
293  return ok;
294 }
295 
296 
298 {
299  if (!stateDictPtr_.valid())
300  {
301  createStateDict();
302  }
303 
304  bool ok = true;
305  updated_ = execution_;
306 
307  // avoid reading/initializing if execution is off
308  if (!execution_)
309  {
310  return ok;
311  }
312 
313  // Update existing and add new functionObjects
314  const entry* entryPtr = parentDict_.lookupEntryPtr
315  (
316  "functions",
317  false,
318  false
319  );
320 
321  if (entryPtr)
322  {
323  PtrList<functionObject> newPtrs;
324  List<SHA1Digest> newDigs;
325  HashTable<label> newIndices;
326 
327  label nFunc = 0;
328 
329  if (entryPtr->isDict())
330  {
331  // a dictionary of functionObjects
332  const dictionary& functionDicts = entryPtr->dict();
333 
334  newPtrs.setSize(functionDicts.size());
335  newDigs.setSize(functionDicts.size());
336 
337  forAllConstIter(dictionary, functionDicts, iter)
338  {
339  // safety:
340  if (!iter().isDict())
341  {
342  continue;
343  }
344  const word& key = iter().keyword();
345  const dictionary& dict = iter().dict();
346 
347  newDigs[nFunc] = dict.digest();
348 
349  label oldIndex;
350  functionObject* objPtr = remove(key, oldIndex);
351  if (objPtr)
352  {
353  // an existing functionObject, and dictionary changed
354  if (newDigs[nFunc] != digests_[oldIndex])
355  {
356  ok = objPtr->read(dict) && ok;
357  }
358  }
359  else
360  {
361  // new functionObject
362  objPtr = functionObject::New(key, time_, dict).ptr();
363  ok = objPtr->start() && ok;
364  }
365 
366  newPtrs.set(nFunc, objPtr);
367  newIndices.insert(key, nFunc);
368  nFunc++;
369  }
370  }
371  else
372  {
373  // a list of functionObjects
374  PtrList<entry> functionDicts(entryPtr->stream());
375 
376  newPtrs.setSize(functionDicts.size());
377  newDigs.setSize(functionDicts.size());
378 
379  forAllIter(PtrList<entry>, functionDicts, iter)
380  {
381  // safety:
382  if (!iter().isDict())
383  {
384  continue;
385  }
386  const word& key = iter().keyword();
387  const dictionary& dict = iter().dict();
388 
389  newDigs[nFunc] = dict.digest();
390 
391  label oldIndex;
392  functionObject* objPtr = remove(key, oldIndex);
393  if (objPtr)
394  {
395  // an existing functionObject, and dictionary changed
396  if (newDigs[nFunc] != digests_[oldIndex])
397  {
398  ok = objPtr->read(dict) && ok;
399  }
400  }
401  else
402  {
403  // new functionObject
404  objPtr = functionObject::New(key, time_, dict).ptr();
405  ok = objPtr->start() && ok;
406  }
407 
408  newPtrs.set(nFunc, objPtr);
409  newIndices.insert(key, nFunc);
410  nFunc++;
411  }
412  }
413 
414  // safety:
415  newPtrs.setSize(nFunc);
416  newDigs.setSize(nFunc);
417 
418  // updating the PtrList of functionObjects also deletes any existing,
419  // but unused functionObjects
421  digests_.transfer(newDigs);
422  indices_.transfer(newIndices);
423  }
424  else
425  {
427  digests_.clear();
428  indices_.clear();
429  }
430 
431  return ok;
432 }
433 
434 
436 {
437  if (execution_)
438  {
439  forAll(*this, objectI)
440  {
441  operator[](objectI).updateMesh(mpm);
442  }
443  }
444 }
445 
446 
448 {
449  if (execution_)
450  {
451  forAll(*this, objectI)
452  {
453  operator[](objectI).movePoints(mesh);
454  }
455  }
456 }
457 
458 
459 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::functionObjectList::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
Definition: functionObjectList.C:447
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::functionObjectList::status
virtual bool status() const
Return the execution status (on/off) of the function objects.
Definition: functionObjectList.C:185
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::functionObjectList::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: functionObjectList.C:276
Foam::functionObject::read
virtual bool read(const dictionary &)=0
Read and set the function object if its data have changed.
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
Foam::functionObjectList::createStateDict
void createStateDict() const
Create state dictionary.
Definition: functionObjectList.C:32
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::functionObjectList::on
virtual void on()
Switch the function objects on.
Definition: functionObjectList.C:172
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
mapPolyMesh.H
functionObjectList.H
Foam::functionObjectList::remove
functionObject * remove(const word &, label &oldIndex)
Remove and return the function object pointer by name,.
Definition: functionObjectList.C:55
Foam::HashTable::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Foam::functionObjectList::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: functionObjectList.C:435
Foam::IOstream::precision_
static unsigned int precision_
Default precision.
Definition: IOstream.H:209
Foam::functionObjectList::stateDict
IOdictionary & stateDict()
Return the state dictionary.
Definition: functionObjectList.C:127
Foam::IOstream::currentVersion
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
Foam::functionObjectList::start
virtual bool start()
Called at the start of the time-loop.
Definition: functionObjectList.C:191
Foam::functionObjectList::findObjectID
virtual label findObjectID(const word &name) const
Find the ID of a given function object by name.
Definition: functionObjectList.C:158
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:58
Foam::functionObjectList::clear
virtual void clear()
Clear the list of function objects.
Definition: functionObjectList.C:149
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::PtrList::set
bool set(const label) const
Is element set.
Foam::label
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
Foam::entry::isDict
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
Foam::functionObjectList::off
virtual void off()
Switch the function objects off.
Definition: functionObjectList.C:178
Foam::functionObject::New
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
Definition: functionObject.C:51
Foam::PtrList< functionObject >
Foam::PtrList::transfer
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::Time::controlDict
const dictionary & controlDict() const
Definition: Time.H:286
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::functionObjectList::functionObjectList
functionObjectList(const functionObjectList &)
Disallow default bitwise copy construct.
Foam::PtrList::clear
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
Foam::HashTable::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::List::setSize
void setSize(const label)
Reset size of List.
Foam::Time::timeName
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:741
Foam::functionObjectList::execute
virtual bool execute(const bool forceWrite=false)
Called at each ++ or += of the time-loop. forceWrite overrides.
Definition: functionObjectList.C:197
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::functionObjectList::stateDictPtr_
autoPtr< IOdictionary > stateDictPtr_
Function object properties - stores state information.
Definition: functionObjectList.H:78
Foam::PtrList::size
label size() const
Return the number of elements in the PtrList.
Definition: PtrListI.H:32
Foam::PtrList::setSize
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
Foam::functionObjectList::time_
const Time & time_
Definition: functionObjectList.H:70
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::dictionary::digest
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:270
Foam::functionObjectList::~functionObjectList
virtual ~functionObjectList()
Destructor.
Definition: functionObjectList.C:121
Foam::functionObject::start
virtual bool start()=0
Called at the start of the time-loop.
Foam::functionObjectList::read
virtual bool read()
Read and set the function objects if their data have changed.
Definition: functionObjectList.C:297
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::functionObjectList::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: functionObjectList.C:234
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::functionObjectList::timeSet
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
Definition: functionObjectList.C:255