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-2019 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:
125  case timeControls::cpuTime:
126  case timeControls::adjustableRunTime:
127  {
128  interval_ = dict.lookup<scalar>(intervalName);
129  break;
130  }
131 
132  default:
133  {
134  break;
135  }
136  }
137 }
138 
139 
141 {
142  switch (timeControl_)
143  {
144  case timeControls::timeStep:
145  {
146  return
147  (
148  (intervalSteps_ <= 1)
149  || !(time_.timeIndex() % intervalSteps_)
150  );
151  break;
152  }
153 
154  case timeControls::writeTime:
155  case timeControls::outputTime:
156  {
157  if (time_.writeTime())
158  {
159  executionIndex_++;
160  return !(executionIndex_ % intervalSteps_);
161  }
162  break;
163  }
164 
166  case timeControls::adjustableRunTime:
167  {
168  label executionIndex = label
169  (
170  (
171  (time_.value() - time_.startTime().value())
172  + 0.5*time_.deltaTValue()
173  )
174  /interval_
175  );
176 
177  if (executionIndex > executionIndex_)
178  {
179  executionIndex_ = executionIndex;
180  return true;
181  }
182  break;
183  }
184 
185  case timeControls::cpuTime:
186  {
187  label executionIndex = label
188  (
189  returnReduce(time_.elapsedCpuTime(), maxOp<double>())
190  /interval_
191  );
192  if (executionIndex > executionIndex_)
193  {
194  executionIndex_ = executionIndex;
195  return true;
196  }
197  break;
198  }
199 
200  case timeControls::clockTime:
201  {
202  label executionIndex = label
203  (
204  returnReduce(label(time_.elapsedClockTime()), maxOp<label>())
205  /interval_
206  );
207  if (executionIndex > executionIndex_)
208  {
209  executionIndex_ = executionIndex;
210  return true;
211  }
212  break;
213  }
214 
215  case timeControls::none:
216  {
217  return false;
218  }
219 
220  default:
221  {
223  << "Undefined output control: "
224  << timeControlNames_[timeControl_] << nl
225  << abort(FatalError);
226  break;
227  }
228  }
229 
230  return false;
231 }
232 
233 
234 // ************************************************************************* //
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:667
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
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:158
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool execute()
Flag to indicate whether to execute.
Definition: timeControl.C:140
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:68
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:812