FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
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 <OpenFOAM/HashSet.H>
28 #include <OpenFOAM/Pstream.H>
29 #include <OpenFOAM/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.
35 template<class GeoField, class Mesh>
37 (
38  const Mesh& mesh,
39  const IOobjectList& objects,
41  const bool syncPar
42 )
43 {
44  // Search list of objects for wanted type
45  IOobjectList fieldObjects(objects.lookupClass(GeoField::typeName));
46 
47  wordList masterNames(fieldObjects.names());
48 
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);
54 
55  HashSet<word> localNamesSet(localNames);
56 
57  forAll(masterNames, i)
58  {
59  const word& masterFld = masterNames[i];
60 
61  HashSet<word>::iterator iter = localNamesSet.find(masterFld);
62 
63  if (iter == localNamesSet.end())
64  {
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  }
80 
81  forAllConstIter(HashSet<word>, localNamesSet, iter)
82  {
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  }
94 
95 
96  fields.setSize(masterNames.size());
97 
98  // Make sure to read in masterNames order.
99 
100  forAll(masterNames, i)
101  {
102  Info<< "Reading " << GeoField::typeName << ' ' << masterNames[i]
103  << endl;
104 
105  const IOobject& io = *fieldObjects[masterNames[i]];
106 
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;
127 }
128 
129 
130 // ************************ vim: set sw=4 sts=4 et: ************************ //