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  {
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  (
238  (
239  "secondaryWriteInterval",
241  )
242  )
243  {
244  if
245  (
248  )
249  {
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  {
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  {
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  {
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  {
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  {
384  (
385  controlDict_.lookup("writeVersion")
386  );
387  }
388 
389  if (controlDict_.found("writeFormat"))
390  {
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  {
420  (
421  controlDict_.lookup("writeCompression")
422  );
423  }
424 
425  controlDict_.readIfPresent("graphFormat", graphFormat_);
426  controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
427 
429  {
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  (
462  || regIOobject::fileModificationChecking == timeStampMaster
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.
526  if (primaryOutputTime_ && purgeWrite_)
527  {
528  previousOutputTimes_.push(tmName);
529 
530  while (previousOutputTimes_.size() > purgeWrite_)
531  {
532  rmDir(objectRegistry::path(previousOutputTimes_.pop()));
533  }
534  }
535  if
536  (
537  !primaryOutputTime_
538  && secondaryOutputTime_
539  && secondaryPurgeWrite_
540  )
541  {
542  // Writing due to secondary
543  previousSecondaryOutputTimes_.push(tmName);
544 
545  while
546  (
547  previousSecondaryOutputTimes_.size()
548  > secondaryPurgeWrite_
549  )
550  {
551  rmDir
552  (
554  (
555  previousSecondaryOutputTimes_.pop()
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 {
581  stopAt_ = saWriteNow;
582  endTime_ = value();
583 
584  return writeNow();
585 }
586 
587 
589 {
590  writeOnce_ = true;
591 }
592 
593 
594 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::Time::format_
static fmtflags format_
Time directory name format.
Definition: Time.H:169
Foam::Time::wcRunTime
@ wcRunTime
Definition: Time.H:94
Foam::Time::general
@ general
Definition: Time.H:112
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::Time::saEndTime
@ saEndTime
Definition: Time.H:103
Foam::Time::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Definition: TimeIO.C:487
Foam::Time::wcTimeStep
@ wcTimeStep
Definition: Time.H:93
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::Time::purgeWrite_
label purgeWrite_
Definition: Time.H:140
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Time.H
Foam::Time::secondaryPurgeWrite_
label secondaryPurgeWrite_
Definition: Time.H:145
Foam::IOstream::compressionType
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Foam::regIOobject::watchIndex
label watchIndex() const
Return file-monitoring handle.
Definition: regIOobjectI.H:91
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::DictionaryBase::lookupPtr
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
Foam::Perr
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
simpleObjectRegistry.H
Foam::dimensioned::name
const word & name() const
Return const reference to name.
Definition: dimensionedType.C:235
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::setEnv
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:119
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:261
Foam::Time::writeOnce
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:588
Foam::Time::wcAdjustableRunTime
@ wcAdjustableRunTime
Definition: Time.H:95
Foam::Time::secondaryWriteInterval_
scalar secondaryWriteInterval_
Definition: Time.H:138
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
Foam::Time::read
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:436
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::Time::writeAndEnd
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:579
Foam::IOstream::versionNumber
Version number type.
Definition: IOstream.H:96
Foam::simpleObjectRegistryEntry
Definition: simpleObjectRegistry.H:48
Foam::Time::timeName
virtual word timeName() const
Return current time name.
Definition: Time.C:751
Foam::IOstream::ASCII
@ ASCII
Definition: IOstream.H:88
Foam::Time::scientific
@ scientific
Definition: Time.H:114
Foam::debug::optimisationObjects
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:333
Foam::readUint
uint readUint(Istream &)
Definition: uintIO.C:31
Foam::Time::graphFormat_
word graphFormat_
Default graph format.
Definition: Time.H:199
Foam::label
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
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::IOobject::NO_READ
@ NO_READ
Definition: IOobject.H:111
Foam::Info
messageStream Info
Foam::debug::debugObjects
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:311
Foam::dimensionSystems
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:79
Foam::Serr
OSstream Serr(cerr, "Serr")
Definition: IOstreams.H:52
Foam::dictionary::merge
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1005
Foam::Time::stopAtControlNames_
static const NamedEnum< stopAtControls, 4 > stopAtControlNames_
Definition: Time.H:126
Foam::Time::fixed
@ fixed
Definition: Time.H:113
Foam::IOstream::formatEnum
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
Foam::debug::dimensionSetObjects
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:344
Foam::Time::stopAt_
stopAtControls stopAt_
Definition: Time.H:127
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::Time::secondaryWriteControl_
writeControls secondaryWriteControl_
Definition: Time.H:136
Foam::Time::runTimeModifiable_
Switch runTimeModifiable_
Is runtime modification of dictionaries allowed?
Definition: Time.H:202
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::Time::writeCompression_
IOstream::compressionType writeCompression_
Default output compression.
Definition: Time.H:196
Foam::Time::controlDict_
IOdictionary controlDict_
The controlDict.
Definition: Time.H:86
Pstream.H
Foam::debug::dimensionedConstantObjects
simpleObjectRegistry & dimensionedConstantObjects()
Get access to registered dimensionedConstant switch objects.
Definition: debug.C:355
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam::TimeState::secondaryOutputTimeIndex_
label secondaryOutputTimeIndex_
Definition: TimeState.H:66
Foam::IOstream::compressionEnum
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
Foam::Time::precision_
static int precision_
Time directory name precision.
Definition: Time.H:172
Foam::TimeState::outputTimeIndex_
label outputTimeIndex_
Definition: TimeState.H:62
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::Time::writeFormat_
IOstream::streamFormat writeFormat_
Default write option.
Definition: Time.H:190
Foam::objectRegistry::modified
virtual bool modified() const
Return true if any of the object's files have been modified.
Definition: objectRegistry.C:293
Foam::objectRegistry::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: objectRegistry.C:307
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:290
Foam::regIOobject::fileModificationChecking
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
Foam::Time::readDict
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:33
Foam::NamedEnum::read
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
Foam::Time::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:450
Foam::IOdictionary::name
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:181
Foam::readScalar
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
Foam::IOstream::defaultPrecision
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::objectRegistry::writeObject
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write the objects.
Definition: objectRegistry.C:331
Foam::List< simpleRegIOobject * >
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
Foam::Time::writeInterval_
scalar writeInterval_
Definition: Time.H:132
Foam::Time::removeWatch
bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: Time.C:715
Foam::Time::writeNow
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:571
Foam::Time::writeControlNames_
static const NamedEnum< writeControls, 5 > writeControlNames_
Definition: Time.H:129
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::Time::writeVersion_
IOstream::versionNumber writeVersion_
Default output file format version number.
Definition: Time.H:193
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::dimensionedConstants
dictionary & dimensionedConstants()
Definition: dimensionedConstants.C:37
Foam::Time::endTime_
scalar endTime_
Definition: Time.H:124
Foam::Sout
OSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::IOobject::path
fileName path() const
Return complete path.
Definition: IOobject.C:293
Foam::Time::writeControl_
writeControls writeControl_
Definition: Time.H:130
Foam::TimeState::deltaT_
scalar deltaT_
Definition: TimeState.H:57
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::rmDir
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:974
Foam::TimeState::deltaTchanged_
bool deltaTchanged_
Definition: TimeState.H:60
write
Tcoeff write()
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
dimensionedConstants.H
Dictionary reading and supplying the dimensioned constants used within OpenFOAM, particularly for the...
Foam::simpleObjectRegistry
Object registry for simpleRegIOobject. Maintains ordering.
Definition: simpleObjectRegistry.H:66
Foam::IOstream::streamFormat
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86