Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / regIOobject / regIOobjectRead.C
blobe2ed73d47492d435e8def988419e1ddb71175fbc
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "regIOobject.H"
27 #include "IFstream.H"
28 #include "Time.H"
29 #include "Pstream.H"
32 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
34 Foam::Istream& Foam::regIOobject::readStream()
36     if (IFstream::debug)
37     {
38         Info<< "regIOobject::readStream() : "
39             << "reading object " << name()
40             << " from file " << objectPath()
41             << endl;
42     }
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     }
52     // Construct object stream and read header if not already constructed
53     if (!isPtr_)
54     {
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();
67             if (!objPath.size())
68             {
69                 FatalIOError
70                 (
71                     "regIOobject::readStream()",
72                     __FILE__,
73                     __LINE__,
74                     objectPath(),
75                     0
76                 )   << "cannot find file"
77                     << exit(FatalIOError);
78             }
79         }
81         if (!(isPtr_ = objectStream(objPath)))
82         {
83             FatalIOError
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     }
101     // Mark as uptodate if read succesfully
102     if (watchIndex_ != -1)
103     {
104         time().setUnmodified(watchIndex_);
105     }
107     return *isPtr_;
111 Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
113     if (IFstream::debug)
114     {
115         Info<< "regIOobject::readStream(const word&) : "
116             << "reading object " << name()
117             << " from file " << objectPath()
118             << endl;
119     }
121     // Construct IFstream if not already constructed
122     if (!isPtr_)
123     {
124         readStream();
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     }
144     return *isPtr_;
148 void Foam::regIOobject::close()
150     if (IFstream::debug)
151     {
152         Info<< "regIOobject::close() : "
153             << "finished reading " << filePath()
154             << endl;
155     }
157     if (isPtr_)
158     {
159         delete isPtr_;
160         isPtr_ = NULL;
161     }
165 bool Foam::regIOobject::readData(Istream&)
167     return false;
171 bool Foam::regIOobject::read()
173     // Note: cannot do anything in readStream itself since this is used by
174     // e.g. GeometricField.
176     bool masterOnly = 
177         regIOobject::fileModificationChecking == timeStampMaster
178      || regIOobject::fileModificationChecking == inotifyMaster;
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         ok = readData(readStream(type()));
190         close();
191     }
193     if (masterOnly && Pstream::parRun())
194     {
195         // Scatter master data using communication scheme
197         const List<Pstream::commsStruct>& comms =
198         (
199             (Pstream::nProcs() < Pstream::nProcsSimpleSum)
200           ? Pstream::linearCommunication()
201           : Pstream::treeCommunication()
202         );
204         // Master reads headerclassname from file. Make sure this gets
205         // transfered as well as contents.
206         Pstream::scatter(comms, const_cast<word&>(headerClassName()));
207         Pstream::scatter(comms, note());
210         // Get my communication order
211         const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
213         // Reveive from up
214         if (myComm.above() != -1)
215         {
216             if (IFstream::debug)
217             {
218                 Pout<< "regIOobject::read() : "
219                     << "reading object " << name()
220                     << " from processor " << myComm.above()
221                     << endl;
222             }
224             // Note: use ASCII for now - binary IO of dictionaries is
225             // not currently supported
226             IPstream fromAbove
227             (
228                 Pstream::scheduled,
229                 myComm.above(),
230                 0,
231                 IOstream::ASCII
232             );
233             ok = readData(fromAbove);
234         }
236         // Send to my downstairs neighbours
237         forAll(myComm.below(), belowI)
238         {
239             OPstream toBelow
240             (
241                 Pstream::scheduled,
242                 myComm.below()[belowI],
243                 0,
244                 Pstream::msgType(),
245                 IOstream::ASCII
246             );
247             writeData(toBelow);
248         }
249     }
250     return ok;
254 bool Foam::regIOobject::modified() const
256     if (watchIndex_ != -1)
257     {
258         return time().getState(watchIndex_) != fileMonitor::UNMODIFIED;
259     }
260     else
261     {
262         return false;
263     }
267 bool Foam::regIOobject::readIfModified()
269     if (watchIndex_ != -1)
270     {
271         if (modified())
272         {
273             const fileName& fName = time().getFile(watchIndex_);
274             Info<< "regIOobject::readIfModified() : " << nl
275                 << "    Re-reading object " << name()
276                 << " from file " << fName << endl;
277             return read();
278         }
279         else
280         {
281             return false;
282         }
283     }
284     else
285     {
286         return false;
287     }
291 // ************************************************************************* //