FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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"
43 #include <OpenFOAM/IOdictionary.H>
44 #include <OpenFOAM/FIFOStack.H>
45 #include <OpenFOAM/clock.H>
46 #include <OSspecific/cpuTime.H>
47 #include "TimeState.H"
48 #include <OpenFOAM/Switch.H>
49 #include <OpenFOAM/instantList.H>
50 #include <OpenFOAM/NamedEnum.H>
51 #include <OpenFOAM/typeInfo.H>
54 
55 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 
57 namespace Foam
58 {
59 
60 /*---------------------------------------------------------------------------*\
61  Class Time Declaration
62 \*---------------------------------------------------------------------------*/
63 
64 class Time
65 :
66  public clock,
67  public cpuTime,
68  public TimePaths,
69  public objectRegistry,
70  public TimeState
71 {
72  // Private data
73 
74  //- The controlDict
75  IOdictionary controlDict_;
76 
77 public:
78 
79  //- Write control options
81  {
87  };
88 
89  //- Stop-run control options
91  {
96  };
97 
98  //- Suported time directory name formats
99  enum fmtflags
100  {
101  general = 0,
104  };
105 
106 
107 protected:
108 
109  // Protected data
110 
112  scalar startTime_;
113  scalar endTime_;
114 
117 
120 
122 
123  label purgeWrite_;
125 
126  //- Is the time currently being sub-cycled?
128 
129  //- If time is being sub-cycled this is the previous TimeState
131 
132  //- Time directory name format
134 
135  //- Time directory name precision
136  static int precision_;
137 
138  //- Adjust the time step so that writing occurs at the specified time
139  void adjustDeltaT();
140 
141  //- Set the controls from the current controlDict
142  void setControls();
143 
144  //- Read the control dictionary and set the write controls etc.
145  virtual void readDict();
146 
147 
148 private:
149 
150  //- Default write option
151  IOstream::streamFormat writeFormat_;
152 
153  //- Default output file format version number
154  IOstream::versionNumber writeVersion_;
155 
156  //- Default output compression
157  IOstream::compressionType writeCompression_;
158 
159  //- Default graph format
160  word graphFormat_;
161 
162  //- Is runtime modification of dictionaries allowed?
163  Switch runTimeModifiable_;
164 
165  //- Instantiate a dummy class to cause the reading of dynamic libraries
167 
168  //- Function objects executed at start and on ++, +=
169  mutable functionObjectList functionObjects_;
170 
171 
172 public:
173 
174  TypeName("time");
175 
176  //- The default control dictionary name (normally "controlDict")
178 
179 
180  // Constructors
181 
182  //- Construct given name, rootPath and casePath
183  Time
184  (
185  const word& name,
186  const fileName& rootPath,
187  const fileName& caseName,
188  const word& systemName = "system",
189  const word& constantName = "constant"
190  );
191 
192  //- Construct given dictionary, rootPath and casePath
193  Time
194  (
195  const dictionary& dict,
196  const fileName& rootPath,
197  const fileName& caseName,
198  const word& systemName = "system",
199  const word& constantName = "constant"
200  );
201 
202  //- Construct given endTime, rootPath and casePath
203  Time
204  (
205  const fileName& rootPath,
206  const fileName& caseName,
207  const word& systemName = "system",
208  const word& constantName = "constant"
209  );
210 
211 
212  // Destructor
213 
214  //- Virtual destructor
215  virtual ~Time();
216 
217 
218  // Member functions
219 
220  // Database functions
221 
222  //- Return root path
223  const fileName& rootPath() const
224  {
225  return TimePaths::rootPath();
226  }
227 
228  //- Return case name
229  const fileName& caseName() const
230  {
231  return TimePaths::caseName();
232  }
233 
234  //- Return path
235  fileName path() const
236  {
237  return rootPath()/caseName();
238  }
239 
240  const dictionary& controlDict() const
241  {
242  return controlDict_;
243  }
244 
245  virtual const fileName& dbDir() const
246  {
247  return fileName::null;
248  }
249 
250  //- Return current time path
252  {
253  return path()/timeName();
254  }
255 
256  //- Default write format
258  {
259  return writeFormat_;
260  }
261 
262  //- Default write version number
264  {
265  return writeVersion_;
266  }
267 
268  //- Default write compression
270  {
271  return writeCompression_;
272  }
273 
274  //- Default graph format
275  const word& graphFormat() const
276  {
277  return graphFormat_;
278  }
279 
280  //- Read control dictionary, update controls and time
281  virtual bool read();
282 
283  //- Read the objects that have been modified
284  void readModifiedObjects();
285 
286  //- Return the location of "dir" containing the file "name".
287  // (eg, used in reading mesh data)
288  // If name is null, search for the directory "dir" only
290  (
291  const fileName& dir,
292  const word& name = word::null,
294  const word& stopInstance = word::null
295  ) const;
296 
297  //- Search the case for valid time directories
298  instantList times() const;
299 
300  //- Search the case for the time directory path
301  // corresponding to the given instance
302  word findInstancePath(const instant&) const;
303 
304  //- Search the case for the time closest to the given time
305  instant findClosestTime(const scalar) const;
306 
307  //- Search instantList for the time index closest to the given time
308  static label findClosestTimeIndex(const instantList&, const scalar);
309 
310  //- Write using given format, version and compression
311  virtual bool writeObject
312  (
316  ) const;
317 
318  //- Write the objects now and continue the run
319  bool writeNow();
320 
321  //- Write the objects now and end the run
322  bool writeAndEnd();
323 
324 
325  // Access
326 
327  //- Return time name of given scalar time
328  static word timeName(const scalar);
329 
330  //- Return current time name
331  virtual word timeName() const;
332 
333  //- Search a given directory for valid time directories
334  static instantList findTimes(const fileName&);
335 
336  //- Return start time index
337  virtual label startTimeIndex() const;
338 
339  //- Return start time
340  virtual dimensionedScalar startTime() const;
341 
342  //- Return end time
343  virtual dimensionedScalar endTime() const;
344 
345  //- Return the list of function objects
347  {
348  return functionObjects_;
349  }
350 
351  //- Return true if time currently being sub-cycled, otherwise false
352  bool subCycling() const
353  {
354  return subCycling_;
355  }
356 
357  //- Return previous TimeState if time is being sub-cycled
358  const TimeState& prevTimeState() const
359  {
360  return prevTimeState_();
361  }
362 
363 
364  // Check
365 
366  //- Return true if run should continue,
367  // also invokes the functionObjectList::end() method
368  // when the time goes out of range
369  // @note
370  // For correct behaviour, the following style of time-loop
371  // is recommended:
372  // @code
373  // while (runTime.run())
374  // {
375  // runTime++;
376  // solve;
377  // runTime.write();
378  // }
379  // @endcode
380  virtual bool run() const;
381 
382  //- Return true if run should continue and if so increment time
383  // also invokes the functionObjectList::end() method
384  // when the time goes out of range
385  // @note
386  // For correct behaviour, the following style of time-loop
387  // is recommended:
388  // @code
389  // while (runTime.loop())
390  // {
391  // solve;
392  // runTime.write();
393  // }
394  // @endcode
395  virtual bool loop();
396 
397  //- Return true if end of run,
398  // does not invoke any functionObject methods
399  // @note
400  // The rounding heuristics near endTime mean that
401  // @code run() @endcode and @code !end() @endcode may
402  // not yield the same result
403  virtual bool end() const;
404 
405 
406  // Edit
407 
408  //- Reset the time and time-index to those of the given time
409  virtual void setTime(const Time&);
410 
411  //- Reset the time and time-index
412  virtual void setTime(const instant&, const label newIndex);
413 
414  //- Reset the time and time-index
415  virtual void setTime
416  (
417  const dimensionedScalar&,
418  const label newIndex
419  );
420 
421  //- Reset the time and time-index
422  virtual void setTime(const scalar, const label newIndex);
423 
424  //- Reset end time
425  virtual void setEndTime(const dimensionedScalar&);
426 
427  //- Reset end time
428  virtual void setEndTime(const scalar);
429 
430  //- Reset time step
431  virtual void setDeltaT(const dimensionedScalar&);
432 
433  //- Reset time step
434  virtual void setDeltaT(const scalar);
435 
436  //- Set time to sub-cycle for the given number of steps
437  virtual TimeState subCycle(const label nSubCycles);
438 
439  //- Reset time after sub-cycling back to previous TimeState
440  virtual void endSubCycle();
441 
442  //- Return non-const access to the list of function objects
444  {
445  return functionObjects_;
446  }
447 
448 
449  // Member operators
450 
451  //- Set deltaT to that specified and increment time via operator++()
452  virtual Time& operator+=(const dimensionedScalar&);
453 
454  //- Set deltaT to that specified and increment time via operator++()
455  virtual Time& operator+=(const scalar);
456 
457  //- Prefix increment,
458  // also invokes the functionObjectList::start() or
459  // functionObjectList::execute() method, depending on the time-index
460  virtual Time& operator++();
461 
462  //- Postfix increment, this is identical to the prefix increment
463  virtual Time& operator++(int);
464 };
465 
466 
467 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
468 
469 } // End namespace Foam
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 #endif
474 
475 // ************************ vim: set sw=4 sts=4 et: ************************ //