IOdictionaryIO.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-2013 OpenFOAM Foundation
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 "IOdictionary.H"
27 #include "Pstream.H"
28 
29 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
30 
31 // Parallel aware reading, using non-virtual type information (typeName instead
32 // of type()) because of use in constructor.
33 void Foam::IOdictionary::readFile(const bool masterOnly)
34 {
35  if (Pstream::master() || !masterOnly)
36  {
37  if (debug)
38  {
39  Pout<< "IOdictionary : Reading " << objectPath()
40  << " from file " << endl;
41  }
42 
43  // Set flag for e.g. codeStream
44  bool oldFlag = regIOobject::masterOnlyReading;
45  regIOobject::masterOnlyReading = masterOnly;
46 
47  // Read file
48  readStream(typeName) >> *this;
49  close();
50 
52 
53  if (writeDictionaries && Pstream::master())
54  {
55  Sout<< nl
56  << "--- IOdictionary " << name()
57  << ' ' << objectPath() << ":" << nl;
59  writeData(Sout);
60  Sout<< "--- End of IOdictionary " << name() << nl << endl;
61  }
62  }
63 
64  if (masterOnly && Pstream::parRun())
65  {
66  // Scatter master data using communication scheme
67 
68  const List<Pstream::commsStruct>& comms =
69  (
73  );
74 
75  // Master reads headerclassname from file. Make sure this gets
76  // transfered as well as contents.
78  (
79  comms,
80  const_cast<word&>(headerClassName()),
83  );
85 
86  // Get my communication order
87  const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
88 
89  // Reveive from up
90  if (myComm.above() != -1)
91  {
92  if (debug)
93  {
94  Pout<< "IOdictionary : Reading " << objectPath()
95  << " from processor " << myComm.above() << endl;
96  }
97 
98  // Note: use ASCII for now - binary IO of dictionaries is
99  // not currently supported or rather the primitiveEntries of
100  // the dictionary think they are in binary form whereas they are
101  // not. Could reset all the ITstreams to ascii?
102  IPstream fromAbove
103  (
105  myComm.above(),
106  0,
110  );
111  IOdictionary::readData(fromAbove);
112  }
113 
114  // Send to my downstairs neighbours
115  forAll(myComm.below(), belowI)
116  {
117  if (debug)
118  {
119  Pout<< "IOdictionary : Sending " << objectPath()
120  << " to processor " << myComm.below()[belowI] << endl;
121  }
122  OPstream toBelow
123  (
125  myComm.below()[belowI],
126  0,
130  );
131  IOdictionary::writeData(toBelow);
132  }
133  }
134 }
135 
136 
137 // * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
138 
140 {
141  is >> *this;
142 
143  if (writeDictionaries && Pstream::master() && !is.bad())
144  {
145  Sout<< nl
146  << "--- IOdictionary " << name()
147  << ' ' << objectPath() << ":" << nl;
148  writeHeader(Sout);
149  writeData(Sout);
150  Sout<< "--- End of IOdictionary " << name() << nl << endl;
151  }
152 
153  return !is.bad();
154 }
155 
156 
158 {
159  dictionary::write(os, false);
160  return os.good();
161 }
162 
163 
164 // ************************************************************************* //
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:380
bool readData(Istream &)
ReadData function required for regIOobject read operation.
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:266
OSstream Sout(cout,"Sout")
Definition: IOstreams.H:51
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:183
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:386
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:434
#define forAll(list, i)
Definition: UList.H:421
void close()
Close Istream.
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:451
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:404
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:398
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:443
bool writeData(Ostream &) const
WriteData function required for regIOobject write operation.
bool writeHeader(Ostream &) const
Write header.
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173
Istream & readStream(const word &)
Return Istream and check object type against that given.
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:261
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:252