ReadFields.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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ReadFields.H"
27 #include "HashSet.H"
28 #include "Pstream.H"
29 #include "IOobjectList.H"
30 
31 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
32 
33 // Read all fields of type. Returns names of fields read. Guarantees all
34 // processors to read fields in same order.
36 (
37  const IOobjectList& fieldObjects,
38  const bool syncPar
39 )
40 {
41  // Get sorted field names. Sorting needed in parallel since different
42  // processors (using different file servers) might pick up the files
43  // in different order.
44  wordList masterNames(fieldObjects.sortedNames());
45 
46  if (syncPar && Pstream::parRun())
47  {
48  // Check that I have the same fields as the master
49  const wordList localNames(masterNames);
50  Pstream::scatter(masterNames);
51 
52  HashSet<word> localNamesSet(localNames);
53 
54  forAll(masterNames, i)
55  {
56  const word& masterFld = masterNames[i];
57 
58  HashSet<word>::iterator iter = localNamesSet.find(masterFld);
59 
60  if (iter == localNamesSet.end())
61  {
63  << "Fields not synchronised across processors." << endl
64  << "Master has fields " << masterNames
65  << " processor " << Pstream::myProcNo()
66  << " has fields " << localNames << exit(FatalError);
67  }
68  else
69  {
70  localNamesSet.erase(iter);
71  }
72  }
73 
74  forAllConstIter(HashSet<word>, localNamesSet, iter)
75  {
77  << "Fields not synchronised across processors." << endl
78  << "Master has fields " << masterNames
79  << " processor " << Pstream::myProcNo()
80  << " has fields " << localNames << exit(FatalError);
81  }
82  }
83 
84  return masterNames;
85 }
86 
87 
88 // ************************************************************************* //
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::HashTable::iterator
An STL-conforming iterator.
Definition: HashTable.H:415
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IOobjectList::sortedNames
wordList sortedNames() const
Return the sorted list of names of the IOobjects.
Definition: IOobjectList.C:227
IOobjectList.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::HashSet
A HashTable with keys but without contents.
Definition: HashSet.H:59
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::HashTable< nil, word, string::hash >::erase
bool erase(const iterator &)
Erase a hashedEntry specified by given iterator.
HashSet.H
Foam::FatalError
error FatalError
Pstream.H
Foam::HashTable< nil, word, string::hash >::find
iterator find(const Key &)
Find and return an iterator set at the hashedEntry.
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:50
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
ReadFields.H
Helper routine to read fields.
Foam::fieldNames
wordList fieldNames(const IOobjectList &objects, const bool syncPar)
Get sorted names of fields of type. If syncPar and running in parallel.
Definition: ReadFields.C:36
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59