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-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 "objectRegistry.H"
27 #include "Time.H"
28 #include "IOmanip.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
35 }
36 
37 
38 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
39 
40 bool Foam::objectRegistry::parentNotTime() const
41 {
42  return (&parent_ != dynamic_cast<const objectRegistry*>(&time_));
43 }
44 
45 
46 void Foam::objectRegistry::readCacheTemporaryObjects() const
47 {
48  if
49  (
50  !cacheTemporaryObjectsSet_
51  && time_.controlDict().found("cacheTemporaryObjects")
52  )
53  {
54  cacheTemporaryObjectsSet_ = true;
55 
56  const dictionary& controlDict = time_.controlDict();
57 
58  wordList cacheTemporaryObjects;
59 
60  if (controlDict.isDict("cacheTemporaryObjects"))
61  {
62  if(controlDict.subDict("cacheTemporaryObjects").found(name()))
63  {
64  controlDict.subDict("cacheTemporaryObjects").lookup(name())
65  >> cacheTemporaryObjects;
66  }
67  }
68  else
69  {
70  controlDict.lookup("cacheTemporaryObjects")
71  >> cacheTemporaryObjects;
72  }
73 
74  forAll(cacheTemporaryObjects, i)
75  {
76  cacheTemporaryObjects_.insert
77  (
78  cacheTemporaryObjects[i],
79  {false, false}
80  );
81  }
82  }
83 }
84 
85 
86 void Foam::objectRegistry::deleteCachedObject(regIOobject& cachedOb) const
87 {
88  cachedOb.release();
89  cachedOb.checkOut();
90  cachedOb.rename(cachedOb.name() + "Cached");
91  delete &cachedOb;
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
96 
98 (
99  const Time& t,
100  const label nIoObjects
101 )
102 :
104  (
105  IOobject
106  (
107  string::validate<word>(t.caseName()),
108  t.path(),
109  t,
110  IOobject::NO_READ,
111  IOobject::AUTO_WRITE,
112  false
113  ),
114  true // to flag that this is the top-level regIOobject
115  ),
116  HashTable<regIOobject*>(nIoObjects),
117  time_(t),
118  parent_(t),
119  dbDir_(fileName::null),
120  event_(1),
121  cacheTemporaryObjectsSet_(false)
122 {}
123 
124 
126 (
127  const IOobject& io,
128  const fileName& dbDir,
129  const label nIoObjects
130 )
131 :
132  regIOobject(io),
133  HashTable<regIOobject*>(nIoObjects),
134  time_(io.time()),
135  parent_(io.db()),
136  dbDir_(dbDir),
137  event_(1),
138  cacheTemporaryObjectsSet_(false)
139 {
141 }
142 
143 
145 (
146  const IOobject& io,
147  const label nIoObjects
148 )
149 :
150  objectRegistry(io, io.db().dbDir()/io.local()/io.name(), nIoObjects)
151 {}
152 
153 
154 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
155 
157 {
158  cacheTemporaryObjects_.clear();
159  clear();
160 }
161 
162 
163 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
164 
166 (
167  const word& instance,
168  const fileName& local
169 ) const
170 {
171  // Note: can only be called with relative instance since is word type
172  return rootPath()/caseName()/instance/dbDir()/local;
173 }
174 
175 
177 {
178  wordList objectNames(size());
179 
180  label count=0;
181  for (const_iterator iter = cbegin(); iter != cend(); ++iter)
182  {
183  if (iter()->type() == ClassName)
184  {
185  objectNames[count++] = iter.key();
186  }
187  }
188 
189  objectNames.setSize(count);
190 
191  return objectNames;
192 }
193 
194 
196 {
197  wordList sortedLst = toc(ClassName);
198  sort(sortedLst);
199 
200  return sortedLst;
201 }
202 
203 
205 (
206  const word& name,
207  const bool forceCreate
208 ) const
209 {
210  if (forceCreate && !foundObject<objectRegistry>(name))
211  {
212  objectRegistry* fieldsCachePtr = new objectRegistry
213  (
214  IOobject
215  (
216  name,
217  time().constant(),
218  *this,
221  )
222  );
223  fieldsCachePtr->store();
224  }
225  return lookupObject<objectRegistry>(name);
226 }
227 
228 
230 {
231  label curEvent = event_++;
232 
233  if (event_ == labelMax)
234  {
235  if (objectRegistry::debug)
236  {
238  << "Event counter has overflowed. "
239  << "Resetting counter on all dependent objects." << nl
240  << "This might cause extra evaluations." << endl;
241  }
242 
243  // Reset event counter
244  curEvent = 1;
245  event_ = 2;
246 
247  for (const_iterator iter = begin(); iter != end(); ++iter)
248  {
249  const regIOobject& io = *iter();
250 
251  if (objectRegistry::debug)
252  {
253  Pout<< "objectRegistry::getEvent() : "
254  << "resetting count on " << iter.key() << endl;
255  }
256 
257  if (io.eventNo() != 0)
258  {
259  const_cast<regIOobject&>(io).eventNo() = curEvent;
260  }
261  }
262  }
263 
264  return curEvent;
265 }
266 
267 
269 {
270  if (objectRegistry::debug)
271  {
272  Pout<< "objectRegistry::checkIn(regIOobject&) : "
273  << name() << " : checking in " << io.name()
274  << " of type " << io.type()
275  << endl;
276  }
277 
278  const objectRegistry& root = time_;
279 
280  // Delete cached object with the same name as io and if it is in the
281  // cacheTemporaryObjects list
282  HashTable<Pair<bool>>::iterator cacheIter
283  (
284  root.cacheTemporaryObjects_.find(io.name())
285  );
286  if (cacheIter != root.cacheTemporaryObjects_.end())
287  {
288  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
289 
290  if (iter != end() && iter() != &io && iter()->ownedByRegistry())
291  {
292  if (objectRegistry::debug)
293  {
294  Pout<< "objectRegistry::checkIn(regIOobject&) : "
295  << name() << " : deleting cached object " << iter.key()
296  << endl;
297  }
298 
299  cacheIter().first() = false;
300  deleteCachedObject(*iter());
301  }
302  }
303 
304  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
305 }
306 
307 
309 {
310  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
311 
312  if (iter != end())
313  {
314  if (objectRegistry::debug)
315  {
316  Pout<< "objectRegistry::checkOut(regIOobject&) : "
317  << name() << " : checking out " << iter.key()
318  << endl;
319  }
320 
321  if (iter() != &io)
322  {
323  if (objectRegistry::debug)
324  {
326  << name() << " : attempt to checkOut copy of "
327  << iter.key()
328  << endl;
329  }
330 
331  return false;
332  }
333  else
334  {
335  regIOobject* object = iter();
336 
337  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
338 
339  if (io.ownedByRegistry())
340  {
341  delete object;
342  }
343 
344  return hasErased;
345  }
346  }
347  else
348  {
349  if (objectRegistry::debug)
350  {
351  Pout<< "objectRegistry::checkOut(regIOobject&) : "
352  << name() << " : could not find " << io.name()
353  << " in registry " << name()
354  << endl;
355  }
356  }
357 
358  return false;
359 }
360 
361 
363 {
364  List<regIOobject*> myObjects(size());
365  label nMyObjects = 0;
366 
367  for (iterator iter = begin(); iter != end(); ++iter)
368  {
369  if (iter()->ownedByRegistry())
370  {
371  myObjects[nMyObjects++] = iter();
372  }
373  }
374 
375  for (label i=0; i < nMyObjects; i++)
376  {
377  checkOut(*myObjects[i]);
378  }
379 }
380 
381 
383 (
384  const word& name
385 ) const
386 {
387  const objectRegistry& root = time_;
388 
389  return root.cacheTemporaryObjects_.found(name);
390 }
391 
392 
394 (
395  const regIOobject& ob
396 ) const
397 {
398  // If object ob if is in the cacheTemporaryObjects list
399  // and has been cached reset the cached flag
400  HashTable<Pair<bool>>::iterator iter
401  (
402  cacheTemporaryObjects_.find(ob.name())
403  );
404  if (iter != cacheTemporaryObjects_.end())
405  {
406  iter().first() = false;
407  }
408 
409  // Reset the object in the time registry also
410  if (this != &time_)
411  {
412  time_.resetCacheTemporaryObject(ob);
413  }
414 }
415 
416 
418 {
420  {
421  const objectRegistry* orPtr_ =
422  dynamic_cast<const objectRegistry*>(iter());
423 
424  // Protect against re-searching the top-level registry
425  if (orPtr_ && orPtr_ != this)
426  {
427  orPtr_->checkCacheTemporaryObjects();
428  }
429  }
430 
431  const objectRegistry& root = time_;
432 
433  if (root.cacheTemporaryObjects_.empty())
434  {
435  return false;
436  }
437 
438  if (this != &root)
439  {
440  forAllIter
441  (
442  typename HashTable<Pair<bool>>,
443  root.cacheTemporaryObjects_,
444  iter
445  )
446  {
447  if (!iter().second())
448  {
449  Warning
450  << "Could not find temporary object " << iter.key()
451  << " in registry " << name() << nl
452  << "Available temporary objects "
453  << temporaryObjects_
454  << endl;
455  }
456  }
457 
458  cacheTemporaryObjects_.clear();
459  }
460  else
461  {
462  forAllIter
463  (
464  typename HashTable<Pair<bool>>,
465  root.cacheTemporaryObjects_,
466  iter
467  )
468  {
469  iter().second() = false;
470  }
471  }
472 
473  temporaryObjects_.clear();
474 
475  return true;
476 }
477 
478 
479 void Foam::objectRegistry::rename(const word& newName)
480 {
481  regIOobject::rename(newName);
482 
483  // adjust dbDir_ as well
484  string::size_type i = dbDir_.rfind('/');
485 
486  if (i == string::npos)
487  {
488  dbDir_ = newName;
489  }
490  else
491  {
492  dbDir_.replace(i+1, string::npos, newName);
493  }
494 }
495 
496 
498 {
500  {
501  if (iter()->modified())
502  {
503  return true;
504  }
505  }
506 
507  return false;
508 }
509 
510 
512 {
513  dependents_.setSize(size());
514 
515  label count=0;
517  {
518  if (iter()->dependenciesModified())
519  {
520  dependents_[count++] = iter();
521  }
522  }
523  dependents_.setSize(count);
524 
525  return count != 0;
526 }
527 
528 
530 {
531  bool modified = false;
532 
533  for (iterator iter = begin(); iter != end(); ++iter)
534  {
535  if (objectRegistry::debug)
536  {
537  Pout<< "objectRegistry::readModifiedObjects() : "
538  << name() << " : Considering reading object "
539  << iter.key() << endl;
540  }
541 
542  modified = modified || iter()->readIfModified();
543  }
544 
545  return modified;
546 }
547 
548 
550 {
551  bool readOk = true;
552 
553  forAll(dependents_, i)
554  {
555  dependents_[i]->read();
556  }
557 
558  return readOk;
559 }
560 
561 
563 {
564  dependenciesModified();
565 
566  const bool modified = readIfModified();
567 
568  // If any objects have been modified and re-read, read the dependants
569  if (modified)
570  {
572  }
573 }
574 
575 
577 {
579  (
580  sorted()
581  );
582 
583  forAll(sortedObjects, i)
584  {
585  os << " " << setf(ios_base::left)
586  << setw(39) << sortedObjects[i].key()
587  << ' ' << sortedObjects[i]()->type()
588  << endl;
589  }
590 }
591 
592 
594 (
598  const bool write
599 ) const
600 {
601  bool ok = true;
602 
604  {
605  if (objectRegistry::debug)
606  {
607  Pout<< "objectRegistry::write() : "
608  << name() << " : Considering writing object "
609  << iter.key()
610  << " of type " << iter()->type()
611  << " with writeOpt " << iter()->writeOpt()
612  << " to file " << iter()->objectPath()
613  << endl;
614  }
615 
616  if (iter()->writeOpt() != NO_WRITE)
617  {
618  ok = iter()->writeObject(fmt, ver, cmp, write) && ok;
619  }
620  }
621 
622  return ok;
623 }
624 
625 
626 // ************************************************************************* //
Istream and Ostream manipulators taking arguments.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:73
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:458
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
An STL-conforming hash table.
Definition: HashTable.H:127
List< word > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:242
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
Definition: HashTable.C:445
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Definition: HashTable.C:167
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
writeOption writeOpt() const
Definition: IOobject.H:367
const word & name() const
Return name.
Definition: IOobject.H:307
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
void setSize(const label)
Reset size of List.
Definition: List.C:281
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
A class for handling file names.
Definition: fileName.H:82
Registry of regIOobjects.
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
wordList toc() const
Return the list of names of the IOobjects of given type.
const objectRegistry & subRegistry(const word &name, const bool forceCreate=false) const
Lookup and return a const sub-objectRegistry. Optionally create.
void printToc(Ostream &os) const
Print the list of object names and their type.
virtual bool modified() const
Return true if any of the object's files have been modified.
objectRegistry(const Time &db, const label nIoObjects=128)
Construct the time objectRegistry given an initial estimate.
virtual bool dependenciesModified() const
Cache pointers to objects who's dependencies have been modified.
virtual ~objectRegistry()
Destructor.
bool checkCacheTemporaryObjects() const
Check that all objects in the cacheTemporaryObjects set.
bool cacheTemporaryObject(const word &name) const
Return true if given name is in the cacheTemporaryObjects set.
virtual bool readIfModified()
Read object if modified.
fileName path() const
Return complete path.
Definition: regIOobject.C:199
void clear()
Remove all regIOobject owned by the registry.
void resetCacheTemporaryObject(const regIOobject &ob) const
Reset the cache state of the given object.
void readModifiedObjects()
Read the objects that have been modified.
virtual void rename(const word &newName)
Rename.
virtual bool read()
Read dependent objects.
label getEvent() const
Return new event number.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
bool ownedByRegistry() const
Is this object owned by the registry?
Definition: regIOobjectI.H:34
label eventNo() const
Event number at last update.
Definition: regIOobjectI.H:89
bool checkOut()
Remove object from registry.
Definition: regIOobject.C:241
virtual void rename(const word &newName)
Rename.
Definition: regIOobject.C:417
void store()
Transfer ownership of this object to its registry.
Definition: regIOobjectI.H:40
bool checkIn()
Add object to registry.
Definition: regIOobject.C:205
A class for handling character strings derived from std::string.
Definition: string.H:79
string & replace(const string &oldStr, const string &newStr, size_type start=0)
In this string replace first occurrence of sub-string oldStr.
Definition: string.C:64
A class for handling words, derived from string.
Definition: word.H:62
#define ClassName(TypeNameString)
Add typeName information from argument TypeNameString to a class.
Definition: className.H:65
tUEqn clear()
#define WarningInFunction
Report a warning using Foam::Warning.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
Namespace for OpenFOAM.
List< word > wordList
A List of words.
Definition: fileName.H:54
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:164
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
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
void sort(UList< T > &)
Definition: UList.C:115
defineTypeNameAndDebug(combustionModel, 0)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
static const label labelMax
Definition: label.H:62
static const char nl
Definition: Ostream.H:267
fileType type(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return the file type: directory or file.
Definition: POSIX.C:488
messageStream Warning
runTime controlDict().lookup("adjustTimeStep") >> adjustTimeStep
thermo validate(args.executable(), "h")
static iteratorEnd end()
iteratorEnd set to beyond the end of any HashTable
Definition: HashTable.H:112