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-2013 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 "Time.H"
28 #include "mapPolyMesh.H"
29 
30 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
31 
32 Foam::functionObject* Foam::functionObjectList::remove
33 (
34  const word& key,
35  label& oldIndex
36 )
37 {
38  functionObject* ptr = 0;
39 
40  // Find index of existing functionObject
41  HashTable<label>::iterator fnd = indices_.find(key);
42 
43  if (fnd != indices_.end())
44  {
45  oldIndex = fnd();
46 
47  // retrieve the pointer and remove it from the old list
48  ptr = this->set(oldIndex, 0).ptr();
49  indices_.erase(fnd);
50  }
51  else
52  {
53  oldIndex = -1;
54  }
55 
56  return ptr;
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 Foam::functionObjectList::functionObjectList
63 (
64  const Time& t,
65  const bool execution
66 )
67 :
69  digests_(),
70  indices_(),
71  time_(t),
72  parentDict_(t.controlDict()),
73  execution_(execution),
74  updated_(false)
75 {}
76 
77 
78 Foam::functionObjectList::functionObjectList
79 (
80  const Time& t,
81  const dictionary& parentDict,
82  const bool execution
83 )
84 :
86  digests_(),
87  indices_(),
88  time_(t),
89  parentDict_(parentDict),
90  execution_(execution),
91  updated_(false)
92 {}
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 {
106  digests_.clear();
107  indices_.clear();
108  updated_ = false;
109 }
110 
111 
113 {
114  forAll(*this, objectI)
115  {
116  if (operator[](objectI).name() == name)
117  {
118  return objectI;
119  }
120  }
121 
122  return -1;
123 }
124 
125 
127 {
128  execution_ = true;
129 }
130 
131 
133 {
134  // for safety, also force a read() when execution is turned back on
135  updated_ = execution_ = false;
136 }
137 
138 
140 {
141  return execution_;
142 }
143 
144 
146 {
147  return read();
148 }
149 
150 
151 bool Foam::functionObjectList::execute(const bool forceWrite)
152 {
153  bool ok = true;
154 
155  if (execution_)
156  {
157  if (!updated_)
158  {
159  read();
160  }
161 
162  forAll(*this, objectI)
163  {
164  ok = operator[](objectI).execute(forceWrite) && ok;
165  }
166  }
167 
168  return ok;
169 }
170 
171 
173 {
174  bool ok = true;
175 
176  if (execution_)
177  {
178  if (!updated_)
179  {
180  read();
181  }
182 
183  forAll(*this, objectI)
184  {
185  ok = operator[](objectI).end() && ok;
186  }
187  }
188 
189  return ok;
190 }
191 
192 
194 {
195  bool ok = true;
196 
197  if (execution_)
198  {
199  if (!updated_)
200  {
201  read();
202  }
203 
204  forAll(*this, objectI)
205  {
206  ok = operator[](objectI).timeSet() && ok;
207  }
208  }
209 
210  return ok;
211 }
212 
213 
215 {
216  bool ok = true;
217 
218  if (execution_)
219  {
220  if (!updated_)
221  {
222  read();
223  }
224 
225  forAll(*this, objectI)
226  {
227  ok = operator[](objectI).adjustTimeStep() && ok;
228  }
229  }
230 
231  return ok;
232 }
233 
234 
236 {
237  bool ok = true;
238  updated_ = execution_;
239 
240  // avoid reading/initializing if execution is off
241  if (!execution_)
242  {
243  return ok;
244  }
245 
246  // Update existing and add new functionObjects
247  const entry* entryPtr = parentDict_.lookupEntryPtr
248  (
249  "functions",
250  false,
251  false
252  );
253 
254  if (entryPtr)
255  {
256  PtrList<functionObject> newPtrs;
257  List<SHA1Digest> newDigs;
258  HashTable<label> newIndices;
259 
260  label nFunc = 0;
261 
262  if (entryPtr->isDict())
263  {
264  // a dictionary of functionObjects
265  const dictionary& functionDicts = entryPtr->dict();
266 
267  newPtrs.setSize(functionDicts.size());
268  newDigs.setSize(functionDicts.size());
269 
270  forAllConstIter(dictionary, functionDicts, iter)
271  {
272  // safety:
273  if (!iter().isDict())
274  {
275  continue;
276  }
277  const word& key = iter().keyword();
278  const dictionary& dict = iter().dict();
279 
280  newDigs[nFunc] = dict.digest();
281 
282  label oldIndex;
283  functionObject* objPtr = remove(key, oldIndex);
284  if (objPtr)
285  {
286  // an existing functionObject, and dictionary changed
287  if (newDigs[nFunc] != digests_[oldIndex])
288  {
289  ok = objPtr->read(dict) && ok;
290  }
291  }
292  else
293  {
294  // new functionObject
295  objPtr = functionObject::New(key, time_, dict).ptr();
296  ok = objPtr->start() && ok;
297  }
298 
299  newPtrs.set(nFunc, objPtr);
300  newIndices.insert(key, nFunc);
301  nFunc++;
302  }
303  }
304  else
305  {
306  // a list of functionObjects
307  PtrList<entry> functionDicts(entryPtr->stream());
308 
309  newPtrs.setSize(functionDicts.size());
310  newDigs.setSize(functionDicts.size());
311 
312  forAllIter(PtrList<entry>, functionDicts, iter)
313  {
314  // safety:
315  if (!iter().isDict())
316  {
317  continue;
318  }
319  const word& key = iter().keyword();
320  const dictionary& dict = iter().dict();
321 
322  newDigs[nFunc] = dict.digest();
323 
324  label oldIndex;
325  functionObject* objPtr = remove(key, oldIndex);
326  if (objPtr)
327  {
328  // an existing functionObject, and dictionary changed
329  if (newDigs[nFunc] != digests_[oldIndex])
330  {
331  ok = objPtr->read(dict) && ok;
332  }
333  }
334  else
335  {
336  // new functionObject
337  objPtr = functionObject::New(key, time_, dict).ptr();
338  ok = objPtr->start() && ok;
339  }
340 
341  newPtrs.set(nFunc, objPtr);
342  newIndices.insert(key, nFunc);
343  nFunc++;
344  }
345  }
346 
347  // safety:
348  newPtrs.setSize(nFunc);
349  newDigs.setSize(nFunc);
350 
351  // updating the PtrList of functionObjects also deletes any existing,
352  // but unused functionObjects
354  digests_.transfer(newDigs);
355  indices_.transfer(newIndices);
356  }
357  else
358  {
360  digests_.clear();
361  indices_.clear();
362  }
363 
364  return ok;
365 }
366 
367 
369 {
370  if (execution_)
371  {
372  forAll(*this, objectI)
373  {
374  operator[](objectI).updateMesh(mpm);
375  }
376  }
377 }
378 
379 
381 {
382  if (execution_)
383  {
384  forAll(*this, objectI)
385  {
386  operator[](objectI).movePoints(mesh);
387  }
388  }
389 }
390 
391 
392 // ************************************************************************* //
virtual void updateMesh(const mapPolyMesh &mpm)=0
Update for changes of mesh.
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
void setSize(const label)
Reset size of PtrList. If extending the PtrList, new entries are.
Definition: PtrList.C:142
void transfer(PtrList< T > &)
Transfer the contents of the argument PtrList into this PtrList.
Definition: PtrList.C:200
bool set(const label) const
Is element set.
Definition: PtrListI.H:107
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:379
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void clear()
Clear the list of function objects.
virtual bool execute(const bool forceWrite=false)
Called at each ++ or += of the time-loop. forceWrite overrides.
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
SHA1Digest digest() const
Return the SHA1 digest of the dictionary contents.
Definition: dictionary.C:272
void clear()
Clear the PtrList, i.e. set size to zero deleting all the.
Definition: PtrList.C:185
Abstract base-class for Time/database function objects.
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
virtual bool end()
Called when Time::run() determines that the time-loop exits.
#define forAllIter(Container, container, iter)
Definition: UList.H:440
A class for handling words, derived from string.
Definition: word.H:59
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
dynamicFvMesh & mesh
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
label size() const
Return number of elements in list.
Definition: DLListBaseI.H:77
virtual ~functionObjectList()
Destructor.
virtual bool read()
Read and set the function objects if their data have changed.
virtual bool execute(const bool forceWrite)=0
Called at each ++ or += of the time-loop. forceWrite overrides the.
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:345
virtual bool end()
Called when Time::run() determines that the time-loop exits.
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
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
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
virtual bool status() const
Return the execution status (on/off) of the function objects.
dictionary dict
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
virtual void off()
Switch the function objects off.
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
void setSize(const label)
Reset size of List.
Definition: List.C:318
void clear()
Clear all entries from table.
Definition: HashTable.C:473
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
#define forAll(list, i)
Definition: UList.H:421
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
virtual bool start()=0
Called at the start of the time-loop.
virtual label findObjectID(const word &name) const
Find the ID of a given function object by name.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
virtual void movePoints(const polyMesh &mesh)=0
Update for changes of mesh.
void transfer(HashTable< T, Key, Hash > &)
Transfer the contents of the argument table into this table.
Definition: HashTable.C:518
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:106
virtual bool start()
Called at the start of the time-loop.
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:139
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
friend class iterator
Declare friendship with the iterator.
Definition: HashTable.H:189
const dictionary & controlDict() const
Definition: Time.H:286
virtual void on()
Switch the function objects on.
virtual bool read(const dictionary &)=0
Read and set the function object if its data have changed.
const functionObject & operator[](const label) const
Return element const reference.