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-2022 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
122  scalar startTime_;
123  mutable scalar endTime_;
124  scalar beginTime_;
125 
126  //- Optional user-time, defaults to realTime in s
130  mutable stopAtControl stopAt_;
135  scalar writeInterval_;
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  //- Default graph format
194  word graphFormat_;
195 
196  //- Is temporary object cache enabled
197  mutable bool cacheTemporaryObjects_;
198 
199  //- Function objects executed at start and on ++, +=
200  mutable functionObjectList functionObjects_;
201 
202 
203 public:
204 
205  TypeName("time");
206 
207  //- The default control dictionary name (normally "controlDict")
208  static word controlDictName;
209 
210 
211  // Constructors
212 
213  //- Construct given name of dictionary to read and argument list
214  Time
215  (
216  const word& name,
217  const argList& args,
218  const word& systemName = "system",
219  const word& constantName = "constant"
220  );
221 
222  //- Construct given name of dictionary to read, rootPath and casePath
223  Time
224  (
225  const word& name,
226  const fileName& rootPath,
227  const fileName& caseName,
228  const word& systemName = "system",
229  const word& constantName = "constant",
230  const bool enableFunctionObjects = true
231  );
232 
233  //- Construct given dictionary, rootPath and casePath
234  Time
235  (
236  const dictionary& dict,
237  const fileName& rootPath,
238  const fileName& caseName,
239  const word& systemName = "system",
240  const word& constantName = "constant",
241  const bool enableFunctionObjects = true
242  );
243 
244  //- Construct given endTime, rootPath and casePath
245  Time
246  (
247  const fileName& rootPath,
248  const fileName& caseName,
249  const word& systemName = "system",
250  const word& constantName = "constant",
251  const bool enableFunctionObjects = true
252  );
253 
254 
255  //- Destructor
256  virtual ~Time();
257 
258 
259  // Member Functions
260 
261  // Database functions
262 
263  //- Explicitly inherit rootPath from TimePaths to disambiguate from
264  // the corresponding method in objectRegistry
265  using TimePaths::rootPath;
266 
267  //- Explicitly inherit caseName from TimePaths to disambiguate from
268  // the corresponding method in objectRegistry
269  using TimePaths::caseName;
270 
271  //- Explicitly inherit path from TimePaths to disambiguate from
272  // the corresponding method in objectRegistry
273  using TimePaths::path;
274 
275  //- Return the control dict
276  const dictionary& controlDict() const
277  {
278  return controlDict_;
279  }
280 
281  //- Local directory path of this objectRegistry relative to time
282  virtual const fileName& dbDir() const
283  {
284  return fileName::null;
285  }
286 
287  //- Return current time path
288  fileName timePath() const
289  {
290  return path()/timeName();
291  }
292 
293  //- Default write format
295  {
296  return writeFormat_;
297  }
298 
299  //- Default write version number
301  {
302  return writeVersion_;
303  }
304 
305  //- Default write compression
307  {
308  return writeCompression_;
309  }
310 
311  //- Default graph format
312  const word& graphFormat() const
313  {
314  return graphFormat_;
315  }
316 
317  //- Supports re-reading
318  const Switch& runTimeModifiable() const
319  {
320  return runTimeModifiable_;
321  }
322 
323  //- Read control dictionary, update controls and time
324  virtual bool read();
325 
326  //- Read the objects that have been modified
327  void readModifiedObjects();
328 
329  //- Return the location of "dir" containing the file "name".
330  // (eg, used in reading mesh data)
331  // If name is null, search for the directory "dir" only.
332  // Does not search beyond stopInstance (if set) or constant.
334  (
335  const fileName& dir,
336  const word& name = word::null,
338  const word& stopInstance = word::null
339  ) const;
340 
341  //- Search the case for valid time directories
342  instantList times() const;
343 
344  //- Search the case for the time directory path
345  // corresponding to the given instance
346  word findInstancePath(const fileName& path, const instant&) const;
347 
348  //- Search the case for the time directory path
349  // corresponding to the given instance
350  word findInstancePath(const instant&) const;
351 
352  //- Search the case for the time closest to the given time
353  instant findClosestTime(const scalar) const;
354 
355  //- Search instantList for the time index closest to the given time
357  (
358  const instantList&,
359  const scalar,
360  const word& constantName = "constant"
361  );
362 
363  //- Write time dictionary to the <time>/uniform directory
364  virtual bool writeTimeDict() const;
365 
366  //- Write using given format, version and compression
367  virtual bool writeObject
368  (
372  const bool write
373  ) const;
374 
375  //- Write the objects now (not at end of iteration) and continue
376  // the run
377  bool writeNow();
378 
379  //- Write the objects now (not at end of iteration) and end the run
380  bool writeAndEnd();
381 
382  //- Write the objects once (one shot) and continue the run
383  void writeOnce();
384 
385 
386  // Access
387 
388  //- Return time name of given scalar time
389  // formatted with given precision
390  static word timeName
391  (
392  const scalar,
393  const int precision = curPrecision_
394  );
395 
396  //- Return current time name
397  virtual word timeName() const;
398 
399  //- Search a given directory for valid time directories
400  static instantList findTimes
401  (
402  const fileName&,
403  const word& constantName = "constant"
404  );
405 
406  //- Return start time index
407  virtual label startTimeIndex() const;
408 
409  //- Return begin time (initial start time)
410  virtual dimensionedScalar beginTime() const;
411 
412  //- Return start time
413  virtual dimensionedScalar startTime() const;
414 
415  //- Return end time
416  virtual dimensionedScalar endTime() const;
417 
418  //- Return the userTime
419  const userTimes::userTime& userTime() const;
420 
421  //- Return current user time value
422  scalar userTimeValue() const;
423 
424  //- Convert the user-time (e.g. CA deg) to real-time (s).
425  scalar userTimeToTime(const scalar tau) const;
426 
427  //- Convert the real-time (s) into user-time (e.g. CA deg)
428  scalar timeToUserTime(const scalar t) const;
429 
430  //- Return current user time name with units
431  word userTimeName() const;
432 
433  //- Return the list of function objects
434  const functionObjectList& functionObjects() const
435  {
436  return functionObjects_;
437  }
438 
439  //- Return true if the run is a restart, i.e. startTime != beginTime
440  bool restart() const
441  {
442  return startTime_ != beginTime_;
443  }
444 
445  //- Return true if time currently being sub-cycled, otherwise false
446  bool subCycling() const
447  {
448  return subCycling_;
449  }
450 
451  //- Return previous TimeState if time is being sub-cycled
452  const TimeState& prevTimeState() const
453  {
454  return prevTimeState_();
455  }
456 
457 
458  // Check
459 
460  //- Return true if run should continue without any side effects
461  virtual bool running() const;
462 
463  //- Return true if run should continue,
464  // also invokes the functionObjectList::end() method
465  // when the time goes out of range
466  // \note
467  // For correct behaviour, the following style of time-loop
468  // is recommended:
469  // \code
470  // while (runTime.run())
471  // {
472  // runTime++;
473  // solve;
474  // runTime.write();
475  // }
476  // \endcode
477  virtual bool run() const;
478 
479  //- Return true if run should continue and if so increment time
480  // also invokes the functionObjectList::end() method
481  // when the time goes out of range
482  // \note
483  // For correct behaviour, the following style of time-loop
484  // is recommended:
485  // \code
486  // while (runTime.loop())
487  // {
488  // solve;
489  // runTime.write();
490  // }
491  // \endcode
492  virtual bool loop();
493 
494  //- Return true if end of run,
495  // does not invoke any functionObject methods
496  // \note
497  // The rounding heuristics near endTime mean that
498  // \code run() \endcode and \code !end() \endcode may
499  // not yield the same result
500  virtual bool end() const;
501 
502 
503  // Edit
504 
505  //- Adjust the current stopAtControl. Note that this value
506  // only persists until the next time the dictionary is read.
507  // Return true if the stopAtControl changed.
508  virtual bool stopAt(const stopAtControl) const;
509 
510  //- Reset the time and time-index to those of the given time
511  virtual void setTime(const Time&);
512 
513  //- Reset the time and time-index
514  virtual void setTime(const instant&, const label newIndex);
515 
516  //- Reset the time and time-index
517  virtual void setTime
518  (
519  const dimensionedScalar&,
520  const label newIndex
521  );
522 
523  //- Reset the time and time-index
524  virtual void setTime(const scalar, const label newIndex);
525 
526  //- Reset end time
527  virtual void setEndTime(const dimensionedScalar&);
528 
529  //- Reset end time
530  virtual void setEndTime(const scalar);
531 
532  //- Reset time step
533  virtual void setDeltaT(const dimensionedScalar&);
534 
535  //- Reset time step
536  virtual void setDeltaT(const scalar);
537 
538  //- Reset time step without additional adjustment or modification
539  // by function objects
540  virtual void setDeltaTNoAdjust(const scalar);
541 
542  //- Reset the write interval
543  virtual void setWriteInterval(const scalar writeInterval);
544 
545  //- Set time to sub-cycle for the given number of steps
546  virtual TimeState subCycle(const label nSubCycles);
547 
548  //- Reset time after sub-cycling back to previous TimeState
549  virtual void endSubCycle();
550 
551  //- Return non-const access to the list of function objects
553  {
554  return functionObjects_;
555  }
556 
557 
558  // Member Operators
559 
560  //- Set deltaT to that specified and increment time via operator++()
561  virtual Time& operator+=(const dimensionedScalar&);
562 
563  //- Set deltaT to that specified and increment time via operator++()
564  virtual Time& operator+=(const scalar);
565 
566  //- Prefix increment,
567  // also invokes the functionObjectList::start() or
568  // functionObjectList::execute() method, depending on the time-index
569  virtual Time& operator++();
570 
571  //- Postfix increment, this is identical to the prefix increment
572  virtual Time& operator++(int);
573 };
574 
575 
576 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
577 
578 } // End namespace Foam
579 
580 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
581 
582 #endif
583 
584 // ************************************************************************* //
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:51
virtual Time & operator++()
Prefix increment,.
Definition: Time.C:1123
dictionary dict
A class for addressing time paths without using the Time class.
Definition: TimePaths.H:48
A class for handling file names.
Definition: fileName.H:79
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:358
sigStopAtWriteNow sigStopAtWriteNow_
Enable write and clean exit upon signal.
Definition: Time.H:154
The time value with time-stepping information, user-defined remapping, etc.
Definition: TimeState.H:52
writeControl writeControl_
Definition: Time.H:132
virtual dimensionedScalar beginTime() const
Return begin time (initial start time)
Definition: Time.C:821
void adjustDeltaT()
Adjust the time step so that writing occurs at the specified time.
Definition: Time.C:83
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
virtual ~Time()
Destructor.
Definition: Time.C:657
fileName timePath() const
Return current time path.
Definition: Time.H:287
scalar writeInterval_
Definition: Time.H:134
Read access to the system clock with formatting.
Definition: clock.H:50
static const fileName null
An empty fileName.
Definition: fileName.H:97
virtual bool loop()
Return true if run should continue and if so increment time.
Definition: Time.C:922
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:827
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:445
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:689
A simple wrapper around bool so that it can be read as a word: true/false, on/off, yes/no, y/n, t/f, or none/any.
Definition: Switch.H:60
virtual dimensionedScalar endTime() const
Return end time.
Definition: Time.C:833
stopAtControl
Stop-run control options.
Definition: Time.H:99
scalar startTime_
Definition: Time.H:121
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:451
readOption
Enumeration defining the read options.
Definition: IOobject.H:116
virtual word timeName() const
Return current time name.
Definition: Time.C:676
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
virtual bool run() const
Return true if run should continue,.
Definition: Time.C:875
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:169
scalar userTimeValue() const
Return current user time value.
Definition: Time.C:845
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:263
const userTimes::userTime & userTime() const
Return the userTime.
Definition: Time.C:839
IOstream::versionNumber writeVersion() const
Default write version number.
Definition: Time.H:299
label purgeWrite_
Definition: Time.H:136
virtual TimeState subCycle(const label nSubCycles)
Set time to sub-cycle for the given number of steps.
Definition: Time.C:1083
virtual bool stopAt(const stopAtControl) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:941
const Switch & runTimeModifiable() const
Supports re-reading.
Definition: Time.H:317
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:299
static int precision_
Time directory name precision.
Definition: Time.H:161
TypeName("time")
A class for handling words, derived from string.
Definition: word.H:59
virtual bool running() const
Return true if run should continue without any side effects.
Definition: Time.C:869
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
const fileName & caseName() const
Return case name.
Definition: TimePaths.H:107
const fileName & rootPath() const
Return root path.
Definition: TimePaths.H:95
static int curPrecision_
Current time directory name precision adjusted as necessary.
Definition: Time.H:166
bool subCycling_
Is the time currently being sub-cycled?
Definition: Time.H:143
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
static const word null
An empty word.
Definition: word.H:77
IOstream::compressionType writeCompression() const
Default write compression.
Definition: Time.H:305
scalar endTime_
Definition: Time.H:122
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:959
format
Supported time directory name formats.
Definition: Time.H:108
List of function objects with start(), execute() and end() functions that is called for each object...
Signal handler for interrupt defined by OptimisationSwitches::writeNowSignal.
Definition: sigWriteNow.H:52
Time(const word &name, const argList &args, const word &systemName="system", const word &constantName="constant")
Construct given name of dictionary to read and argument list.
Definition: Time.C:417
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:207
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static format format_
Time directory name format.
Definition: Time.H:158
scalar beginTime_
Definition: Time.H:123
FIFOStack< word > previousWriteTimes_
Definition: Time.H:137
virtual void setDeltaTNoAdjust(const scalar)
Reset time step without additional adjustment or modification.
Definition: Time.C:1048
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:293
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:32
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:128
const word & name() const
Return const reference to name.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:227
static instantList findTimes(const fileName &, const word &constantName="constant")
Search a given directory for valid time directories.
Definition: findTimes.C:36
virtual void setEndTime(const dimensionedScalar &)
Reset end time.
Definition: Time.C:1019
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:367
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:131
An instant of time. Contains the time value and name.
Definition: instant.H:66
virtual bool end() const
Return true if end of run,.
Definition: Time.C:935
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:212
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:433
const word & graphFormat() const
Default graph format.
Definition: Time.H:311
bool writeOnce_
Definition: Time.H:140
word userTimeName() const
Return current user time name with units.
Definition: Time.C:863
sigWriteNow sigWriteNow_
Enable one-shot writing upon signal.
Definition: Time.H:151
instantList times() const
Search the case for valid time directories.
Definition: Time.C:682
const dictionary & controlDict() const
Return the control dict.
Definition: Time.H:275
Version number type.
Definition: IOstream.H:96
instant findClosestTime(const scalar) const
Search the case for the time closest to the given time.
Definition: Time.C:753
label startTimeIndex_
Definition: Time.H:120
virtual void setDeltaT(const dimensionedScalar &)
Reset time step.
Definition: Time.C:1031
writeControl
Write control options.
Definition: Time.H:89
stopAtControl stopAt_
Definition: Time.H:129
scalar userTimeToTime(const scalar tau) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: Time.C:851
virtual void endSubCycle()
Reset time after sub-cycling back to previous TimeState.
Definition: Time.C:1097
Signal handler for interrupt defined by OptimisationSwitches::stopAtWriteNowSignal.
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:1055
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: Time.C:857
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:54
virtual bool write(const bool write=true) const
Write using setting from DB.
Registry of regIOobjects.
Foam::argList args(argc, argv)
word findInstancePath(const fileName &path, const instant &) const
Search the case for the time directory path.
Definition: Time.C:719
fileName path() const
Return path.
Definition: TimePaths.H:139
autoPtr< userTimes::userTime > userTime_
Optional user-time, defaults to realTime in s.
Definition: Time.H:126
virtual label startTimeIndex() const
Return start time index.
Definition: Time.C:815
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to time.
Definition: Time.H:281
virtual Time & operator+=(const dimensionedScalar &)
Set deltaT to that specified and increment time via operator++()
Definition: Time.C:1110
autoPtr< TimeState > prevTimeState_
If time is being sub-cycled this is the previous TimeState.
Definition: Time.H:146
bool restart() const
Return true if the run is a restart, i.e. startTime != beginTime.
Definition: Time.H:439
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:351
void setControls()
Set the controls from the current controlDict.
Definition: Time.C:109
IOstream & scientific(IOstream &io)
Definition: IOstream.H:579
Namespace for OpenFOAM.
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:790