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-------------------------------------------------------------------------------
11License
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
34const Foam::Enum
35<
37>
38Foam::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
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
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// ************************************************************************* //
Inter-processor communication reduction functions.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
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
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:55
General time dependent execution controller. The execution parameters are given by the "Control" and ...
Definition: timeControl.H:62
static bool entriesPresent(const dictionary &dict, const word &prefix)
Identify if a timeControl object is present in the dictionary.
Definition: timeControl.C:87
void read(const dictionary &dict)
Read from dictionary.
Definition: timeControl.C:109
timeControls
The time control options.
Definition: timeControl.H:67
void clear()
Reset control to 'always' - ie, no intervention.
Definition: timeControl.C:100
bool execute()
Flag to indicate whether to execute.
Definition: timeControl.C:170
A class for handling words, derived from Foam::string.
Definition: word.H:68
engineTime & runTime
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
T returnReduce(const T &value, const BinaryOp &bop, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Reduce (copy) and return value.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict