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

Time.H

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 Class
00025     Foam::Time
00026 
00027 Description
00028     Class to control time during OpenFOAM simulations that is also the
00029     top-level objectRegistry.
00030 
00031 SourceFiles
00032     Time.C
00033     TimeIO.C
00034     findInstance.C
00035 
00036 \*---------------------------------------------------------------------------*/
00037 
00038 #ifndef Time_H
00039 #define Time_H
00040 
00041 #include "TimePaths.H"
00042 #include <OpenFOAM/objectRegistry.H>
00043 #include <OpenFOAM/IOdictionary.H>
00044 #include <OpenFOAM/FIFOStack.H>
00045 #include <OpenFOAM/clock.H>
00046 #include <OSspecific/cpuTime.H>
00047 #include "TimeState.H"
00048 #include <OpenFOAM/Switch.H>
00049 #include <OpenFOAM/instantList.H>
00050 #include <OpenFOAM/NamedEnum.H>
00051 #include <OpenFOAM/typeInfo.H>
00052 #include <OpenFOAM/dlLibraryTable.H>
00053 #include <OpenFOAM/functionObjectList.H>
00054 
00055 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00056 
00057 namespace Foam
00058 {
00059 
00060 /*---------------------------------------------------------------------------*\
00061                              Class Time Declaration
00062 \*---------------------------------------------------------------------------*/
00063 
00064 class Time
00065 :
00066     public clock,
00067     public cpuTime,
00068     public TimePaths,
00069     public objectRegistry,
00070     public TimeState
00071 {
00072     // Private data
00073 
00074         //- The controlDict
00075         IOdictionary controlDict_;
00076 
00077 public:
00078 
00079         //- Write control options
00080         enum writeControls
00081         {
00082             wcTimeStep,
00083             wcRunTime,
00084             wcAdjustableRunTime,
00085             wcClockTime,
00086             wcCpuTime
00087         };
00088 
00089         //- Stop-run control options
00090         enum stopAtControls
00091         {
00092             saEndTime,
00093             saNoWriteNow,
00094             saWriteNow,
00095             saNextWrite
00096         };
00097 
00098         //- Suported time directory name formats
00099         enum fmtflags
00100         {
00101             general = 0,
00102             fixed = ios_base::fixed,
00103             scientific = ios_base::scientific
00104         };
00105 
00106 
00107 protected:
00108 
00109     // Protected data
00110 
00111         label  startTimeIndex_;
00112         scalar startTime_;
00113         scalar endTime_;
00114 
00115         static const NamedEnum<stopAtControls, 4> stopAtControlNames_;
00116         stopAtControls stopAt_;
00117 
00118         static const NamedEnum<writeControls, 5> writeControlNames_;
00119         writeControls writeControl_;
00120 
00121         scalar writeInterval_;
00122 
00123         label  purgeWrite_;
00124         mutable FIFOStack<word> previousOutputTimes_;
00125 
00126         //- Is the time currently being sub-cycled?
00127         bool subCycling_;
00128 
00129         //- If time is being sub-cycled this is the previous TimeState
00130         autoPtr<TimeState> prevTimeState_;
00131 
00132         //- Time directory name format
00133         static fmtflags format_;
00134 
00135         //- Time directory name precision
00136         static int precision_;
00137 
00138         //- Adjust the time step so that writing occurs at the specified time
00139         void adjustDeltaT();
00140 
00141         //- Set the controls from the current controlDict
00142         void setControls();
00143 
00144         //- Read the control dictionary and set the write controls etc.
00145         virtual void readDict();
00146 
00147 
00148 private:
00149 
00150         //- Default write option
00151         IOstream::streamFormat writeFormat_;
00152 
00153         //- Default output file format version number
00154         IOstream::versionNumber writeVersion_;
00155 
00156         //- Default output compression
00157         IOstream::compressionType writeCompression_;
00158 
00159         //- Default graph format
00160         word graphFormat_;
00161 
00162         //- Is runtime modification of dictionaries allowed?
00163         Switch runTimeModifiable_;
00164 
00165         //- Instantiate a dummy class to cause the reading of dynamic libraries
00166         dlLibraryTable::readDlLibrary readLibs_;
00167 
00168         //- Function objects executed at start and on ++, +=
00169         mutable functionObjectList functionObjects_;
00170 
00171 
00172 public:
00173 
00174     TypeName("time");
00175 
00176     //- The default control dictionary name (normally "controlDict")
00177     static word controlDictName;
00178 
00179 
00180     // Constructors
00181 
00182         //- Construct given name, rootPath and casePath
00183         Time
00184         (
00185             const word& name,
00186             const fileName& rootPath,
00187             const fileName& caseName,
00188             const word& systemName = "system",
00189             const word& constantName = "constant"
00190         );
00191 
00192         //- Construct given dictionary, rootPath and casePath
00193         Time
00194         (
00195             const dictionary& dict,
00196             const fileName& rootPath,
00197             const fileName& caseName,
00198             const word& systemName = "system",
00199             const word& constantName = "constant"
00200         );
00201 
00202         //- Construct given endTime, rootPath and casePath
00203         Time
00204         (
00205             const fileName& rootPath,
00206             const fileName& caseName,
00207             const word& systemName = "system",
00208             const word& constantName = "constant"
00209         );
00210 
00211 
00212     // Destructor
00213 
00214         //- Virtual destructor
00215         virtual ~Time();
00216 
00217 
00218     // Member functions
00219 
00220         // Database functions
00221 
00222             //- Return root path
00223             const fileName& rootPath() const
00224             {
00225                 return TimePaths::rootPath();
00226             }
00227 
00228             //- Return case name
00229             const fileName& caseName() const
00230             {
00231                 return TimePaths::caseName();
00232             }
00233 
00234             //- Return path
00235             fileName path() const
00236             {
00237                 return rootPath()/caseName();
00238             }
00239 
00240             const dictionary& controlDict() const
00241             {
00242                 return controlDict_;
00243             }
00244 
00245             virtual const fileName& dbDir() const
00246             {
00247                 return fileName::null;
00248             }
00249 
00250             //- Return current time path
00251             fileName timePath() const
00252             {
00253                 return path()/timeName();
00254             }
00255 
00256             //- Default write format
00257             IOstream::streamFormat writeFormat() const
00258             {
00259                 return writeFormat_;
00260             }
00261 
00262             //- Default write version number
00263             IOstream::versionNumber writeVersion() const
00264             {
00265                 return writeVersion_;
00266             }
00267 
00268             //- Default write compression
00269             IOstream::compressionType writeCompression() const
00270             {
00271                 return writeCompression_;
00272             }
00273 
00274             //- Default graph format
00275             const word& graphFormat() const
00276             {
00277                 return graphFormat_;
00278             }
00279 
00280             //- Read control dictionary, update controls and time
00281             virtual bool read();
00282 
00283             //- Read the objects that have been modified
00284             void readModifiedObjects();
00285 
00286             //- Return the location of "dir" containing the file "name".
00287             //  (eg, used in reading mesh data)
00288             //  If name is null, search for the directory "dir" only
00289             word findInstance
00290             (
00291                 const fileName& dir,
00292                 const word& name = word::null,
00293                 const IOobject::readOption rOpt = IOobject::MUST_READ,
00294                 const word& stopInstance = word::null
00295             ) const;
00296 
00297             //- Search the case for valid time directories
00298             instantList times() const;
00299 
00300             //- Search the case for the time directory path
00301             //  corresponding to the given instance
00302             word findInstancePath(const instant&) const;
00303 
00304             //- Search the case for the time closest to the given time
00305             instant findClosestTime(const scalar) const;
00306 
00307             //- Search instantList for the time index closest to the given time
00308             static label findClosestTimeIndex(const instantList&, const scalar);
00309 
00310             //- Write using given format, version and compression
00311             virtual bool writeObject
00312             (
00313                 IOstream::streamFormat,
00314                 IOstream::versionNumber,
00315                 IOstream::compressionType
00316             ) const;
00317 
00318             //- Write the objects now and continue the run
00319             bool writeNow();
00320 
00321             //- Write the objects now and end the run
00322             bool writeAndEnd();
00323 
00324 
00325         // Access
00326 
00327             //- Return time name of given scalar time
00328             static word timeName(const scalar);
00329 
00330             //- Return current time name
00331             virtual word timeName() const;
00332 
00333             //- Search a given directory for valid time directories
00334             static instantList findTimes(const fileName&);
00335 
00336             //- Return start time index
00337             virtual label startTimeIndex() const;
00338 
00339             //- Return start time
00340             virtual dimensionedScalar startTime() const;
00341 
00342             //- Return end time
00343             virtual dimensionedScalar endTime() const;
00344 
00345             //- Return the list of function objects
00346             const functionObjectList& functionObjects() const
00347             {
00348                 return functionObjects_;
00349             }
00350 
00351             //- Return true if time currently being sub-cycled, otherwise false
00352             bool subCycling() const
00353             {
00354                 return subCycling_;
00355             }
00356 
00357             //- Return previous TimeState if time is being sub-cycled
00358             const TimeState& prevTimeState() const
00359             {
00360                 return prevTimeState_();
00361             }
00362 
00363 
00364         // Check
00365 
00366             //- Return true if run should continue,
00367             //  also invokes the functionObjectList::end() method
00368             //  when the time goes out of range
00369             //  @note
00370             //  For correct behaviour, the following style of time-loop
00371             //  is recommended:
00372             //  @code
00373             //      while (runTime.run())
00374             //      {
00375             //          runTime++;
00376             //          solve;
00377             //          runTime.write();
00378             //      }
00379             //  @endcode
00380             virtual bool run() const;
00381 
00382             //- Return true if run should continue and if so increment time
00383             //  also invokes the functionObjectList::end() method
00384             //  when the time goes out of range
00385             //  @note
00386             //  For correct behaviour, the following style of time-loop
00387             //  is recommended:
00388             //  @code
00389             //      while (runTime.loop())
00390             //      {
00391             //          solve;
00392             //          runTime.write();
00393             //      }
00394             //  @endcode
00395             virtual bool loop();
00396 
00397             //- Return true if end of run,
00398             //  does not invoke any functionObject methods
00399             //  @note
00400             //      The rounding heuristics near endTime mean that
00401             //      @code run() @endcode and @code !end() @endcode may
00402             //      not yield the same result
00403             virtual bool end() const;
00404 
00405 
00406         // Edit
00407 
00408             //- Reset the time and time-index to those of the given time
00409             virtual void setTime(const Time&);
00410 
00411             //- Reset the time and time-index
00412             virtual void setTime(const instant&, const label newIndex);
00413 
00414             //- Reset the time and time-index
00415             virtual void setTime
00416             (
00417                 const dimensionedScalar&,
00418                 const label newIndex
00419             );
00420 
00421             //- Reset the time and time-index
00422             virtual void setTime(const scalar, const label newIndex);
00423 
00424             //- Reset end time
00425             virtual void setEndTime(const dimensionedScalar&);
00426 
00427             //- Reset end time
00428             virtual void setEndTime(const scalar);
00429 
00430             //- Reset time step
00431             virtual void setDeltaT(const dimensionedScalar&);
00432 
00433             //- Reset time step
00434             virtual void setDeltaT(const scalar);
00435 
00436             //- Set time to sub-cycle for the given number of steps
00437             virtual TimeState subCycle(const label nSubCycles);
00438 
00439             //- Reset time after sub-cycling back to previous TimeState
00440             virtual void endSubCycle();
00441 
00442             //- Return non-const access to the list of function objects
00443             functionObjectList& functionObjects()
00444             {
00445                 return functionObjects_;
00446             }
00447 
00448 
00449     // Member operators
00450 
00451         //- Set deltaT to that specified and increment time via operator++()
00452         virtual Time& operator+=(const dimensionedScalar&);
00453 
00454         //- Set deltaT to that specified and increment time via operator++()
00455         virtual Time& operator+=(const scalar);
00456 
00457         //- Prefix increment,
00458         //  also invokes the functionObjectList::start() or
00459         //  functionObjectList::execute() method, depending on the time-index
00460         virtual Time& operator++();
00461 
00462         //- Postfix increment, this is identical to the prefix increment
00463         virtual Time& operator++(int);
00464 };
00465 
00466 
00467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00468 
00469 } // End namespace Foam
00470 
00471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00472 
00473 #endif
00474 
00475 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines