Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / db / Time / foamTimeIO.C
blob3e8370a3353c1ec54ac946d1664968acade6fff0
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "objectRegistry.H"
27 #include "foamTime.H"
28 #include "PstreamReduceOps.H"
30 #include "profiling.H"
32 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
34 void Foam::Time::readDict()
36     if (debug)
37     Info << "Time::readDict(): reading " << controlDict_.name() << endl;
39     if (!deltaTchanged_)
40     {
41         deltaT_ = readScalar(controlDict_.lookup("deltaT"));
42     }
44     if (controlDict_.found("writeControl"))
45     {
46         word wcName(controlDict_.lookup("writeControl"));
48         if (writeControlNames_.found(wcName))
49         {
50             writeControl_ = writeControlNames_[wcName];
51         }
52         else
53         {
54             WarningIn("void Time::readDict()")
55                 << wcName << " not found in enumeration "
56                 << writeControlNames_.toc() << nl
57                 << " setting writeControl to runTime" << endl;
59             writeControl_ = wcTimeStep;
60         }
62     }
64     scalar oldWriteInterval = writeInterval_;
65     if (controlDict_.readIfPresent("writeInterval", writeInterval_))
66     {
67         if (writeControl_ == wcTimeStep && label(writeInterval_) < 1)
68         {
69             WarningIn("Time::readDict()")
70                 << "writeInterval < 1 for writeControl timeStep.  "
71                 << "Re-setting to 1."
72                 << endl;
74             writeInterval_ = 1;
75         }
76     }
77     else
78     {
79         controlDict_.lookup("writeFrequency") >> writeInterval_;
80     }
82     if (oldWriteInterval != writeInterval_)
83     {
84         switch (writeControl_)
85         {
86             case wcRunTime:
87             case wcAdjustableRunTime:
88                 // Recalculate outputTimeIndex_ to be in units of current
89                 // writeInterval.
90                 outputTimeIndex_ = label
91                 (
92                     outputTimeIndex_
93                   * oldWriteInterval
94                   / writeInterval_
95                 );
96             break;
98             default:
99             break;
100         }
101     }
103     if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
104     {
105         if (purgeWrite_ < 0)
106         {
107             WarningIn("Time::readDict()")
108                 << "invalid value for purgeWrite " << purgeWrite_
109                 << ", should be >= 0, setting to 0"
110                 << endl;
112             purgeWrite_ = 0;
113         }
114     }
116     if (controlDict_.found("timeFormat"))
117     {
118         word formatName(controlDict_.lookup("timeFormat"));
120         if (formatName == "general")
121         {
122             format_ = general;
123         }
124         else if (formatName == "fixed")
125         {
126             format_ = fixed;
127         }
128         else if (formatName == "scientific")
129         {
130             format_ = scientific;
131         }
132         else
133         {
134             WarningIn("void Time::readDict()")
135                 << "unsupported time format " << formatName
136                 << endl;
137         }
138     }
140     controlDict_.readIfPresent("timePrecision", precision_);
142     // stopAt at 'endTime' or a specified value
143     // if nothing is specified, the endTime is zero
144     if (controlDict_.found("stopAt"))
145     {
146         word saName(controlDict_.lookup("stopAt"));
148         if (stopAtControlNames_.found(saName))
149         {
150             stopAt_ = stopAtControlNames_[saName];
151         }
152         else
153         {
154             WarningIn("void Time::readDict()")
155                 << saName << " not found in enumeration "
156                 << stopAtControlNames_.toc() << nl
157                 << "Setting to writeNow" << endl;
159             stopAt_ = saWriteNow;
161             // Set output time
162             outputTime_ = true;
163         }
165         if (stopAt_ == saEndTime)
166         {
167             controlDict_.lookup("endTime") >> endTime_;
168         }
169         else
170         {
171             endTime_ = GREAT;
172         }
173     }
174     else if (!controlDict_.readIfPresent("endTime", endTime_))
175     {
176         endTime_ = 0;
177     }
179     dimensionedScalar::name() = timeName(value());
181     if (controlDict_.found("writeVersion"))
182     {
183         writeVersion_ = IOstream::versionNumber
184         (
185             controlDict_.lookup("writeVersion")
186         );
187     }
189     if (controlDict_.found("writeFormat"))
190     {
191         writeFormat_ = IOstream::formatEnum
192         (
193             controlDict_.lookup("writeFormat")
194         );
195     }
197     if (controlDict_.found("writePrecision"))
198     {
199         IOstream::defaultPrecision
200         (
201             readUint(controlDict_.lookup("writePrecision"))
202         );
204         Sout.precision(IOstream::defaultPrecision());
205         Serr.precision(IOstream::defaultPrecision());
207         Pout.precision(IOstream::defaultPrecision());
208         Perr.precision(IOstream::defaultPrecision());
209     }
211     if (controlDict_.found("writeCompression"))
212     {
213         writeCompression_ = IOstream::compressionEnum
214         (
215             controlDict_.lookup("writeCompression")
216         );
217     }
219     controlDict_.readIfPresent("graphFormat", graphFormat_);
220     controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
224 bool Foam::Time::read()
226     if (controlDict_.regIOobject::read())
227     {
228         readDict();
230         return true;
231     }
232     else
233     {
234         return false;
235     }
239 void Foam::Time::readModifiedObjects()
241     if (runTimeModifiable_)
242     {
243         // For parallel runs check if any object's file has been modified
244         // and only call readIfModified on each object if this is the case
245         // to avoid unnecessary reductions in readIfModified for each object
247         bool anyModified = true;
249         if (Pstream::parRun())
250         {
251             anyModified = controlDict_.modified()
252                 || objectRegistry::modified();
254             bool anyModifiedOnThisProc = anyModified;
255             reduce(anyModified, andOp<bool>());
257             if (anyModifiedOnThisProc && !anyModified)
258             {
259                 WarningIn("Time::readModifiedObjects()")
260                     << "Delaying reading objects due to inconsistent "
261                        "file time-stamps between processors"
262                     << endl;
263             }
264         }
266         if (anyModified)
267         {
268             if (controlDict_.readIfModified())
269             {
270                 readDict();
271                 functionObjects_.read();
272             }
274             objectRegistry::readModifiedObjects();
275         }
276     }
280 bool Foam::Time::writeObject
282     IOstream::streamFormat fmt,
283     IOstream::versionNumber ver,
284     IOstream::compressionType cmp
285 ) const
287     addProfile2(getCalled,"Foam::Time::writeObject");
289     if (outputTime())
290     {
291         addProfile2(actualOutput,"Foam::Time::writeObject - outputTime");
293         IOdictionary timeDict
294         (
295             IOobject
296             (
297                 "time",
298                 timeName(),
299                 "uniform",
300                 *this,
301                 IOobject::NO_READ,
302                 IOobject::NO_WRITE,
303                 false
304             )
305         );
307         timeDict.add("index", timeIndex_);
308         timeDict.add("deltaT", deltaT_);
309         timeDict.add("deltaT0", deltaT0_);
311         timeDict.regIOobject::writeObject(fmt, ver, cmp);
312         bool writeOK = objectRegistry::writeObject(fmt, ver, cmp);
314         if (writeOK && purgeWrite_)
315         {
316             previousOutputTimes_.push(timeName());
318             while (previousOutputTimes_.size() > purgeWrite_)
319             {
320                 rmDir(objectRegistry::path(previousOutputTimes_.pop()));
321             }
322         }
324         return writeOK;
325     }
326     else
327     {
328         return false;
329     }
333 bool Foam::Time::writeNow()
335     outputTime_ = true;
336     return write();
340 bool Foam::Time::writeAndEnd()
342     stopAt_  = saWriteNow;
343     endTime_ = value();
345     return writeNow();
349 // ************************************************************************* //