OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

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