regIOobjectRead.C 7.55 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 1991-2010 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
102
    // Mark as uptodate if read succesfully
    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
176
177
178
179
    // Note: cannot do anything in readStream itself since this is used by
    // e.g. GeometricField.

    bool masterOnly = 
        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
        ok = readData(readStream(type()));
        close();
    }

193
    if (masterOnly && Pstream::parRun())
194
    {
195
196
197
198
199
200
201
202
203
        // Scatter master data using communication scheme

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

204
205
        // Master reads headerclassname from file. Make sure this gets
        // transfered as well as contents.
206
207
        Pstream::scatter(comms, const_cast<word&>(headerClassName()));
        Pstream::scatter(comms, note());
208

209
210
211
212
213
214

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

        // Reveive from up
        if (myComm.above() != -1)
215
216
217
218
219
        {
            if (IFstream::debug)
            {
                Pout<< "regIOobject::read() : "
                    << "reading object " << name()
220
                    << " from processor " << myComm.above()
221
222
                    << endl;
            }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

            // 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
240
241
            (
                Pstream::scheduled,
242
                myComm.below()[belowI],
243
                0,
244
                Pstream::msgType(),
245
246
                IOstream::ASCII
            );
247
            writeData(toBelow);
248
249
        }
    }
250
251
252
253
    return ok;
}


Mark Olesen's avatar
Mark Olesen committed
254
bool Foam::regIOobject::modified() const
255
{
256
257
258
259
260
261
262
263
    if (watchIndex_ != -1)
    {
        return time().getState(watchIndex_) != fileMonitor::UNMODIFIED;
    }
    else
    {
        return false;
    }
264
265
266
}


Mark Olesen's avatar
Mark Olesen committed
267
bool Foam::regIOobject::readIfModified()
268
{
269
    if (watchIndex_ != -1)
270
    {
271
        if (modified())
272
        {
273
            const fileName& fName = time().getFile(watchIndex_);
274
            Info<< "regIOobject::readIfModified() : " << nl
275
276
                << "    Re-reading object " << name()
                << " from file " << fName << endl;
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
            return read();
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}


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