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-2020 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 "OSspecific.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
32 {
33  word application;
34  if (controlDict_.readIfPresent("application", application))
35  {
36  // Do not override if already set so external application can override
37  setEnv("FOAM_APPLICATION", application, false);
38  }
39 
40  if (!deltaTchanged_)
41  {
42  deltaT_ = controlDict_.lookup<scalar>("deltaT");
43  }
44 
45  if (controlDict_.found("writeControl"))
46  {
48  (
49  controlDict_.lookup("writeControl")
50  );
51  }
52 
53  scalar newWriteInterval = writeInterval_;
54 
55  if (controlDict_.readIfPresent("writeInterval", newWriteInterval))
56  {
57  if
58  (
60  && label(newWriteInterval) < 1
61  )
62  {
63  FatalIOErrorInFunction(controlDict_)
64  << "writeInterval < 1 for writeControl timeStep"
65  << exit(FatalIOError);
66  }
67  }
68  else
69  {
70  controlDict_.lookup("writeFrequency") >> newWriteInterval;
71  }
72 
73  setWriteInterval(newWriteInterval);
74 
75  if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
76  {
77  if (purgeWrite_ < 0)
78  {
80  << "invalid value for purgeWrite " << purgeWrite_
81  << ", should be >= 0, setting to 0"
82  << endl;
83 
84  purgeWrite_ = 0;
85  }
86  }
87 
88  if (controlDict_.found("timeFormat"))
89  {
90  const word formatName(controlDict_.lookup("timeFormat"));
91 
92  if (formatName == "general")
93  {
95  }
96  else if (formatName == "fixed")
97  {
99  }
100  else if (formatName == "scientific")
101  {
103  }
104  else
105  {
107  << "unsupported time format " << formatName
108  << endl;
109  }
110  }
111 
112  controlDict_.readIfPresent("timePrecision", precision_);
113 
114  // stopAt at 'endTime' or a specified value
115  // if nothing is specified, the endTime is zero
116  if (controlDict_.found("stopAt"))
117  {
118  stopAt_ = stopAtControlNames_.read(controlDict_.lookup("stopAt"));
119 
121  {
122  controlDict_.lookup("endTime") >> endTime_;
123  }
124  else
125  {
126  endTime_ = great;
127  }
128  }
129  else if (!controlDict_.readIfPresent("endTime", endTime_))
130  {
131  endTime_ = 0;
132  }
133 
135 
136  if (controlDict_.found("writeVersion"))
137  {
138  writeVersion_ = IOstream::versionNumber
139  (
140  controlDict_.lookup("writeVersion")
141  );
142  }
143 
144  if (controlDict_.found("writeFormat"))
145  {
146  writeFormat_ = IOstream::formatEnum
147  (
148  controlDict_.lookup("writeFormat")
149  );
150  }
151 
152  if (controlDict_.found("writePrecision"))
153  {
155  (
156  controlDict_.lookup<unsigned int>("writePrecision")
157  );
158 
161 
164 
165  FatalError().precision(IOstream::defaultPrecision());
166  FatalIOError.error::operator()().precision
167  (
169  );
170  }
171 
172  if (controlDict_.found("writeCompression"))
173  {
174  writeCompression_ = IOstream::compressionEnum
175  (
176  controlDict_.lookup("writeCompression")
177  );
178 
179  if
180  (
181  writeFormat_ == IOstream::BINARY
182  && writeCompression_ == IOstream::COMPRESSED
183  )
184  {
185  IOWarningInFunction(controlDict_)
186  << "Selecting compressed binary is inefficient and ineffective"
187  ", resetting to uncompressed binary"
188  << endl;
189 
190  writeCompression_ = IOstream::UNCOMPRESSED;
191  }
192  }
193 
194  controlDict_.readIfPresent("graphFormat", graphFormat_);
195  controlDict_.readIfPresent("runTimeModifiable", runTimeModifiable_);
196 }
197 
198 
200 {
201  if (controlDict_.regIOobject::read())
202  {
203  readDict();
204 
205  return true;
206  }
207  else
208  {
209  return false;
210  }
211 }
212 
213 
215 {
216  if (runTimeModifiable_)
217  {
218  // Get state of all monitored objects (=registered objects with a
219  // valid filePath).
220  // Note: requires same ordering in objectRegistries on different
221  // processors!
223  (
224  (
227  ),
229  );
230 
231  // Time handling is special since controlDict_ is the one dictionary
232  // that is not registered to any database.
233 
234  if (controlDict_.readIfModified())
235  {
236  readDict();
237  functionObjects_.read();
238  }
239 
240  bool registryModified = objectRegistry::modified();
241 
242  if (registryModified)
243  {
245  }
246  }
247 }
248 
249 
251 {
252  const word tmName(timeName());
253 
254  IOdictionary timeDict
255  (
256  IOobject
257  (
258  "time",
259  tmName,
260  "uniform",
261  *this,
264  false
265  )
266  );
267 
268  timeDict.add("beginTime", beginTime_);
269  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
270  timeDict.add("name", string(tmName));
271  timeDict.add("index", timeIndex_);
272  timeDict.add("deltaT", timeToUserTime(deltaT_));
273  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
274 
275  return timeDict.regIOobject::writeObject
276  (
280  true
281  );
282 }
283 
284 
286 (
290  const bool write
291 ) const
292 {
293  if (writeTime())
294  {
295  bool writeOK = writeTimeDict();
296 
297  if (writeOK)
298  {
299  writeOK = objectRegistry::writeObject(fmt, ver, cmp, write);
300  }
301 
302  if (writeOK)
303  {
304  // Does the writeTime trigger purging?
305  if (writeTime_ && purgeWrite_)
306  {
307  if
308  (
309  previousWriteTimes_.size() == 0
310  || previousWriteTimes_.top() != timeName()
311  )
312  {
314  }
315 
316  while (previousWriteTimes_.size() > purgeWrite_)
317  {
319  (
321  (
323  )
324  );
325  }
326  }
327  }
328 
329  return writeOK;
330  }
331  else
332  {
333  return false;
334  }
335 }
336 
337 
339 {
340  writeTime_ = true;
341  return write();
342 }
343 
344 
346 {
348  endTime_ = value();
349 
350  return writeNow();
351 }
352 
353 
355 {
356  writeOnce_ = true;
357 }
358 
359 
360 // ************************************************************************* //
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:643
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
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:345
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
writeControl writeControl_
Definition: Time.H:128
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:130
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
virtual fileName filePath() const
Return complete path + object name if the file exists.
Definition: regIOobject.C:391
virtual word timeName() const
Return current time name.
Definition: Time.C:646
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1133
static const int maxPrecision_
Maximum time directory name precision.
Definition: Time.H:161
fileName path() const
Return complete path.
Definition: IOobject.C:348
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
virtual scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: TimeState.C:58
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:250
bool writeTime() const
Return true if this is a write time.
Definition: TimeStateI.H:65
scalar deltaT0_
Definition: TimeState.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:132
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:286
static int precision_
Time directory name precision.
Definition: Time.H:158
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)
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:121
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:155
scalar beginTime_
Definition: Time.H:122
FIFOStack< word > previousWriteTimes_
Definition: Time.H:133
virtual void readDict()
Read the control dictionary and set the write controls etc.
Definition: TimeIO.C:31
static fileCheckTypes fileModificationChecking
Type of file modification checking.
Definition: IOobject.H:219
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
static const NamedEnum< stopAtControl, 4 > stopAtControlNames_
Definition: Time.H:124
const word & name() const
Return const reference to name.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:214
bool deltaTchanged_
Definition: TimeState.H:59
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:354
static const NamedEnum< writeControl, 5 > writeControlNames_
Definition: Time.H:127
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:199
#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:335
bool writeOnce_
Definition: Time.H:136
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:125
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:986
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
virtual bool write(const bool write=true) const
Write using setting from DB.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
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:338
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:844
label timeIndex_
Definition: TimeState.H:55
IOerror FatalIOError