timeControl.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-2022 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 "timeControl.H"
27 #include "PstreamReduceOps.H"
28 
29 // * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33  template<>
34  const char* NamedEnum<timeControl::timeControls, 8>::
35  names[] =
36  {
37  "timeStep",
38  "writeTime",
39  "outputTime",
40  "adjustableRunTime",
41  "runTime",
42  "clockTime",
43  "cpuTime",
44  "none"
45  };
46 }
47 
49  Foam::timeControl::timeControlNames_;
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
55 (
56  const Time& t,
57  const dictionary& dict,
58  const word& prefix
59 )
60 :
61  time_(t),
62  prefix_(prefix),
63  timeControl_(timeControls::timeStep),
64  intervalSteps_(0),
65  interval_(-1),
66  executionIndex_(0)
67 {
68  read(dict);
69 }
70 
71 
72 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
73 
75 {}
76 
77 
78 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
79 
81 {
82  word controlName(prefix_ + "Control");
83  word intervalName(prefix_ + "Interval");
84 
85  // For backward compatibility support the deprecated 'outputControl' option
86  // now superseded by 'writeControl' for compatibility with Time
87  if (prefix_ == "write" && dict.found("outputControl"))
88  {
90  << "Using deprecated 'outputControl'" << nl
91  << " Please use 'writeControl' with 'writeInterval'"
92  << endl;
93 
94  // Change to the old names for this option
95  controlName = "outputControl";
96  intervalName = "outputInterval";
97  }
98 
99  if (dict.found(controlName))
100  {
101  timeControl_ = timeControlNames_.read(dict.lookup(controlName));
102  }
103  else
104  {
105  timeControl_ = timeControls::timeStep;
106  }
107 
108  switch (timeControl_)
109  {
110  case timeControls::timeStep:
111  {
112  intervalSteps_ = dict.lookupOrDefault<label>(intervalName, 0);
113  break;
114  }
115 
116  case timeControls::writeTime:
117  case timeControls::outputTime:
118  {
119  intervalSteps_ = dict.lookupOrDefault<label>(intervalName, 1);
120  break;
121  }
122 
123  case timeControls::clockTime:
124  case timeControls::runTime:
125  case timeControls::cpuTime:
126  case timeControls::adjustableRunTime:
127  {
128  interval_ = time_.userTimeToTime(dict.lookup<scalar>(intervalName));
129 
130  if (timeControl_ == timeControls::adjustableRunTime)
131  {
132  executionIndex_ = label
133  (
134  (
135  (time_.value() - time_.beginTime().value())
136  + 0.5*time_.deltaTValue()
137  )
138  /interval_
139  );
140  }
141 
142  break;
143  }
144 
145  default:
146  {
147  break;
148  }
149  }
150 }
151 
152 
154 {
155  switch (timeControl_)
156  {
157  case timeControls::timeStep:
158  {
159  return
160  (
161  (intervalSteps_ <= 1)
162  || !(time_.timeIndex() % intervalSteps_)
163  );
164  break;
165  }
166 
167  case timeControls::writeTime:
168  case timeControls::outputTime:
169  {
170  if (time_.writeTime())
171  {
172  executionIndex_++;
173  return !(executionIndex_ % intervalSteps_);
174  }
175  break;
176  }
177 
178  case timeControls::runTime:
179  case timeControls::adjustableRunTime:
180  {
181  label executionIndex = label
182  (
183  (
184  (time_.value() - time_.beginTime().value())
185  + 0.5*time_.deltaTValue()
186  )
187  /interval_
188  );
189 
190  if (executionIndex > executionIndex_)
191  {
192  executionIndex_ = executionIndex;
193  return true;
194  }
195  break;
196  }
197 
198  case timeControls::cpuTime:
199  {
200  label executionIndex = label
201  (
202  returnReduce(time_.elapsedCpuTime(), maxOp<double>())
203  /interval_
204  );
205  if (executionIndex > executionIndex_)
206  {
207  executionIndex_ = executionIndex;
208  return true;
209  }
210  break;
211  }
212 
213  case timeControls::clockTime:
214  {
215  label executionIndex = label
216  (
217  returnReduce(label(time_.elapsedClockTime()), maxOp<label>())
218  /interval_
219  );
220  if (executionIndex > executionIndex_)
221  {
222  executionIndex_ = executionIndex;
223  return true;
224  }
225  break;
226  }
227 
228  case timeControls::none:
229  {
230  return false;
231  }
232 
233  default:
234  {
236  << "Undefined output control: "
237  << timeControlNames_[timeControl_] << nl
238  << abort(FatalError);
239  break;
240  }
241  }
242 
243  return false;
244 }
245 
246 
247 // ************************************************************************* //
timeControl(const Time &, const dictionary &, const word &prefix)
Construct from Time object and dictionary.
Definition: timeControl.C:55
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
FvWallInfoData< WallInfo, label > label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Inter-processor communication reduction functions.
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool execute()
Flag to indicate whether to execute.
Definition: timeControl.C:153
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:51
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
errorManip< error > abort(error &err)
Definition: errorManip.H:131
static const char nl
Definition: Ostream.H:260
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
~timeControl()
Destructor.
Definition: timeControl.C:74
void read(const dictionary &)
Read from dictionary.
Definition: timeControl.C:80
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:864