Transferred copyright to the OpenFOAM Foundation
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / Time / TimeIO.C
blob905e508a6aa4d989d5e6764ba698e4ff43d13113
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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 "Time.H"
27 #include "Pstream.H"
29 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
31 void Foam::Time::readDict()
33     if (!deltaTchanged_)
34     {
35         deltaT_ = readScalar(controlDict_.lookup("deltaT"));
36     }
38     if (controlDict_.found("writeControl"))
39     {
40         writeControl_ = writeControlNames_.read
41         (
42             controlDict_.lookup("writeControl")
43         );
44     }
46     scalar oldWriteInterval = writeInterval_;
47     if (controlDict_.readIfPresent("writeInterval", writeInterval_))
48     {
49         if (writeControl_ == wcTimeStep && label(writeInterval_) < 1)
50         {
51             FatalIOErrorIn("Time::readDict()", controlDict_)
52                 << "writeInterval < 1 for writeControl timeStep"
53                 << exit(FatalIOError);
54         }
55     }
56     else
57     {
58         controlDict_.lookup("writeFrequency") >> writeInterval_;
59     }
61     if (oldWriteInterval != writeInterval_)
62     {
63         switch (writeControl_)
64         {
65             case wcRunTime:
66             case wcAdjustableRunTime:
67                 // Recalculate outputTimeIndex_ to be in units of current
68                 // writeInterval.
69                 outputTimeIndex_ = label
70                 (
71                     outputTimeIndex_
72                   * oldWriteInterval
73                   / writeInterval_
74                 );
75             break;
77             default:
78             break;
79         }
80     }
82     if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
83     {
84         if (purgeWrite_ < 0)
85         {
86             WarningIn("Time::readDict()")
87                 << "invalid value for purgeWrite " << purgeWrite_
88                 << ", should be >= 0, setting to 0"
89                 << endl;
91             purgeWrite_ = 0;
92         }
93     }
95     if (controlDict_.found("timeFormat"))
96     {
97         const word formatName(controlDict_.lookup("timeFormat"));
99         if (formatName == "general")
100         {
101             format_ = general;
102         }
103         else if (formatName == "fixed")
104         {
105             format_ = fixed;
106         }
107         else if (formatName == "scientific")
108         {
109             format_ = scientific;
110         }
111         else
112         {
113             WarningIn("Time::readDict()")
114                 << "unsupported time format " << formatName
115                 << endl;
116         }
117     }
119     controlDict_.readIfPresent("timePrecision", precision_);
121     // stopAt at 'endTime' or a specified value
122     // if nothing is specified, the endTime is zero
123     if (controlDict_.found("stopAt"))
124     {
125         stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
127         if (stopAt_ == saEndTime)
128         {
129             controlDict_.lookup("endTime") >> endTime_;
130         }
131         else
132         {
133             endTime_ = GREAT;
134         }
135     }
136     else if (!controlDict_.readIfPresent("endTime", endTime_))
137     {
138         endTime_ = 0;
139     }
141     dimensionedScalar::name() = timeName(value());
143     if (controlDict_.found("writeVersion"))
144     {
145         writeVersion_ = IOstream::versionNumber
146         (
147             controlDict_.lookup("writeVersion")
148         );
149     }
151     if (controlDict_.found("writeFormat"))
152     {
153         writeFormat_ = IOstream::formatEnum
154         (
155             controlDict_.lookup("writeFormat")
156         );
157     }
159     if (controlDict_.found("writePrecision"))
160     {
161         IOstream::defaultPrecision
162         (
163             readUint(controlDict_.lookup("writePrecision"))
164         );
166         Sout.precision(IOstream::defaultPrecision());
167         Serr.precision(IOstream::defaultPrecision());
169         Pout.precision(IOstream::defaultPrecision());
170         Perr.precision(IOstream::defaultPrecision());
172         FatalError().precision(IOstream::defaultPrecision());
173         FatalIOError.error::operator()().precision
174         (
175             IOstream::defaultPrecision()
176         );
177     }
179     if (controlDict_.found("writeCompression"))
180     {
181         writeCompression_ = IOstream::compressionEnum
182         (
183             controlDict_.lookup("writeCompression")
184         );
185     }
187     controlDict_.readIfPresent("graphFormat", graphFormat_);
188     controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
190     if (!runTimeModifiable_ && controlDict_.watchIndex() != -1)
191     {
192         removeWatch(controlDict_.watchIndex());
193         controlDict_.watchIndex() = -1;
194     }
198 bool Foam::Time::read()
200     if (controlDict_.regIOobject::read())
201     {
202         readDict();
203         return true;
204     }
205     else
206     {
207         return false;
208     }
212 void Foam::Time::readModifiedObjects()
214     if (runTimeModifiable_)
215     {
216         // Get state of all monitored objects (=registered objects with a
217         // valid filePath).
218         // Note: requires same ordering in objectRegistries on different
219         // processors!
220         monitorPtr_().updateStates
221         (
222             (
223                 regIOobject::fileModificationChecking == inotifyMaster
224              || regIOobject::fileModificationChecking == timeStampMaster
225             ),
226             Pstream::parRun()
227         );
229         // Time handling is special since controlDict_ is the one dictionary
230         // that is not registered to any database.
232         if (controlDict_.readIfModified())
233         {
234            readDict();
235            functionObjects_.read();
236         }
238         bool registryModified = objectRegistry::modified();
240         if (registryModified)
241         {
242             objectRegistry::readModifiedObjects();
243         }
244     }
248 bool Foam::Time::writeObject
250     IOstream::streamFormat fmt,
251     IOstream::versionNumber ver,
252     IOstream::compressionType cmp
253 ) const
255     if (outputTime())
256     {
257         IOdictionary timeDict
258         (
259             IOobject
260             (
261                 "time",
262                 timeName(),
263                 "uniform",
264                 *this,
265                 IOobject::NO_READ,
266                 IOobject::NO_WRITE,
267                 false
268             )
269         );
271         timeDict.add("index", timeIndex_);
272         timeDict.add("deltaT", deltaT_);
273         timeDict.add("deltaT0", deltaT0_);
275         timeDict.regIOobject::writeObject(fmt, ver, cmp);
276         bool writeOK = objectRegistry::writeObject(fmt, ver, cmp);
278         if (writeOK && purgeWrite_)
279         {
280             previousOutputTimes_.push(timeName());
282             while (previousOutputTimes_.size() > purgeWrite_)
283             {
284                 rmDir(objectRegistry::path(previousOutputTimes_.pop()));
285             }
286         }
288         return writeOK;
289     }
290     else
291     {
292         return false;
293     }
297 bool Foam::Time::writeNow()
299     outputTime_ = true;
300     return write();
304 bool Foam::Time::writeAndEnd()
306     stopAt_  = saWriteNow;
307     endTime_ = value();
309     return writeNow();
313 // ************************************************************************* //