Loading...
Searching...
No Matches
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, 2025 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::ocOnStart, "onStart" },
51 { timeControl::ocOnEnd, "onEnd" },
52});
53
54
55// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
56
57Foam::timeControl::timeControl
58(
59 const Time& runTime,
60 const word& prefix
61)
62:
63 time_(runTime),
64 prefix_(prefix),
65 timeControl_(ocAlways),
66 intInterval_(0),
67 interval_(0),
68 executionIndex_(0)
69{}
70
71
72Foam::timeControl::timeControl
73(
74 const Time& runTime,
75 const dictionary& dict,
76 const word& prefix
77)
78:
79 timeControl(runTime, prefix)
81 read(dict);
82}
83
84
85// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
86
88(
89 const dictionary& dict,
90 const word& prefix
91)
92{
93 const word controlName(prefix + "Control");
95 return dict.found(controlName);
96}
97
98
99// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100
102{
103 timeControl_ = ocAlways;
104 intInterval_ = 0;
105 interval_ = 0;
106 executionIndex_ = 0;
107}
108
109
111{
112 // Default is timeStep
113 timeControl_ = ocTimeStep;
114 intInterval_ = 0;
115 interval_ = 0;
116
117 word controlName(prefix_ + "Control");
118 word intervalName(prefix_ + "Interval");
119
120 if (prefix_ == "write")
121 {
122 // TBD: Could have timeControl_ = ocWriteTime;
123
124 if (dict.found("outputControl"))
125 {
126 // Accept deprecated 'outputControl' instead of 'writeControl'
127
128 // Change to the old names for this option
129 controlName = "outputControl";
130 intervalName = "outputInterval";
131
133 << "Found deprecated 'outputControl'" << nl
134 << " Use 'writeControl' with 'writeInterval'"
135 << endl;
136 error::warnAboutAge("keyword", 1606);
137 }
138 }
139
140
141 timeControl_ = controlNames_.getOrDefault(controlName, dict, timeControl_);
142
143 switch (timeControl_)
144 {
145 case ocTimeStep:
146 case ocWriteTime:
147 {
148 intInterval_ = dict.getOrDefault<label>(intervalName, 0);
149 interval_ = intInterval_; // Mirrored as scalar (for output)
150 break;
151 }
152
153 case ocClockTime:
154 case ocRunTime:
155 case ocCpuTime:
156 case ocAdjustableRunTime:
157 {
158 const scalar userTime = dict.get<scalar>(intervalName);
159 interval_ = time_.userTimeToTime(userTime);
160 break;
161 }
162
163 default:
165 break;
166 }
167 }
168}
169
170
172{
173 switch (timeControl_)
174 {
175 case ocNone:
176 {
177 return false;
178 break;
179 }
180
181 case ocAlways:
182 {
183 return true;
184 break;
185 }
186
187 case ocTimeStep:
188 {
189 return
190 (
191 (intInterval_ <= 1)
192 || !(time_.timeIndex() % intInterval_)
193 );
194 break;
195 }
196
197 case ocWriteTime:
198 {
199 if (time_.writeTime())
200 {
201 ++executionIndex_;
202 return
203 (
204 (intInterval_ <= 1)
205 || !(executionIndex_ % intInterval_)
206 );
207 }
208 break;
209 }
210
211 case ocRunTime:
212 case ocAdjustableRunTime:
213 {
214 label executionIndex = label
215 (
216 (
217 (time_.value() - time_.startTime().value())
218 + 0.5*time_.deltaTValue()
219 )
220 /interval_
221 );
222
223 if (executionIndex > executionIndex_)
224 {
225 executionIndex_ = executionIndex;
226 return true;
227 }
228 break;
229 }
230
231 case ocCpuTime:
232 {
233 label executionIndex = label
234 (
235 returnReduce(time_.elapsedCpuTime(), maxOp<double>())
236 /interval_
237 );
238 if (executionIndex > executionIndex_)
239 {
240 executionIndex_ = executionIndex;
241 return true;
242 }
243 break;
244 }
245
246 case ocClockTime:
247 {
248 label executionIndex = label
249 (
250 returnReduce(time_.elapsedClockTime(), maxOp<double>())
251 /interval_
252 );
253 if (executionIndex > executionIndex_)
254 {
255 executionIndex_ = executionIndex;
256 return true;
257 }
258 break;
259 }
260
261 case ocOnStart:
262 {
263 return time_.timeIndex() == 1;
264 break;
265 }
266
267 case ocOnEnd:
268 {
269 scalar endTime = time_.endTime().value() - 0.5*time_.deltaTValue();
270 return time_.value() > endTime;
271 break;
272 }
273
274 default:
275 {
277 << "Undefined time control: "
278 << controlNames_[timeControl_] << nl
279 << abort(FatalError);
280 break;
281 }
282 }
283
284 return false;
285}
286
287
288// ************************************************************************* //
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:57
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition error.C:67
General time dependent execution controller. The execution parameters are given by the "Control" and ...
Definition timeControl.H:58
static bool entriesPresent(const dictionary &dict, const word &prefix)
Identify if a timeControl object is present in the dictionary.
Definition timeControl.C:81
void read(const dictionary &dict)
Read from dictionary.
timeControls
The time control options.
Definition timeControl.H:65
@ ocClockTime
Use clock time for execution.
Definition timeControl.H:72
@ ocOnStart
Execute on first step of run.
Definition timeControl.H:74
@ ocNone
No execution.
Definition timeControl.H:66
@ ocAlways
Always execute.
Definition timeControl.H:67
@ ocCpuTime
Use CPU time for execution.
Definition timeControl.H:73
@ ocRunTime
Use run-time for execution.
Definition timeControl.H:70
@ ocOnEnd
Execute on end of run.
Definition timeControl.H:75
@ ocAdjustableRunTime
Currently identical to "runTime".
Definition timeControl.H:71
@ ocWriteTime
Execution coupled to write-time.
Definition timeControl.H:69
@ ocTimeStep
Execution coupled to time-step (default).
Definition timeControl.H:68
void clear()
Reset control to 'always' - ie, no intervention.
Definition timeControl.C:94
label executionIndex() const
Return the index of the previous execution.
bool execute()
Flag to indicate whether to execute.
A class for handling words, derived from Foam::string.
Definition word.H:66
engineTime & runTime
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
T returnReduce(const T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Perform reduction on a copy, using specified binary operation.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict