1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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/>.
28 Class to control time during OpenFOAM simulations that is also the
29 top-level objectRegistry.
36 \*---------------------------------------------------------------------------*/
41 #include "TimePaths.H"
42 #include "objectRegistry.H"
43 #include "IOdictionary.H"
44 #include "FIFOStack.H"
47 #include "TimeState.H"
49 #include "instantList.H"
50 #include "NamedEnum.H"
52 #include "dlLibraryTable.H"
53 #include "functionObjectList.H"
54 #include "fileMonitor.H"
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 // Forward declaration of classes
63 /*---------------------------------------------------------------------------*\
64 Class Time Declaration
65 \*---------------------------------------------------------------------------*/
72 public objectRegistry,
77 //- file-change monitor for all registered files
78 mutable autoPtr<fileMonitor> monitorPtr_;
80 //- Any loaded dynamic libraries. Make sure to construct before
81 // reading controlDict.
85 IOdictionary controlDict_;
89 //- Write control options
99 //- Stop-run control options
102 saEndTime, /*!< stop when Time reaches the prescribed endTime */
103 saNoWriteNow, /*!< set endTime to stop immediately w/o writing */
104 saWriteNow, /*!< set endTime to stop immediately w/ writing */
105 saNextWrite /*!< stop the next time data are written */
108 //- Supported time directory name formats
112 fixed = ios_base::fixed,
113 scientific = ios_base::scientific
121 label startTimeIndex_;
123 mutable scalar endTime_;
125 static const NamedEnum<stopAtControls, 4> stopAtControlNames_;
126 mutable stopAtControls stopAt_;
128 static const NamedEnum<writeControls, 5> writeControlNames_;
129 writeControls writeControl_;
131 scalar writeInterval_;
134 mutable FIFOStack<word> previousOutputTimes_;
136 //- Is the time currently being sub-cycled?
139 //- If time is being sub-cycled this is the previous TimeState
140 autoPtr<TimeState> prevTimeState_;
142 //- Time directory name format
143 static fmtflags format_;
145 //- Time directory name precision
146 static int precision_;
148 //- Adjust the time step so that writing occurs at the specified time
151 //- Set the controls from the current controlDict
154 //- Read the control dictionary and set the write controls etc.
155 virtual void readDict();
160 //- Default write option
161 IOstream::streamFormat writeFormat_;
163 //- Default output file format version number
164 IOstream::versionNumber writeVersion_;
166 //- Default output compression
167 IOstream::compressionType writeCompression_;
169 //- Default graph format
172 //- Is runtime modification of dictionaries allowed?
173 Switch runTimeModifiable_;
175 //- Function objects executed at start and on ++, +=
176 mutable functionObjectList functionObjects_;
183 //- The default control dictionary name (normally "controlDict")
184 static word controlDictName;
189 //- Construct given name of dictionary to read and argument list
194 const word& systemName = "system",
195 const word& constantName = "constant"
198 //- Construct given name of dictionary to read, rootPath and casePath
202 const fileName& rootPath,
203 const fileName& caseName,
204 const word& systemName = "system",
205 const word& constantName = "constant",
206 const bool enableFunctionObjects = true
209 //- Construct given dictionary, rootPath and casePath
212 const dictionary& dict,
213 const fileName& rootPath,
214 const fileName& caseName,
215 const word& systemName = "system",
216 const word& constantName = "constant",
217 const bool enableFunctionObjects = true
220 //- Construct given endTime, rootPath and casePath
223 const fileName& rootPath,
224 const fileName& caseName,
225 const word& systemName = "system",
226 const word& constantName = "constant",
227 const bool enableFunctionObjects = true
237 // Database functions
240 const fileName& rootPath() const
242 return TimePaths::rootPath();
246 const fileName& caseName() const
248 return TimePaths::caseName();
252 fileName path() const
254 return rootPath()/caseName();
257 const dictionary& controlDict() const
262 virtual const fileName& dbDir() const
264 return fileName::null;
267 //- Return current time path
268 fileName timePath() const
270 return path()/timeName();
273 //- Default write format
274 IOstream::streamFormat writeFormat() const
279 //- Default write version number
280 IOstream::versionNumber writeVersion() const
282 return writeVersion_;
285 //- Default write compression
286 IOstream::compressionType writeCompression() const
288 return writeCompression_;
291 //- Default graph format
292 const word& graphFormat() const
297 //- Supports re-reading
298 const Switch& runTimeModifiable() const
300 return runTimeModifiable_;
303 //- Read control dictionary, update controls and time
306 // Automatic rereading
308 //- Read the objects that have been modified
309 void readModifiedObjects();
311 //- Add watching of a file. Returns handle
312 label addWatch(const fileName&) const;
314 //- Remove watch on a file (using handle)
315 bool removeWatch(const label) const;
317 //- Get name of file being watched (using handle)
318 const fileName& getFile(const label) const;
320 //- Get current state of file (using handle)
321 fileMonitor::fileState getState(const label) const;
323 //- Set current state of file (using handle) to unmodified
324 void setUnmodified(const label) const;
327 //- Return the location of "dir" containing the file "name".
328 // (eg, used in reading mesh data)
329 // If name is null, search for the directory "dir" only.
330 // Does not search beyond stopInstance (if set) or constant.
334 const word& name = word::null,
335 const IOobject::readOption rOpt = IOobject::MUST_READ,
336 const word& stopInstance = word::null
339 //- Search the case for valid time directories
340 instantList times() const;
342 //- Search the case for the time directory path
343 // corresponding to the given instance
344 word findInstancePath(const instant&) const;
346 //- Search the case for the time closest to the given time
347 instant findClosestTime(const scalar) const;
349 //- Search instantList for the time index closest to the given time
350 static label findClosestTimeIndex(const instantList&, const scalar);
352 //- Write using given format, version and compression
353 virtual bool writeObject
355 IOstream::streamFormat,
356 IOstream::versionNumber,
357 IOstream::compressionType
360 //- Write the objects now and continue the run
363 //- Write the objects now and end the run
369 //- Return time name of given scalar time
370 static word timeName(const scalar);
372 //- Return current time name
373 virtual word timeName() const;
375 //- Search a given directory for valid time directories
376 static instantList findTimes(const fileName&);
378 //- Return start time index
379 virtual label startTimeIndex() const;
381 //- Return start time
382 virtual dimensionedScalar startTime() const;
385 virtual dimensionedScalar endTime() const;
387 //- Return the list of function objects
388 const functionObjectList& functionObjects() const
390 return functionObjects_;
393 //- External access to the loaded libraries
394 dlLibraryTable& libs()
399 //- Return true if time currently being sub-cycled, otherwise false
400 bool subCycling() const
405 //- Return previous TimeState if time is being sub-cycled
406 const TimeState& prevTimeState() const
408 return prevTimeState_();
414 //- Return true if run should continue,
415 // also invokes the functionObjectList::end() method
416 // when the time goes out of range
418 // For correct behaviour, the following style of time-loop
421 // while (runTime.run())
428 virtual bool run() const;
430 //- Return true if run should continue and if so increment time
431 // also invokes the functionObjectList::end() method
432 // when the time goes out of range
434 // For correct behaviour, the following style of time-loop
437 // while (runTime.loop())
445 //- Return true if end of run,
446 // does not invoke any functionObject methods
448 // The rounding heuristics near endTime mean that
449 // \code run() \endcode and \code !end() \endcode may
450 // not yield the same result
451 virtual bool end() const;
456 //- Adjust the current stopAtControl. Note that this value
457 // only persists until the next time the dictionary is read.
458 // Return true if the stopAtControl changed.
459 virtual bool stopAt(const stopAtControls) const;
461 //- Reset the time and time-index to those of the given time
462 virtual void setTime(const Time&);
464 //- Reset the time and time-index
465 virtual void setTime(const instant&, const label newIndex);
467 //- Reset the time and time-index
470 const dimensionedScalar&,
474 //- Reset the time and time-index
475 virtual void setTime(const scalar, const label newIndex);
478 virtual void setEndTime(const dimensionedScalar&);
481 virtual void setEndTime(const scalar);
484 virtual void setDeltaT(const dimensionedScalar&);
487 virtual void setDeltaT(const scalar);
489 //- Set time to sub-cycle for the given number of steps
490 virtual TimeState subCycle(const label nSubCycles);
492 //- Reset time after sub-cycling back to previous TimeState
493 virtual void endSubCycle();
495 //- Return non-const access to the list of function objects
496 functionObjectList& functionObjects()
498 return functionObjects_;
504 //- Set deltaT to that specified and increment time via operator++()
505 virtual Time& operator+=(const dimensionedScalar&);
507 //- Set deltaT to that specified and increment time via operator++()
508 virtual Time& operator+=(const scalar);
510 //- Prefix increment,
511 // also invokes the functionObjectList::start() or
512 // functionObjectList::execute() method, depending on the time-index
513 virtual Time& operator++();
515 //- Postfix increment, this is identical to the prefix increment
516 virtual Time& operator++(int);
520 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
522 } // End namespace Foam
524 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
528 // ************************************************************************* //