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-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 "regIOobject.H"
27 #include "IFstream.H"
28 #include "Time.H"
29 #include "Pstream.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 Foam::Istream& Foam::regIOobject::readStream()
34 {
35  if (IFstream::debug)
36  {
38  << "Reading object " << name()
39  << " from file " << objectPath()
40  << endl;
41  }
42 
43  if (readOpt() == NO_READ)
44  {
46  << "NO_READ specified for read-constructor of object " << name()
47  << " of class " << headerClassName()
48  << abort(FatalError);
49  }
50 
51  // Construct object stream and read header if not already constructed
52  if (!isPtr_)
53  {
54 
55  fileName objPath;
56  if (watchIndex_ != -1)
57  {
58  // File is being watched. Read exact file that is being watched.
59  objPath = time().getFile(watchIndex_);
60  }
61  else
62  {
63  // Search intelligently for file
64  objPath = filePath();
65 
66  if (!objPath.size())
67  {
69  (
70  "regIOobject::readStream()",
71  __FILE__,
72  __LINE__,
73  objectPath(),
74  0
75  ) << "cannot find file"
76  << exit(FatalIOError);
77  }
78  }
79 
80  if (!(isPtr_ = objectStream(objPath)))
81  {
83  (
84  "regIOobject::readStream()",
85  __FILE__,
86  __LINE__,
87  objPath,
88  0
89  ) << "cannot open file"
90  << exit(FatalIOError);
91  }
92  else if (!readHeader(*isPtr_))
93  {
94  FatalIOErrorInFunction(*isPtr_)
95  << "problem while reading header for object " << name()
96  << exit(FatalIOError);
97  }
98  }
99 
100  // Mark as uptodate if read successfully
101  if (watchIndex_ != -1)
102  {
103  time().setUnmodified(watchIndex_);
104  }
105 
106  return *isPtr_;
107 }
108 
109 
110 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
111 {
112  if (IFstream::debug)
113  {
115  << "Reading object " << name()
116  << " from file " << objectPath()
117  << endl;
118  }
119 
120  // Construct IFstream if not already constructed
121  if (!isPtr_)
122  {
123  readStream();
124 
125  // Check the className of the regIOobject
126  // dictionary is an allowable name in case the actual class
127  // instantiated is a dictionary
128  if
129  (
130  expectName.size()
131  && headerClassName() != expectName
132  && headerClassName() != "dictionary"
133  )
134  {
135  FatalIOErrorInFunction(*isPtr_)
136  << "unexpected class name " << headerClassName()
137  << " expected " << expectName << endl
138  << " while reading object " << name()
139  << exit(FatalIOError);
140  }
141  }
142 
143  return *isPtr_;
144 }
145 
146 
148 {
149  if (IFstream::debug)
150  {
152  << "Finished reading " << filePath()
153  << endl;
154  }
155 
156  if (isPtr_)
157  {
158  delete isPtr_;
159  isPtr_ = NULL;
160  }
161 }
162 
163 
165 {
166  return false;
167 }
168 
169 
171 {
172  // Note: cannot do anything in readStream itself since this is used by
173  // e.g. GeometricField.
174 
175  bool masterOnly =
178 
179  bool ok = true;
180  if (Pstream::master() || !masterOnly)
181  {
182  if (IFstream::debug)
183  {
184  Pout<< "regIOobject::read() : "
185  << "reading object " << name()
186  << " from file " << endl;
187  }
188 
189  // Set flag for e.g. codeStream
190  bool oldFlag = regIOobject::masterOnlyReading;
191  regIOobject::masterOnlyReading = masterOnly;
192 
193  // Read file
194  ok = readData(readStream(type()));
195  close();
196 
198  }
199 
200  if (masterOnly && Pstream::parRun())
201  {
202  // Scatter master data using communication scheme
203 
204  const List<Pstream::commsStruct>& comms =
205  (
209  );
210 
211  // Master reads headerclassname from file. Make sure this gets
212  // transfered as well as contents.
214  (
215  comms,
216  const_cast<word&>(headerClassName()),
219  );
221 
222 
223  // Get my communication order
224  const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
225 
226  // Reveive from up
227  if (myComm.above() != -1)
228  {
229  if (IFstream::debug)
230  {
231  Pout<< "regIOobject::read() : "
232  << "reading object " << name()
233  << " from processor " << myComm.above()
234  << endl;
235  }
236 
237  // Note: use ASCII for now - binary IO of dictionaries is
238  // not currently supported
239  IPstream fromAbove
240  (
242  myComm.above(),
243  0,
247  );
248  ok = readData(fromAbove);
249  }
250 
251  // Send to my downstairs neighbours. Note reverse order not
252  // nessecary here - just for consistency reasons.
253  forAllReverse(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_);
292  << nl
293  << " Re-reading object " << name()
294  << " from file " << fName << endl;
295  return read();
296  }
297  else
298  {
299  return false;
300  }
301  }
302  else
303  {
304  return false;
305  }
306 }
307 
308 
309 // ************************************************************************* //
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:363
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual bool read()
Read object.
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
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
fileMonitor::fileState getState(const label) const
Get current state of file (using handle)
Definition: Time.C:701
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
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
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
virtual bool modified() const
Return true if the object&#39;s file (or files for objectRegistry)
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
Input inter-processor communications stream.
Definition: IPstream.H:50
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:272
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:299
void close()
Close Istream.
A class for handling words, derived from string.
Definition: word.H:59
const Time & time() const
Return time.
Definition: IOobject.C:227
virtual bool readIfModified()
Read object if modified (as set by call to modified)
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:456
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Structure for communicating between processors.
Definition: UPstream.H:76
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
static const char nl
Definition: Ostream.H:262
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:371
Output inter-processor communications stream.
Definition: OPstream.H:50
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
virtual bool readData(Istream &)
Virtual readData function.
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:461
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
const fileName & getFile(const label) const
Get name of file being watched (using handle)
Definition: Time.C:694
readOption readOpt() const
Definition: IOobject.H:304
const labelList & below() const
Definition: UPstream.H:130
label above() const
Definition: UPstream.H:125
virtual bool writeData(Ostream &) const =0
Pure virtual writaData function.
void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: Time.C:709
const word & name() const
Return name.
Definition: IOobject.H:260
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.