timeControl.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "timeControl.H"
30 #include "PstreamReduceOps.H"
31 
32 // * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * * //
33 
34 const Foam::Enum
35 <
37 >
38 Foam::timeControl::controlNames_
39 ({
40  { timeControl::ocNone, "none" },
41  { timeControl::ocAlways, "always" },
42  { timeControl::ocTimeStep, "timeStep" },
43  { timeControl::ocWriteTime, "writeTime" },
44  { timeControl::ocWriteTime, "outputTime" },
45  { timeControl::ocRunTime, "runTime" },
46  { timeControl::ocAdjustableRunTime, "adjustable" },
47  { timeControl::ocAdjustableRunTime, "adjustableRunTime" },
48  { timeControl::ocClockTime, "clockTime" },
49  { timeControl::ocCpuTime, "cpuTime" },
50  { timeControl::ocOnEnd, "onEnd" },
51 });
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
56 Foam::timeControl::timeControl
57 (
58  const Time& runTime,
59  const word& prefix
60 )
61 :
62  time_(runTime),
63  prefix_(prefix),
64  timeControl_(ocAlways),
65  intInterval_(0),
66  interval_(0),
67  executionIndex_(0)
68 {}
69 
70 
71 Foam::timeControl::timeControl
72 (
73  const Time& runTime,
74  const dictionary& dict,
75  const word& prefix
76 )
77 :
78  timeControl(runTime, prefix)
79 {
80  read(dict);
81 }
82 
83 
84 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
85 
87 (
88  const dictionary& dict,
89  const word& prefix
90 )
91 {
92  const word controlName(prefix + "Control");
93 
94  return dict.found(controlName);
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99 
101 {
102  timeControl_ = ocAlways;
103  intInterval_ = 0;
104  interval_ = 0;
105  executionIndex_ = 0;
106 }
107 
108 
110 {
111  // Default is timeStep
112  timeControl_ = ocTimeStep;
113  intInterval_ = 0;
114  interval_ = 0;
115 
116  word controlName(prefix_ + "Control");
117  word intervalName(prefix_ + "Interval");
118 
119  if (prefix_ == "write")
120  {
121  // TBD: Could have timeControl_ = ocWriteTime;
122 
123  if (dict.found("outputControl"))
124  {
125  // Accept deprecated 'outputControl' instead of 'writeControl'
126 
127  // Change to the old names for this option
128  controlName = "outputControl";
129  intervalName = "outputInterval";
130 
132  << "Found deprecated 'outputControl'" << nl
133  << " Use 'writeControl' with 'writeInterval'"
134  << endl;
135  error::warnAboutAge("keyword", 1606);
136  }
137  }
138 
139 
140  timeControl_ = controlNames_.getOrDefault(controlName, dict, timeControl_);
141 
142  switch (timeControl_)
143  {
144  case ocTimeStep:
145  case ocWriteTime:
146  {
147  intInterval_ = dict.getOrDefault<label>(intervalName, 0);
148  interval_ = intInterval_; // Mirrored as scalar (for output)
149  break;
150  }
151 
152  case ocClockTime:
153  case ocRunTime:
154  case ocCpuTime:
155  case ocAdjustableRunTime:
156  {
157  const scalar userTime = dict.get<scalar>(intervalName);
158  interval_ = time_.userTimeToTime(userTime);
159  break;
160  }
161 
162  default:
163  {
164  break;
165  }
166  }
167 }
168 
169 
171 {
172  switch (timeControl_)
173  {
174  case ocNone:
175  {
176  return false;
177  break;
178  }
179 
180  case ocAlways:
181  {
182  return true;
183  break;
184  }
185 
186  case ocTimeStep:
187  {
188  return
189  (
190  (intInterval_ <= 1)
191  || !(time_.timeIndex() % intInterval_)
192  );
193  break;
194  }
195 
196  case ocWriteTime:
197  {
198  if (time_.writeTime())
199  {
200  ++executionIndex_;
201  return
202  (
203  (intInterval_ <= 1)
204  || !(executionIndex_ % intInterval_)
205  );
206  }
207  break;
208  }
209 
210  case ocRunTime:
211  case ocAdjustableRunTime:
212  {
213  label executionIndex = label
214  (
215  (
216  (time_.value() - time_.startTime().value())
217  + 0.5*time_.deltaTValue()
218  )
219  /interval_
220  );
221 
222  if (executionIndex > executionIndex_)
223  {
224  executionIndex_ = executionIndex;
225  return true;
226  }
227  break;
228  }
229 
230  case ocCpuTime:
231  {
232  label executionIndex = label
233  (
234  returnReduce(time_.elapsedCpuTime(), maxOp<double>())
235  /interval_
236  );
237  if (executionIndex > executionIndex_)
238  {
239  executionIndex_ = executionIndex;
240  return true;
241  }
242  break;
243  }
244 
245  case ocClockTime:
246  {
247  label executionIndex = label
248  (
249  returnReduce(time_.elapsedClockTime(), maxOp<double>())
250  /interval_
251  );
252  if (executionIndex > executionIndex_)
253  {
254  executionIndex_ = executionIndex;
255  return true;
256  }
257  break;
258  }
259 
260  case ocOnEnd:
261  {
262  scalar endTime = time_.endTime().value() - 0.5*time_.deltaTValue();
263  return time_.value() > endTime;
264  break;
265  }
266 
267  default:
268  {
270  << "Undefined time control: "
271  << controlNames_[timeControl_] << nl
272  << abort(FatalError);
273  break;
274  }
275  }
276 
277  return false;
278 }
279 
280 
281 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::maxOp
Definition: ops.H:223
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::timeControl::timeControls
timeControls
The time control options.
Definition: timeControl.H:66
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::timeControl::execute
bool execute()
Flag to indicate whether to execute.
Definition: timeControl.C:170
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:94
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
Foam::timeControl::entriesPresent
static bool entriesPresent(const dictionary &dict, const word &prefix)
Identify if a timeControl object is present in the dictionary.
Definition: timeControl.C:87
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
timeControl.H
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::timeControl
General time dependent execution controller. The execution parameters are given by the "Control" and ...
Definition: timeControl.H:61
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
PstreamReduceOps.H
Inter-processor communication reduction functions.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::timeControl::clear
void clear()
Reset control to 'always' - ie, no intervention.
Definition: timeControl.C:100
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::timeControl::ocAlways
Always execute.
Definition: timeControl.H:69
Foam::error::warnAboutAge
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:55
IOWarningInFunction
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:340
Foam::timeControl::read
void read(const dictionary &dict)
Read from dictionary.
Definition: timeControl.C:109
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148