Loading...
Searching...
No Matches
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-2025 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 "predicates.H"
32#include "OSspecific.H"
33
34// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35
36void 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
59void 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
87void Foam::IOobjectList::syncNames(wordList& objNames)
88{
89 // Synchronize names
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{
105 word newInstance;
106 fileNameList objNames = fileHandler().readObjects
107 (
108 db,
109 instance,
110 local,
111 newInstance
112 );
113
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(), std::move(objectPtr));
146 }
147}
148
149
150// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151
152const Foam::IOobject* Foam::IOobjectList::cfindObject
153(
154 const word& objName
155) const
156{
157 // Like HashPtrTable::get(), or lookup() with a nullptr
158 const IOobject* io = nullptr;
159
160 if (objName.empty())
161 {
162 return nullptr;
163 }
164 else if (auto iter = cfind(objName); iter.good())
165 {
166 io = iter.val();
167 }
168
169 if (IOobject::debug)
170 {
171 if (io)
172 {
173 InfoInFunction << "Found " << objName << endl;
174 }
175 else
176 {
177 InfoInFunction << "Could not find " << objName << endl;
179 }
180
181 return io;
182}
183
184
185const Foam::IOobject* Foam::IOobjectList::findObject
186(
187 const word& objName
188) const
189{
190 return cfindObject(objName);
191}
192
195{
196 return const_cast<IOobject*>(cfindObject(objName));
197}
198
201{
202 return const_cast<IOobject*>(cfindObject(objName));
203}
204
205
207{
208 // No nullptr check - only called with string literals
209 return lookupClass(static_cast<word>(clsName));
210}
211
214{
215 return classesImpl(*this, predicates::always());
216}
217
218
219Foam::label Foam::IOobjectList::count(const char* clsName) const
220{
221 // No nullptr check - only called with string literals
222 return count(static_cast<word>(clsName));
223}
224
225
226// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231}
232
234Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
235{
236 return sortedNames(syncPar);
237}
238
239
241{
242 // No nullptr check - only called with string literals
243 return names(static_cast<word>(clsName));
244}
245
246
248(
249 const char* clsName,
250 const bool syncPar
251) const
252{
253 // No nullptr check - only called with string literals
254 return sortedNames(static_cast<word>(clsName), syncPar);
255}
256
257
258// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266Foam::wordList Foam::IOobjectList::sortedNames(const bool syncPar) const
267{
269
270 checkNameOrder(objNames, syncPar);
271 return objNames;
272}
273
274
276{
277 // No nullptr check - only called with string literals
278 return sortedNames(static_cast<word>(clsName));
279}
280
281
283(
284 const char* clsName,
285 const bool syncPar
286) const
287{
288 // No nullptr check - only called with string literals
289 return sortedNames(static_cast<word>(clsName), syncPar);
290}
291
292
293// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294
295Foam::label Foam::IOobjectList::prune_0()
296{
297 return
300 [](const word& k){ return k.ends_with("_0"); },
301 true // prune
302 );
303}
304
305
307{
309
310 syncNames(objNames);
311 return objNames;
312}
313
314
315void Foam::IOobjectList::checkNames(const bool syncPar) const
316{
317 if (syncPar && UPstream::is_parallel())
318 {
320
321 checkNameOrder(objNames, syncPar);
322 }
323}
324
325
326// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
327
328Foam::Ostream& Foam::operator<<(Ostream& os, const IOobjectList& list)
329{
330 os << nl << list.size() << nl << token::BEGIN_LIST << nl;
331
332 forAllConstIters(list, iter)
333 {
334 os << iter.key() << token::SPACE
335 << iter.val()->headerClassName() << nl;
336 }
337
340
341 return os;
342}
343
344
345// ************************************************************************* //
label k
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A HashTable similar to std::unordered_map.
Definition HashTable.H:124
List< Key > sortedToc() const
The table of contents (the keys) in sorted order.
Definition HashTable.C:156
List< Key > toc() const
The table of contents (the keys) in unsorted order.
Definition HashTable.C:141
const_iterator cfind(const Key &key) const
Find and return an const_iterator set at the hashed entry.
Definition HashTableI.H:113
void reserve(label numEntries)
Reserve space for at least the specified number of elements (not the number of buckets) and regenerat...
Definition HashTable.C:729
label filterKeys(const UnaryPredicate &pred, const bool pruning=false)
Generalized means to filter table entries based on their keys.
label size() const noexcept
The number of elements in table.
Definition HashTable.H:358
Report an I/O error.
Definition error.H:370
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable,...
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
wordList sortedNames() const
The sorted names of the IOobjects.
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by 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...
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
void checkNames(const bool syncPar=true) const
Verify that object names are synchronised across processors.
IOobjectList lookupClass(const char *clsName) const
The list of IOobjects with the given headerClassName.
IOobjectList() noexcept=default
Default construct: empty without allocation (capacity=0).
label prune_0()
Remove objects with names ending with "_0" (restart fields).
wordList allNames() const
The sorted names of all objects (synchronised across processors).
label count() const
The number of objects with headerClassName == Type::typeName.
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
wordList names() const
The unsorted names of the IOobjects.
label count(const char *clsName) const
The number of objects of the given headerClassName.
A simple container of IOobject preferences. Can also be used for general handling of read/no-read/rea...
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition IOstream.C:45
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
static void combineReduce(T &value, CombineOp cop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce) applying cop to inplace combine value from different processors.
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition UPstream.H:1743
@ broadcast
broadcast [MPI]
Definition UPstream.H:189
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
A class for handling file names.
Definition fileName.H:75
Registry of regIOobjects.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ END_LIST
End list [isseparator].
Definition token.H:175
@ SPACE
Space [isspace].
Definition token.H:144
A class for handling words, derived from Foam::string.
Definition word.H:66
bool local
Definition EEqn.H:20
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
OBJstream os(runTime.globalPath()/outputName)
const auto & io
auto & names
#define FUNCTION_NAME
#define InfoInFunction
Report an information message using Foam::Info.
List< word > wordList
List of word.
Definition fileName.H:60
List< fileName > fileNameList
List of fileName.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
void sort(UList< T > &list)
Sort the list.
Definition UList.C:283
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional 'FOAM Warning' header text.
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
nonInt insert("surfaceSum(((S|magSf)*S)")
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition stdFoam.H:235
List helper to append y unique elements onto the end of x.
Definition ListOps.H:721
Unary and binary predicates that always return true, useful for templating.
Definition predicates.H:54