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-2024 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_ = controlDict_.lookup<scalar>("deltaT", userUnits());
44  }
45 
46  if (controlDict_.found("writeControl"))
47  {
49  (
50  controlDict_.lookup("writeControl")
51  );
52  }
53 
54  const scalar newWriteInterval =
55  controlDict_.lookupBackwardsCompatible<scalar>
56  (
57  {"writeInterval", "writeFrequency"},
59  );
60 
61  if
62  (
64  && label(newWriteInterval) < 1
65  )
66  {
67  FatalIOErrorInFunction(controlDict_)
68  << "writeInterval < 1 for writeControl timeStep"
69  << exit(FatalIOError);
70  }
71 
72  setWriteInterval(newWriteInterval);
73 
74  if (controlDict_.readIfPresent("purgeWrite", purgeWrite_))
75  {
76  if (purgeWrite_ < 0)
77  {
79  << "invalid value for purgeWrite " << purgeWrite_
80  << ", should be >= 0, setting to 0"
81  << endl;
82 
83  purgeWrite_ = 0;
84  }
85  }
86 
87  if (controlDict_.found("timeFormat"))
88  {
89  const word formatName(controlDict_.lookup("timeFormat"));
90 
91  if (formatName == "general")
92  {
94  }
95  else if (formatName == "fixed")
96  {
98  }
99  else if (formatName == "scientific")
100  {
102  }
103  else
104  {
106  << "unsupported time format " << formatName
107  << endl;
108  }
109  }
110 
111  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 
120  if (stopAt_ == stopAtControl::endTime || controlDict_.found("endTime"))
121  {
122  endTime_ = userTimeToTime(controlDict_.lookup<scalar>("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("runTimeModifiable", runTimeModifiable_);
195 
196  userTime_->read(controlDict_);
197 }
198 
199 
201 {
202  if (runTimeModifiable_)
203  {
204  // Get state of all monitored objects (=registered objects with a
205  // valid filePath).
206  // Note: requires same ordering in objectRegistries on different
207  // processors!
209  (
210  (
212  || regIOobject::fileModificationChecking == timeStampMaster
213  ),
215  );
216 
218  }
219 }
220 
221 
223 {
224  const word tmName(name());
225 
226  timeIOdictionary timeDict
227  (
228  IOobject
229  (
230  "time",
231  tmName,
232  "uniform",
233  *this,
236  false
237  )
238  );
239 
240  timeDict.add("beginTime", timeToUserTime(beginTime_));
241  timeDict.add("value", timeName(timeToUserTime(value()), maxPrecision_));
242  timeDict.add("name", string(tmName));
243  timeDict.add("index", timeIndex_);
244  timeDict.add("deltaT", timeToUserTime(deltaT_));
245  timeDict.add("deltaT0", timeToUserTime(deltaT0_));
246 
247  return timeDict.regIOobject::writeObject
248  (
252  true
253  );
254 }
255 
256 
258 (
262  const bool write
263 ) const
264 {
265  if (writeTime())
266  {
267  bool writeOK = writeTimeDict();
268 
269  if (writeOK)
270  {
271  writeOK = objectRegistry::writeObject(fmt, ver, cmp, write);
272  }
273 
274  if (writeOK)
275  {
276  // Does the writeTime trigger purging?
277  if (writeTime_ && purgeWrite_)
278  {
279  if
280  (
281  previousWriteTimes_.size() == 0
282  || previousWriteTimes_.top() != name()
283  )
284  {
285  previousWriteTimes_.push(name());
286  }
287 
288  while (previousWriteTimes_.size() > purgeWrite_)
289  {
291  (
292  fileHandler().filePath
293  (
294  objectRegistry::path(previousWriteTimes_.pop())
295  )
296  );
297  }
298  }
299  }
300 
301  return writeOK;
302  }
303  else
304  {
305  return false;
306  }
307 }
308 
309 
311 {
312  writeTime_ = true;
313  return write();
314 }
315 
316 
318 {
319  stopAt_ = stopAtControl::writeNow;
320  endTime_ = value();
321 
322  return writeNow();
323 }
324 
325 
327 {
328  writeOnce_ = true;
329 }
330 
331 
332 // ************************************************************************* //
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:262
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:165
scalar timeToUserTime(const scalar t) const
Convert the real-time (s) into user-time (e.g. CA deg)
Definition: Time.C:850
const unitConversion & userUnits() const
Return the user-time unit conversion.
Definition: Time.C:869
virtual bool writeTimeDict() const
Write time dictionary to the <time>/uniform directory.
Definition: TimeIO.C:222
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:317
static const NamedEnum< stopAtControl, 4 > stopAtControlNames
Definition: Time.H:117
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:310
const unitConversion & writeIntervalUnits() const
Return the write interval units.
Definition: Time.C:875
static int curPrecision_
Current time directory name precision adjusted as necessary.
Definition: Time.H:170
scalar userTimeToTime(const scalar tau) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: Time.C:844
stopAtControl stopAt_
Definition: Time.H:134
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:326
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
Definition: TimeIO.C:258
virtual void setWriteInterval(const scalar writeInterval)
Reset the write interval.
Definition: Time.C:1081
label purgeWrite_
Definition: Time.H:140
writeControl writeControl_
Definition: Time.H:136
scalar endTime_
Definition: Time.H:128
static word timeName(const scalar, const int precision=curPrecision_)
Return time name of given scalar time.
Definition: Time.C:641
autoPtr< userTimes::userTime > userTime_
Optional user-time, defaults to realTime in s.
Definition: Time.H:132
@ endTime
stop when Time reaches the prescribed endTime
static format format_
Time directory name format.
Definition: Time.H:162
static const NamedEnum< writeControl, 5 > writeControlNames
Definition: Time.H:119
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:200
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:710
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1014
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
ITstream & lookupBackwardsCompatible(const wordList &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream, trying a list of keywords.
Definition: dictionary.C:721
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:509
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.
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:346
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:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
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