regIOobjectRead.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 "regIOobject.H"
27 #include "IFstream.H"
28 #include "Time.H"
29 #include "Pstream.H"
30 
31 
32 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33 
34 Foam::Istream& Foam::regIOobject::readStream()
35 {
36  if (IFstream::debug)
37  {
38  Info<< "regIOobject::readStream() : "
39  << "reading object " << name()
40  << " from file " << objectPath()
41  << endl;
42  }
43 
44  if (readOpt() == NO_READ)
45  {
46  FatalErrorIn("regIOobject::readStream()")
47  << "NO_READ specified for read-constructor of object " << name()
48  << " of class " << headerClassName()
49  << abort(FatalError);
50  }
51 
52  // Construct object stream and read header if not already constructed
53  if (!isPtr_)
54  {
55 
56  fileName objPath;
57  if (watchIndex_ != -1)
58  {
59  // File is being watched. Read exact file that is being watched.
60  objPath = time().getFile(watchIndex_);
61  }
62  else
63  {
64  // Search intelligently for file
65  objPath = filePath();
66 
67  if (!objPath.size())
68  {
70  (
71  "regIOobject::readStream()",
72  __FILE__,
73  __LINE__,
74  objectPath(),
75  0
76  ) << "cannot find file"
77  << exit(FatalIOError);
78  }
79  }
80 
81  if (!(isPtr_ = objectStream(objPath)))
82  {
84  (
85  "regIOobject::readStream()",
86  __FILE__,
87  __LINE__,
88  objPath,
89  0
90  ) << "cannot open file"
91  << exit(FatalIOError);
92  }
93  else if (!readHeader(*isPtr_))
94  {
95  FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
96  << "problem while reading header for object " << name()
97  << exit(FatalIOError);
98  }
99  }
100 
101  // Mark as uptodate if read successfully
102  if (watchIndex_ != -1)
103  {
104  time().setUnmodified(watchIndex_);
105  }
106 
107  return *isPtr_;
108 }
109 
110 
111 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
112 {
113  if (IFstream::debug)
114  {
115  Info<< "regIOobject::readStream(const word&) : "
116  << "reading object " << name()
117  << " from file " << objectPath()
118  << endl;
119  }
120 
121  // Construct IFstream if not already constructed
122  if (!isPtr_)
123  {
124  readStream();
125 
126  // Check the className of the regIOobject
127  // dictionary is an allowable name in case the actual class
128  // instantiated is a dictionary
129  if
130  (
131  expectName.size()
132  && headerClassName() != expectName
133  && headerClassName() != "dictionary"
134  )
135  {
136  FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
137  << "unexpected class name " << headerClassName()
138  << " expected " << expectName << endl
139  << " while reading object " << name()
140  << exit(FatalIOError);
141  }
142  }
143 
144  return *isPtr_;
145 }
146 
147 
149 {
150  if (IFstream::debug)
151  {
152  Info<< "regIOobject::close() : "
153  << "finished reading " << filePath()
154  << endl;
155  }
156 
157  if (isPtr_)
158  {
159  delete isPtr_;
160  isPtr_ = NULL;
161  }
162 }
163 
164 
166 {
167  return false;
168 }
169 
170 
172 {
173  // Note: cannot do anything in readStream itself since this is used by
174  // e.g. GeometricField.
175 
176  bool masterOnly =
179 
180  bool ok = true;
181  if (Pstream::master() || !masterOnly)
182  {
183  if (IFstream::debug)
184  {
185  Pout<< "regIOobject::read() : "
186  << "reading object " << name()
187  << " from file " << endl;
188  }
189 
190  // Set flag for e.g. codeStream
191  bool oldFlag = regIOobject::masterOnlyReading;
192  regIOobject::masterOnlyReading = masterOnly;
193 
194  // Read file
195  ok = readData(readStream(type()));
196  close();
197 
199  }
200 
201  if (masterOnly && Pstream::parRun())
202  {
203  // Scatter master data using communication scheme
204 
205  const List<Pstream::commsStruct>& comms =
206  (
210  );
211 
212  // Master reads headerclassname from file. Make sure this gets
213  // transfered as well as contents.
215  (
216  comms,
217  const_cast<word&>(headerClassName()),
220  );
222 
223 
224  // Get my communication order
225  const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
226 
227  // Reveive from up
228  if (myComm.above() != -1)
229  {
230  if (IFstream::debug)
231  {
232  Pout<< "regIOobject::read() : "
233  << "reading object " << name()
234  << " from processor " << myComm.above()
235  << endl;
236  }
237 
238  // Note: use ASCII for now - binary IO of dictionaries is
239  // not currently supported
240  IPstream fromAbove
241  (
243  myComm.above(),
244  0,
248  );
249  ok = readData(fromAbove);
250  }
251 
252  // Send to my downstairs neighbours
253  forAll(myComm.below(), belowI)
254  {
255  OPstream toBelow
256  (
258  myComm.below()[belowI],
259  0,
263  );
264  writeData(toBelow);
265  }
266  }
267  return ok;
268 }
269 
270 
272 {
273  if (watchIndex_ != -1)
274  {
275  return time().getState(watchIndex_) != fileMonitor::UNMODIFIED;
276  }
277  else
278  {
279  return false;
280  }
281 }
282 
283 
285 {
286  if (watchIndex_ != -1)
287  {
288  if (modified())
289  {
290  const fileName& fName = time().getFile(watchIndex_);
291  Info<< "regIOobject::readIfModified() : " << nl
292  << " Re-reading object " << name()
293  << " from file " << fName << endl;
294  return read();
295  }
296  else
297  {
298  return false;
299  }
300  }
301  else
302  {
303  return false;
304  }
305 }
306 
307 
308 // ************************************************************************* //
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:380
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:266
readOption readOpt() const
Definition: IOobject.H:304
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:323
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
Structure for communicating between processors.
Definition: UPstream.H:76
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Output inter-processor communications stream.
Definition: OPstream.H:50
messageStream Info
void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: Time.C:735
virtual bool readData(Istream &)
Virtual readData function.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
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
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
const fileName & getFile(const label) const
Get name of file being watched (using handle)
Definition: Time.C:720
IOerror FatalIOError
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:434
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:395
virtual bool readIfModified()
Read object if modified (as set by call to modified)
virtual bool writeData(Ostream &) const =0
Pure virtual writaData function.
virtual bool modified() const
Return true if the object&#39;s file (or files for objectRegistry)
label above() const
Definition: UPstream.H:125
#define forAll(list, i)
Definition: UList.H:421
void close()
Close Istream.
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
virtual bool read()
Read object.
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
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: IOobject.H:260
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:404
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:589
const labelList & below() const
Definition: UPstream.H:130
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
Input inter-processor communications stream.
Definition: IPstream.H:50
A class for handling file names.
Definition: fileName.H:69
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
const Time & time() const
Return time.
Definition: IOobject.C:251
fileMonitor::fileState getState(const label) const
Get current state of file (using handle)
Definition: Time.C:727
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
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
bool readHeader(Istream &)
Read header.
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:261
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
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