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-2023 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("runTimeModifiable", runTimeModifiable_);
206 
207  userTime_->read(controlDict_);
208 }
209 
210 
212 {
213  if (controlDict_.regIOobject::read())
214  {
215  readDict();
216 
217  return true;
218  }
219  else
220  {
221  return false;
222  }
223 }
224 
225 
227 {
228  if (runTimeModifiable_)
229  {
230  // Get state of all monitored objects (=registered objects with a
231  // valid filePath).
232  // Note: requires same ordering in objectRegistries on different
233  // processors!
235  (
236  (
238  || regIOobject::fileModificationChecking == timeStampMaster
239  ),
241  );
242 
243  // Time handling is special since controlDict_ is the one dictionary
244  // that is not registered to any database.
245 
246  if (controlDict_.readIfModified())
247  {
248  readDict();
249  functionObjects_.read();
250  }
251 
252  bool registryModified = objectRegistry::modified();
253 
254  if (registryModified)
255  {
257  }
258  }
259 }
260 
261 
263 {
264  const word tmName(name());
265 
266  timeIOdictionary timeDict
267  (
268  IOobject
269  (
270  "time",
271  tmName,
272  "uniform",
273  *this,
276  false
277  )
278  );
279 
280  timeDict.add("beginTime", timeToUserTime(beginTime_));
281  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
282  timeDict.add("name", string(tmName));
283  timeDict.add("index", timeIndex_);
284  timeDict.add("deltaT", timeToUserTime(deltaT_));
285  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
286 
287  return timeDict.regIOobject::writeObject
288  (
292  true
293  );
294 }
295 
296 
298 (
302  const bool write
303 ) const
304 {
305  if (writeTime())
306  {
307  bool writeOK = writeTimeDict();
308 
309  if (writeOK)
310  {
311  writeOK = objectRegistry::writeObject(fmt, ver, cmp, write);
312  }
313 
314  if (writeOK)
315  {
316  // Does the writeTime trigger purging?
317  if (writeTime_ && purgeWrite_)
318  {
319  if
320  (
321  previousWriteTimes_.size() == 0
322  || previousWriteTimes_.top() != name()
323  )
324  {
325  previousWriteTimes_.push(name());
326  }
327 
328  while (previousWriteTimes_.size() > purgeWrite_)
329  {
331  (
332  fileHandler().filePath
333  (
334  objectRegistry::path(previousWriteTimes_.pop())
335  )
336  );
337  }
338  }
339  }
340 
341  return writeOK;
342  }
343  else
344  {
345  return false;
346  }
347 }
348 
349 
351 {
352  writeTime_ = true;
353  return write();
354 }
355 
356 
358 {
359  stopAt_ = stopAtControl::writeNow;
360  endTime_ = value();
361 
362  return writeNow();
363 }
364 
365 
367 {
368  writeOnce_ = true;
369 }
370 
371 
372 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:226
Version number type.
Definition: IOstream.H:97
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
static unsigned int defaultPrecision()
Return the default precision.
Definition: IOstream.H:458
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:194
static compressionType compressionEnum(const word &)
Return compression of given compression name.
Definition: IOstream.C:61
Enum read(Istream &) const
Read a word from Istream and return the corresponding.
Definition: NamedEnum.C:61
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:246
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:32
static int precision_
Time directory name precision.
Definition: Time.H:161
scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: Time.C:830
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:128
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:262
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:357
const word & timeName() const
Return current time name (for backwards-compatibility)
Definition: Time.H:382
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:350
static int curPrecision_
Current time directory name precision adjusted as necessary.
Definition: Time.H:166
scalar userTimeToTime(const scalar tau) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: Time.C:824
scalar writeInterval_
Definition: Time.H:134
stopAtControl stopAt_
Definition: Time.H:129
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:131
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:366
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:298
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:1035
label purgeWrite_
Definition: Time.H:136
writeControl writeControl_
Definition: Time.H:132
scalar endTime_
Definition: Time.H:122
autoPtr< userTimes::userTime > userTime_
Optional user-time, defaults to realTime in s.
Definition: Time.H:126
@ endTime
stop when Time reaches the prescribed endTime
static format format_
Time directory name format.
Definition: Time.H:158
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:226
virtual bool read()
Read control dictionary, update controls and time.
Definition: TimeIO.C:211
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:860
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1169
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:659
const scalar & value() const
Return const reference to value.
const word & name() const
Return const reference to name.
virtual bool rmDir(const fileName &) const =0
Remove a directory and its contents.
virtual void updateStates(const bool masterOnly, const bool syncPar) const
Update state of all files.
virtual bool writeObject(IOstream::streamFormat fmt, IOstream::versionNumber ver, IOstream::compressionType cmp, const bool write) const
Write the objects.
virtual bool modified() const
Return true if any of the object's files have been modified.
fileName path() const
Return complete path.
Definition: regIOobject.C:199
void readModifiedObjects()
Read the objects that have been modified.
timeIOdictionary derived from IOdictionary with globalFile set false to enable writing to processor t...
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
word timeName
Definition: getTimeIndex.H:3
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
#define WarningInFunction
Report a warning using Foam::Warning.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
bool setEnv(const word &name, const std::string &value, const bool overwrite)
Set an environment variable.
Definition: POSIX.C:115
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
OSstream Serr(cerr, "Serr")
Definition: IOstreams.H:52
error FatalError
prefixOSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47