/
regIOobjectRead.C
291 lines (248 loc) · 7.55 KB
/
regIOobjectRead.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "regIOobject.H"
#include "IFstream.H"
#include "Time.H"
#include "Pstream.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::Istream& Foam::regIOobject::readStream()
{
if (IFstream::debug)
{
Info<< "regIOobject::readStream() : "
<< "reading object " << name()
<< " from file " << objectPath()
<< endl;
}
if (readOpt() == NO_READ)
{
FatalErrorIn("regIOobject::readStream()")
<< "NO_READ specified for read-constructor of object " << name()
<< " of class " << headerClassName()
<< abort(FatalError);
}
// Construct object stream and read header if not already constructed
if (!isPtr_)
{
fileName objPath;
if (watchIndex_ != -1)
{
// File is being watched. Read exact file that is being watched.
objPath = time().getFile(watchIndex_);
}
else
{
// Search intelligently for file
objPath = filePath();
if (!objPath.size())
{
FatalIOError
(
"regIOobject::readStream()",
__FILE__,
__LINE__,
objectPath(),
0
) << "cannot find file"
<< exit(FatalIOError);
}
}
if (!(isPtr_ = objectStream(objPath)))
{
FatalIOError
(
"regIOobject::readStream()",
__FILE__,
__LINE__,
objPath,
0
) << "cannot open file"
<< exit(FatalIOError);
}
else if (!readHeader(*isPtr_))
{
FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
<< "problem while reading header for object " << name()
<< exit(FatalIOError);
}
}
// Mark as uptodate if read successfully
if (watchIndex_ != -1)
{
time().setUnmodified(watchIndex_);
}
return *isPtr_;
}
Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
{
if (IFstream::debug)
{
Info<< "regIOobject::readStream(const word&) : "
<< "reading object " << name()
<< " from file " << objectPath()
<< endl;
}
// Construct IFstream if not already constructed
if (!isPtr_)
{
readStream();
// Check the className of the regIOobject
// dictionary is an allowable name in case the actual class
// instantiated is a dictionary
if
(
expectName.size()
&& headerClassName() != expectName
&& headerClassName() != "dictionary"
)
{
FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
<< "unexpected class name " << headerClassName()
<< " expected " << expectName << endl
<< " while reading object " << name()
<< exit(FatalIOError);
}
}
return *isPtr_;
}
void Foam::regIOobject::close()
{
if (IFstream::debug)
{
Info<< "regIOobject::close() : "
<< "finished reading " << filePath()
<< endl;
}
if (isPtr_)
{
delete isPtr_;
isPtr_ = NULL;
}
}
bool Foam::regIOobject::readData(Istream&)
{
return false;
}
bool Foam::regIOobject::read()
{
// Note: cannot do anything in readStream itself since this is used by
// e.g. GeometricField.
bool masterOnly =
regIOobject::fileModificationChecking == timeStampMaster
|| regIOobject::fileModificationChecking == inotifyMaster;
bool ok = true;
if (Pstream::master() || !masterOnly)
{
if (IFstream::debug)
{
Pout<< "regIOobject::read() : "
<< "reading object " << name()
<< " from file " << endl;
}
ok = readData(readStream(type()));
close();
}
if (masterOnly && Pstream::parRun())
{
// Scatter master data using communication scheme
const List<Pstream::commsStruct>& comms =
(
(Pstream::nProcs() < Pstream::nProcsSimpleSum)
? Pstream::linearCommunication()
: Pstream::treeCommunication()
);
// Master reads headerclassname from file. Make sure this gets
// transfered as well as contents.
Pstream::scatter(comms, const_cast<word&>(headerClassName()));
Pstream::scatter(comms, note());
// Get my communication order
const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];
// Reveive from up
if (myComm.above() != -1)
{
if (IFstream::debug)
{
Pout<< "regIOobject::read() : "
<< "reading object " << name()
<< " from processor " << myComm.above()
<< endl;
}
// Note: use ASCII for now - binary IO of dictionaries is
// not currently supported
IPstream fromAbove
(
Pstream::scheduled,
myComm.above(),
0,
IOstream::ASCII
);
ok = readData(fromAbove);
}
// Send to my downstairs neighbours
forAll(myComm.below(), belowI)
{
OPstream toBelow
(
Pstream::scheduled,
myComm.below()[belowI],
0,
Pstream::msgType(),
IOstream::ASCII
);
writeData(toBelow);
}
}
return ok;
}
bool Foam::regIOobject::modified() const
{
if (watchIndex_ != -1)
{
return time().getState(watchIndex_) != fileMonitor::UNMODIFIED;
}
else
{
return false;
}
}
bool Foam::regIOobject::readIfModified()
{
if (watchIndex_ != -1)
{
if (modified())
{
const fileName& fName = time().getFile(watchIndex_);
Info<< "regIOobject::readIfModified() : " << nl
<< " Re-reading object " << name()
<< " from file " << fName << endl;
return read();
}
else
{
return false;
}
}
else
{
return false;
}
}
// ************************************************************************* //