IOobjectList.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-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 "IOobjectList.H"
30 #include "Time.H"
31 #include "predicates.H"
32 #include "OSspecific.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 void Foam::IOobjectList::checkObjectOrder
37 (
38  const UPtrList<const IOobject>& objs,
39  bool syncPar
40 )
41 {
42  if (syncPar && UPstream::is_parallel())
43  {
44  wordList objectNames(objs.size());
45 
46  auto iter = objectNames.begin();
47 
48  for (const IOobject& io : objs)
49  {
50  *iter = io.name(); // nameOp<IOobject>()
51  ++iter;
52  }
53 
54  checkNameOrder(objectNames, syncPar);
55  }
56 }
57 
58 
59 void Foam::IOobjectList::checkNameOrder
60 (
61  const wordList& objectNames,
62  bool syncPar
63 )
64 {
65  if (syncPar && UPstream::is_parallel())
66  {
67  wordList masterNames;
68  if (UPstream::master())
69  {
70  masterNames = objectNames;
71  }
72  Pstream::broadcast(masterNames);
73 
74  if (!UPstream::master() && (objectNames != masterNames))
75  {
77  << "Objects not synchronised across processors." << nl
78  << "Master has " << flatOutput(masterNames) << nl
79  << "Processor " << UPstream::myProcNo()
80  << " has " << flatOutput(objectNames) << endl
81  << exit(FatalError);
82  }
83  }
84 }
85 
86 
87 void Foam::IOobjectList::syncNames(wordList& objNames)
88 {
89  // Synchronize names
90  Pstream::combineReduce(objNames, ListOps::uniqueEqOp<word>());
91  Foam::sort(objNames); // Consistent order
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
96 
98 (
99  const objectRegistry& db,
100  const fileName& instance,
101  const fileName& local,
102  IOobjectOption ioOpt
103 )
104 :
106 {
107  word newInstance;
108  fileNameList objNames = fileHandler().readObjects
109  (
110  db,
111  instance,
112  local,
113  newInstance
114  );
115 
116  for (const auto& objName : objNames)
117  {
118  auto objectPtr = autoPtr<IOobject>::New
119  (
120  objName,
121  newInstance,
122  local,
123  db,
124  ioOpt
125  );
126 
127  bool ok = false;
128  const bool oldThrowingIOerr = FatalIOError.throwing(true);
129 
130  try
131  {
132  // Use object with local scope and current instance (no searching)
133  ok = objectPtr->typeHeaderOk<regIOobject>(false, false);
134  }
135  catch (const Foam::IOerror& err)
136  {
137  Warning << err << nl << endl;
138  }
139 
140  FatalIOError.throwing(oldThrowingIOerr);
141 
142  if (ok)
143  {
144  insert(objectPtr->name(), objectPtr);
145  }
146  }
147 }
148 
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
153 (
154  const word& objName
155 ) const
156 {
157  // Like HashPtrTable::get(), or lookup() with a nullptr
158  const IOobject* io = nullptr;
159 
160  const const_iterator iter(cfind(objName));
161  if (iter.good())
162  {
163  io = iter.val();
164  }
165 
166  if (IOobject::debug)
167  {
168  if (io)
169  {
170  InfoInFunction << "Found " << objName << endl;
171  }
172  else
173  {
174  InfoInFunction << "Could not find " << objName << endl;
175  }
176  }
177 
178  return io;
179 }
180 
181 
183 (
184  const word& objName
185 ) const
186 {
187  return cfindObject(objName);
188 }
189 
192 {
193  return const_cast<IOobject*>(cfindObject(objName));
194 }
195 
198 {
199  return const_cast<IOobject*>(cfindObject(objName));
200 }
201 
202 
204 {
205  // No nullptr check - only called with string literals
206  return lookupClass(static_cast<word>(clsName));
207 }
208 
211 {
212  return classesImpl(*this, predicates::always());
213 }
214 
215 
216 Foam::label Foam::IOobjectList::count(const char* clsName) const
217 {
218  // No nullptr check - only called with string literals
219  return count(static_cast<word>(clsName));
220 }
221 
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
226 {
228 }
229 
231 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
232 {
233  return sortedNames(syncPar);
234 }
235 
236 
237 Foam::wordList Foam::IOobjectList::names(const char* clsName) const
238 {
239  // No nullptr check - only called with string literals
240  return names(static_cast<word>(clsName));
241 }
242 
243 
245 (
246  const char* clsName,
247  const bool syncPar
248 ) const
249 {
250  // No nullptr check - only called with string literals
251  return sortedNames(static_cast<word>(clsName), syncPar);
252 }
253 
254 
255 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 {
260 }
261 
262 
263 Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
264 {
266 
267  checkNameOrder(objNames, syncPar);
268  return objNames;
269 }
270 
271 
272 Foam::wordList Foam::IOobjectList::sortedNames(const char* clsName) const
273 {
274  // No nullptr check - only called with string literals
275  return sortedNames(static_cast<word>(clsName));
276 }
277 
278 
280 (
281  const char* clsName,
282  const bool syncPar
283 ) const
284 {
285  // No nullptr check - only called with string literals
286  return sortedNames(static_cast<word>(clsName), syncPar);
287 }
288 
289 
290 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
291 
292 Foam::label Foam::IOobjectList::prune_0()
293 {
294  return
296  (
297  [](const word& k){ return k.ends_with("_0"); },
298  true // prune
299  );
300 }
301 
302 
304 {
306 
307  syncNames(objNames);
308  return objNames;
309 }
310 
311 
312 void Foam::IOobjectList::checkNames(const bool syncPar) const
313 {
314  if (syncPar && UPstream::is_parallel())
315  {
317 
318  checkNameOrder(objNames, syncPar);
319  }
320 }
321 
322 
323 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
324 
325 Foam::Ostream& Foam::operator<<(Ostream& os, const IOobjectList& list)
326 {
327  os << nl << list.size() << nl << token::BEGIN_LIST << nl;
328 
329  forAllConstIters(list, iter)
330  {
331  os << iter.key() << token::SPACE
332  << iter.val()->headerClassName() << nl;
333  }
334 
335  os << token::END_LIST;
337 
338  return os;
339 }
340 
341 
342 // ************************************************************************* //
label prune_0()
Remove objects with names ending with "_0" (restart fields)
Definition: IOobjectList.C:285
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
A class for handling file names.
Definition: fileName.H:72
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable, so the various sorted methods should be used if traversing in parallel.
Definition: IOobjectList.H:55
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:218
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:250
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:218
Begin list [isseparator].
Definition: token.H:161
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
label k
Boltzmann constant.
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers...
Definition: HashPtrTable.H:51
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1074
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:53
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
Space [isspace].
Definition: token.H:131
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:176
bool local
Definition: EEqn.H:20
End list [isseparator].
Definition: token.H:162
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1111
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:296
A HashTable similar to std::unordered_map.
Definition: HashTable.H:108
Report an I/O error.
Definition: error.H:370
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:391
static void combineReduce(const List< commsStruct > &comms, T &value, const CombineOp &cop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) applying cop to inplace combine value from different processors...
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
IOobject * getObject(const word &objName) const
Return non-const pointer to the object found by name, using a const-cast to have it behave like a mut...
Definition: IOobjectList.C:190
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional &#39;FOAM Warning&#39; header text...
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
List< word > wordList
List of word.
Definition: fileName.H:59
bool insert(const word &, IOobject *)=delete
No insert() with raw pointers (potential memory leaks). Use insert() with autoPtr or set() ...
void checkNames(const bool syncPar=true) const
Verify that object names are synchronised across processors.
Definition: IOobjectList.C:305
label count() const
The number of objects with headerClassName == Type::typeName.
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:139
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition: HashTable.C:124
IOobjectList() noexcept=default
Default construct: empty without allocation (capacity=0).
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Registry of regIOobjects.
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:203
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:146