objectRegistry.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-2019 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 "objectRegistry.H"
27 #include "Time.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  defineTypeNameAndDebug(objectRegistry, 0);
34 }
35 
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 bool Foam::objectRegistry::parentNotTime() const
40 {
41  return (&parent_ != dynamic_cast<const objectRegistry*>(&time_));
42 }
43 
44 
45 void Foam::objectRegistry::readCacheTemporaryObjects() const
46 {
47  if
48  (
49  !cacheTemporaryObjectsSet_
50  && time_.controlDict().found("cacheTemporaryObjects")
51  )
52  {
53  cacheTemporaryObjectsSet_ = true;
54 
55  const dictionary& controlDict = time_.controlDict();
56 
57  wordList cacheTemporaryObjects;
58 
59  if (controlDict.isDict("cacheTemporaryObjects"))
60  {
61  if(controlDict.subDict("cacheTemporaryObjects").found(name()))
62  {
63  controlDict.subDict("cacheTemporaryObjects").lookup(name())
64  >> cacheTemporaryObjects;
65  }
66  }
67  else
68  {
69  controlDict.lookup("cacheTemporaryObjects")
70  >> cacheTemporaryObjects;
71  }
72 
73  forAll(cacheTemporaryObjects, i)
74  {
75  cacheTemporaryObjects_.insert
76  (
77  cacheTemporaryObjects[i],
78  {false, false}
79  );
80  }
81  }
82 }
83 
84 
85 void Foam::objectRegistry::deleteCachedObject(regIOobject& cachedOb) const
86 {
87  cachedOb.release();
88  cachedOb.checkOut();
89  cachedOb.rename(cachedOb.name() + "Cached");
90  delete &cachedOb;
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
97 (
98  const Time& t,
99  const label nIoObjects
100 )
101 :
103  (
104  IOobject
105  (
106  string::validate<word>(t.caseName()),
107  t.path(),
108  t,
111  false
112  ),
113  true // to flag that this is the top-level regIOobject
114  ),
115  HashTable<regIOobject*>(nIoObjects),
116  time_(t),
117  parent_(t),
118  dbDir_(name()),
119  event_(1),
120  cacheTemporaryObjectsSet_(false)
121 {}
122 
123 
125 (
126  const IOobject& io,
127  const label nIoObjects
128 )
129 :
130  regIOobject(io),
131  HashTable<regIOobject*>(nIoObjects),
132  time_(io.time()),
133  parent_(io.db()),
134  dbDir_(parent_.dbDir()/local()/name()),
135  event_(1),
136  cacheTemporaryObjectsSet_(false)
137 {
138  writeOpt() = IOobject::AUTO_WRITE;
139 }
140 
141 
142 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
143 
145 {
146  cacheTemporaryObjects_.clear();
147  clear();
148 }
149 
150 
151 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152 
154 {
156 }
157 
158 
160 {
162 }
163 
164 
166 {
167  wordList objectNames(size());
168 
169  label count=0;
170  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
171  {
172  if (iter()->type() == ClassName)
173  {
174  objectNames[count++] = iter.key();
175  }
176  }
177 
178  objectNames.setSize(count);
179 
180  return objectNames;
181 }
182 
183 
185 {
186  wordList sortedLst = names(ClassName);
187  sort(sortedLst);
188 
189  return sortedLst;
190 }
191 
192 
194 (
195  const word& name,
196  const bool forceCreate
197 ) const
198 {
199  if (forceCreate && !foundObject<objectRegistry>(name))
200  {
201  objectRegistry* fieldsCachePtr = new objectRegistry
202  (
203  IOobject
204  (
205  name,
206  time().constant(),
207  *this,
210  )
211  );
212  fieldsCachePtr->store();
213  }
214  return lookupObject<objectRegistry>(name);
215 }
216 
217 
219 {
220  label curEvent = event_++;
221 
222  if (event_ == labelMax)
223  {
224  if (objectRegistry::debug)
225  {
227  << "Event counter has overflowed. "
228  << "Resetting counter on all dependent objects." << nl
229  << "This might cause extra evaluations." << endl;
230  }
231 
232  // Reset event counter
233  curEvent = 1;
234  event_ = 2;
235 
236  for (const_iterator iter = begin(); iter != end(); ++iter)
237  {
238  const regIOobject& io = *iter();
239 
240  if (objectRegistry::debug)
241  {
242  Pout<< "objectRegistry::getEvent() : "
243  << "resetting count on " << iter.key() << endl;
244  }
245 
246  if (io.eventNo() != 0)
247  {
248  const_cast<regIOobject&>(io).eventNo() = curEvent;
249  }
250  }
251  }
252 
253  return curEvent;
254 }
255 
256 
258 {
259  if (objectRegistry::debug)
260  {
261  Pout<< "objectRegistry::checkIn(regIOobject&) : "
262  << name() << " : checking in " << io.name()
263  << " of type " << io.type()
264  << endl;
265  }
266 
267  // Delete cached object with the same name as io and if it is in the
268  // cacheTemporaryObjects list
269  if (cacheTemporaryObjects_.size())
270  {
271  HashTable<Pair<bool>>::iterator cacheIter
272  (
273  cacheTemporaryObjects_.find(io.name())
274  );
275 
276  if (cacheIter != cacheTemporaryObjects_.end())
277  {
278  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
279 
280  if (iter != end() && iter() != &io && iter()->ownedByRegistry())
281  {
282  if (objectRegistry::debug)
283  {
284  Pout<< "objectRegistry::checkIn(regIOobject&) : "
285  << name() << " : deleting cached object " << iter.key()
286  << endl;
287  }
288 
289  cacheIter().first() = false;
290  deleteCachedObject(*iter());
291  }
292  }
293  }
294 
295  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
296 }
297 
298 
300 {
301  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
302 
303  if (iter != end())
304  {
305  if (objectRegistry::debug)
306  {
307  Pout<< "objectRegistry::checkOut(regIOobject&) : "
308  << name() << " : checking out " << iter.key()
309  << endl;
310  }
311 
312  if (iter() != &io)
313  {
314  if (objectRegistry::debug)
315  {
317  << name() << " : attempt to checkOut copy of "
318  << iter.key()
319  << endl;
320  }
321 
322  return false;
323  }
324  else
325  {
326  regIOobject* object = iter();
327 
328  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
329 
330  if (io.ownedByRegistry())
331  {
332  delete object;
333  }
334 
335  return hasErased;
336  }
337  }
338  else
339  {
340  if (objectRegistry::debug)
341  {
342  Pout<< "objectRegistry::checkOut(regIOobject&) : "
343  << name() << " : could not find " << io.name()
344  << " in registry " << name()
345  << endl;
346  }
347  }
348 
349  return false;
350 }
351 
352 
354 {
355  List<regIOobject*> myObjects(size());
356  label nMyObjects = 0;
357 
358  for (iterator iter = begin(); iter != end(); ++iter)
359  {
360  if (iter()->ownedByRegistry())
361  {
362  myObjects[nMyObjects++] = iter();
363  }
364  }
365 
366  for (label i=0; i < nMyObjects; i++)
367  {
368  checkOut(*myObjects[i]);
369  }
370 }
371 
372 
374 (
375  const word& name
376 ) const
377 {
378  if (!cacheTemporaryObjects_.found(name))
379  {
380  cacheTemporaryObjects_.insert(name, {false, false});
381  }
382 }
383 
384 
386 (
387  const word& name
388 ) const
389 {
390  return cacheTemporaryObjects_.found(name);
391 }
392 
393 
395 (
396  const regIOobject& ob
397 ) const
398 {
399  if (cacheTemporaryObjects_.size())
400  {
401  HashTable<Pair<bool>>::iterator iter
402  (
403  cacheTemporaryObjects_.find(ob.name())
404  );
405 
406  // If object ob if is in the cacheTemporaryObjects list
407  // and has been cached reset the cached flag
408  if (iter != cacheTemporaryObjects_.end())
409  {
410  iter().first() = false;
411  }
412  }
413 }
414 
415 
417 {
418  bool enabled = cacheTemporaryObjects_.size();
419 
421  {
422  const objectRegistry* orPtr_ =
423  dynamic_cast<const objectRegistry*>(iter());
424 
425  // Protect against re-searching the top-level registry
426  if (orPtr_ && orPtr_ != this)
427  {
428  enabled = orPtr_->checkCacheTemporaryObjects() || enabled;
429  }
430  }
431 
432  if (enabled)
433  {
434  forAllIter
435  (
436  typename HashTable<Pair<bool>>,
437  cacheTemporaryObjects_,
438  iter
439  )
440  {
441  if (!iter().second())
442  {
443  Warning
444  << "Could not find temporary object " << iter.key()
445  << " in registry " << name() << nl
446  << "Available temporary objects "
447  << temporaryObjects_
448  << endl;
449  }
450  else
451  {
452  iter().second() = false;
453  }
454  }
455 
456  temporaryObjects_.clear();
457  }
458 
459  return enabled;
460 }
461 
462 
463 void Foam::objectRegistry::rename(const word& newName)
464 {
465  regIOobject::rename(newName);
466 
467  // adjust dbDir_ as well
468  string::size_type i = dbDir_.rfind('/');
469 
470  if (i == string::npos)
471  {
472  dbDir_ = newName;
473  }
474  else
475  {
476  dbDir_.replace(i+1, string::npos, newName);
477  }
478 }
479 
480 
482 {
484  {
485  if (iter()->modified())
486  {
487  return true;
488  }
489  }
490 
491  return false;
492 }
493 
494 
496 {
497  for (iterator iter = begin(); iter != end(); ++iter)
498  {
499  if (objectRegistry::debug)
500  {
501  Pout<< "objectRegistry::readModifiedObjects() : "
502  << name() << " : Considering reading object "
503  << iter.key() << endl;
504  }
505 
506  iter()->readIfModified();
507  }
508 }
509 
510 
512 {
513  readModifiedObjects();
514  return true;
515 }
516 
517 
519 (
523  const bool write
524 ) const
525 {
526  bool ok = true;
527 
529  {
530  if (objectRegistry::debug)
531  {
532  Pout<< "objectRegistry::write() : "
533  << name() << " : Considering writing object "
534  << iter.key()
535  << " of type " << iter()->type()
536  << " with writeOpt " << iter()->writeOpt()
537  << " to file " << iter()->objectPath()
538  << endl;
539  }
540 
541  if (iter()->writeOpt() != NO_WRITE)
542  {
543  ok = iter()->writeObject(fmt, ver, cmp, write) && ok;
544  }
545  }
546 
547  return ok;
548 }
549 
550 
551 // ************************************************************************* //
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
void addTemporaryObject(const word &name) const
Add the given name to the set of temporary objects to cache.
tUEqn clear()
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
const word & name() const
Return name.
Definition: IOobject.H:303
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112
void readModifiedObjects()
Read the objects that have been modified.
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:459
wordList names() const
Return the list of names of the IOobjects.
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:28
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:371
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:65
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:142
objectRegistry(const Time &db, const label nIoObjects=128)
Construct the time objectRegistry given an initial estimate.
An ordered pair of two objects of type <T> with first() and second() elements.
Definition: contiguous.H:49
A class for handling words, derived from string.
Definition: word.H:59
wordList sortedNames() const
Return the sorted list of names of the IOobjects.
const fileName & caseName() const
Return case name.
Definition: TimePaths.H:107
void sort(UList< T > &)
Definition: UList.C:115
virtual ~objectRegistry()
Destructor.
label getEvent() const
Return new event number.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:83
static const label labelMax
Definition: label.H:62
virtual bool readIfModified()
Read object if modified.
An STL-conforming hash table.
Definition: HashTable.H:61
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
bool checkCacheTemporaryObjects() const
Check that all objects in the cacheTemporaryObjects set.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:34
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:200
static const char nl
Definition: Ostream.H:260
defineTypeNameAndDebug(combustionModel, 0)
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:376
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
messageStream Warning
List< word > wordList
A List of words.
Definition: fileName.H:54
void setSize(const label)
Reset size of List.
Definition: List.C:281
string & replace(const string &oldStr, const string &newStr, size_type start=0)
Replace first occurrence of sub-string oldStr with newStr.
Definition: string.C:64
#define WarningInFunction
Report a warning using Foam::Warning.
const Time & time() const
Return time.
Definition: IOobject.C:318
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:217
void clear()
Remove all regIOobject owned by the registry.
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:52
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
Registry of regIOobjects.
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:312
fileName path() const
Return path.
Definition: TimePaths.H:139
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
bool checkIn()
Add object to registry.
Definition: regIOobject.C:164
virtual void rename(const word &newName)
Rename.
Namespace for OpenFOAM.