timeSelector.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-2018 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 "timeSelector.H"
27 #include "ListOps.H"
28 #include "argList.H"
29 #include "Time.H"
30 #include "IStringStream.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 :
36  scalarRanges()
37 {}
38 
39 
41 :
42  scalarRanges(is)
43 {}
44 
45 
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
47 
48 bool Foam::timeSelector::selected(const instant& value) const
49 {
50  return scalarRanges::selected(value.value());
51 }
52 
53 
55 {
56  List<bool> lst(Times.size(), false);
57 
58  // Check ranges, avoid false positive on constant/
59  forAll(Times, timeI)
60  {
61  if (Times[timeI].name() != "constant" && selected(Times[timeI]))
62  {
63  lst[timeI] = true;
64  }
65  }
66 
67  // Check specific values
68  forAll(*this, rangeI)
69  {
70  if (operator[](rangeI).isExact())
71  {
72  scalar target = operator[](rangeI).value();
73 
74  int nearestIndex = -1;
75  scalar nearestDiff = Foam::great;
76 
77  forAll(Times, timeI)
78  {
79  if (Times[timeI].name() == "constant") continue;
80 
81  scalar diff = fabs(Times[timeI].value() - target);
82  if (diff < nearestDiff)
83  {
84  nearestDiff = diff;
85  nearestIndex = timeI;
86  }
87  }
88 
89  if (nearestIndex >= 0)
90  {
91  lst[nearestIndex] = true;
92  }
93  }
94  }
95 
96  return lst;
97 }
98 
99 
101 const
102 {
103  return subset(selected(Times), Times);
104 }
105 
106 
108 {
109  inplaceSubset(selected(Times), Times);
110 }
111 
112 
114 (
115  const bool constant,
116  const bool withZero
117 )
118 {
119  if (constant)
120  {
122  (
123  "constant",
124  "include the 'constant/' dir in the times list"
125  );
126  }
127  if (withZero)
128  {
130  (
131  "withZero",
132  "include the '0/' dir in the times list"
133  );
134  }
136  (
137  "noZero",
138  string("exclude the '0/' dir from the times list")
139  + (
140  withZero
141  ? ", has precedence over the -withZero option"
142  : ""
143  )
144  );
146  (
147  "latestTime",
148  "select the latest time"
149  );
151  (
152  "time",
153  "ranges",
154  "comma-separated time ranges - eg, ':10,20,40:70,1000:'"
155  );
156 }
157 
158 
160 (
161  const instantList& timeDirs,
162  const argList& args,
163  const word& constantName
164 )
165 {
166  if (timeDirs.size())
167  {
168  List<bool> selectTimes(timeDirs.size(), true);
169 
170  // Determine locations of constant/ and 0/ directories
171  label constantIdx = -1;
172  label zeroIdx = -1;
173 
174  forAll(timeDirs, timeI)
175  {
176  if (timeDirs[timeI].name() == constantName)
177  {
178  constantIdx = timeI;
179  }
180  else if (timeDirs[timeI].value() == 0)
181  {
182  zeroIdx = timeI;
183  }
184 
185  if (constantIdx >= 0 && zeroIdx >= 0)
186  {
187  break;
188  }
189  }
190 
191  // Determine latestTime selection (if any)
192  // This must appear before the -time option processing
193  label latestIdx = -1;
194  if (args.optionFound("latestTime"))
195  {
196  selectTimes = false;
197  latestIdx = timeDirs.size() - 1;
198 
199  // Avoid false match on constant/
200  if (latestIdx == constantIdx)
201  {
202  latestIdx = -1;
203  }
204  }
205 
206  if (args.optionFound("time"))
207  {
208  // Can match 0/, but can never match constant/
209  selectTimes = timeSelector
210  (
211  args.optionLookup("time")()
212  ).selected(timeDirs);
213  }
214 
215  // Add in latestTime (if selected)
216  if (latestIdx >= 0)
217  {
218  selectTimes[latestIdx] = true;
219  }
220 
221  if (constantIdx >= 0)
222  {
223  // Only add constant/ if specifically requested
224  selectTimes[constantIdx] = args.optionFound("constant");
225  }
226 
227  // Special treatment for 0/
228  if (zeroIdx >= 0)
229  {
230  if (args.optionFound("noZero"))
231  {
232  // Exclude 0/ if specifically requested
233  selectTimes[zeroIdx] = false;
234  }
235  else if (argList::validOptions.found("withZero"))
236  {
237  // With -withZero enabled, drop 0/ unless specifically requested
238  selectTimes[zeroIdx] = args.optionFound("withZero");
239  }
240  }
241 
242  return subset(selectTimes, timeDirs);
243  }
244  else
245  {
246  return timeDirs;
247  }
248 }
249 
250 
252 (
253  Time& runTime,
254  const argList& args
255 )
256 {
258  (
260  (
261  runTime.times(),
262  args,
263  runTime.constant()
264  )
265  );
266 
267  if (timeDirs.empty())
268  {
270  << "No time specified or available, selecting 'constant'"
271  << endl;
272 
273  timeDirs.append(instant(0, runTime.constant()));
274  }
275 
276  runTime.setTime(timeDirs[0], 0);
277 
278  return timeDirs;
279 }
280 
281 
283 (
284  Time& runTime,
285  const argList& args
286 )
287 {
288  if
289  (
290  args.optionFound("latestTime")
291  || args.optionFound("time")
292  || args.optionFound("constant")
293  || args.optionFound("noZero")
294  || args.optionFound("withZero")
295  )
296  {
297  return select0(runTime, args);
298  }
299  else
300  {
301  // No timeSelector option specified. Do not change runTime.
302  return instantList(1, instant(runTime.value(), runTime.timeName()));
303  }
304 }
305 
306 
307 // ************************************************************************* //
void inplaceSubset(const UList< T > &select, const T &value, ListType &)
Inplace extract elements of List when select is a certain value.
bool selected(const instant &) const
Return true if the given instant is within the ranges.
Definition: timeSelector.C:48
List< instant > instantList
List of instants.
Definition: instantList.H:42
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:403
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
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
void inplaceSelect(instantList &) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:107
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
timeSelector()
Construct Null.
Definition: timeSelector.C:34
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:622
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Various functions to operate on Lists.
static instantList timeDirs
Definition: globalFoam.H:44
A class for handling words, derived from string.
Definition: word.H:59
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
const word & constant() const
Return constant name.
Definition: TimePaths.H:124
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
const Type & value() const
Return const reference to value.
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:283
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:879
bool selected(const scalar) const
Return true if the given value is within the ranges.
Definition: scalarRanges.C:59
instantList select(const instantList &) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:100
scalar value() const
Value (const access)
Definition: instant.H:114
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:252
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
An instant of time. Contains the time value and name.
Definition: instant.H:66
#define WarningInFunction
Report a warning using Foam::Warning.
A List of scalarRange.
Definition: scalarRanges.H:49
instantList times() const
Search the case for valid time directories.
Definition: Time.C:638
ListType subset(const UList< T > &select, const T &value, const ListType &)
Extract elements of List when select is a certain value.
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
Foam::argList args(argc, argv)
bool found
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:156
static void addOptions(const bool constant=true, const bool withZero=false)
Add the options handled by timeSelector to argList::validOptions.
Definition: timeSelector.C:114
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:120