Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / fields / ReadFields / ReadFields.C
blob968c612e4398244c8170b7c0f2eeb923dd9f50ec
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "ReadFields.H"
27 #include "HashSet.H"
28 #include "Pstream.H"
29 #include "IOobjectList.H"
31 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
33 // Read all fields of type. Returns names of fields read. Guarantees all
34 // processors to read fields in same order.
35 template<class GeoField, class Mesh>
36 Foam::wordList Foam::ReadFields
38     const Mesh& mesh,
39     const IOobjectList& objects,
40     PtrList<GeoField>& fields,
41     const bool syncPar
44     // Search list of objects for wanted type
45     IOobjectList fieldObjects(objects.lookupClass(GeoField::typeName));
47     wordList masterNames(fieldObjects.names());
49     if (syncPar && Pstream::parRun())
50     {
51         // Check that I have the same fields as the master
52         const wordList localNames(masterNames);
53         Pstream::scatter(masterNames);
55         HashSet<word> localNamesSet(localNames);
57         forAll(masterNames, i)
58         {
59             const word& masterFld = masterNames[i];
61             HashSet<word>::iterator iter = localNamesSet.find(masterFld);
63             if (iter == localNamesSet.end())
64             {
65                 FatalErrorIn
66                 (
67                     "ReadFields<class GeoField, class Mesh>"
68                     "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
69                     ", const bool)"
70                 )   << "Fields not synchronised across processors." << endl
71                     << "Master has fields " << masterNames
72                     << "  processor " << Pstream::myProcNo()
73                     << " has fields " << localNames << exit(FatalError);
74             }
75             else
76             {
77                 localNamesSet.erase(iter);
78             }
79         }
81         forAllConstIter(HashSet<word>, localNamesSet, iter)
82         {
83             FatalErrorIn
84             (
85                 "ReadFields<class GeoField, class Mesh>"
86                 "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
87                 ", const bool)"
88             )   << "Fields not synchronised across processors." << endl
89                 << "Master has fields " << masterNames
90                 << "  processor " << Pstream::myProcNo()
91                 << " has fields " << localNames << exit(FatalError);
92         }
93     }
96     fields.setSize(masterNames.size());
98     // Make sure to read in masterNames order.
100     forAll(masterNames, i)
101     {
102         Info<< "Reading " << GeoField::typeName << ' ' << masterNames[i]
103             << endl;
105         const IOobject& io = *fieldObjects[masterNames[i]];
107         fields.set
108         (
109             i,
110             new GeoField
111             (
112                 IOobject
113                 (
114                     io.name(),
115                     io.instance(),
116                     io.local(),
117                     io.db(),
118                     IOobject::MUST_READ,
119                     IOobject::AUTO_WRITE,
120                     io.registerObject()
121                 ),
122                 mesh
123             )
124         );
125     }
126     return masterNames;
130 // ************************************************************************* //