Time.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 Class
25  Foam::Time
26 
27 Description
28  Class to control time during OpenFOAM simulations that is also the
29  top-level objectRegistry.
30 
31 SourceFiles
32  Time.C
33  TimeIO.C
34  findInstance.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Time_H
39 #define Time_H
40 
41 #include "TimePaths.H"
42 #include "objectRegistry.H"
43 #include "IOdictionary.H"
44 #include "FIFOStack.H"
45 #include "clock.H"
46 #include "cpuTime.H"
47 #include "TimeState.H"
48 #include "userTime.H"
49 #include "Switch.H"
50 #include "instantList.H"
51 #include "NamedEnum.H"
52 #include "typeInfo.H"
53 #include "dlLibraryTable.H"
54 #include "functionObjectList.H"
55 #include "sigWriteNow.H"
56 #include "sigStopAtWriteNow.H"
57 
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
59 
60 namespace Foam
61 {
62 
63 // Forward declaration of classes
64 class argList;
65 
66 /*---------------------------------------------------------------------------*\
67  Class Time Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 class Time
71 :
72  public clock,
73  public cpuTime,
74  public TimePaths,
75  public objectRegistry,
76  public TimeState
77 {
78  // Private Data
79 
80  //- Is runtime modification of dictionaries allowed?
81  Switch runTimeModifiable_;
82 
83  //- The controlDict
84  IOdictionary controlDict_;
85 
86 
87 public:
88 
89  //- Write control options
90  enum class writeControl
91  {
92  timeStep,
93  runTime,
95  clockTime,
96  cpuTime
97  };
98 
99  //- Stop-run control options
100  enum class stopAtControl
101  {
102  endTime,
103  noWriteNow,
104  writeNow,
105  nextWrite
106  };
107 
108  //- Supported time directory name formats
109  enum class format
110  {
111  general = 0,
114  };
115 
116 
117 protected:
118 
119  // Protected data
120 
122  scalar startTime_;
123  mutable scalar endTime_;
124  scalar beginTime_;
125 
126  //- Optional user-time, defaults to realTime in s
128 
130  mutable stopAtControl stopAt_;
131 
134 
135  scalar writeInterval_;
136 
139 
140  // One-shot writing
141  bool writeOnce_;
142 
143  //- Is the time currently being sub-cycled?
144  bool subCycling_;
145 
146  //- If time is being sub-cycled this is the previous TimeState
148 
149  // Signal handlers for writing
150 
151  //- Enable one-shot writing upon signal
153 
154  //- Enable write and clean exit upon signal
156 
157 
158  //- Time directory name format
159  static format format_;
160 
161  //- Time directory name precision
162  static int precision_;
163 
164  //- Current time directory name precision adjusted as necessary
165  // to ensure time directory names are unique
166  // particularly if the time-step is reduced during the run
167  static int curPrecision_;
168 
169  //- Maximum time directory name precision
170  static const int maxPrecision_;
171 
172  //- Adjust the time step so that writing occurs at the specified time
173  void adjustDeltaT();
174 
175  //- Set the controls from the current controlDict
176  void setControls();
177 
178  //- Read the control dictionary and set the write controls etc.
179  virtual void readDict();
180 
181 
182 private:
183 
184  //- Default write option
185  IOstream::streamFormat writeFormat_;
186 
187  //- Default output file format version number
188  IOstream::versionNumber writeVersion_;
189 
190  //- Default output compression
191  IOstream::compressionType writeCompression_;
192 
193  //- Is temporary object cache enabled
194  mutable bool cacheTemporaryObjects_;
195 
196  //- Function objects executed at start and on ++, +=
197  mutable functionObjectList functionObjects_;
198 
199 
200 public:
201 
202  TypeName("time");
203 
204  //- The default control dictionary name (normally "controlDict")
205  static word controlDictName;
206 
207 
208  // Constructors
209 
210  //- Construct given name of dictionary to read and argument list
211  Time
212  (
213  const word& name,
214  const argList& args,
215  const bool enableFunctionObjects = true
216  );
217 
218  //- Construct given name of dictionary to read, rootPath and casePath
219  Time
220  (
221  const word& name,
222  const fileName& rootPath,
223  const fileName& caseName,
224  const bool enableFunctionObjects = true
225  );
226 
227  //- Construct given dictionary, rootPath and casePath
228  Time
229  (
230  const dictionary& dict,
231  const fileName& rootPath,
232  const fileName& caseName,
233  const bool enableFunctionObjects = true
234  );
235 
236  //- Construct given endTime, rootPath and casePath
237  Time
238  (
239  const fileName& rootPath,
240  const fileName& caseName,
241  const bool enableFunctionObjects = true
242  );
243 
244 
245  //- Destructor
246  virtual ~Time();
247 
248 
249  // Member Functions
250 
251  // Database functions
252 
253  //- Explicitly inherit rootPath from TimePaths to disambiguate from
254  // the corresponding method in objectRegistry
255  using TimePaths::rootPath;
256 
257  //- Explicitly inherit caseName from TimePaths to disambiguate from
258  // the corresponding method in objectRegistry
259  using TimePaths::caseName;
260 
261  //- Explicitly inherit path from TimePaths to disambiguate from
262  // the corresponding method in objectRegistry
263  using TimePaths::path;
264 
265  //- Return the control dict
266  const dictionary& controlDict() const
267  {
268  return controlDict_;
269  }
270 
271  //- Local directory path of this objectRegistry relative to time
272  virtual const fileName& dbDir() const
273  {
274  return fileName::null;
275  }
276 
277  //- Return current time path
278  fileName timePath() const
279  {
280  return path()/name();
281  }
282 
283  //- Default write format
285  {
286  return writeFormat_;
287  }
288 
289  //- Default write version number
291  {
292  return writeVersion_;
293  }
294 
295  //- Default write compression
297  {
298  return writeCompression_;
299  }
300 
301  //- Supports re-reading
302  const Switch& runTimeModifiable() const
303  {
304  return runTimeModifiable_;
305  }
306 
307  //- Read control dictionary, update controls and time
308  virtual bool read();
309 
310  //- Read the objects that have been modified
311  void readModifiedObjects();
312 
313  //- Return the location of "dir" containing the file "name".
314  // (eg, used in reading mesh data)
315  // If name is null, search for the directory "dir" only.
316  // Does not search beyond stopInstance (if set) or constant.
318  (
319  const fileName& dir,
320  const word& name = word::null,
322  const word& stopInstance = word::null
323  ) const;
324 
325  //- Search the case for valid time directories
326  instantList times() const;
327 
328  //- Search the case for the time directory path
329  // corresponding to the given instance
330  word findInstancePath(const fileName& path, const instant&) const;
331 
332  //- Search the case for the time directory path
333  // corresponding to the given instance
334  word findInstancePath(const instant&) const;
335 
336  //- Search the case for the time closest to the given time
337  instant findClosestTime(const scalar) const;
338 
339  //- Search instantList for the time index closest to the given time
341  (
342  const instantList&,
343  const scalar,
344  const word& constantName = "constant"
345  );
346 
347  //- Write time dictionary to the <time>/uniform directory
348  virtual bool writeTimeDict() const;
349 
350  //- Write using given format, version and compression
351  virtual bool writeObject
352  (
356  const bool write
357  ) const;
358 
359  //- Write the objects now (not at end of iteration) and continue
360  // the run
361  bool writeNow();
362 
363  //- Write the objects now (not at end of iteration) and end the run
364  bool writeAndEnd();
365 
366  //- Write the objects once (one shot) and continue the run
367  void writeOnce();
368 
369 
370  // Access
371 
373 
374  //- Return time name of given scalar time
375  // formatted with given precision
376  static word timeName
377  (
378  const scalar,
379  const int precision = curPrecision_
380  );
381 
382  //- Return current time name (for backwards-compatibility)
383  const word& timeName() const
384  {
385  return dimensionedScalar::name();
386  }
387 
388  //- Search a given directory for valid time directories
389  static instantList findTimes
390  (
391  const fileName&,
392  const word& constantName = "constant"
393  );
394 
395  //- Return start time index
396  virtual label startTimeIndex() const;
397 
398  //- Return begin time (initial start time)
399  virtual dimensionedScalar beginTime() const;
400 
401  //- Return start time
402  virtual dimensionedScalar startTime() const;
403 
404  //- Return end time
405  virtual dimensionedScalar endTime() const;
406 
407  //- Return the userTime
408  const userTimes::userTime& userTime() const;
409 
410  //- Return current user time value
411  scalar userTimeValue() const;
412 
413  //- Convert the user-time (e.g. CA deg) to real-time (s).
414  scalar userTimeToTime(const scalar tau) const;
415 
416  //- Convert the real-time (s) into user-time (e.g. CA deg)
417  scalar timeToUserTime(const scalar t) const;
418 
419  //- Return current user time name with units
420  word userTimeName() const;
421 
422  //- Return the list of function objects
423  const functionObjectList& functionObjects() const
424  {
425  return functionObjects_;
426  }
427 
428  //- Return true if the run is a restart, i.e. startTime != beginTime
429  bool restart() const
430  {
431  return startTime_ != beginTime_;
432  }
433 
434  //- Return true if time currently being sub-cycled, otherwise false
435  bool subCycling() const
436  {
437  return subCycling_;
438  }
439 
440  //- Return previous TimeState if time is being sub-cycled
441  const TimeState& prevTimeState() const
442  {
443  return prevTimeState_();
444  }
445 
446 
447  // Check
448 
449  //- Return true if run should continue without any side effects
450  virtual bool running() const;
451 
452  //- Return true if run should continue,
453  // also invokes the functionObjectList::end() method
454  // when the time goes out of range
455  // \note
456  // For correct behaviour, the following style of time-loop
457  // is recommended:
458  // \code
459  // while (runTime.run())
460  // {
461  // runTime++;
462  // solve;
463  // runTime.write();
464  // }
465  // \endcode
466  virtual bool run() const;
467 
468  //- Return true if run should continue and if so increment time
469  // also invokes the functionObjectList::end() method
470  // when the time goes out of range
471  // \note
472  // For correct behaviour, the following style of time-loop
473  // is recommended:
474  // \code
475  // while (runTime.loop())
476  // {
477  // solve;
478  // runTime.write();
479  // }
480  // \endcode
481  virtual bool loop();
482 
483  //- Return true if end of run,
484  // does not invoke any functionObject methods
485  // \note
486  // The rounding heuristics near endTime mean that
487  // \code run() \endcode and \code !end() \endcode may
488  // not yield the same result
489  virtual bool end() const;
490 
491 
492  // Edit
493 
494  //- Adjust the current stopAtControl. Note that this value
495  // only persists until the next time the dictionary is read.
496  // Return true if the stopAtControl changed.
497  virtual bool stopAt(const stopAtControl) const;
498 
499  //- Reset the time and time-index to those of the given time
500  virtual void setTime(const Time&);
501 
502  //- Reset the time and time-index
503  virtual void setTime(const instant&, const label newIndex);
504 
505  //- Reset the time and time-index
506  virtual void setTime
507  (
508  const dimensionedScalar&,
509  const label newIndex
510  );
511 
512  //- Reset the time and time-index
513  virtual void setTime(const scalar, const label newIndex);
514 
515  //- Reset end time
516  virtual void setEndTime(const dimensionedScalar&);
517 
518  //- Reset end time
519  virtual void setEndTime(const scalar);
520 
521  //- Reset time step
522  virtual void setDeltaT(const dimensionedScalar&);
523 
524  //- Reset time step
525  virtual void setDeltaT(const scalar);
526 
527  //- Reset time step without additional adjustment or modification
528  // by function objects
529  virtual void setDeltaTNoAdjust(const scalar);
530 
531  //- Reset the write interval
532  virtual void setWriteInterval(const scalar writeInterval);
533 
534  //- Set time to sub-cycle for the given number of steps
535  virtual TimeState subCycle(const label nSubCycles);
536 
537  //- Reset time after sub-cycling back to previous TimeState
538  virtual void endSubCycle();
539 
540  //- Return non-const access to the list of function objects
542  {
543  return functionObjects_;
544  }
545 
546 
547  // Member Operators
548 
549  //- Set deltaT to that specified and increment time via operator++()
550  virtual Time& operator+=(const dimensionedScalar&);
551 
552  //- Set deltaT to that specified and increment time via operator++()
553  virtual Time& operator+=(const scalar);
554 
555  //- Prefix increment,
556  // also invokes the functionObjectList::start() or
557  // functionObjectList::execute() method, depending on the time-index
558  virtual Time& operator++();
559 
560  //- Postfix increment, this is identical to the prefix increment
561  virtual Time& operator++(int);
562 };
563 
564 
565 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
566 
567 } // End namespace Foam
568 
569 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
570 
571 #endif
572 
573 // ************************************************************************* //
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:54
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
readOption
Enumeration defining the read options.
Definition: IOobject.H:117
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:194
A simple wrapper around bool so that it can be read as a word: true/false, on/off,...
Definition: Switch.H:61
A class for addressing time paths without using the Time class.
Definition: TimePaths.H:49
fileName path() const
Return path.
Definition: TimePaths.H:138
static const word constantName
Definition: TimePaths.H:62
const fileName & caseName() const
Return case name.
Definition: TimePaths.H:106
const fileName & rootPath() const
Return root path.
Definition: TimePaths.H:94
The time value with time-stepping information, user-defined remapping, etc.
Definition: TimeState.H:55
friend class Time
Declare friendship with the Time class.
Definition: TimeState.H:70
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
virtual bool run() const
Return true if run should continue,.
Definition: Time.C:855
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:32
static int precision_
Time directory name precision.
Definition: Time.H:161
scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: Time.C:830
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:128
instantList times() const
Search the case for valid time directories.
Definition: Time.C:640
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:939
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:440
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:262
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:357
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:790
const word & timeName() const
Return current time name (for backwards-compatibility)
Definition: Time.H:382
sigStopAtWriteNow sigStopAtWriteNow_
Enable write and clean exit upon signal.
Definition: Time.H:154
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:350
static instantList findTimes(const fileName &, const word &constantName="constant")
Search a given directory for valid time directories.
Definition: findTimes.C:36
static int curPrecision_
Current time directory name precision adjusted as necessary.
Definition: Time.H:166
void adjustDeltaT()
Adjust the time step so that writing occurs at the specified time.
Definition: Time.C:83
format
Supported time directory name formats.
Definition: Time.H:109
scalar userTimeToTime(const scalar tau) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: Time.C:824
fileName path() const
Explicitly inherit path from TimePaths to disambiguate from.
Definition: TimePaths.H:138
scalar writeInterval_
Definition: Time.H:134
TypeName("time")
stopAtControl stopAt_
Definition: Time.H:129
virtual dimensionedScalar beginTime() const
Return begin time (initial start time)
Definition: Time.C:779
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:131
FIFOStack< word > previousWriteTimes_
Definition: Time.H:137
scalar userTimeValue() const
Return current user time value.
Definition: Time.C:818
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:366
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:169
autoPtr< TimeState > prevTimeState_
If time is being sub-cycled this is the previous TimeState.
Definition: Time.H:146
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: Time.C:647
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:298
const Switch & runTimeModifiable() const
Supports re-reading.
Definition: Time.H:301
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:1035
virtual void setEndTime(const dimensionedScalar &)
Reset end time.
Definition: Time.C:999
IOstream::versionNumber writeVersion() const
Default write version number.
Definition: Time.H:289
word userTimeName() const
Return current user time name with units.
Definition: Time.C:836
label purgeWrite_
Definition: Time.H:136
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:434
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:283
writeControl writeControl_
Definition: Time.H:132
sigWriteNow sigWriteNow_
Enable one-shot writing upon signal.
Definition: Time.H:151
virtual bool running() const
Return true if run should continue without any side effects.
Definition: Time.C:849
void setControls()
Set the controls from the current controlDict.
Definition: Time.C:109
word findInstancePath(const fileName &path, const instant &) const
Search the case for the time directory path.
Definition: Time.C:677
virtual void setDeltaT(const dimensionedScalar &)
Reset time step.
Definition: Time.C:1011
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:204
scalar endTime_
Definition: Time.H:122
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to time.
Definition: Time.H:271
bool writeOnce_
Definition: Time.H:140
autoPtr< userTimes::userTime > userTime_
Optional user-time, defaults to realTime in s.
Definition: Time.H:126
const dictionary & controlDict() const
Return the control dict.
Definition: Time.H:265
stopAtControl
Stop-run control options.
Definition: Time.H:100
@ nextWrite
stop the next time data are written
@ writeNow
set endTime to stop immediately w/ writing
@ endTime
stop when Time reaches the prescribed endTime
@ noWriteNow
set endTime to stop immediately w/o writing
static format format_
Time directory name format.
Definition: Time.H:158
virtual bool stopAt(const stopAtControl) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:921
const userTimes::userTime & userTime() const
Return the userTime.
Definition: Time.C:812
virtual label startTimeIndex() const
Return start time index.
Definition: Time.C:773
label startTimeIndex_
Definition: Time.H:120
virtual void endSubCycle()
Reset time after sub-cycling back to previous TimeState.
Definition: Time.C:1077
virtual void setDeltaTNoAdjust(const scalar)
Reset time step without additional adjustment or modification.
Definition: Time.C:1028
IOstream::compressionType writeCompression() const
Default write compression.
Definition: Time.H:295
virtual Time & operator++()
Prefix increment,.
Definition: Time.C:1103
scalar startTime_
Definition: Time.H:121
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:226
fileName timePath() const
Return current time path.
Definition: Time.H:277
scalar beginTime_
Definition: Time.H:123
virtual TimeState subCycle(const label nSubCycles)
Set time to sub-cycle for the given number of steps.
Definition: Time.C:1063
writeControl
Write control options.
Definition: Time.H:90
virtual bool loop()
Return true if run should continue and if so increment time.
Definition: Time.C:902
bool subCycling_
Is the time currently being sub-cycled?
Definition: Time.H:143
bool restart() const
Return true if the run is a restart, i.e. startTime != beginTime.
Definition: Time.H:428
virtual dimensionedScalar endTime() const
Return end time.
Definition: Time.C:801
virtual ~Time()
Destructor.
Definition: Time.C:621
static label findClosestTimeIndex(const instantList &, const scalar, const word &constantName="constant")
Search instantList for the time index closest to the given time.
Definition: Time.C:748
virtual Time & operator+=(const dimensionedScalar &)
Set deltaT to that specified and increment time via operator++()
Definition: Time.C:1090
virtual bool end() const
Return true if end of run,.
Definition: Time.C:915
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:211
instant findClosestTime(const scalar) const
Search the case for the time closest to the given time.
Definition: Time.C:711
const fileName & caseName() const
Explicitly inherit caseName from TimePaths to disambiguate from.
Definition: TimePaths.H:106
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:422
const fileName & rootPath() const
Explicitly inherit rootPath from TimePaths to disambiguate from.
Definition: TimePaths.H:94
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Read access to the system clock with formatting.
Definition: clock.H:51
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:55
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
const word & name() const
Return const reference to name.
A class for handling file names.
Definition: fileName.H:82
static const fileName null
An empty fileName.
Definition: fileName.H:97
List of function objects with start(), execute() and end() functions that is called for each object.
An instant of time. Contains the time value and name.
Definition: instant.H:67
Registry of regIOobjects.
virtual bool write(const bool write=true) const
Write using setting from DB.
Signal handler to write and exit when the stopAtWriteNowSignal is sent.
Signal handler to write when the writeNowSignal is sent.
Definition: sigWriteNow.H:64
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
IOstream & scientific(IOstream &io)
Definition: IOstream.H:579
dictionary dict
Foam::argList args(argc, argv)