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-------------------------------------------------------------------------------
11License
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
37void 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
60void 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
88void Foam::IOobjectList::syncNames(wordList& objNames)
89{
90 if (Pstream::parRun())
91 {
92 // Synchronize names
93 Pstream::combineGather(objNames, ListOps::uniqueEqOp<word>());
94 Pstream::broadcast(objNames);
95 }
96
97 // Consistent order on all processors
98 Foam::sort(objNames);
99}
100
101
102// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103
105:
107{}
108
109
111:
112 HashPtrTable<IOobject>(nObjects) // Could also use 2*nObjects instead
113{}
114
115
117:
119{}
120
121
123:
124 HashPtrTable<IOobject>(std::move(list))
125{}
126
127
129(
130 const objectRegistry& db,
131 const fileName& instance,
132 const fileName& local,
135 bool registerObject
136)
137:
139{
140 word newInstance;
142 (
143 db,
144 instance,
145 local,
146 newInstance
147 );
148
149 for (const auto& objName : objNames)
150 {
151 auto objectPtr = autoPtr<IOobject>::New
152 (
153 objName,
154 newInstance,
155 local,
156 db,
157 rOpt,
158 wOpt,
159 registerObject
160 );
161
162 bool ok = false;
163 const bool oldThrowingIOerr = FatalIOError.throwing(true);
164
165 try
166 {
167 // Use object with local scope and current instance (no searching)
168 ok = objectPtr->typeHeaderOk<IOList<label>>(false, false);
169 }
170 catch (const Foam::IOerror& err)
171 {
172 Warning << err << nl << endl;
173 }
174
175 FatalIOError.throwing(oldThrowingIOerr);
176
177 if (ok)
178 {
179 insert(objectPtr->name(), objectPtr);
180 }
181 }
182}
183
184
185// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
186
188{
189 if (objectPtr)
190 {
191 return insert(objectPtr->name(), objectPtr);
192 }
193
194 return false;
195}
196
197
199{
200 if (objectPtr)
201 {
202 return insert(objectPtr->name(), objectPtr);
203 }
204
205 return false;
206}
207
208
210{
211 label count = 0;
212
213 forAllConstIters(other, iter)
214 {
215 if (!found(iter.key()))
216 {
217 if (IOobject::debug)
218 {
219 InfoInFunction << "Copy append " << iter.key() << nl;
220 }
221
222 set(iter.key(), new IOobject(*iter.val()));
223 ++count;
224 }
225 }
226
227 return count;
228}
229
230
232{
233 // Remove by name to avoid uncertainties about invalid iterators
234
235 label count = 0;
236
237 wordList keys(other.toc());
238
239 for (const word& key : keys)
240 {
241 if (!found(key))
242 {
243 if (IOobject::debug)
244 {
245 InfoInFunction << "Move append " << key << nl;
246 }
247
248 if (add(other.remove(key)))
249 {
250 ++count;
251 }
252 }
253 }
254
255 return count;
256}
257
258
260{
261 return erase(io.name());
262}
263
264
265// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266
268(
269 const word& objName
270) const
271{
272 const_iterator iter = cfind(objName);
273
274 if (iter.found())
275 {
276 if (IOobject::debug)
277 {
278 InfoInFunction << "Found " << objName << endl;
279 }
280
281 return iter.val();
282 }
283 else if (IOobject::debug)
284 {
285 InfoInFunction << "Could not find " << objName << endl;
286 }
287
288 return nullptr;
289}
290
291
293(
294 const word& objName
295) const
296{
297 return cfindObject(objName);
298}
299
300
302{
303 return const_cast<IOobject*>(cfindObject(objName));
304}
305
306
308{
309 return const_cast<IOobject*>(cfindObject(objName));
310}
311
312
314{
315 // No nullptr check - only called with string literals
316 return lookupClass(static_cast<word>(clsName));
317}
318
319
321{
322 return classesImpl(*this, predicates::always());
323}
324
325
326Foam::label Foam::IOobjectList::count(const char* clsName) const
327{
328 // No nullptr check - only called with string literals
329 return count(static_cast<word>(clsName));
330}
331
332
333// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334
337{
338 return sorted<void>();
339}
340
341
343Foam::IOobjectList::sorted(const bool syncPar) const
344{
345 return sorted<void>(syncPar);
346}
347
348
349// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350
352{
354}
355
356
358{
359 return sortedNames(syncPar);
360}
361
362
364{
365 // No nullptr check - only called with string literals
366 return names(static_cast<word>(clsName));
367}
368
369
371(
372 const char* clsName,
373 const bool syncPar
374) const
375{
376 // No nullptr check - only called with string literals
377 return sortedNames(static_cast<word>(clsName), syncPar);
378}
379
380
381// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
382
384{
386}
387
388
390{
392
393 checkNameOrder(objNames, syncPar);
394 return objNames;
395}
396
397
399{
400 // No nullptr check - only called with string literals
401 return sortedNames(static_cast<word>(clsName));
402}
403
404
406(
407 const char* clsName,
408 const bool syncPar
409) const
410{
411 // No nullptr check - only called with string literals
412 return sortedNames(static_cast<word>(clsName), syncPar);
413}
414
415
416// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
417
419{
420 return
422 (
423 [](const word& k){ return k.ends_with("_0"); },
424 true // prune
425 );
426}
427
428
430{
432
433 syncNames(objNames);
434 return objNames;
435}
436
437
438void Foam::IOobjectList::checkNames(const bool syncPar) const
439{
440 if (syncPar && Pstream::parRun())
441 {
443
444 checkNameOrder(objNames, syncPar);
445 }
446}
447
448
449// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
450
452{
453 transfer(list);
454}
455
456
457// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
458
460{
461 os << nl << list.size() << nl << token::BEGIN_LIST << nl;
462
463 forAllConstIters(list, iter)
464 {
465 os << iter.key() << token::SPACE
466 << iter.val()->headerClassName() << nl;
467 }
468
471
472 return os;
473}
474
475
476// ************************************************************************* //
label k
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool found
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:68
bool insert(const word &, IOobject *)=delete
bool found() const noexcept
True if iterator points to an entry - same as good()
Forward iterator with const access.
Definition: HashTable.H:794
reference val() const
Const access to referenced object (value)
Definition: HashTable.H:846
A HashTable similar to std::unordered_map.
Definition: HashTable.H:123
label size() const noexcept
The number of elements in table.
Definition: HashTableI.H:52
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
A List of objects of type <T> with automated input and output.
Definition: IOList.H:58
Report an I/O error.
Definition: error.H:282
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:59
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:293
IOobjectList()
Construct null with default (128) table capacity.
Definition: IOobjectList.C:104
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:383
void operator=(const IOobjectList &)=delete
No copy assignment.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:268
IOobject * getObject(const word &objName) const
Definition: IOobjectList.C:307
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:320
void checkNames(const bool syncPar=true) const
Verify that object names are synchronised across processors.
Definition: IOobjectList.C:438
label prune_0()
Remove objects with names ending with "_0" (restart fields)
Definition: IOobjectList.C:418
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:429
label count() const
The number of objects with headerClassName == Type::typeName.
UPtrList< const IOobject > sorted() const
The sorted list of IOobjects.
Definition: IOobjectList.C:336
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList names() const
The unsorted names of the IOobjects.
Definition: IOobjectList.C:351
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:170
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:65
writeOption
Enumeration defining the write options.
Definition: IOobject.H:186
readOption
Enumeration defining the read options.
Definition: IOobject.H:177
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
unsigned int remove()
Remove and return the last element.
Definition: PackedListI.H:709
static void combineGather(const List< commsStruct > &comms, T &value, const CombineOp &cop, const int tag, const label comm)
static void broadcast(Type &value, const label comm=UPstream::worldComm)
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition: bitSetI.H:533
labelList toc() const
The indices of the on bits as a sorted labelList.
Definition: bitSet.C:474
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:169
A class for handling file names.
Definition: fileName.H:76
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
Sums a given list of (at least two or more) fields and outputs the result into a new field,...
Definition: add.H:161
Registry of regIOobjects.
int myProcNo() const noexcept
Return processor number.
splitCell * master() const
Definition: splitCell.H:113
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ END_LIST
End list [isseparator].
Definition: token.H:156
@ SPACE
Space [isspace].
Definition: token.H:125
bool append() const noexcept
True if output format uses an append mode.
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, false)
#define FUNCTION_NAME
#define InfoInFunction
Report an information message using Foam::Info.
const fileOperation & fileHandler()
Get current file handler.
List< word > wordList
A List of words.
Definition: fileName.H:63
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:342
IOerror FatalIOError
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
messageStream Warning
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
srcOptions erase("case")
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56