objectRegistryCache.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenFOAM Foundation
9  Copyright (C) 2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "objectRegistry.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::objectRegistry::readCacheTemporaryObjects() const
35 {
36  if (cacheTemporaryObjectsActive_) return;
37 
38  const auto* eptr = time_.controlDict().findEntry
39  (
40  "cacheTemporaryObjects",
42  );
43 
44  if (eptr)
45  {
46  cacheTemporaryObjectsActive_ = true;
47 
48  // Clear old cache?
49  // cacheTemporaryObjects_.clear();
50 
51  wordList objectNames;
52 
53  if (eptr->isDict())
54  {
55  // Per region (sub-dictionary syntax)
56  eptr->dict().readIfPresent(name(), objectNames);
57  }
58  else
59  {
60  // All regions
61  eptr->readEntry(objectNames);
62  }
63 
64  for (const word& objName : objectNames)
65  {
66  cacheTemporaryObjects_.emplace(objName, false, false);
67  }
68 
70  {
71  Info<< "objectRegistry::cacheTemporaryObjects : "
72  << flatOutput(objectNames) << endl;
73  }
74  }
75 }
76 
77 
78 void Foam::objectRegistry::deleteCachedObject(regIOobject* io) const
79 {
80  if (io)
81  {
82  io->release(); // Relinquish any ownership by registry
83  io->checkOut();
84  // Additional safety - not certain this is actually needed...
85  io->rename(io->name() + "-Cache");
86  delete io;
87  }
88 }
89 
90 
91 // FUTURE: (currently not needed)
92 // void Foam::objectRegistry::addTemporaryObject
93 // (
94 // const word& name
95 // ) const
96 // {
97 // cacheTemporaryObjects_.emplace(name, false, false);
98 // }
99 
100 
102 (
103  const word& name
104 ) const
105 {
106  return cacheTemporaryObjects_.contains(name);
107 }
108 
111 {
112  return io && cacheTemporaryObjects_.contains(io->name());
113 }
114 
115 
117 {
118  return cacheTemporaryObjects_.contains(io.name());
119 }
120 
121 
123 (
124  const regIOobject* io
125 ) const
126 {
127  if (io && !cacheTemporaryObjects_.empty())
128  {
129  auto iter = cacheTemporaryObjects_.find(io->name());
130 
131  // Reset the cached flag
132  if (iter.good())
133  {
134  iter.val().first() = false;
135  }
136  }
137 }
138 
139 
141 (
143 ) const
144 {
145  resetCacheTemporaryObject(&io);
146 }
147 
148 
150 {
151  bool enabled = cacheTemporaryObjects_.size();
152 
153  forAllConstIters(*this, iter)
154  {
155  const auto* subObr = dynamic_cast<const objectRegistry*>(iter.val());
156 
157  // Protect against re-searching the top-level registry
158  if (subObr && subObr != this)
159  {
160  enabled = subObr->checkCacheTemporaryObjects() || enabled;
161  }
162  }
163 
164  if (enabled)
165  {
166  OSstream* emitWarning = nullptr;
167 
168  forAllIters(cacheTemporaryObjects_, iter)
169  {
170  if (!iter.val().second())
171  {
172  if (!emitWarning)
173  {
174  emitWarning = &(Foam::Warning.stream());
175 
176  *emitWarning
177  << "objectRegistry '"
178  << name() << "' has missing temporary objects:" << nl;
179  }
180 
181  *emitWarning<< " " << iter.key() << nl;
182  }
183  else
184  {
185  iter.val().second() = false;
186  }
187  }
188 
189  if (emitWarning)
190  {
191  *emitWarning
192  << "Available temporary objects: "
193  << temporaryObjects_.sortedToc() << endl;
194  }
195 
196  temporaryObjects_.clear();
197  }
198 
199  return enabled;
200 }
201 
202 
203 // ************************************************************************* //
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual void rename(const word &newName)
Rename the object.
Definition: IOobject.H:677
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool is_cacheTemporaryObject(const word &name) const
True if given name is in the cacheTemporaryObjects set.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:336
String literal.
Definition: keyType.H:82
const dictionary & controlDict() const noexcept
Return read access to the controlDict dictionary.
Definition: Time.H:539
bool checkCacheTemporaryObjects() const
Check that all objects specified in the cacheTemporaryObjects were also cached.
OSstream & stream(OSstream *alternative=nullptr)
Return OSstream for output operations. Use the alternative stream for serial-only output if it is a v...
Definition: messageStream.C:68
void resetCacheTemporaryObject(const regIOobject *io) const
Reset the cache state of the given object (nullptr is ignored)
int debug
Static debugging option.
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional &#39;FOAM Warning&#39; header text...
List< word > wordList
List of word.
Definition: fileName.H:59
const entry * findEntry(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find an entry (const access) with the given keyword.
Definition: dictionaryI.H:84
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225