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-2025 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  // Delete cached object with the same name as io and if it is in the
279  // cacheTemporaryObjects list
280  if (cacheTemporaryObjects_.size())
281  {
282  HashTable<Pair<bool>>::iterator cacheIter
283  (
284  cacheTemporaryObjects_.find(io.name())
285  );
286 
287  if (cacheIter != cacheTemporaryObjects_.end())
288  {
289  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
290 
291  if (iter != end() && iter() != &io && iter()->ownedByRegistry())
292  {
293  if (objectRegistry::debug)
294  {
295  Pout<< "objectRegistry::checkIn(regIOobject&) : "
296  << name() << " : deleting cached object " << iter.key()
297  << endl;
298  }
299 
300  cacheIter().first() = false;
301  deleteCachedObject(*iter());
302  }
303  }
304  }
305 
306  return const_cast<objectRegistry&>(*this).insert(io.name(), &io);
307 }
308 
309 
311 {
312  iterator iter = const_cast<objectRegistry&>(*this).find(io.name());
313 
314  if (iter != end())
315  {
316  if (objectRegistry::debug)
317  {
318  Pout<< "objectRegistry::checkOut(regIOobject&) : "
319  << name() << " : checking out " << iter.key()
320  << endl;
321  }
322 
323  if (iter() != &io)
324  {
325  if (objectRegistry::debug)
326  {
328  << name() << " : attempt to checkOut copy of "
329  << iter.key()
330  << endl;
331  }
332 
333  return false;
334  }
335  else
336  {
337  regIOobject* object = iter();
338 
339  bool hasErased = const_cast<objectRegistry&>(*this).erase(iter);
340 
341  if (io.ownedByRegistry())
342  {
343  delete object;
344  }
345 
346  return hasErased;
347  }
348  }
349  else
350  {
351  if (objectRegistry::debug)
352  {
353  Pout<< "objectRegistry::checkOut(regIOobject&) : "
354  << name() << " : could not find " << io.name()
355  << " in registry " << name()
356  << endl;
357  }
358  }
359 
360  return false;
361 }
362 
363 
365 {
366  List<regIOobject*> myObjects(size());
367  label nMyObjects = 0;
368 
369  for (iterator iter = begin(); iter != end(); ++iter)
370  {
371  if (iter()->ownedByRegistry())
372  {
373  myObjects[nMyObjects++] = iter();
374  }
375  }
376 
377  for (label i=0; i < nMyObjects; i++)
378  {
379  checkOut(*myObjects[i]);
380  }
381 }
382 
383 
385 (
386  const word& name
387 ) const
388 {
389  const objectRegistry& root = time_;
390 
391  return root.cacheTemporaryObjects_.found(name);
392 }
393 
394 
396 (
397  const regIOobject& ob
398 ) const
399 {
400  if (cacheTemporaryObjects_.size())
401  {
402  HashTable<Pair<bool>>::iterator iter
403  (
404  cacheTemporaryObjects_.find(ob.name())
405  );
406 
407  // If object ob if is in the cacheTemporaryObjects list
408  // and has been cached reset the cached flag
409  if (iter != cacheTemporaryObjects_.end())
410  {
411  iter().first() = false;
412  }
413  }
414 
415  // Reset the object in the time registry also
416  if (this != &time_)
417  {
418  time_.resetCacheTemporaryObject(ob);
419  }
420 }
421 
422 
424 {
426  {
427  const objectRegistry* orPtr_ =
428  dynamic_cast<const objectRegistry*>(iter());
429 
430  // Protect against re-searching the top-level registry
431  if (orPtr_ && orPtr_ != this)
432  {
433  orPtr_->checkCacheTemporaryObjects();
434  }
435  }
436 
437  const objectRegistry& root = time_;
438 
439  if (root.cacheTemporaryObjects_.empty())
440  {
441  return false;
442  }
443 
444  if (this != &root)
445  {
446  forAllIter
447  (
448  typename HashTable<Pair<bool>>,
449  root.cacheTemporaryObjects_,
450  iter
451  )
452  {
453  if (!iter().second())
454  {
455  Warning
456  << "Could not find temporary object " << iter.key()
457  << " in registry " << name() << nl
458  << "Available temporary objects "
459  << temporaryObjects_
460  << endl;
461  }
462  }
463 
464  cacheTemporaryObjects_.clear();
465  }
466  else
467  {
468  forAllIter
469  (
470  typename HashTable<Pair<bool>>,
471  root.cacheTemporaryObjects_,
472  iter
473  )
474  {
475  iter().second() = false;
476  }
477  }
478 
479  temporaryObjects_.clear();
480 
481  return true;
482 }
483 
484 
485 void Foam::objectRegistry::rename(const word& newName)
486 {
487  regIOobject::rename(newName);
488 
489  // adjust dbDir_ as well
490  string::size_type i = dbDir_.rfind('/');
491 
492  if (i == string::npos)
493  {
494  dbDir_ = newName;
495  }
496  else
497  {
498  dbDir_.replace(i+1, string::npos, newName);
499  }
500 }
501 
502 
504 {
506  {
507  if (iter()->modified())
508  {
509  return true;
510  }
511  }
512 
513  return false;
514 }
515 
516 
518 {
519  dependents_.setSize(size());
520 
521  label count=0;
523  {
524  if (iter()->dependenciesModified())
525  {
526  dependents_[count++] = iter();
527  }
528  }
529  dependents_.setSize(count);
530 
531  return count != 0;
532 }
533 
534 
536 {
537  bool modified = false;
538 
539  for (iterator iter = begin(); iter != end(); ++iter)
540  {
541  if (objectRegistry::debug)
542  {
543  Pout<< "objectRegistry::readModifiedObjects() : "
544  << name() << " : Considering reading object "
545  << iter.key() << endl;
546  }
547 
548  modified = modified || iter()->readIfModified();
549  }
550 
551  return modified;
552 }
553 
554 
556 {
557  bool readOk = true;
558 
559  forAll(dependents_, i)
560  {
561  dependents_[i]->read();
562  }
563 
564  return readOk;
565 }
566 
567 
569 {
570  dependenciesModified();
571 
572  const bool modified = readIfModified();
573 
574  // If any objects have been modified and re-read, read the dependants
575  if (modified)
576  {
578  }
579 }
580 
581 
583 {
585  (
586  sorted()
587  );
588 
589  forAll(sortedObjects, i)
590  {
591  os << " " << setf(ios_base::left)
592  << setw(39) << sortedObjects[i].key()
593  << ' ' << sortedObjects[i]()->type()
594  << endl;
595  }
596 }
597 
598 
600 (
604  const bool write
605 ) const
606 {
607  bool ok = true;
608 
610  {
611  if (objectRegistry::debug)
612  {
613  Pout<< "objectRegistry::write() : "
614  << name() << " : Considering writing object "
615  << iter.key()
616  << " of type " << iter()->type()
617  << " with writeOpt " << iter()->writeOpt()
618  << " to file " << iter()->objectPath()
619  << endl;
620  }
621 
622  if (iter()->writeOpt() != NO_WRITE)
623  {
624  ok = iter()->writeObject(fmt, ver, cmp, write) && ok;
625  }
626  }
627 
628  return ok;
629 }
630 
631 
632 // ************************************************************************* //
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