FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

TimeIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "Time.H"
00027 #include <OpenFOAM/PstreamReduceOps.H>
00028 
00029 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00030 
00031 void Foam::Time::readDict()
00032 {
00033     if (!deltaTchanged_)
00034     {
00035         deltaT_ = readScalar(controlDict_.lookup("deltaT"));
00036     }
00037 
00038     if (controlDict_.found("writeControl"))
00039     {
00040         writeControl_ = writeControlNames_.read
00041         (
00042             controlDict_.lookup("writeControl")
00043         );
00044     }
00045 
00046     scalar oldWriteInterval = writeInterval_;
00047     if (controlDict_.readIfPresent("writeInterval", writeInterval_))
00048     {
00049         if (writeControl_ == wcTimeStep && label(writeInterval_) < 1)
00050         {
00051             FatalIOErrorIn("Time::readDict()", controlDict_)
00052                 << "writeInterval < 1 for writeControl timeStep"
00053                 << exit(FatalIOError);
00054         }
00055     }
00056     else
00057     {
00058         controlDict_.lookup("writeFrequency") >> writeInterval_;
00059     }
00060 
00061     if (oldWriteInterval != writeInterval_)
00062     {
00063         switch (writeControl_)
00064         {
00065             case wcRunTime:
00066             case wcAdjustableRunTime:
00067                 // Recalculate outputTimeIndex_ to be in units of current
00068                 // writeInterval.
00069                 outputTimeIndex_ = label
00070                 (
00071                     outputTimeIndex_
00072                   * oldWriteInterval
00073                   / writeInterval_
00074                 );
00075             break;
00076 
00077             default:
00078             break;
00079         }
00080     }
00081 
00082     if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
00083     {
00084         if (purgeWrite_ < 0)
00085         {
00086             WarningIn("Time::readDict()")
00087                 << "invalid value for purgeWrite " << purgeWrite_
00088                 << ", should be >= 0, setting to 0"
00089                 << endl;
00090 
00091             purgeWrite_ = 0;
00092         }
00093     }
00094 
00095     if (controlDict_.found("timeFormat"))
00096     {
00097         word formatName(controlDict_.lookup("timeFormat"));
00098 
00099         if (formatName == "general")
00100         {
00101             format_ = general;
00102         }
00103         else if (formatName == "fixed")
00104         {
00105             format_ = fixed;
00106         }
00107         else if (formatName == "scientific")
00108         {
00109             format_ = scientific;
00110         }
00111         else
00112         {
00113             WarningIn("Time::readDict()")
00114                 << "unsupported time format " << formatName
00115                 << endl;
00116         }
00117     }
00118 
00119     controlDict_.readIfPresent("timePrecision", precision_);
00120 
00121     // stopAt at 'endTime' or a specified value
00122     // if nothing is specified, the endTime is zero
00123     if (controlDict_.found("stopAt"))
00124     {
00125         stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
00126 
00127         if (stopAt_ == saEndTime)
00128         {
00129             controlDict_.lookup("endTime") >> endTime_;
00130         }
00131         else
00132         {
00133             endTime_ = GREAT;
00134         }
00135     }
00136     else if (!controlDict_.readIfPresent("endTime", endTime_))
00137     {
00138         endTime_ = 0;
00139     }
00140 
00141     dimensionedScalar::name() = timeName(value());
00142 
00143     if (controlDict_.found("writeVersion"))
00144     {
00145         writeVersion_ = IOstream::versionNumber
00146         (
00147             controlDict_.lookup("writeVersion")
00148         );
00149     }
00150 
00151     if (controlDict_.found("writeFormat"))
00152     {
00153         writeFormat_ = IOstream::formatEnum
00154         (
00155             controlDict_.lookup("writeFormat")
00156         );
00157     }
00158 
00159     if (controlDict_.found("writePrecision"))
00160     {
00161         IOstream::defaultPrecision
00162         (
00163             readUint(controlDict_.lookup("writePrecision"))
00164         );
00165 
00166         Sout.precision(IOstream::defaultPrecision());
00167         Serr.precision(IOstream::defaultPrecision());
00168 
00169         Pout.precision(IOstream::defaultPrecision());
00170         Perr.precision(IOstream::defaultPrecision());
00171     }
00172 
00173     if (controlDict_.found("writeCompression"))
00174     {
00175         writeCompression_ = IOstream::compressionEnum
00176         (
00177             controlDict_.lookup("writeCompression")
00178         );
00179     }
00180 
00181     controlDict_.readIfPresent("graphFormat", graphFormat_);
00182     controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
00183 }
00184 
00185 
00186 bool Foam::Time::read()
00187 {
00188     if (controlDict_.regIOobject::read())
00189     {
00190         readDict();
00191         return true;
00192     }
00193     else
00194     {
00195         return false;
00196     }
00197 }
00198 
00199 
00200 void Foam::Time::readModifiedObjects()
00201 {
00202     if (runTimeModifiable_)
00203     {
00204         // For parallel runs check if any object's file has been modified
00205         // and only call readIfModified on each object if this is the case
00206         // to avoid unnecessary reductions in readIfModified for each object
00207 
00208         bool anyModified = true;
00209 
00210         if (Pstream::parRun())
00211         {
00212             anyModified = controlDict_.modified() || objectRegistry::modified();
00213             bool anyModifiedOnThisProc = anyModified;
00214             reduce(anyModified, andOp<bool>());
00215 
00216             if (anyModifiedOnThisProc && !anyModified)
00217             {
00218                 WarningIn("Time::readModifiedObjects()")
00219                     << "Delaying reading objects due to inconsistent "
00220                        "file time-stamps between processors"
00221                     << endl;
00222             }
00223         }
00224 
00225         if (anyModified)
00226         {
00227             if (controlDict_.readIfModified())
00228             {
00229                 readDict();
00230                 functionObjects_.read();
00231             }
00232 
00233             objectRegistry::readModifiedObjects();
00234         }
00235     }
00236 }
00237 
00238 
00239 bool Foam::Time::writeObject
00240 (
00241     IOstream::streamFormat fmt,
00242     IOstream::versionNumber ver,
00243     IOstream::compressionType cmp
00244 ) const
00245 {
00246     if (outputTime())
00247     {
00248         IOdictionary timeDict
00249         (
00250             IOobject
00251             (
00252                 "time",
00253                 timeName(),
00254                 "uniform",
00255                 *this,
00256                 IOobject::NO_READ,
00257                 IOobject::NO_WRITE,
00258                 false
00259             )
00260         );
00261 
00262         timeDict.add("index", timeIndex_);
00263         timeDict.add("deltaT", deltaT_);
00264         timeDict.add("deltaT0", deltaT0_);
00265 
00266         timeDict.regIOobject::writeObject(fmt, ver, cmp);
00267         bool writeOK = objectRegistry::writeObject(fmt, ver, cmp);
00268 
00269         if (writeOK && purgeWrite_)
00270         {
00271             previousOutputTimes_.push(timeName());
00272 
00273             while (previousOutputTimes_.size() > purgeWrite_)
00274             {
00275                 rmDir(objectRegistry::path(previousOutputTimes_.pop()));
00276             }
00277         }
00278 
00279         return writeOK;
00280     }
00281     else
00282     {
00283         return false;
00284     }
00285 }
00286 
00287 
00288 bool Foam::Time::writeNow()
00289 {
00290     outputTime_ = true;
00291     return write();
00292 }
00293 
00294 
00295 bool Foam::Time::writeAndEnd()
00296 {
00297     stopAt_  = saWriteNow;
00298     endTime_ = value();
00299 
00300     return writeNow();
00301 }
00302 
00303 
00304 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines