Time.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2017 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 writeControls
90  {
95  wcCpuTime
96  };
97 
98  //- Stop-run control options
99  enum stopAtControls
100  {
104  saNextWrite
105  };
106 
107  //- Supported time directory name formats
108  enum fmtflags
109  {
110  general = 0,
113  };
114 
115 
116 protected:
117 
118  // Protected data
121  scalar startTime_;
122  mutable scalar endTime_;
125  mutable stopAtControls 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 fmtflags 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  //- Find IOobject in the objectPath
173  static bool exists(IOobject&);
174 
175 
176 private:
177 
178  //- Default write option
179  IOstream::streamFormat writeFormat_;
180 
181  //- Default output file format version number
182  IOstream::versionNumber writeVersion_;
183 
184  //- Default output compression
185  IOstream::compressionType writeCompression_;
186 
187  //- Default graph format
188  word graphFormat_;
189 
190  //- Is runtime modification of dictionaries allowed?
191  Switch runTimeModifiable_;
192 
193  //- Function objects executed at start and on ++, +=
194  mutable functionObjectList functionObjects_;
195 
196 
197 public:
198 
199  TypeName("time");
200 
201  //- The default control dictionary name (normally "controlDict")
202  static word controlDictName;
203 
204 
205  // Constructors
206 
207  //- Construct given name of dictionary to read and argument list
208  Time
209  (
210  const word& name,
211  const argList& args,
212  const word& systemName = "system",
213  const word& constantName = "constant"
214  );
215 
216  //- Construct given name of dictionary to read, rootPath and casePath
217  Time
218  (
219  const word& name,
220  const fileName& rootPath,
221  const fileName& caseName,
222  const word& systemName = "system",
223  const word& constantName = "constant",
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 word& systemName = "system",
234  const word& constantName = "constant",
235  const bool enableFunctionObjects = true
236  );
237 
238  //- Construct given endTime, rootPath and casePath
239  Time
240  (
241  const fileName& rootPath,
242  const fileName& caseName,
243  const word& systemName = "system",
244  const word& constantName = "constant",
245  const bool enableFunctionObjects = true
246  );
247 
248 
249  //- Destructor
250  virtual ~Time();
251 
252 
253  // Member functions
254 
255  // Database functions
256 
257  //- Return root path
258  const fileName& rootPath() const
259  {
260  return TimePaths::rootPath();
261  }
262 
263  //- Return case name
264  const fileName& caseName() const
265  {
266  return TimePaths::caseName();
267  }
268 
269  //- Return path
270  fileName path() const
271  {
272  return rootPath()/caseName();
273  }
275  const dictionary& controlDict() const
276  {
277  return controlDict_;
278  }
280  virtual const fileName& dbDir() const
281  {
282  return fileName::null;
283  }
284 
285  //- Return current time path
286  fileName timePath() const
287  {
288  return path()/timeName();
289  }
290 
291  //- Default write format
293  {
294  return writeFormat_;
295  }
296 
297  //- Default write version number
299  {
300  return writeVersion_;
301  }
302 
303  //- Default write compression
305  {
306  return writeCompression_;
307  }
308 
309  //- Default graph format
310  const word& graphFormat() const
311  {
312  return graphFormat_;
313  }
314 
315  //- Supports re-reading
316  const Switch& runTimeModifiable() const
317  {
318  return runTimeModifiable_;
319  }
320 
321  //- Read control dictionary, update controls and time
322  virtual bool read();
323 
324  //- Read the objects that have been modified
325  void readModifiedObjects();
326 
327  //- Return the location of "dir" containing the file "name".
328  // (eg, used in reading mesh data)
329  // If name is null, search for the directory "dir" only.
330  // Does not search beyond stopInstance (if set) or constant.
332  (
333  const fileName& dir,
334  const word& name = word::null,
336  const word& stopInstance = word::null
337  ) const;
338 
339  //- Search the case for valid time directories
340  instantList times() const;
341 
342  //- Search the case for the time directory path
343  // corresponding to the given instance
344  word findInstancePath(const fileName& path, const instant&) const;
345 
346  //- Search the case for the time directory path
347  // corresponding to the given instance
348  word findInstancePath(const instant&) const;
349 
350  //- Search the case for the time closest to the given time
351  instant findClosestTime(const scalar) const;
352 
353  //- Search instantList for the time index closest to the given time
355  (
356  const instantList&,
357  const scalar,
358  const word& constantName = "constant"
359  );
360 
361  //- Write time dictionary to the <time>/uniform directory
362  virtual bool writeTimeDict() const;
363 
364  //- Write using given format, version and compression
365  virtual bool writeObject
366  (
370  const bool valid
371  ) const;
372 
373  //- Write the objects now (not at end of iteration) and continue
374  // the run
375  bool writeNow();
376 
377  //- Write the objects now (not at end of iteration) and end the run
378  bool writeAndEnd();
379 
380  //- Write the objects once (one shot) and continue the run
381  void writeOnce();
382 
383 
384  // Access
385 
386  //- Return time name of given scalar time
387  // formatted with given precision
388  static word timeName
389  (
390  const scalar,
391  const int precision = precision_
392  );
393 
394  //- Return current time name
395  virtual word timeName() const;
396 
397  //- Search a given directory for valid time directories
398  static instantList findTimes
399  (
400  const fileName&,
401  const word& constantName = "constant"
402  );
403 
404  //- Return start time index
405  virtual label startTimeIndex() const;
406 
407  //- Return start time
408  virtual dimensionedScalar startTime() const;
409 
410  //- Return end time
411  virtual dimensionedScalar endTime() const;
412 
413  //- Return the list of function objects
414  const functionObjectList& functionObjects() const
415  {
416  return functionObjects_;
417  }
418 
419  //- External access to the loaded libraries
420  const dlLibraryTable& libs() const
421  {
422  return libs_;
423  }
424 
425  //- External access to the loaded libraries
427  {
428  return libs_;
429  }
430 
431  //- Return true if time currently being sub-cycled, otherwise false
432  bool subCycling() const
433  {
434  return subCycling_;
435  }
436 
437  //- Return previous TimeState if time is being sub-cycled
438  const TimeState& prevTimeState() const
439  {
440  return prevTimeState_();
441  }
442 
443 
444  // Check
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 stopAtControls) 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
517  (
518  const dimensionedScalar&,
519  const bool adjustDeltaT = true
520  );
521 
522  //- Reset time step
523  virtual void setDeltaT
524  (
525  const scalar,
526  const bool adjustDeltaT = true
527  );
528 
529  //- Set time to sub-cycle for the given number of steps
530  virtual TimeState subCycle(const label nSubCycles);
531 
532  //- Reset time after sub-cycling back to previous TimeState
533  virtual void endSubCycle();
534 
535  //- Return non-const access to the list of function objects
537  {
538  return functionObjects_;
539  }
540 
541 
542  // Member operators
543 
544  //- Set deltaT to that specified and increment time via operator++()
545  virtual Time& operator+=(const dimensionedScalar&);
546 
547  //- Set deltaT to that specified and increment time via operator++()
548  virtual Time& operator+=(const scalar);
549 
550  //- Prefix increment,
551  // also invokes the functionObjectList::start() or
552  // functionObjectList::execute() method, depending on the time-index
553  virtual Time& operator++();
554 
555  //- Postfix increment, this is identical to the prefix increment
556  virtual Time& operator++(int);
557 };
558 
559 
560 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
561 
562 } // End namespace Foam
563 
564 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
565 
566 #endif
567 
568 // ************************************************************************* //
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:51
virtual Time & operator++()
Prefix increment,.
Definition: Time.C:981
dictionary dict
unwatchedIOdictionary is like IOdictionary but stores dependencies as files instead of fileMonitor wa...
fileName path() const
Return path.
Definition: Time.H:269
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:69
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:575
stopAtControls stopAt_
Definition: Time.H:124
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
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:137
set endTime to stop immediately w/ writing
Definition: Time.H:102
virtual ~Time()
Destructor.
Definition: Time.C:630
fileName timePath() const
Return current time path.
Definition: Time.H:285
scalar writeInterval_
Definition: Time.H:129
general distribution model
Read access to the system clock with formatting.
Definition: clock.H:50
static const fileName null
An empty fileName.
Definition: fileName.H:97
writeControls
Write control options.
Definition: Time.H:88
virtual bool loop()
Return true if run should continue and if so increment time.
Definition: Time.C:819
virtual dimensionedScalar startTime() const
Return start time.
Definition: Time.C:769
bool subCycling() const
Return true if time currently being sub-cycled, otherwise false.
Definition: Time.H:431
static fmtflags format_
Time directory name format.
Definition: Time.H:154
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: findInstance.C:82
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.
Definition: Switch.H:60
virtual dimensionedScalar endTime() const
Return end time.
Definition: Time.C:775
scalar startTime_
Definition: Time.H:120
IOstream & fixed(IOstream &io)
Definition: IOstream.H:576
writeControls writeControl_
Definition: Time.H:127
const TimeState & prevTimeState() const
Return previous TimeState if time is being sub-cycled.
Definition: Time.H:437
readOption
Enumeration defining the read options.
Definition: IOobject.H:107
virtual word timeName() const
Return current time name.
Definition: Time.C:654
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:781
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:494
IOstream::versionNumber writeVersion() const
Default write version number.
Definition: Time.H:297
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:941
static const NamedEnum< stopAtControls, 4 > stopAtControlNames_
Definition: Time.H:123
const Switch & runTimeModifiable() const
Supports re-reading.
Definition: Time.H:315
virtual void setDeltaT(const dimensionedScalar &, const bool adjustDeltaT=true)
Reset time step.
Definition: Time.C:920
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
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:263
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
static bool exists(IOobject &)
Find IOobject in the objectPath.
Definition: findInstance.C:38
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:303
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:856
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:419
Signal handler for interupt 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:409
static word controlDictName
The default control dictionary name (normally "controlDict")
Definition: Time.H:201
A table of dynamically loaded libraries.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
FIFOStack< word > previousWriteTimes_
Definition: Time.H:132
IOstream::streamFormat writeFormat() const
Default write format.
Definition: Time.H:291
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:35
const word & name() const
Return const reference to name.
static const NamedEnum< writeControls, 5 > writeControlNames_
Definition: Time.H:126
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:447
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:907
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:584
set endTime to stop immediately w/o writing
Definition: Time.H:101
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:832
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:422
const functionObjectList & functionObjects() const
Return the list of function objects.
Definition: Time.H:413
const word & graphFormat() const
Default graph format.
Definition: Time.H:309
stop when Time reaches the prescribed endTime
Definition: Time.H:100
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:660
const dictionary & controlDict() const
Definition: Time.H:274
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:701
label startTimeIndex_
Definition: Time.H:119
virtual void endSubCycle()
Reset time after sub-cycling back to previous TimeState.
Definition: Time.C:955
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: TimeIO.C:529
fmtflags
Supported time directory name formats.
Definition: Time.H:107
Signal handler for interupt 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
stop the next time data are written
Definition: Time.H:103
Starts timing CPU usage and return elapsed time from start.
Definition: cpuTime.H:52
stopAtControls
Stop-run control options.
Definition: Time.H:98
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:667
virtual label startTimeIndex() const
Return start time index.
Definition: Time.C:763
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
virtual const fileName & dbDir() const
Local directory path of this objectRegistry relative to the time.
Definition: Time.H:279
virtual bool stopAt(const stopAtControls) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:838
virtual Time & operator+=(const dimensionedScalar &)
Set deltaT to that specified and increment time via operator++()
Definition: Time.C:968
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:568
void setControls()
Set the controls from the current controlDict.
Definition: Time.C:127
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:738
const fileName & rootPath() const
Return root path.
Definition: Time.H:257