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-2019 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 "unwatchedIOdictionary.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  //- Any loaded dynamic libraries. Make sure to construct before
80  // reading controlDict.
81  dlLibraryTable libs_;
82 
83  //- The controlDict
84  unwatchedIOdictionary controlDict_;
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_;
125  mutable stopAtControl stopAt_;
130  scalar writeInterval_;
134 
135  // One-shot writing
136  bool writeOnce_;
137 
138  //- Is the time currently being sub-cycled?
139  bool subCycling_;
140 
141  //- If time is being sub-cycled this is the previous TimeState
143 
144 
145  // Signal handlers for writing
146 
147  //- Enable one-shot writing upon signal
149 
150  //- Enable write and clean exit upon signal
152 
153 
154  //- Time directory name format
155  static format format_;
156 
157  //- Time directory name precision
158  static int precision_;
159 
160  //- Maximum time directory name precision
161  static const int maxPrecision_;
162 
163  //- Adjust the time step so that writing occurs at the specified time
164  void adjustDeltaT();
165 
166  //- Set the controls from the current controlDict
167  void setControls();
168 
169  //- Read the control dictionary and set the write controls etc.
170  virtual void readDict();
171 
172 
173 private:
174 
175  //- Default write option
176  IOstream::streamFormat writeFormat_;
177 
178  //- Default output file format version number
179  IOstream::versionNumber writeVersion_;
180 
181  //- Default output compression
182  IOstream::compressionType writeCompression_;
183 
184  //- Default graph format
185  word graphFormat_;
186 
187  //- Is runtime modification of dictionaries allowed?
188  Switch runTimeModifiable_;
189 
190  //- Function objects executed at start and on ++, +=
191  mutable functionObjectList functionObjects_;
192 
193 
194 public:
195 
196  TypeName("time");
197 
198  //- The default control dictionary name (normally "controlDict")
199  static word controlDictName;
200 
201 
202  // Constructors
203 
204  //- Construct given name of dictionary to read and argument list
205  Time
206  (
207  const word& name,
208  const argList& args,
209  const word& systemName = "system",
210  const word& constantName = "constant"
211  );
212 
213  //- Construct given name of dictionary to read, rootPath and casePath
214  Time
215  (
216  const word& name,
217  const fileName& rootPath,
218  const fileName& caseName,
219  const word& systemName = "system",
220  const word& constantName = "constant",
221  const bool enableFunctionObjects = true
222  );
223 
224  //- Construct given dictionary, rootPath and casePath
225  Time
226  (
227  const dictionary& dict,
228  const fileName& rootPath,
229  const fileName& caseName,
230  const word& systemName = "system",
231  const word& constantName = "constant",
232  const bool enableFunctionObjects = true
233  );
234 
235  //- Construct given endTime, rootPath and casePath
236  Time
237  (
238  const fileName& rootPath,
239  const fileName& caseName,
240  const word& systemName = "system",
241  const word& constantName = "constant",
242  const bool enableFunctionObjects = true
243  );
244 
245 
246  //- Destructor
247  virtual ~Time();
248 
249 
250  // Member Functions
251 
252  // Database functions
253 
254  //- Return root path
255  const fileName& rootPath() const
256  {
257  return TimePaths::rootPath();
258  }
259 
260  //- Return case name
261  const fileName& caseName() const
262  {
263  return TimePaths::caseName();
264  }
265 
266  //- Return path
267  fileName path() const
268  {
269  return rootPath()/caseName();
270  }
272  const dictionary& controlDict() const
273  {
274  return controlDict_;
275  }
277  virtual const fileName& dbDir() const
278  {
279  return fileName::null;
280  }
281 
282  //- Return current time path
283  fileName timePath() const
284  {
285  return path()/timeName();
286  }
287 
288  //- Default write format
290  {
291  return writeFormat_;
292  }
293 
294  //- Default write version number
296  {
297  return writeVersion_;
298  }
299 
300  //- Default write compression
302  {
303  return writeCompression_;
304  }
305 
306  //- Default graph format
307  const word& graphFormat() const
308  {
309  return graphFormat_;
310  }
311 
312  //- Supports re-reading
313  const Switch& runTimeModifiable() const
314  {
315  return runTimeModifiable_;
316  }
317 
318  //- Read control dictionary, update controls and time
319  virtual bool read();
320 
321  //- Read the objects that have been modified
322  void readModifiedObjects();
323 
324  //- Return the location of "dir" containing the file "name".
325  // (eg, used in reading mesh data)
326  // If name is null, search for the directory "dir" only.
327  // Does not search beyond stopInstance (if set) or constant.
329  (
330  const fileName& dir,
331  const word& name = word::null,
333  const word& stopInstance = word::null
334  ) const;
335 
336  //- Search the case for valid time directories
337  instantList times() const;
338 
339  //- Search the case for the time directory path
340  // corresponding to the given instance
341  word findInstancePath(const fileName& path, const instant&) const;
342 
343  //- Search the case for the time directory path
344  // corresponding to the given instance
345  word findInstancePath(const instant&) const;
346 
347  //- Search the case for the time closest to the given time
348  instant findClosestTime(const scalar) const;
349 
350  //- Search instantList for the time index closest to the given time
352  (
353  const instantList&,
354  const scalar,
355  const word& constantName = "constant"
356  );
357 
358  //- Write time dictionary to the <time>/uniform directory
359  virtual bool writeTimeDict() const;
360 
361  //- Write using given format, version and compression
362  virtual bool writeObject
363  (
367  const bool write
368  ) const;
369 
370  //- Write the objects now (not at end of iteration) and continue
371  // the run
372  bool writeNow();
373 
374  //- Write the objects now (not at end of iteration) and end the run
375  bool writeAndEnd();
376 
377  //- Write the objects once (one shot) and continue the run
378  void writeOnce();
379 
380 
381  // Access
382 
383  //- Return time name of given scalar time
384  // formatted with given precision
385  static word timeName
386  (
387  const scalar,
388  const int precision = precision_
389  );
390 
391  //- Return current time name
392  virtual word timeName() const;
393 
394  //- Search a given directory for valid time directories
395  static instantList findTimes
396  (
397  const fileName&,
398  const word& constantName = "constant"
399  );
400 
401  //- Return start time index
402  virtual label startTimeIndex() 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  //- External access to the loaded libraries
417  const dlLibraryTable& libs() const
418  {
419  return libs_;
420  }
421 
422  //- External access to the loaded libraries
424  {
425  return libs_;
426  }
427 
428  //- Return true if time currently being sub-cycled, otherwise false
429  bool subCycling() const
430  {
431  return subCycling_;
432  }
433 
434  //- Return previous TimeState if time is being sub-cycled
435  const TimeState& prevTimeState() const
436  {
437  return prevTimeState_();
438  }
439 
440 
441  // Check
442 
443  //- Return true if run should continue without any side effects
444  virtual bool running() const;
445 
446  //- Return true if run should continue,
447  // also invokes the functionObjectList::end() method
448  // when the time goes out of range
449  // \note
450  // For correct behaviour, the following style of time-loop
451  // is recommended:
452  // \code
453  // while (runTime.run())
454  // {
455  // runTime++;
456  // solve;
457  // runTime.write();
458  // }
459  // \endcode
460  virtual bool run() const;
461 
462  //- Return true if run should continue and if so increment time
463  // also invokes the functionObjectList::end() method
464  // when the time goes out of range
465  // \note
466  // For correct behaviour, the following style of time-loop
467  // is recommended:
468  // \code
469  // while (runTime.loop())
470  // {
471  // solve;
472  // runTime.write();
473  // }
474  // \endcode
475  virtual bool loop();
476 
477  //- Return true if end of run,
478  // does not invoke any functionObject methods
479  // \note
480  // The rounding heuristics near endTime mean that
481  // \code run() \endcode and \code !end() \endcode may
482  // not yield the same result
483  virtual bool end() const;
484 
485 
486  // Edit
487 
488  //- Adjust the current stopAtControl. Note that this value
489  // only persists until the next time the dictionary is read.
490  // Return true if the stopAtControl changed.
491  virtual bool stopAt(const stopAtControl) const;
492 
493  //- Reset the time and time-index to those of the given time
494  virtual void setTime(const Time&);
495 
496  //- Reset the time and time-index
497  virtual void setTime(const instant&, const label newIndex);
498 
499  //- Reset the time and time-index
500  virtual void setTime
501  (
502  const dimensionedScalar&,
503  const label newIndex
504  );
505 
506  //- Reset the time and time-index
507  virtual void setTime(const scalar, const label newIndex);
508 
509  //- Reset end time
510  virtual void setEndTime(const dimensionedScalar&);
511 
512  //- Reset end time
513  virtual void setEndTime(const scalar);
514 
515  //- Reset time step
516  virtual void setDeltaT(const dimensionedScalar&);
517 
518  //- Reset time step
519  virtual void setDeltaT(const scalar);
520 
521  //- Reset time step without additional adjustment or modification
522  // by function objects
523  virtual void setDeltaTNoAdjust(const scalar);
524 
525  //- Set time to sub-cycle for the given number of steps
526  virtual TimeState subCycle(const label nSubCycles);
527 
528  //- Reset time after sub-cycling back to previous TimeState
529  virtual void endSubCycle();
530 
531  //- Return non-const access to the list of function objects
533  {
534  return functionObjects_;
535  }
536 
537 
538  // Member Operators
539 
540  //- Set deltaT to that specified and increment time via operator++()
541  virtual Time& operator+=(const dimensionedScalar&);
542 
543  //- Set deltaT to that specified and increment time via operator++()
544  virtual Time& operator+=(const scalar);
545 
546  //- Prefix increment,
547  // also invokes the functionObjectList::start() or
548  // functionObjectList::execute() method, depending on the time-index
549  virtual Time& operator++();
550 
551  //- Postfix increment, this is identical to the prefix increment
552  virtual Time& operator++(int);
553 };
554 
555 
556 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
557 
558 } // End namespace Foam
559 
560 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
561 
562 #endif
563 
564 // ************************************************************************* //
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:51
virtual Time & operator++()
Prefix increment,.
Definition: Time.C:1002
dictionary dict
unwatchedIOdictionary is like IOdictionary but stores dependencies as files instead of fileMonitor wa...
fileName path() const
Return path.
Definition: Time.H:266
A class for addressing time paths without using the Time class.
Definition: TimePaths.H:49
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:601
sigStopAtWriteNow sigStopAtWriteNow_
Enable write and clean exit upon signal.
Definition: Time.H:150
The time value with time-stepping information, user-defined remapping, etc.
Definition: TimeState.H:48
writeControl writeControl_
Definition: Time.H:127
void adjustDeltaT()
Adjust the time step so that writing occurs at the specified time.
Definition: Time.C:84
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
virtual ~Time()
Destructor.
Definition: Time.C:612
fileName timePath() const
Return current time path.
Definition: Time.H:282
scalar writeInterval_
Definition: Time.H:129
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:836
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:781
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:428
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:649
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:787
stopAtControl
Stop-run control options.
Definition: Time.H:98
scalar startTime_
Definition: Time.H:120
IOstream & fixed(IOstream &io)
Definition: IOstream.H:576
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:434
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
virtual word timeName() const
Return current time name.
Definition: Time.C:636
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:799
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:160
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:507
IOstream::versionNumber writeVersion() const
Default write version number.
Definition: Time.H:294
label purgeWrite_
Definition: Time.H:131
virtual TimeState subCycle(const label nSubCycles)
Set time to sub-cycle for the given number of steps.
Definition: Time.C:962
virtual bool stopAt(const stopAtControl) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:855
const Switch & runTimeModifiable() const
Supports re-reading.
Definition: Time.H:312
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:542
static int precision_
Time directory name precision.
Definition: Time.H:157
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:793
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
const fileName & caseName() const
Return case name.
Definition: Time.H:260
const fileName & caseName() const
Return case name.
Definition: TimePaths.H:108
const fileName & rootPath() const
Return root path.
Definition: TimePaths.H:96
bool subCycling_
Is the time currently being sub-cycled?
Definition: Time.H:138
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:300
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:873
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...
const dlLibraryTable & libs() const
External access to the loaded libraries.
Definition: Time.H:416
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:391
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:198
A table of dynamically loaded libraries.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static format format_
Time directory name format.
Definition: Time.H:154
FIFOStack< word > previousWriteTimes_
Definition: Time.H:132
virtual void setDeltaTNoAdjust(const scalar)
Reset time step without additional adjustment or modification.
Definition: Time.C:955
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:288
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:35
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:123
const word & name() const
Return const reference to name.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:460
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:924
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:610
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:126
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:849
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:435
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:410
const word & graphFormat() const
Default graph format.
Definition: Time.H:306
bool writeOnce_
Definition: Time.H:135
sigWriteNow sigWriteNow_
Enable one-shot writing upon signal.
Definition: Time.H:147
instantList times() const
Search the case for valid time directories.
Definition: Time.C:642
const dictionary & controlDict() const
Definition: Time.H:271
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:713
label startTimeIndex_
Definition: Time.H:119
virtual void setDeltaT(const dimensionedScalar &)
Reset time step.
Definition: Time.C:936
writeControl
Write control options.
Definition: Time.H:88
stopAtControl stopAt_
Definition: Time.H:124
virtual void endSubCycle()
Reset time after sub-cycling back to previous TimeState.
Definition: Time.C:976
Signal handler for interrupt defined by OptimisationSwitches::stopAtWriteNowSignal.
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:679
virtual label startTimeIndex() const
Return start time index.
Definition: Time.C:775
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: Time.H:276
virtual Time & operator+=(const dimensionedScalar &)
Set deltaT to that specified and increment time via operator++()
Definition: Time.C:989
autoPtr< TimeState > prevTimeState_
If time is being sub-cycled this is the previous TimeState.
Definition: Time.H:141
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:594
void setControls()
Set the controls from the current controlDict.
Definition: Time.C:109
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
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:750
const fileName & rootPath() const
Return root path.
Definition: Time.H:254