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-2016 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 void Foam::IOdictionary::readFile(const bool masterOnly)
32 {
33  if (Pstream::master() || !masterOnly)
34  {
35  if (debug)
36  {
37  Pout<< "IOdictionary : Reading " << objectPath()
38  << " from file " << endl;
39  }
40 
41  // Set flag for e.g. codeStream
42  bool oldFlag = regIOobject::masterOnlyReading;
43  regIOobject::masterOnlyReading = masterOnly;
44 
45  // Read file
46  readStream(typeName) >> *this;
47  close();
48 
50 
51  if (writeDictionaries && Pstream::master())
52  {
53  Sout<< nl
54  << "--- IOdictionary " << name()
55  << ' ' << objectPath() << ":" << nl;
57  writeData(Sout);
58  Sout<< "--- End of IOdictionary " << name() << nl << endl;
59  }
60  }
61 
62  if (masterOnly && Pstream::parRun())
63  {
64  // Scatter master data using communication scheme
65 
66  const List<Pstream::commsStruct>& comms =
67  (
71  );
72 
73  // Master reads headerclassname from file. Make sure this gets
74  // transfered as well as contents.
76  (
77  comms,
78  const_cast<word&>(headerClassName()),
81  );
83 
84  // Get my communication order
85  const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
86 
87  // Reveive from up
88  if (myComm.above() != -1)
89  {
90  if (debug)
91  {
92  Pout<< "IOdictionary : Reading " << objectPath()
93  << " from processor " << myComm.above() << endl;
94  }
95 
96  // Note: use ASCII for now - binary IO of dictionaries is
97  // not currently supported or rather the primitiveEntries of
98  // the dictionary think they are in binary form whereas they are
99  // not. Could reset all the ITstreams to ascii?
100  IPstream fromAbove
101  (
103  myComm.above(),
104  0,
108  );
109  IOdictionary::readData(fromAbove);
110  }
111 
112  // Send to my downstairs neighbours. Note reverse order not
113  // necessary here but just for consistency with other uses
114  // (e.g. gatherScatter.C)
115  forAllReverse(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 // ************************************************************************* //
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:417
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:265
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool writeHeader(Ostream &) const
Write header.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:411
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:440
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:464
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:274
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:266
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:447
OSstream Sout(cout,"Sout")
Definition: IOstreams.H:51
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
Istream & readStream(const word &)
Return Istream and check object type against that given.
void close()
Close Istream.
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:456
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:181
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
bool writeData(Ostream &) const
WriteData function required for regIOobject write operation.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const char nl
Definition: Ostream.H:262
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:393
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:399
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173
bool readData(Istream &)
ReadData function required for regIOobject read operation.