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-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 
35 {
36  if (IFstream::debug)
37  {
38  Pout<< "regIOobject::readStream() : "
39  << "reading object " << name()
40  << " from file " << objectPath()
41  << endl;
42  }
43 
44  if (readOpt() == NO_READ)
45  {
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  {
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  {
105  }
106 
107  return *isPtr_;
108 }
109 
110 
112 {
113  if (IFstream::debug)
114  {
115  Pout<< "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  FatalIOErrorInFunction(*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  Pout<< "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 =
177  regIOobject::fileModificationChecking == timeStampMaster
178  || regIOobject::fileModificationChecking == inotifyMaster;
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 // ************************************************************************* //
regIOobject.H
Foam::IOobject::filePath
fileName filePath() const
Return complete path + object name if the file exists.
Definition: IOobject.C:336
Foam::IOobject::objectStream
Istream * objectStream()
Construct and return an IFstream for the object.
Definition: IOobject.C:410
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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::fileMonitor::UNMODIFIED
@ UNMODIFIED
Definition: fileMonitor.H:71
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
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::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:171
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::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::time
const Time & time() const
Return time.
Definition: IOobject.C:245
Foam::IOobject::objectPath
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:376
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::Time::setUnmodified
void setUnmodified(const label) const
Set current state of file (using handle) to unmodified.
Definition: Time.C:735
Foam::regIOobject::modified
virtual bool modified() const
Return true if the object's file (or files for objectRegistry)
Definition: regIOobjectRead.C:271
Foam::IOobject::readOpt
readOption readOpt() const
Definition: IOobject.H:317
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::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::regIOobject::watchIndex_
label watchIndex_
Modification watch index.
Definition: regIOobject.H:96
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobject.H:273
Foam::regIOobject::isPtr_
Istream * isPtr_
Istream for reading.
Definition: regIOobject.H:102
IFstream.H
writeData
const bool writeData(readBool(pdfDictionary.lookup("writeData")))
Foam::regIOobject::readIfModified
virtual bool readIfModified()
Read object if modified (as set by call to modified)
Definition: regIOobjectRead.C:284
Foam::FatalError
error FatalError
Pstream.H
Foam::regIOobject::masterOnlyReading
static bool masterOnlyReading
To flag master-only reading of objects.
Definition: regIOobject.H:82
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::UPstream::commsStruct::above
label above() const
Definition: UPstream.H:125
Foam::UPstream::commsStruct
Structure for communicating between processors.
Definition: UPstream.H:76
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
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
Foam::regIOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::regIOobject::readStream
Istream & readStream()
Return Istream.
Definition: regIOobjectRead.C:34
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::regIOobject::readData
virtual bool readData(Istream &)
Virtual readData function.
Definition: regIOobjectRead.C:165
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:50
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::IOobject::readHeader
bool readHeader(Istream &)
Read header.
Definition: IOobjectReadHeader.C:31
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::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
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::Time::getFile
const fileName & getFile(const label) const
Get name of file being watched (using handle)
Definition: Time.C:720