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-2021 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 bool Foam::IOobjectList::checkNames(wordList& masterNames, const bool syncPar)
38 {
39  // Sort for consistent order on all processors.
40  // Even do this for serial runs, for consistent behaviour
41  Foam::sort(masterNames);
42 
43  if (syncPar && Pstream::parRun())
44  {
45  const wordList localNames(masterNames);
46  Pstream::scatter(masterNames);
47 
48  if (localNames != masterNames)
49  {
51  << "Objects not synchronised across processors." << nl
52  << "Master has " << flatOutput(masterNames) << nl
53  << "Processor " << Pstream::myProcNo()
54  << " has " << flatOutput(localNames)
55  << exit(FatalError);
56 
57  return false;
58  }
59  }
60 
61  return true;
62 }
63 
64 
65 void Foam::IOobjectList::syncNames(wordList& objNames)
66 {
67  if (Pstream::parRun())
68  {
69  // Synchronize names
70  Pstream::combineGather(objNames, ListOps::uniqueEqOp<word>());
71  Pstream::combineScatter(objNames);
72  }
73 
74  // Sort for consistent order on all processors
75  Foam::sort(objNames);
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 :
84 {}
85 
86 
87 Foam::IOobjectList::IOobjectList(const label nObjects)
88 :
89  HashPtrTable<IOobject>(nObjects) // Could also use 2*nObjects instead
90 {}
91 
92 
94 :
95  HashPtrTable<IOobject>(list)
96 {}
97 
98 
100 :
101  HashPtrTable<IOobject>(std::move(list))
102 {}
103 
104 
106 (
107  const objectRegistry& db,
108  const fileName& instance,
109  const fileName& local,
112  bool registerObject
113 )
114 :
116 {
117  word newInstance;
119  (
120  db,
121  instance,
122  local,
123  newInstance
124  );
125 
126  for (const auto& objName : objNames)
127  {
128  auto objectPtr = autoPtr<IOobject>::New
129  (
130  objName,
131  newInstance,
132  local,
133  db,
134  r,
135  w,
136  registerObject
137  );
138 
139  bool ok = false;
140  const bool oldThrowingIOerr = FatalIOError.throwing(true);
141 
142  try
143  {
144  // Use object with local scope and current instance (no searching)
145  ok = objectPtr->typeHeaderOk<IOList<label>>(false, false);
146  }
147  catch (const Foam::IOerror& err)
148  {
149  Warning << err << nl << endl;
150  }
151 
152  FatalIOError.throwing(oldThrowingIOerr);
153 
154  if (ok)
155  {
156  insert(objectPtr->name(), objectPtr);
157  }
158  }
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
163 
165 {
166  if (objectPtr)
167  {
168  return insert(objectPtr->name(), objectPtr);
169  }
170 
171  return false;
172 }
173 
174 
176 {
177  if (objectPtr)
178  {
179  return insert(objectPtr->name(), objectPtr);
180  }
181 
182  return false;
183 }
184 
185 
186 Foam::label Foam::IOobjectList::append(const IOobjectList& other)
187 {
188  label count = 0;
189 
190  forAllConstIters(other, iter)
191  {
192  if (!found(iter.key()))
193  {
194  if (IOobject::debug)
195  {
196  InfoInFunction << "Copy append " << iter.key() << nl;
197  }
198 
199  set(iter.key(), new IOobject(*(iter.val())));
200  ++count;
201  }
202  }
203 
204  return count;
205 }
206 
207 
209 {
210  // Remove by name to avoid uncertainties about invalid iterators
211 
212  label count = 0;
213 
214  wordList keys(other.toc());
215 
216  for (const word& key : keys)
217  {
218  if (!found(key))
219  {
220  if (IOobject::debug)
221  {
222  InfoInFunction << "Move append " << key << nl;
223  }
224 
225  if (add(other.remove(key)))
226  {
227  ++count;
228  }
229  }
230  }
231 
232  return count;
233 }
234 
235 
237 {
238  return erase(io.name());
239 }
240 
241 
242 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
243 
245 (
246  const word& objName
247 ) const
248 {
249  const_iterator iter = cfind(objName);
250 
251  if (iter.found())
252  {
253  if (IOobject::debug)
254  {
255  InfoInFunction << "Found " << objName << endl;
256  }
257 
258  return iter.val();
259  }
260  else if (IOobject::debug)
261  {
262  InfoInFunction << "Could not find " << objName << endl;
263  }
264 
265  return nullptr;
266 }
267 
268 
270 (
271  const word& objName
272 ) const
273 {
274  return cfindObject(objName);
275 }
276 
277 
279 {
280  return const_cast<IOobject*>(cfindObject(objName));
281 }
282 
283 
285 {
286  return const_cast<IOobject*>(cfindObject(objName));
287 }
288 
289 
291 {
292  // No nullptr check - only called with string literals
293  return lookupClass(static_cast<word>(clsName));
294 }
295 
296 
298 {
299  return classesImpl(*this, predicates::always());
300 }
301 
302 
303 Foam::label Foam::IOobjectList::count(const char* clsName) const
304 {
305  // No nullptr check - only called with string literals
306  return count(static_cast<word>(clsName));
307 }
308 
309 
311 {
313 }
314 
315 
316 Foam::wordList Foam::IOobjectList::names(const bool syncPar) const
317 {
319 
320  checkNames(objNames, syncPar);
321  return objNames;
322 }
323 
324 
325 Foam::wordList Foam::IOobjectList::names(const char* clsName) const
326 {
327  // No nullptr check - only called with string literals
328  return names(static_cast<word>(clsName));
329 }
330 
331 
333 (
334  const char* clsName,
335  const bool syncPar
336 ) const
337 {
338  // No nullptr check - only called with string literals
339  return names(static_cast<word>(clsName), syncPar);
340 }
341 
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
346 {
348 }
349 
350 
352 {
354 
355  checkNames(objNames, syncPar);
356  return objNames;
357 }
358 
359 
361 {
362  // No nullptr check - only called with string literals
363  return sortedNames(static_cast<word>(clsName));
364 }
365 
366 
368 (
369  const char* clsName,
370  const bool syncPar
371 ) const
372 {
373  // No nullptr check - only called with string literals
374  return names(static_cast<word>(clsName), syncPar);
375 }
376 
377 
378 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
379 
381 {
382  return
384  (
385  [](const word& k){ return k.ends_with("_0"); },
386  true // prune
387  );
388 }
389 
390 
392 {
394 
395  syncNames(objNames);
396  return objNames;
397 }
398 
399 
400 bool Foam::IOobjectList::checkNames(const bool syncPar) const
401 {
402  if (syncPar && Pstream::parRun())
403  {
405 
406  return checkNames(objNames, syncPar);
407  }
408 
409  return true;
410 }
411 
412 
413 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
414 
416 {
417  transfer(list);
418 }
419 
420 
421 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
422 
424 {
425  os << nl << list.size() << nl << token::BEGIN_LIST << nl;
426 
427  forAllConstIters(list, iter)
428  {
429  os << iter.key() << token::SPACE
430  << iter.val()->headerClassName() << nl;
431  }
432 
433  os << token::END_LIST;
435 
436  return os;
437 }
438 
439 
440 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
insert
srcOptions insert("case", fileName(rootDirSource/caseDirSource))
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::Warning
messageStream Warning
Foam::IOobjectList::findObject
const IOobject * findObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:270
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::error::throwing
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:169
Foam::IOobjectList::sortedNames
wordList sortedNames() const
The sorted names of the IOobjects.
Definition: IOobjectList.C:345
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
IOobjectList.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::IOobjectList::append
label append(const IOobjectList &other)
Definition: IOobjectList.C:186
Foam::IOobjectList::count
label count() const
The number of objects with headerClassName == Type::typeName.
Foam::IOobjectList::names
wordList names() const
The names of the IOobjects.
Definition: IOobjectList.C:310
erase
srcOptions erase("case")
IOList.H
Foam::predicates::always
Unary and binary predicates that always return true, useful for templating.
Definition: predicates.H:56
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:62
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IOobject::writeOption
writeOption
Enumeration defining the write options.
Definition: IOobject.H:192
Foam::IOobjectList::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: IOobjectList.C:297
Foam::IOobjectList::getObject
IOobject * getObject(const word &objName) const
Definition: IOobjectList.C:284
Foam::IOobjectList::remove
bool remove(const IOobject &io)
Remove IOobject from the list.
Definition: IOobjectList.C:236
Foam::sort
void sort(UList< T > &a)
Definition: UList.C:261
Foam::IOobjectList::cfindObject
const IOobject * cfindObject(const word &objName) const
Return const pointer to the object found by name.
Definition: IOobjectList.C:245
Foam::IOobjectList::prune_0
label prune_0()
Remove objects with names ending with "_0" (restart fields)
Definition: IOobjectList.C:380
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:58
Foam::FatalError
error FatalError
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IOobjectList::allNames
wordList allNames() const
The sorted names of all objects (synchronised across processors)
Definition: IOobjectList.C:391
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
Foam::Pstream::combineGather
static void combineGather(const List< commsStruct > &comms, T &Value, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:48
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:55
Foam::IOobjectList::IOobjectList
IOobjectList()
Construct null with default (128) table capacity.
Definition: IOobjectList.C:81
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::HashPtrTable< IOobject >::const_iterator
typename parent_type::const_iterator const_iterator
Definition: HashPtrTable.H:89
Foam::IOobject::name
const word & name() const noexcept
Return name.
Definition: IOobjectI.H:65
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::IOobjectList::add
bool add(autoPtr< IOobject > &objectPtr)
Add IOobject to the list.
Definition: IOobjectList.C:164
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::HashPtrTable
A HashTable of pointers to objects of type <T>, with deallocation management of the pointers.
Definition: HashPtrTable.H:54
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::IOobjectList::lookupClass
IOobjectList lookupClass() const
The list of IOobjects with headerClassName == Type::typeName.
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::List< fileName >
Foam::IOobjectList::operator=
void operator=(const IOobjectList &)=delete
No copy assignment.
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::IOList< label >
Foam::fileOperation::readObjects
virtual fileNameList readObjects(const objectRegistry &db, const fileName &instance, const fileName &local, word &newInstance) const
Search directory for objects. Used in IOobjectList.
Definition: fileOperation.C:1149
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
Foam::IOerror
Report an I/O error.
Definition: error.H:279
predicates.H
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:183
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
Foam::Pstream::combineScatter
static void combineScatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:183