Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / fields / ReadFields / ReadFields.C
blobd6c45a935bc98d28a5cc34414d4ea1c19bc8cae7
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
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 // ************************************************************************* //