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-2021 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 "Switch.H"
49 #include "instantList.H"
50 #include "NamedEnum.H"
51 #include "typeInfo.H"
52 #include "dlLibraryTable.H"
53 #include "functionObjectList.H"
54 #include "sigWriteNow.H"
55 #include "sigStopAtWriteNow.H"
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward declaration of classes
63 class argList;
64 
65 /*---------------------------------------------------------------------------*\
66  Class Time Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class Time
70 :
71  public clock,
72  public cpuTime,
73  public TimePaths,
74  public objectRegistry,
75  public TimeState
76 {
77  // Private Data
78 
79  //- Is runtime modification of dictionaries allowed?
80  Switch runTimeModifiable_;
81 
82  //- The controlDict
83  IOdictionary controlDict_;
84 
85 
86 public:
87 
88  //- Write control options
89  enum class writeControl
90  {
91  timeStep,
92  runTime,
94  clockTime,
95  cpuTime
96  };
97 
98  //- Stop-run control options
99  enum class stopAtControl
100  {
101  endTime,
102  noWriteNow,
103  writeNow,
104  nextWrite
105  };
106 
107  //- Supported time directory name formats
108  enum class format
109  {
110  general = 0,
113  };
114 
115 
116 protected:
117 
118  // Protected data
121  scalar startTime_;
122  mutable scalar endTime_;
123  scalar beginTime_;
126  mutable stopAtControl stopAt_;
131  scalar writeInterval_;
135 
136  // One-shot writing
137  bool writeOnce_;
138 
139  //- Is the time currently being sub-cycled?
140  bool subCycling_;
141 
142  //- If time is being sub-cycled this is the previous TimeState
144 
145 
146  // Signal handlers for writing
147 
148  //- Enable one-shot writing upon signal
150 
151  //- Enable write and clean exit upon signal
153 
154 
155  //- Time directory name format
156  static format format_;
157 
158  //- Time directory name precision
159  static int precision_;
160 
161  //- Maximum time directory name precision
162  static const int maxPrecision_;
163 
164  //- Adjust the time step so that writing occurs at the specified time
165  void adjustDeltaT();
166 
167  //- Set the controls from the current controlDict
168  void setControls();
169 
170  //- Read the control dictionary and set the write controls etc.
171  virtual void readDict();
172 
173 
174 private:
175 
176  //- Default write option
177  IOstream::streamFormat writeFormat_;
178 
179  //- Default output file format version number
180  IOstream::versionNumber writeVersion_;
181 
182  //- Default output compression
183  IOstream::compressionType writeCompression_;
184 
185  //- Default graph format
186  word graphFormat_;
187 
188  //- Is temporary object cache enabled
189  mutable bool cacheTemporaryObjects_;
190 
191  //- Function objects executed at start and on ++, +=
192  mutable functionObjectList functionObjects_;
193 
194 
195 public:
196 
197  TypeName("time");
198 
199  //- The default control dictionary name (normally "controlDict")
200  static word controlDictName;
201 
202 
203  // Constructors
204 
205  //- Construct given name of dictionary to read and argument list
206  Time
207  (
208  const word& name,
209  const argList& args,
210  const word& systemName = "system",
211  const word& constantName = "constant"
212  );
213 
214  //- Construct given name of dictionary to read, rootPath and casePath
215  Time
216  (
217  const word& name,
218  const fileName& rootPath,
219  const fileName& caseName,
220  const word& systemName = "system",
221  const word& constantName = "constant",
222  const bool enableFunctionObjects = true
223  );
224 
225  //- Construct given dictionary, rootPath and casePath
226  Time
227  (
228  const dictionary& dict,
229  const fileName& rootPath,
230  const fileName& caseName,
231  const word& systemName = "system",
232  const word& constantName = "constant",
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 word& systemName = "system",
242  const word& constantName = "constant",
243  const bool enableFunctionObjects = true
244  );
245 
246 
247  //- Destructor
248  virtual ~Time();
249 
250 
251  // Member Functions
252 
253  // Database functions
254 
255  //- Explicitly inherit rootPath from TimePaths to disambiguate from
256  // the corresponding method in objectRegistry
257  using TimePaths::rootPath;
258 
259  //- Explicitly inherit caseName from TimePaths to disambiguate from
260  // the corresponding method in objectRegistry
261  using TimePaths::caseName;
262 
263  //- Explicitly inherit path from TimePaths to disambiguate from
264  // the corresponding method in objectRegistry
265  using TimePaths::path;
266 
267  //- Return the control dict
268  const dictionary& controlDict() const
269  {
270  return controlDict_;
271  }
272 
273  //- Local directory path of this objectRegistry relative to time
274  virtual const fileName& dbDir() const
275  {
276  return fileName::null;
277  }
278 
279  //- Return current time path
280  fileName timePath() const
281  {
282  return path()/timeName();
283  }
284 
285  //- Default write format
287  {
288  return writeFormat_;
289  }
290 
291  //- Default write version number
293  {
294  return writeVersion_;
295  }
296 
297  //- Default write compression
299  {
300  return writeCompression_;
301  }
302 
303  //- Default graph format
304  const word& graphFormat() const
305  {
306  return graphFormat_;
307  }
308 
309  //- Supports re-reading
310  const Switch& runTimeModifiable() const
311  {
312  return runTimeModifiable_;
313  }
314 
315  //- Read control dictionary, update controls and time
316  virtual bool read();
317 
318  //- Read the objects that have been modified
319  void readModifiedObjects();
320 
321  //- Return the location of "dir" containing the file "name".
322  // (eg, used in reading mesh data)
323  // If name is null, search for the directory "dir" only.
324  // Does not search beyond stopInstance (if set) or constant.
326  (
327  const fileName& dir,
328  const word& name = word::null,
330  const word& stopInstance = word::null
331  ) const;
332 
333  //- Search the case for valid time directories
334  instantList times() const;
335 
336  //- Search the case for the time directory path
337  // corresponding to the given instance
338  word findInstancePath(const fileName& path, const instant&) const;
339 
340  //- Search the case for the time directory path
341  // corresponding to the given instance
342  word findInstancePath(const instant&) const;
343 
344  //- Search the case for the time closest to the given time
345  instant findClosestTime(const scalar) const;
346 
347  //- Search instantList for the time index closest to the given time
349  (
350  const instantList&,
351  const scalar,
352  const word& constantName = "constant"
353  );
354 
355  //- Write time dictionary to the <time>/uniform directory
356  virtual bool writeTimeDict() const;
357 
358  //- Write using given format, version and compression
359  virtual bool writeObject
360  (
364  const bool write
365  ) const;
366 
367  //- Write the objects now (not at end of iteration) and continue
368  // the run
369  bool writeNow();
370 
371  //- Write the objects now (not at end of iteration) and end the run
372  bool writeAndEnd();
373 
374  //- Write the objects once (one shot) and continue the run
375  void writeOnce();
376 
377 
378  // Access
379 
380  //- Return time name of given scalar time
381  // formatted with given precision
382  static word timeName
383  (
384  const scalar,
385  const int precision = precision_
386  );
387 
388  //- Return current time name
389  virtual word timeName() const;
390 
391  //- Search a given directory for valid time directories
392  static instantList findTimes
393  (
394  const fileName&,
395  const word& constantName = "constant"
396  );
397 
398  //- Return start time index
399  virtual label startTimeIndex() const;
400 
401  //- Return begin time (initial start time)
402  virtual dimensionedScalar beginTime() const;
403 
404  //- Return start time
405  virtual dimensionedScalar startTime() const;
406 
407  //- Return end time
408  virtual dimensionedScalar endTime() const;
409 
410  //- Return the list of function objects
411  const functionObjectList& functionObjects() const
412  {
413  return functionObjects_;
414  }
415 
416  //- Return true if the run is a restart, i.e. startTime != beginTime
417  bool restart() const
418  {
419  return startTime_ != beginTime_;
420  }
421 
422  //- Return true if time currently being sub-cycled, otherwise false
423  bool subCycling() const
424  {
425  return subCycling_;
426  }
427 
428  //- Return previous TimeState if time is being sub-cycled
429  const TimeState& prevTimeState() const
430  {
431  return prevTimeState_();
432  }
433 
434 
435  // Check
436 
437  //- Return true if run should continue without any side effects
438  virtual bool running() const;
439 
440  //- Return true if run should continue,
441  // also invokes the functionObjectList::end() method
442  // when the time goes out of range
443  // \note
444  // For correct behaviour, the following style of time-loop
445  // is recommended:
446  // \code
447  // while (runTime.run())
448  // {
449  // runTime++;
450  // solve;
451  // runTime.write();
452  // }
453  // \endcode
454  virtual bool run() const;
455 
456  //- Return true if run should continue and if so increment time
457  // also invokes the functionObjectList::end() method
458  // when the time goes out of range
459  // \note
460  // For correct behaviour, the following style of time-loop
461  // is recommended:
462  // \code
463  // while (runTime.loop())
464  // {
465  // solve;
466  // runTime.write();
467  // }
468  // \endcode
469  virtual bool loop();
470 
471  //- Return true if end of run,
472  // does not invoke any functionObject methods
473  // \note
474  // The rounding heuristics near endTime mean that
475  // \code run() \endcode and \code !end() \endcode may
476  // not yield the same result
477  virtual bool end() const;
478 
479 
480  // Edit
481 
482  //- Adjust the current stopAtControl. Note that this value
483  // only persists until the next time the dictionary is read.
484  // Return true if the stopAtControl changed.
485  virtual bool stopAt(const stopAtControl) const;
486 
487  //- Reset the time and time-index to those of the given time
488  virtual void setTime(const Time&);
489 
490  //- Reset the time and time-index
491  virtual void setTime(const instant&, const label newIndex);
492 
493  //- Reset the time and time-index
494  virtual void setTime
495  (
496  const dimensionedScalar&,
497  const label newIndex
498  );
499 
500  //- Reset the time and time-index
501  virtual void setTime(const scalar, const label newIndex);
502 
503  //- Reset end time
504  virtual void setEndTime(const dimensionedScalar&);
505 
506  //- Reset end time
507  virtual void setEndTime(const scalar);
508 
509  //- Reset time step
510  virtual void setDeltaT(const dimensionedScalar&);
511 
512  //- Reset time step
513  virtual void setDeltaT(const scalar);
514 
515  //- Reset time step without additional adjustment or modification
516  // by function objects
517  virtual void setDeltaTNoAdjust(const scalar);
518 
519  //- Reset the write interval
520  virtual void setWriteInterval(const scalar writeInterval);
521 
522  //- Set time to sub-cycle for the given number of steps
523  virtual TimeState subCycle(const label nSubCycles);
524 
525  //- Reset time after sub-cycling back to previous TimeState
526  virtual void endSubCycle();
527 
528  //- Return non-const access to the list of function objects
530  {
531  return functionObjects_;
532  }
533 
534 
535  // Member Operators
536 
537  //- Set deltaT to that specified and increment time via operator++()
538  virtual Time& operator+=(const dimensionedScalar&);
539 
540  //- Set deltaT to that specified and increment time via operator++()
541  virtual Time& operator+=(const scalar);
542 
543  //- Prefix increment,
544  // also invokes the functionObjectList::start() or
545  // functionObjectList::execute() method, depending on the time-index
546  virtual Time& operator++();
547 
548  //- Postfix increment, this is identical to the prefix increment
549  virtual Time& operator++(int);
550 };
551 
552 
553 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
554 
555 } // End namespace Foam
556 
557 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
558 
559 #endif
560 
561 // ************************************************************************* //
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:51
virtual Time & operator++()
Prefix increment,.
Definition: Time.C:1054
dictionary dict
A class for addressing time paths without using the Time class.
Definition: TimePaths.H:48
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
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:345
sigStopAtWriteNow sigStopAtWriteNow_
Enable write and clean exit upon signal.
Definition: Time.H:151
The time value with time-stepping information, user-defined remapping, etc.
Definition: TimeState.H:48
writeControl writeControl_
Definition: Time.H:128
virtual dimensionedScalar beginTime() const
Return begin time (initial start time)
Definition: Time.C:791
void adjustDeltaT()
Adjust the time step so that writing occurs at the specified time.
Definition: Time.C:80
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:627
fileName timePath() const
Return current time path.
Definition: Time.H:279
scalar writeInterval_
Definition: Time.H:130
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:862
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:797
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:422
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:659
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:803
stopAtControl
Stop-run control options.
Definition: Time.H:98
scalar startTime_
Definition: Time.H:120
IOstream & fixed(IOstream &io)
Definition: IOstream.H:573
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:428
readOption
Enumeration defining the read options.
Definition: IOobject.H:110
virtual word timeName() const
Return current time name.
Definition: Time.C:646
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
virtual bool run() const
Return true if run should continue,.
Definition: Time.C:815
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:161
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:250
IOstream::versionNumber writeVersion() const
Default write version number.
Definition: Time.H:291
label purgeWrite_
Definition: Time.H:132
virtual TimeState subCycle(const label nSubCycles)
Set time to sub-cycle for the given number of steps.
Definition: Time.C:1014
virtual bool stopAt(const stopAtControl) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:881
const Switch & runTimeModifiable() const
Supports re-reading.
Definition: Time.H:309
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:286
static int precision_
Time directory name precision.
Definition: Time.H:158
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:809
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
bool subCycling_
Is the time currently being sub-cycled?
Definition: Time.H:139
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:297
scalar endTime_
Definition: Time.H:121
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:899
format
Supported time directory name formats.
Definition: Time.H:107
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:398
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:199
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static format format_
Time directory name format.
Definition: Time.H:155
scalar beginTime_
Definition: Time.H:122
FIFOStack< word > previousWriteTimes_
Definition: Time.H:133
virtual void setDeltaTNoAdjust(const scalar)
Reset time step without additional adjustment or modification.
Definition: Time.C:979
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:285
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:31
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:124
const word & name() const
Return const reference to name.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:214
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:950
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:354
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:127
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:875
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:199
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:410
const word & graphFormat() const
Default graph format.
Definition: Time.H:303
bool writeOnce_
Definition: Time.H:136
sigWriteNow sigWriteNow_
Enable one-shot writing upon signal.
Definition: Time.H:148
instantList times() const
Search the case for valid time directories.
Definition: Time.C:652
const dictionary & controlDict() const
Return the control dict.
Definition: Time.H:267
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:723
label startTimeIndex_
Definition: Time.H:119
virtual void setDeltaT(const dimensionedScalar &)
Reset time step.
Definition: Time.C:962
writeControl
Write control options.
Definition: Time.H:88
stopAtControl stopAt_
Definition: Time.H:125
virtual void endSubCycle()
Reset time after sub-cycling back to previous TimeState.
Definition: Time.C:1028
Signal handler for interrupt defined by OptimisationSwitches::stopAtWriteNowSignal.
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:986
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
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:689
fileName path() const
Return path.
Definition: TimePaths.H:139
virtual label startTimeIndex() const
Return start time index.
Definition: Time.C:785
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to time.
Definition: Time.H:273
virtual Time & operator+=(const dimensionedScalar &)
Set deltaT to that specified and increment time via operator++()
Definition: Time.C:1041
autoPtr< TimeState > prevTimeState_
If time is being sub-cycled this is the previous TimeState.
Definition: Time.H:142
bool restart() const
Return true if the run is a restart, i.e. startTime != beginTime.
Definition: Time.H:416
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:338
void setControls()
Set the controls from the current controlDict.
Definition: Time.C:106
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:760