regIOobjectRead.C 7.9 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
mattijs's avatar
mattijs committed
5
    \\  /    A nd           | Copyright (C) 2004-2011 OpenCFD Ltd.
6
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28

\*---------------------------------------------------------------------------*/

#include "regIOobject.H"
#include "IFstream.H"
#include "Time.H"
29
#include "Pstream.H"
30
31
32
33


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

Mark Olesen's avatar
Mark Olesen committed
34
Foam::Istream& Foam::regIOobject::readStream()
35
36
37
38
39
40
41
42
43
44
45
{
    if (IFstream::debug)
    {
        Info<< "regIOobject::readStream() : "
            << "reading object " << name()
            << " from file " << objectPath()
            << endl;
    }

    if (readOpt() == NO_READ)
    {
Mark Olesen's avatar
Mark Olesen committed
46
        FatalErrorIn("regIOobject::readStream()")
47
48
49
50
51
52
53
54
            << "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_)
    {
55
56
57
58
59
60
61
62
63

        fileName objPath;
        if (watchIndex_ != -1)
        {
            // File is being watched. Read exact file that is being watched.
            objPath = time().getFile(watchIndex_);
        }
        else
        {
64
            // Search intelligently for file
65
            objPath = filePath();
66
67
68
69
70
71
72
73
74
75
76
77
78

            if (!objPath.size())
            {
                FatalIOError
                (
                    "regIOobject::readStream()",
                    __FILE__,
                    __LINE__,
                    objectPath(),
                    0
                )   << "cannot find file"
                    << exit(FatalIOError);
            }
79
80
81
        }

        if (!(isPtr_ = objectStream(objPath)))
82
83
84
        {
            FatalIOError
            (
Mark Olesen's avatar
Mark Olesen committed
85
                "regIOobject::readStream()",
86
87
                __FILE__,
                __LINE__,
88
                objPath,
89
90
91
92
93
94
                0
            )   << "cannot open file"
                << exit(FatalIOError);
        }
        else if (!readHeader(*isPtr_))
        {
Mark Olesen's avatar
Mark Olesen committed
95
            FatalIOErrorIn("regIOobject::readStream()", *isPtr_)
96
97
98
99
100
                << "problem while reading header for object " << name()
                << exit(FatalIOError);
        }
    }

101
    // Mark as uptodate if read successfully
102
    if (watchIndex_ != -1)
103
    {
104
        time().setUnmodified(watchIndex_);
105
106
107
108
109
110
    }

    return *isPtr_;
}


Mark Olesen's avatar
Mark Olesen committed
111
Foam::Istream& Foam::regIOobject::readStream(const word& expectName)
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
    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
        (
Mark Olesen's avatar
Mark Olesen committed
131
132
            expectName.size()
         && headerClassName() != expectName
133
134
135
136
137
         && headerClassName() != "dictionary"
        )
        {
            FatalIOErrorIn("regIOobject::readStream(const word&)", *isPtr_)
                << "unexpected class name " << headerClassName()
Mark Olesen's avatar
Mark Olesen committed
138
                << " expected " << expectName << endl
139
140
141
142
143
144
145
146
147
                << "    while reading object " << name()
                << exit(FatalIOError);
        }
    }

    return *isPtr_;
}


Mark Olesen's avatar
Mark Olesen committed
148
void Foam::regIOobject::close()
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{
    if (IFstream::debug)
    {
        Info<< "regIOobject::close() : "
            << "finished reading " << filePath()
            << endl;
    }

    if (isPtr_)
    {
        delete isPtr_;
        isPtr_ = NULL;
    }
}


Mark Olesen's avatar
Mark Olesen committed
165
bool Foam::regIOobject::readData(Istream&)
166
167
168
169
170
{
    return false;
}


Mark Olesen's avatar
Mark Olesen committed
171
bool Foam::regIOobject::read()
172
{
173
174
175
    // Note: cannot do anything in readStream itself since this is used by
    // e.g. GeometricField.

176
    bool masterOnly =
177
178
179
        regIOobject::fileModificationChecking == timeStampMaster
     || regIOobject::fileModificationChecking == inotifyMaster;

180
    bool ok = true;
181
182
    if (Pstream::master() || !masterOnly)
    {
183
184
185
186
187
188
        if (IFstream::debug)
        {
            Pout<< "regIOobject::read() : "
                << "reading object " << name()
                << " from file " << endl;
        }
189
190
191
192
193
194

        // Set flag for e.g. codeStream
        bool oldFlag = regIOobject::masterOnlyReading;
        regIOobject::masterOnlyReading = masterOnly;

        // Read file
195
196
        ok = readData(readStream(type()));
        close();
197
198

        regIOobject::masterOnlyReading = oldFlag;
199
200
    }

201
    if (masterOnly && Pstream::parRun())
202
    {
203
204
205
206
207
208
209
210
211
        // Scatter master data using communication scheme

        const List<Pstream::commsStruct>& comms =
        (
            (Pstream::nProcs() < Pstream::nProcsSimpleSum)
          ? Pstream::linearCommunication()
          : Pstream::treeCommunication()
        );

212
213
        // Master reads headerclassname from file. Make sure this gets
        // transfered as well as contents.
mattijs's avatar
mattijs committed
214
215
216
217
218
219
220
        Pstream::scatter
        (
            comms,
            const_cast<word&>(headerClassName()),
            Pstream::msgType()
        );
        Pstream::scatter(comms, note(), Pstream::msgType());
221

222
223
224
225
226
227

        // Get my communication order
        const Pstream::commsStruct& myComm = comms[Pstream::myProcNo()];

        // Reveive from up
        if (myComm.above() != -1)
228
229
230
231
232
        {
            if (IFstream::debug)
            {
                Pout<< "regIOobject::read() : "
                    << "reading object " << name()
233
                    << " from processor " << myComm.above()
234
235
                    << endl;
            }
236
237
238
239
240
241
242
243

            // Note: use ASCII for now - binary IO of dictionaries is
            // not currently supported
            IPstream fromAbove
            (
                Pstream::scheduled,
                myComm.above(),
                0,
mattijs's avatar
mattijs committed
244
                Pstream::msgType(),
245
246
247
248
249
250
251
252
253
                IOstream::ASCII
            );
            ok = readData(fromAbove);
        }

        // Send to my downstairs neighbours
        forAll(myComm.below(), belowI)
        {
            OPstream toBelow
254
255
            (
                Pstream::scheduled,
256
                myComm.below()[belowI],
257
                0,
258
                Pstream::msgType(),
259
260
                IOstream::ASCII
            );
261
            writeData(toBelow);
262
263
        }
    }
264
265
266
267
    return ok;
}


Mark Olesen's avatar
Mark Olesen committed
268
bool Foam::regIOobject::modified() const
269
{
270
271
272
273
274
275
276
277
    if (watchIndex_ != -1)
    {
        return time().getState(watchIndex_) != fileMonitor::UNMODIFIED;
    }
    else
    {
        return false;
    }
278
279
280
}


Mark Olesen's avatar
Mark Olesen committed
281
bool Foam::regIOobject::readIfModified()
282
{
283
    if (watchIndex_ != -1)
284
    {
285
        if (modified())
286
        {
287
            const fileName& fName = time().getFile(watchIndex_);
288
            Info<< "regIOobject::readIfModified() : " << nl
289
290
                << "    Re-reading object " << name()
                << " from file " << fName << endl;
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
            return read();
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}


// ************************************************************************* //