TimeIO.C
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-2015 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 \*---------------------------------------------------------------------------*/
25 
26 #include "Time.H"
27 #include "Pstream.H"
28 #include "simpleObjectRegistry.H"
29 #include "dimensionedConstants.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
34 {
35  word application;
36  if (controlDict_.readIfPresent("application", application))
37  {
38  // Do not override if already set so external application can override
39  setEnv("FOAM_APPLICATION", application, false);
40  }
41 
42 
43  // Check for local switches and settings
44  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45 
46  // Debug switches
47  if (controlDict_.found("DebugSwitches"))
48  {
49  Info<< "Overriding DebugSwitches according to " << controlDict_.name()
50  << endl;
51 
53  const dictionary& localSettings = controlDict_.subDict("DebugSwitches");
54  forAllConstIter(dictionary, localSettings, iter)
55  {
56  const word& name = iter().keyword();
57 
58  simpleObjectRegistryEntry* objPtr = objects.lookupPtr(name);
59 
60  if (objPtr)
61  {
62  Info<< " " << iter() << endl;
63 
64  const List<simpleRegIOobject*>& objects = *objPtr;
65 
66  if (iter().isDict())
67  {
68  forAll(objects, i)
69  {
71  os << iter().dict();
72  IStringStream is(os.str());
73  objects[i]->readData(is);
74  }
75  }
76  else
77  {
78  forAll(objects, i)
79  {
80  objects[i]->readData(iter().stream());
81  }
82  }
83  }
84  }
85  }
86 
87  // Optimisation Switches
88  if (controlDict_.found("OptimisationSwitches"))
89  {
90  Info<< "Overriding OptimisationSwitches according to "
91  << controlDict_.name() << endl;
92 
94  const dictionary& localSettings = controlDict_.subDict
95  (
96  "OptimisationSwitches"
97  );
98  forAllConstIter(dictionary, localSettings, iter)
99  {
100  const word& name = iter().keyword();
101 
102  simpleObjectRegistryEntry* objPtr = objects.lookupPtr(name);
103 
104  if (objPtr)
105  {
106  Info<< " " << iter() << endl;
107 
108  const List<simpleRegIOobject*>& objects = *objPtr;
109 
110  if (iter().isDict())
111  {
112  forAll(objects, i)
113  {
115  os << iter().dict();
116  IStringStream is(os.str());
117  objects[i]->readData(is);
118  }
119  }
120  else
121  {
122  forAll(objects, i)
123  {
124  objects[i]->readData(iter().stream());
125  }
126  }
127  }
128  }
129  }
130 
131 
132  // DimensionedConstants. Handled as a special case since both e.g.
133  // the 'unitSet' might be changed and the individual values
134  if (controlDict_.found("DimensionedConstants"))
135  {
136  Info<< "Overriding DimensionedConstants according to "
137  << controlDict_.name() << endl;
138 
139  // Change in-memory
141  (
142  controlDict_.subDict("DimensionedConstants")
143  );
144 
145 
147 
148  IStringStream dummyIs("");
149 
150  forAllConstIter(simpleObjectRegistry, objects, iter)
151  {
152  const List<simpleRegIOobject*>& objects = *iter;
153 
154  forAll(objects, i)
155  {
156  objects[i]->readData(dummyIs);
157 
158  Info<< " ";
159  objects[i]->writeData(Info);
160  Info<< endl;
161  }
162  }
163  }
164 
165 
166  // Dimension sets
167  if (controlDict_.found("DimensionSets"))
168  {
169  Info<< "Overriding DimensionSets according to "
170  << controlDict_.name() << endl;
171 
173  dict.merge(controlDict_.subDict("DimensionSets"));
174 
176 
177  simpleObjectRegistryEntry* objPtr = objects.lookupPtr("DimensionSets");
178 
179  if (objPtr)
180  {
181  Info<< controlDict_.subDict("DimensionSets") << endl;
182 
183  const List<simpleRegIOobject*>& objects = *objPtr;
184 
185  forAll(objects, i)
186  {
188  os << dict;
189  IStringStream is(os.str());
190  objects[i]->readData(is);
191  }
192  }
193  }
194 
195 
196  if (!deltaTchanged_)
197  {
198  deltaT_ = readScalar(controlDict_.lookup("deltaT"));
199  }
200 
201  if (controlDict_.found("writeControl"))
202  {
204  (
205  controlDict_.lookup("writeControl")
206  );
207  }
208 
209  scalar oldWriteInterval = writeInterval_;
210  scalar oldSecondaryWriteInterval = secondaryWriteInterval_;
211 
212  if (controlDict_.readIfPresent("writeInterval", writeInterval_))
213  {
215  {
216  FatalIOErrorIn("Time::readDict()", controlDict_)
217  << "writeInterval < 1 for writeControl timeStep"
218  << exit(FatalIOError);
219  }
220  }
221  else
222  {
223  controlDict_.lookup("writeFrequency") >> writeInterval_;
224  }
225 
226 
227  // Additional writing
228  if (controlDict_.found("secondaryWriteControl"))
229  {
231  (
232  controlDict_.lookup("secondaryWriteControl")
233  );
234 
235  if
236  (
237  controlDict_.readIfPresent
238  (
239  "secondaryWriteInterval",
241  )
242  )
243  {
244  if
245  (
248  )
249  {
250  FatalIOErrorIn("Time::readDict()", controlDict_)
251  << "secondaryWriteInterval < 1"
252  << " for secondaryWriteControl timeStep"
253  << exit(FatalIOError);
254  }
255  }
256  else
257  {
258  controlDict_.lookup("secondaryWriteFrequency")
260  }
261  }
262 
263 
264 
265  if (oldWriteInterval != writeInterval_)
266  {
267  switch (writeControl_)
268  {
269  case wcRunTime:
270  case wcAdjustableRunTime:
271  // Recalculate outputTimeIndex_ to be in units of current
272  // writeInterval.
274  (
276  * oldWriteInterval
278  );
279  break;
280 
281  default:
282  break;
283  }
284  }
285  if (oldSecondaryWriteInterval != secondaryWriteInterval_)
286  {
287  switch (secondaryWriteControl_)
288  {
289  case wcRunTime:
290  case wcAdjustableRunTime:
291  // Recalculate secondaryOutputTimeIndex_ to be in units of
292  // current writeInterval.
294  (
296  * oldSecondaryWriteInterval
298  );
299  break;
300 
301  default:
302  break;
303  }
304  }
305 
306  if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
307  {
308  if (purgeWrite_ < 0)
309  {
310  WarningIn("Time::readDict()")
311  << "invalid value for purgeWrite " << purgeWrite_
312  << ", should be >= 0, setting to 0"
313  << endl;
314 
315  purgeWrite_ = 0;
316  }
317  }
318 
319  if (controlDict_.readIfPresent("secondaryPurgeWrite", secondaryPurgeWrite_))
320  {
321  if (secondaryPurgeWrite_ < 0)
322  {
323  WarningIn("Time::readDict()")
324  << "invalid value for secondaryPurgeWrite "
326  << ", should be >= 0, setting to 0"
327  << endl;
328 
330  }
331  }
332 
333  if (controlDict_.found("timeFormat"))
334  {
335  const word formatName(controlDict_.lookup("timeFormat"));
336 
337  if (formatName == "general")
338  {
339  format_ = general;
340  }
341  else if (formatName == "fixed")
342  {
343  format_ = fixed;
344  }
345  else if (formatName == "scientific")
346  {
348  }
349  else
350  {
351  WarningIn("Time::readDict()")
352  << "unsupported time format " << formatName
353  << endl;
354  }
355  }
356 
357  controlDict_.readIfPresent("timePrecision", precision_);
358 
359  // stopAt at 'endTime' or a specified value
360  // if nothing is specified, the endTime is zero
361  if (controlDict_.found("stopAt"))
362  {
363  stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
364 
365  if (stopAt_ == saEndTime)
366  {
367  controlDict_.lookup("endTime") >> endTime_;
368  }
369  else
370  {
371  endTime_ = GREAT;
372  }
373  }
374  else if (!controlDict_.readIfPresent("endTime", endTime_))
375  {
376  endTime_ = 0;
377  }
378 
380 
381  if (controlDict_.found("writeVersion"))
382  {
383  writeVersion_ = IOstream::versionNumber
384  (
385  controlDict_.lookup("writeVersion")
386  );
387  }
388 
389  if (controlDict_.found("writeFormat"))
390  {
391  writeFormat_ = IOstream::formatEnum
392  (
393  controlDict_.lookup("writeFormat")
394  );
395  }
396 
397  if (controlDict_.found("writePrecision"))
398  {
400  (
401  readUint(controlDict_.lookup("writePrecision"))
402  );
403 
406 
409 
410  FatalError().precision(IOstream::defaultPrecision());
411  FatalIOError.error::operator()().precision
412  (
414  );
415  }
416 
417  if (controlDict_.found("writeCompression"))
418  {
419  writeCompression_ = IOstream::compressionEnum
420  (
421  controlDict_.lookup("writeCompression")
422  );
423  }
424 
425  controlDict_.readIfPresent("graphFormat", graphFormat_);
426  controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
427 
428  if (!runTimeModifiable_ && controlDict_.watchIndex() != -1)
429  {
430  removeWatch(controlDict_.watchIndex());
431  controlDict_.watchIndex() = -1;
432  }
433 }
434 
435 
437 {
438  if (controlDict_.regIOobject::read())
439  {
440  readDict();
441  return true;
442  }
443  else
444  {
445  return false;
446  }
447 }
448 
449 
451 {
452  if (runTimeModifiable_)
453  {
454  // Get state of all monitored objects (=registered objects with a
455  // valid filePath).
456  // Note: requires same ordering in objectRegistries on different
457  // processors!
458  monitorPtr_().updateStates
459  (
460  (
463  ),
465  );
466 
467  // Time handling is special since controlDict_ is the one dictionary
468  // that is not registered to any database.
469 
470  if (controlDict_.readIfModified())
471  {
472  readDict();
473  functionObjects_.read();
474  }
475 
476  bool registryModified = objectRegistry::modified();
477 
478  if (registryModified)
479  {
481  }
482  }
483 }
484 
485 
487 (
491 ) const
492 {
493  if (outputTime())
494  {
495  const word tmName(timeName());
496 
497  IOdictionary timeDict
498  (
499  IOobject
500  (
501  "time",
502  tmName,
503  "uniform",
504  *this,
507  false
508  )
509  );
510 
511  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
512  timeDict.add("name", string(tmName));
513  timeDict.add("index", timeIndex_);
514  timeDict.add("deltaT", timeToUserTime(deltaT_));
515  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
516 
517  timeDict.regIOobject::writeObject(fmt, ver, cmp);
518  bool writeOK = objectRegistry::writeObject(fmt, ver, cmp);
519 
520  if (writeOK)
521  {
522  // Does primary or secondary time trigger purging?
523  // Note that primary times can only be purged by primary
524  // purging. Secondary times can be purged by either primary
525  // or secondary purging.
527  {
528  previousOutputTimes_.push(tmName);
529 
530  while (previousOutputTimes_.size() > purgeWrite_)
531  {
533  }
534  }
535  if
536  (
540  )
541  {
542  // Writing due to secondary
543  previousSecondaryOutputTimes_.push(tmName);
544 
545  while
546  (
549  )
550  {
551  rmDir
552  (
554  (
556  )
557  );
558  }
559  }
560  }
561 
562  return writeOK;
563  }
564  else
565  {
566  return false;
567  }
568 }
569 
570 
572 {
573  primaryOutputTime_ = true;
574  outputTime_ = true;
575  return write();
576 }
577 
578 
580 {
582  endTime_ = value();
583 
584  return writeNow();
585 }
586 
587 
589 {
590  writeOnce_ = true;
591 }
592 
593 
594 // ************************************************************************* //
Input from memory buffer stream.
Definition: IStringStream.H:49
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:380
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:450
FIFOStack< word > previousOutputTimes_
Definition: Time.H:141
writeControls secondaryWriteControl_
Definition: Time.H:136
bool secondaryOutputTime_
Is outputTime because of secondary?
Definition: TimeState.H:68
OSstream Sout(cout,"Sout")
Definition: IOstreams.H:51
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write the objects.
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:975
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:436
static int precision_
Time directory name precision.
Definition: Time.H:172
static const NamedEnum< stopAtControls, 4 > stopAtControlNames_
Definition: Time.H:126
FIFOStack< word > previousSecondaryOutputTimes_
Definition: Time.H:146
static fmtflags format_
Time directory name format.
Definition: Time.H:169
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:175
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:120
bool writeOnce_
Definition: Time.H:150
string str() const
Return the string.
A class for handling words, derived from string.
Definition: word.H:59
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:571
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:79
prefixOSstream Perr(cerr,"Perr")
Definition: IOstreams.H:54
label secondaryOutputTimeIndex_
Definition: TimeState.H:66
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:33
messageStream Info
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:579
scalar writeInterval_
Definition: Time.H:132
static const NamedEnum< writeControls, 5 > writeControlNames_
Definition: Time.H:129
bool deltaTchanged_
Definition: TimeState.H:60
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1017
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:638
scalar secondaryWriteInterval_
Definition: Time.H:138
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:183
virtual bool read()
Read and set the function objects if their data have changed.
OSstream Serr(cerr,"Serr")
Definition: IOstreams.H:52
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
virtual bool write() const
Write using setting from DB.
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
label outputTimeIndex_
Definition: TimeState.H:62
dictionary dict
fileName path() const
Return complete path.
Definition: IOobject.C:299
virtual scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: TimeState.C:61
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
bool primaryOutputTime_
Is outputTime because of primary?
Definition: TimeState.H:64
IOerror FatalIOError
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
#define WarningIn(functionName)
Report a warning using Foam::Warning.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:290
const word & name() const
Return const reference to name.
virtual bool readIfModified()
Read object if modified (as set by call to modified)
label timeIndex_
Definition: TimeState.H:56
bool outputTime_
Definition: TimeState.H:70
#define forAll(list, i)
Definition: UList.H:421
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
label purgeWrite_
Definition: Time.H:140
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Dictionary reading and supplying the dimensioned constants used within OpenFOAM, particularly for the...
Output to memory buffer stream.
Definition: OStringStream.H:49
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Definition: TimeIO.C:487
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:739
void readModifiedObjects()
Read the objects that have been modified.
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
label watchIndex() const
Return file-monitoring handle.
Definition: regIOobjectI.H:93
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:312
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
dictionary & dimensionedConstants()
scalar deltaT0_
Definition: TimeState.H:59
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
error FatalError
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:334
scalar endTime_
Definition: Time.H:124
bool outputTime() const
Return true if this is an output time (primary or secondary)
Definition: TimeState.C:91
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:588
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
writeControls writeControl_
Definition: Time.H:130
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:345
Object registry for simpleRegIOobject. Maintains ordering.
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
Version number type.
Definition: IOstream.H:96
uint readUint(Istream &)
Definition: uintIO.C:31
stopAtControls stopAt_
Definition: Time.H:127
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
label secondaryPurgeWrite_
Definition: Time.H:145
scalar deltaT_
Definition: TimeState.H:57
const scalar & value() const
Return const reference to value.
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
simpleObjectRegistry & dimensionedConstantObjects()
Get access to registered dimensionedConstant switch objects.
Definition: debug.C:356
bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: Time.C:715
virtual word timeName() const
Return current time name.
Definition: Time.C:751