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 
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 // ************************************************************************* //
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::UPstream::scheduled
@ scheduled
Definition: UPstream.H:67
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:50
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:147
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOdictionary::writeData
bool writeData(Ostream &) const
WriteData function required for regIOobject write operation.
Definition: IOdictionaryIO.C:157
Foam::IOobject::objectPath
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:376
Foam::IOobject::writeHeader
bool writeHeader(Ostream &) const
Write header.
Definition: IOobjectWriteHeader.C:67
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:279
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::IOdictionary::readData
bool readData(Istream &)
ReadData function required for regIOobject read operation.
Definition: IOdictionaryIO.C:139
writeData
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Foam::IOdictionary::readFile
void readFile(const bool)
Read dictionary from file.
Definition: IOdictionaryIO.C:33
Pstream.H
Foam::regIOobject::masterOnlyReading
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
Foam::UPstream::commsStruct::above
label above() const
Definition: UPstream.H:125
Foam::UPstream::commsStruct
Structure for communicating between processors.
Definition: UPstream.H:76
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::UPstream::nProcsSimpleSum
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:249
IOdictionary.H
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
Foam::regIOobject::readStream
Istream & readStream()
Return Istream.
Definition: regIOobjectRead.C:34
Foam::IOdictionary::name
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:181
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::regIOobject::close
void close()
Close Istream.
Definition: regIOobjectRead.C:148
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:258
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::IOdictionary::writeDictionaries
static bool writeDictionaries
Definition: IOdictionary.H:60
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:50
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:435
Foam::Sout
OSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::IOobject::note
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:285
Foam::UPstream::commsStruct::below
const labelList & below() const
Definition: UPstream.H:130
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:444
Foam::dictionary::write
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173