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-2016 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 
211  if (controlDict_.readIfPresent("writeInterval", writeInterval_))
212  {
214  {
215  FatalIOErrorInFunction(controlDict_)
216  << "writeInterval < 1 for writeControl timeStep"
217  << exit(FatalIOError);
218  }
219  }
220  else
221  {
222  controlDict_.lookup("writeFrequency") >> writeInterval_;
223  }
224 
225 
226  if (oldWriteInterval != writeInterval_)
227  {
228  switch (writeControl_)
229  {
230  case wcRunTime:
231  case wcAdjustableRunTime:
232  // Recalculate writeTimeIndex_ to be in units of current
233  // writeInterval.
235  (
237  * oldWriteInterval
239  );
240  break;
241 
242  default:
243  break;
244  }
245  }
246 
247  if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
248  {
249  if (purgeWrite_ < 0)
250  {
252  << "invalid value for purgeWrite " << purgeWrite_
253  << ", should be >= 0, setting to 0"
254  << endl;
255 
256  purgeWrite_ = 0;
257  }
258  }
259 
260  if (controlDict_.found("timeFormat"))
261  {
262  const word formatName(controlDict_.lookup("timeFormat"));
263 
264  if (formatName == "general")
265  {
266  format_ = general;
267  }
268  else if (formatName == "fixed")
269  {
270  format_ = fixed;
271  }
272  else if (formatName == "scientific")
273  {
275  }
276  else
277  {
279  << "unsupported time format " << formatName
280  << endl;
281  }
282  }
283 
284  controlDict_.readIfPresent("timePrecision", precision_);
285 
286  // stopAt at 'endTime' or a specified value
287  // if nothing is specified, the endTime is zero
288  if (controlDict_.found("stopAt"))
289  {
290  stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
291 
292  if (stopAt_ == saEndTime)
293  {
294  controlDict_.lookup("endTime") >> endTime_;
295  }
296  else
297  {
298  endTime_ = GREAT;
299  }
300  }
301  else if (!controlDict_.readIfPresent("endTime", endTime_))
302  {
303  endTime_ = 0;
304  }
305 
307 
308  if (controlDict_.found("writeVersion"))
309  {
310  writeVersion_ = IOstream::versionNumber
311  (
312  controlDict_.lookup("writeVersion")
313  );
314  }
315 
316  if (controlDict_.found("writeFormat"))
317  {
318  writeFormat_ = IOstream::formatEnum
319  (
320  controlDict_.lookup("writeFormat")
321  );
322  }
323 
324  if (controlDict_.found("writePrecision"))
325  {
327  (
328  readUint(controlDict_.lookup("writePrecision"))
329  );
330 
333 
336 
337  FatalError().precision(IOstream::defaultPrecision());
338  FatalIOError.error::operator()().precision
339  (
341  );
342  }
343 
344  if (controlDict_.found("writeCompression"))
345  {
346  writeCompression_ = IOstream::compressionEnum
347  (
348  controlDict_.lookup("writeCompression")
349  );
350  }
351 
352  controlDict_.readIfPresent("graphFormat", graphFormat_);
353  controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
354 
355  if (!runTimeModifiable_ && controlDict_.watchIndex() != -1)
356  {
357  removeWatch(controlDict_.watchIndex());
358  controlDict_.watchIndex() = -1;
359  }
360 }
361 
362 
364 {
365  if (controlDict_.regIOobject::read())
366  {
367  readDict();
368  return true;
369  }
370  else
371  {
372  return false;
373  }
374 }
375 
376 
378 {
379  if (runTimeModifiable_)
380  {
381  // Get state of all monitored objects (=registered objects with a
382  // valid filePath).
383  // Note: requires same ordering in objectRegistries on different
384  // processors!
385  monitorPtr_().updateStates
386  (
387  (
390  ),
392  );
393 
394  // Time handling is special since controlDict_ is the one dictionary
395  // that is not registered to any database.
396 
397  if (controlDict_.readIfModified())
398  {
399  readDict();
400  functionObjects_.read();
401  }
402 
403  bool registryModified = objectRegistry::modified();
404 
405  if (registryModified)
406  {
408  }
409  }
410 }
411 
412 
414 {
415  const word tmName(timeName());
416 
417  IOdictionary timeDict
418  (
419  IOobject
420  (
421  "time",
422  tmName,
423  "uniform",
424  *this,
427  false
428  )
429  );
430 
431  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
432  timeDict.add("name", string(tmName));
433  timeDict.add("index", timeIndex_);
434  timeDict.add("deltaT", timeToUserTime(deltaT_));
435  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
436 
437  return timeDict.regIOobject::writeObject
438  (
442  );
443 }
444 
445 
447 (
451 ) const
452 {
453  if (writeTime())
454  {
455  bool writeOK = writeTimeDict();
456 
457  if (writeOK)
458  {
459  writeOK = objectRegistry::writeObject(fmt, ver, cmp);
460  }
461 
462  if (writeOK)
463  {
464  // Does the writeTime trigger purging?
465  if (writeTime_ && purgeWrite_)
466  {
468 
469  while (previousWriteTimes_.size() > purgeWrite_)
470  {
472  }
473  }
474  }
475 
476  return writeOK;
477  }
478  else
479  {
480  return false;
481  }
482 }
483 
484 
486 {
487  writeTime_ = true;
488  return write();
489 }
490 
491 
493 {
495  endTime_ = value();
496 
497  return writeNow();
498 }
499 
500 
502 {
503  writeOnce_ = true;
504 }
505 
506 
507 // ************************************************************************* //
bool read()
Read and set the function objects if their data have changed.
string str() const
Return the string.
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
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
scalar deltaT_
Definition: TimeState.H:56
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:492
stopAtControls stopAt_
Definition: Time.H:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void readModifiedObjects()
Read the objects that have been modified.
Dictionary reading and supplying the dimensioned constants used within OpenFOAM, particularly for the...
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual word timeName() const
Return current time name.
Definition: Time.C:725
set endTime to stop immediately w/ writing
Definition: Time.H:105
scalar writeInterval_
Definition: Time.H:132
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:461
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:633
label watchIndex() const
Return file-monitoring handle.
Definition: regIOobjectI.H:91
static fmtflags format_
Time directory name format.
Definition: Time.H:157
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const scalar & value() const
Return const reference to value.
OSstream Serr(cerr,"Serr")
Definition: IOstreams.H:52
virtual scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: TimeState.C:58
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:313
bool writeTime() const
Return true if this is a write time.
Definition: TimeStateI.H:65
writeControls writeControl_
Definition: Time.H:130
label writeTimeIndex_
Definition: TimeState.H:60
dictionary & dimensionedConstants()
bool removeWatch(const label) const
Remove watch on a file (using handle)
Definition: Time.C:689
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:335
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:737
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:163
OSstream Sout(cout,"Sout")
Definition: IOstreams.H:51
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:285
scalar deltaT0_
Definition: TimeState.H:58
fileName path() const
Return complete path.
Definition: IOobject.C:275
static fileCheckTypes fileModificationChecking
Definition: regIOobject.H:128
const word & name() const
Return const reference to name.
label purgeWrite_
Definition: Time.H:134
static const NamedEnum< stopAtControls, 4 > stopAtControlNames_
Definition: Time.H:126
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
static int precision_
Time directory name precision.
Definition: Time.H:160
A class for handling words, derived from string.
Definition: word.H:59
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp) const
Write the objects.
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
virtual bool readIfModified()
Read object if modified (as set by call to modified)
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
scalar endTime_
Definition: Time.H:124
const word & name() const
Name function is needed to disambiguate those inherited.
Definition: IOdictionary.C:181
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
bool rmDir(const fileName &)
Remove a dirctory and its contents.
Definition: POSIX.C:839
prefixOSstream Pout(cout,"Pout")
Definition: IOstreams.H:53
FIFOStack< word > previousWriteTimes_
Definition: Time.H:135
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType) const
Write using given format, version and compression.
Definition: TimeIO.C:447
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:33
static const NamedEnum< writeControls, 5 > writeControlNames_
Definition: Time.H:129
prefixOSstream Perr(cerr,"Perr")
Definition: IOstreams.H:54
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:377
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:346
bool deltaTchanged_
Definition: TimeState.H:59
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:501
uint readUint(Istream &)
Definition: uintIO.C:31
dictionary & dimensionSystems()
Top level dictionary.
Definition: dimensionSets.C:79
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:393
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:363
#define WarningInFunction
Report a warning using Foam::Warning.
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Object registry for simpleRegIOobject. Maintains ordering.
stop when Time reaches the prescribed endTime
Definition: Time.H:103
bool writeOnce_
Definition: Time.H:138
Input from memory buffer stream.
Definition: IStringStream.H:49
simpleObjectRegistry & dimensionedConstantObjects()
Get access to registered dimensionedConstant switch objects.
Definition: debug.C:357
Version number type.
Definition: IOstream.H:96
virtual bool write() const
Write using setting from DB.
messageStream Info
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1013
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:413
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
Output to memory buffer stream.
Definition: OStringStream.H:49
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:120
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:485
label timeIndex_
Definition: TimeState.H:55
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451
IOerror FatalIOError