initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / fields / ReadFields / ReadFields.C
blobedbf82c7c9af5d79ffeba004ae0a90d933e21aae
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "ReadFields.H"
28 #include "HashSet.H"
29 #include "Pstream.H"
30 #include "IOobjectList.H"
32 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
34 // Read all fields of type. Returns names of fields read. Guarantees all
35 // processors to read fields in same order.
36 template<class GeoField, class Mesh>
37 Foam::wordList Foam::ReadFields
39     const Mesh& mesh,
40     const IOobjectList& objects,
41     PtrList<GeoField>& fields,
42     const bool syncPar
45     // Search list of objects for wanted type
46     IOobjectList fieldObjects(objects.lookupClass(GeoField::typeName));
48     wordList masterNames(fieldObjects.names());
50     if (syncPar && Pstream::parRun())
51     {
52         // Check that I have the same fields as the master
53         const wordList localNames(masterNames);
54         Pstream::scatter(masterNames);
56         HashSet<word> localNamesSet(localNames);
58         forAll(masterNames, i)
59         {
60             const word& masterFld = masterNames[i];
62             HashSet<word>::iterator iter = localNamesSet.find(masterFld);
64             if (iter == localNamesSet.end())
65             {
66                 FatalErrorIn
67                 (
68                     "ReadFields<class GeoField, class Mesh>"
69                     "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
70                     ", const bool)"
71                 )   << "Fields not synchronised across processors." << endl
72                     << "Master has fields " << masterNames
73                     << "  processor " << Pstream::myProcNo()
74                     << " has fields " << localNames << exit(FatalError);
75             }
76             else
77             {
78                 localNamesSet.erase(iter);
79             }
80         }
82         forAllConstIter(HashSet<word>, localNamesSet, iter)
83         {
84             FatalErrorIn
85             (
86                 "ReadFields<class GeoField, class Mesh>"
87                 "(const Mesh&, const IOobjectList&, PtrList<GeoField>&"
88                 ", const bool)"
89             )   << "Fields not synchronised across processors." << endl
90                 << "Master has fields " << masterNames
91                 << "  processor " << Pstream::myProcNo()
92                 << " has fields " << localNames << exit(FatalError);
93         }
94     }
97     fields.setSize(masterNames.size());
99     // Make sure to read in masterNames order.
101     forAll(masterNames, i)
102     {
103         Info<< "Reading " << GeoField::typeName << ' ' << masterNames[i]
104             << endl;
106         const IOobject& io = *fieldObjects[masterNames[i]];
108         fields.set
109         (
110             i,
111             new GeoField
112             (
113                 IOobject
114                 (
115                     io.name(),
116                     io.instance(),
117                     io.local(),
118                     io.db(),
119                     IOobject::MUST_READ,
120                     IOobject::AUTO_WRITE,
121                     io.registerObject()
122                 ),
123                 mesh
124             )
125         );
126     }
127     return masterNames;    
131 // ************************************************************************* //