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-2022 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 "IOList.H"
32 #include "predicates.H"
33 #include "OSspecific.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 void Foam::IOobjectList::checkObjectOrder
38 (
39  const UPtrList<const IOobject>& objs,
40  bool syncPar
41 )
42 {
43  if (syncPar && Pstream::parRun())
44  {
45  wordList objectNames(objs.size());
46 
47  auto iter = objectNames.begin();
48 
49  for (const IOobject& io : objs)
50  {
51  *iter = io.name(); // nameOp<IOobject>()
52  ++iter;
53  }
54 
55  checkNameOrder(objectNames, syncPar);
56  }
57 }
58 
59 
60 void Foam::IOobjectList::checkNameOrder
61 (
62  const wordList& objectNames,
63  bool syncPar
64 )
65 {
66  if (syncPar && Pstream::parRun())
67  {
68  wordList masterNames;
69  if (Pstream::master())
70  {
71  masterNames = objectNames;
72  }
73  Pstream::broadcast(masterNames);
74 
75  if (objectNames != masterNames)
76  {
78  << "Objects not synchronised across processors." << nl
79  << "Master has " << flatOutput(masterNames) << nl
80  << "Processor " << Pstream::myProcNo()
81  << " has " << flatOutput(objectNames) << endl
82  << exit(FatalError);
83  }
84  }
85 }
86 
87 
88 void Foam::IOobjectList::syncNames(wordList& objNames)
89 {
90  if (Pstream::parRun())
91  {
92  // Synchronize names
93  Pstream::combineReduce(objNames, ListOps::uniqueEqOp<word>());
94  }
95 
96  Foam::sort(objNames); // Consistent order
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
101 
103 (
104  const objectRegistry& db,
105  const fileName& instance,
106  const fileName& local,
107  IOobjectOption ioOpt
108 )
109 :
111 {
112  word newInstance;
113  fileNameList objNames = fileHandler().readObjects
114  (
115  db,
116  instance,
117  local,
118  newInstance
119  );
120 
121  for (const auto& objName : objNames)
122  {
123  auto objectPtr = autoPtr<IOobject>::New
124  (
125  objName,
126  newInstance,
127  local,
128  db,
129  ioOpt
130  );
131 
132  bool ok = false;
133  const bool oldThrowingIOerr = FatalIOError.throwing(true);
134 
135  try
136  {
137  // Use object with local scope and current instance (no searching)
138  ok = objectPtr->typeHeaderOk<IOList<label>>(false, false);
139  }
140  catch (const Foam::IOerror& err)
141  {
142  Warning << err << nl << endl;
143  }
144 
145  FatalIOError.throwing(oldThrowingIOerr);
146 
147  if (ok)
148  {
149  insert(objectPtr->name(), objectPtr);
150  }
151  }
152 }
153 
154 
155 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
156 
157 Foam::label Foam::IOobjectList::merge(IOobjectList&& other)
158 {
159  // Remove by name to avoid uncertainties about invalid iterators
160 
161  label count = 0;
162 
163  wordList keys(other.toc());
164 
165  for (const word& key : keys)
166  {
167  if (!found(key))
168  {
169  if (IOobject::debug)
170  {
171  InfoInFunction << "Merge " << key << nl;
172  }
173 
174  if (add(other.remove(key)))
175  {
176  ++count;
177  }
178  }
179  }
180 
181  return count;
182 }
183 
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
188 (
189  const word& objName
190 ) const
191 {
192  // Like HashPtrTable::get(), or lookup() with a nullptr
193  const IOobject* io = nullptr;
194 
195  const const_iterator iter(cfind(objName));
196  if (iter.good())
197  {
198  io = iter.val();
199  }
200 
201  if (IOobject::debug)
202  {
203  if (io)
204  {
205  InfoInFunction << "Found " << objName << endl;
206  }
207  else
208  {
209  InfoInFunction << "Could not find " << objName << endl;
210  }
211  }
212 
213  return io;
214 }
215 
216 
218 (
219  const word& objName
220 ) const
221 {
222  return cfindObject(objName);
223 }
224 
227 {
228  return const_cast<IOobject*>(cfindObject(objName));
229 }
230 
233 {
234  return const_cast<IOobject*>(cfindObject(objName));
235 }
236 
237 
239 {
240  // No nullptr check - only called with string literals
241  return lookupClass(static_cast<word>(clsName));
242 }
243 
246 {
247  return classesImpl(*this, predicates::always());
248 }
249 
250 
251 Foam::label Foam::IOobjectList::count(const char* clsName) const
252 {
253  // No nullptr check - only called with string literals
254  return count(static_cast<word>(clsName));
255 }
256 
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
262 {
263  return sorted<void>();
264 }
265 
266 
268 Foam::IOobjectList::sorted(const bool syncPar) const
269 {
270  return sorted<void>(syncPar);
271 }
272 
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
277 {
279 }
280 
282 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
283 {
284  return sortedNames(syncPar);
285 }
286 
287 
288 Foam::wordList Foam::IOobjectList::names(const char* clsName) const
289 {
290  // No nullptr check - only called with string literals
291  return names(static_cast<word>(clsName));
292 }
293 
294 
296 (
297  const char* clsName,
298  const bool syncPar
299 ) const
300 {
301  // No nullptr check - only called with string literals
302  return sortedNames(static_cast<word>(clsName), syncPar);
303 }
304 
305 
306 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
309 {
311 }
312 
313 
314 Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
315 {
317 
318  checkNameOrder(objNames, syncPar);
319  return objNames;
320 }
321 
322 
323 Foam::wordList Foam::IOobjectList::sortedNames(const char* clsName) const
324 {
325  // No nullptr check - only called with string literals
326  return sortedNames(static_cast<word>(clsName));
327 }
328 
329 
331 (
332  const char* clsName,
333  const bool syncPar
334 ) const
335 {
336  // No nullptr check - only called with string literals
337  return sortedNames(static_cast<word>(clsName), syncPar);
338 }
339 
340 
341 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
342 
343 Foam::label Foam::IOobjectList::prune_0()
344 {
345  return
347  (
348  [](const word& k){ return k.ends_with("_0"); },
349  true // prune
350  );
351 }
352 
353 
355 {
357 
358  syncNames(objNames);
359  return objNames;
360 }
361 
362 
363 void Foam::IOobjectList::checkNames(const bool syncPar) const
364 {
365  if (syncPar && Pstream::parRun())
366  {
368 
369  checkNameOrder(objNames, syncPar);
370  }
371 }
372 
373 
374 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
375 
376 Foam::Ostream& Foam::operator<<(Ostream& os, const IOobjectList& list)
377 {
378  os << nl << list.size() << nl << token::BEGIN_LIST << nl;
379 
380  forAllConstIters(list, iter)
381  {
382  os << iter.key() << token::SPACE
383  << iter.val()->headerClassName() << nl;
384  }
385 
386  os << token::END_LIST;
388 
389  return os;
390 }
391 
392 
393 // ************************************************************************* //
IOobjectList()
Default construct (empty) with default (128) table capacity.
Definition: IOobjectListI.H:23
label prune_0()
Remove objects with names ending with "_0" (restart fields)
Definition: IOobjectList.C:336
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:71
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:578
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
UPtrList< const IOobject > sorted() const
The sorted list of IOobjects.
Definition: IOobjectList.C:254
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:198
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:301
label merge(IOobjectList &&other)
Add objects from other to this list without overwriting existing keys.
Definition: IOobjectList.C:150
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:269
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:639
Begin list [isseparator].
Definition: token.H:158
autoPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file 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)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:688
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all processes in communicator.
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
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
A class for handling words, derived from Foam::string.
Definition: word.H:63
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:334
Space [isspace].
Definition: token.H:128
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:211
bool local
Definition: EEqn.H:20
End list [isseparator].
Definition: token.H:159
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:347
A HashTable similar to std::unordered_map.
Definition: HashTable.H:102
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
Report an I/O error.
Definition: error.H:350
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition: UListI.H:322
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:55
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
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:225
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:76
List< word > wordList
A List of words.
Definition: fileName.H:58
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:356
label count() const
The number of objects with headerClassName == Type::typeName.
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition: HashTable.C:130
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
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:115
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:166
bool found
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:238
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:181