TimeIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2022 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 "timeIOdictionary.H"
28 #include "OSspecific.H"
29 
30 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
31 
33 {
34  word application;
35  if (controlDict_.readIfPresent("application", application))
36  {
37  // Do not override if already set so external application can override
38  setEnv("FOAM_APPLICATION", application, false);
39  }
40 
41  if (!deltaTchanged_)
42  {
43  deltaT_ = userTimeToTime(controlDict_.lookup<scalar>("deltaT"));
44  }
45 
46  if (controlDict_.found("writeControl"))
47  {
49  (
50  controlDict_.lookup("writeControl")
51  );
52  }
53 
54  scalar newWriteInterval = writeInterval_;
55 
56  if (controlDict_.readIfPresent("writeInterval", newWriteInterval))
57  {
58  if
59  (
62  )
63  {
64  newWriteInterval = userTimeToTime(newWriteInterval);
65  }
66 
67  if
68  (
70  && label(newWriteInterval) < 1
71  )
72  {
73  FatalIOErrorInFunction(controlDict_)
74  << "writeInterval < 1 for writeControl timeStep"
75  << exit(FatalIOError);
76  }
77  }
78  else
79  {
80  controlDict_.lookup("writeFrequency") >> newWriteInterval;
81  }
82 
83  setWriteInterval(newWriteInterval);
84 
85  if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
86  {
87  if (purgeWrite_ < 0)
88  {
90  << "invalid value for purgeWrite " << purgeWrite_
91  << ", should be >= 0, setting to 0"
92  << endl;
93 
94  purgeWrite_ = 0;
95  }
96  }
97 
98  if (controlDict_.found("timeFormat"))
99  {
100  const word formatName(controlDict_.lookup("timeFormat"));
101 
102  if (formatName == "general")
103  {
105  }
106  else if (formatName == "fixed")
107  {
109  }
110  else if (formatName == "scientific")
111  {
113  }
114  else
115  {
117  << "unsupported time format " << formatName
118  << endl;
119  }
120  }
121 
122  controlDict_.readIfPresent("timePrecision", precision_);
124 
125  // stopAt at 'endTime' or a specified value
126  // if nothing is specified, the endTime is zero
127  if (controlDict_.found("stopAt"))
128  {
129  stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
130 
132  {
133  endTime_ = userTimeToTime(controlDict_.lookup<scalar>("endTime"));
134  }
135  else
136  {
137  endTime_ = great;
138  }
139  }
140  else if (!controlDict_.readIfPresent("endTime", endTime_))
141  {
142  endTime_ = 0;
143  }
144 
146 
147  if (controlDict_.found("writeVersion"))
148  {
149  writeVersion_ = IOstream::versionNumber
150  (
151  controlDict_.lookup("writeVersion")
152  );
153  }
154 
155  if (controlDict_.found("writeFormat"))
156  {
157  writeFormat_ = IOstream::formatEnum
158  (
159  controlDict_.lookup("writeFormat")
160  );
161  }
162 
163  if (controlDict_.found("writePrecision"))
164  {
166  (
167  controlDict_.lookup<unsigned int>("writePrecision")
168  );
169 
172 
175 
176  FatalError().precision(IOstream::defaultPrecision());
177  FatalIOError.error::operator()().precision
178  (
180  );
181  }
182 
183  if (controlDict_.found("writeCompression"))
184  {
185  writeCompression_ = IOstream::compressionEnum
186  (
187  controlDict_.lookup("writeCompression")
188  );
189 
190  if
191  (
192  writeFormat_ == IOstream::BINARY
193  && writeCompression_ == IOstream::COMPRESSED
194  )
195  {
196  IOWarningInFunction(controlDict_)
197  << "Selecting compressed binary is inefficient and ineffective"
198  ", resetting to uncompressed binary"
199  << endl;
200 
201  writeCompression_ = IOstream::UNCOMPRESSED;
202  }
203  }
204 
205  controlDict_.readIfPresent("graphFormat", graphFormat_);
206  controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
207 
208  userTime_->read(controlDict_);
209 }
210 
211 
213 {
214  if (controlDict_.regIOobject::read())
215  {
216  readDict();
217 
218  return true;
219  }
220  else
221  {
222  return false;
223  }
224 }
225 
226 
228 {
229  if (runTimeModifiable_)
230  {
231  // Get state of all monitored objects (=registered objects with a
232  // valid filePath).
233  // Note: requires same ordering in objectRegistries on different
234  // processors!
236  (
237  (
240  ),
242  );
243 
244  // Time handling is special since controlDict_ is the one dictionary
245  // that is not registered to any database.
246 
247  if (controlDict_.readIfModified())
248  {
249  readDict();
250  functionObjects_.read();
251  }
252 
253  bool registryModified = objectRegistry::modified();
254 
255  if (registryModified)
256  {
258  }
259  }
260 }
261 
262 
264 {
265  const word tmName(timeName());
266 
267  timeIOdictionary timeDict
268  (
269  IOobject
270  (
271  "time",
272  tmName,
273  "uniform",
274  *this,
277  false
278  )
279  );
280 
281  timeDict.add("beginTime", timeToUserTime(beginTime_));
282  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
283  timeDict.add("name", string(tmName));
284  timeDict.add("index", timeIndex_);
285  timeDict.add("deltaT", timeToUserTime(deltaT_));
286  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
287 
288  return timeDict.regIOobject::writeObject
289  (
293  true
294  );
295 }
296 
297 
299 (
303  const bool write
304 ) const
305 {
306  if (writeTime())
307  {
308  bool writeOK = writeTimeDict();
309 
310  if (writeOK)
311  {
312  writeOK = objectRegistry::writeObject(fmt, ver, cmp, write);
313  }
314 
315  if (writeOK)
316  {
317  // Does the writeTime trigger purging?
318  if (writeTime_ && purgeWrite_)
319  {
320  if
321  (
322  previousWriteTimes_.size() == 0
323  || previousWriteTimes_.top() != timeName()
324  )
325  {
327  }
328 
329  while (previousWriteTimes_.size() > purgeWrite_)
330  {
332  (
334  (
336  )
337  );
338  }
339  }
340  }
341 
342  return writeOK;
343  }
344  else
345  {
346  return false;
347  }
348 }
349 
350 
352 {
353  writeTime_ = true;
354  return write();
355 }
356 
357 
359 {
361  endTime_ = value();
362 
363  return writeNow();
364 }
365 
366 
368 {
369  writeOnce_ = true;
370 }
371 
372 
373 // ************************************************************************* //
bool read()
Read and set the function objects if their data have changed.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
virtual bool rmDir(const fileName &) const =0
Remove a directory and its contents.
stop when Time reaches the prescribed endTime
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
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:358
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
writeControl writeControl_
Definition: Time.H:132
void readModifiedObjects()
Read the objects that have been modified.
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:246
error FatalError
scalar writeInterval_
Definition: Time.H:134
set endTime to stop immediately w/ writing
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
virtual bool modified() const
Return true if any of the object&#39;s files have been modified.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:437
virtual word timeName() const
Return current time name.
Definition: Time.C:676
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1153
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:169
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:263
bool writeTime() const
Return true if this is a write time.
Definition: TimeStateI.H:58
virtual void updateStates(const bool masterOnly, const bool syncPar) const
Update state of all files.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
label purgeWrite_
Definition: Time.H:136
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:299
static int precision_
Time directory name precision.
Definition: Time.H:161
A class for handling words, derived from string.
Definition: word.H:59
virtual bool readIfModified()
Read object if modified (as set by call to modified)
static int curPrecision_
Current time directory name precision adjusted as necessary.
Definition: Time.H:166
const scalar & value() const
Return const reference to value.
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
scalar endTime_
Definition: Time.H:122
const fileOperation & fileHandler()
Get current file handler.
OSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
static format format_
Time directory name format.
Definition: Time.H:158
scalar beginTime_
Definition: Time.H:123
FIFOStack< word > previousWriteTimes_
Definition: Time.H:137
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:32
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:231
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:128
const word & name() const
Return const reference to name.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:227
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:367
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:131
timeIOdictionary derived from IOdictionary with globalFile set false to enable writing to processor t...
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:212
#define WarningInFunction
Report a warning using Foam::Warning.
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
bool writeOnce_
Definition: Time.H:140
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Version number type.
Definition: IOstream.H:96
OSstream Serr(cerr, "Serr")
Definition: IOstreams.H:52
stopAtControl stopAt_
Definition: Time.H:129
scalar userTimeToTime(const scalar tau) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: Time.C:851
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:1055
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: Time.C:857
virtual bool write(const bool write=true) const
Write using setting from DB.
fileName path() const
Return complete path.
Definition: regIOobject.C:199
autoPtr< userTimes::userTime > userTime_
Optional user-time, defaults to realTime in s.
Definition: Time.H:126
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:98
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:115
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:351
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864
IOerror FatalIOError