timeSelector.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 | Copyright (C) 2011-2017 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  "newTimes",
153  "select the new times"
154  );
156  (
157  "time",
158  "ranges",
159  "comma-separated time ranges - eg, ':10,20,40:70,1000:'"
160  );
161 }
162 
163 
165 (
166  const instantList& timeDirs,
167  const argList& args,
168  const word& constantName
169 )
170 {
171  if (timeDirs.size())
172  {
173  List<bool> selectTimes(timeDirs.size(), true);
174 
175  // Determine locations of constant/ and 0/ directories
176  label constantIdx = -1;
177  label zeroIdx = -1;
178 
179  forAll(timeDirs, timeI)
180  {
181  if (timeDirs[timeI].name() == constantName)
182  {
183  constantIdx = timeI;
184  }
185  else if (timeDirs[timeI].value() == 0)
186  {
187  zeroIdx = timeI;
188  }
189 
190  if (constantIdx >= 0 && zeroIdx >= 0)
191  {
192  break;
193  }
194  }
195 
196  // Determine latestTime selection (if any)
197  // This must appear before the -time option processing
198  label latestIdx = -1;
199  if (args.optionFound("latestTime"))
200  {
201  selectTimes = false;
202  latestIdx = timeDirs.size() - 1;
203 
204  // Avoid false match on constant/
205  if (latestIdx == constantIdx)
206  {
207  latestIdx = -1;
208  }
209  }
210 
211  if (args.optionFound("time"))
212  {
213  // Can match 0/, but can never match constant/
214  selectTimes = timeSelector
215  (
216  args.optionLookup("time")()
217  ).selected(timeDirs);
218  }
219 
220  // Add in latestTime (if selected)
221  if (latestIdx >= 0)
222  {
223  selectTimes[latestIdx] = true;
224  }
225 
226  if (constantIdx >= 0)
227  {
228  // Only add constant/ if specifically requested
229  selectTimes[constantIdx] = args.optionFound("constant");
230  }
231 
232  // Special treatment for 0/
233  if (zeroIdx >= 0)
234  {
235  if (args.optionFound("noZero"))
236  {
237  // Exclude 0/ if specifically requested
238  selectTimes[zeroIdx] = false;
239  }
240  else if (argList::validOptions.found("withZero"))
241  {
242  // With -withZero enabled, drop 0/ unless specifically requested
243  selectTimes[zeroIdx] = args.optionFound("withZero");
244  }
245  }
246 
247  return subset(selectTimes, timeDirs);
248  }
249  else
250  {
251  return timeDirs;
252  }
253 }
254 
255 
257 (
258  Time& runTime,
259  const argList& args
260 )
261 {
263  (
265  (
266  runTime.times(),
267  args,
268  runTime.constant()
269  )
270  );
271 
272  if (timeDirs.empty())
273  {
275  << "No time specified or available, selecting 'constant'"
276  << endl;
277 
278  timeDirs.append(instant(0, runTime.constant()));
279  }
280 
281  runTime.setTime(timeDirs[0], 0);
282 
283  return timeDirs;
284 }
285 
286 
288 (
289  Time& runTime,
290  const argList& args
291 )
292 {
293  if
294  (
295  args.optionFound("latestTime")
296  || args.optionFound("time")
297  || args.optionFound("constant")
298  || args.optionFound("noZero")
299  || args.optionFound("withZero")
300  )
301  {
302  return select0(runTime, args);
303  }
304  else
305  {
306  // No timeSelector option specified. Do not change runTime.
307  return instantList(1, instant(runTime.value(), runTime.timeName()));
308  }
309 }
310 
311 
313 (
314  Time& runTime,
315  const argList& args,
316  const word& fName
317 )
318 {
320 
321  if (timeDirs.size() && args.optionFound("newTimes"))
322  {
323  List<bool> selectTimes(timeDirs.size(), true);
324 
325  forAll(timeDirs, timeI)
326  {
327  selectTimes[timeI] =
329  (
330  runTime.path()
331  /timeDirs[timeI].name()
332  /fName
333  );
334  }
335 
336  return subset(selectTimes, timeDirs);
337  }
338  else
339  {
340  return timeDirs;
341  }
342 }
343 
344 
345 // ************************************************************************* //
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:394
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
fileName path() const
Return path.
Definition: Time.H:269
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:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
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:644
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Various functions to operate on Lists.
virtual bool exists(const fileName &, const bool checkGzip=true, const bool followLink=true) const =0
Does the name exist (as DIRECTORY or FILE) in the file system?
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:184
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:95
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:288
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:856
const fileOperation & fileHandler()
Get current file handler.
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:257
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:660
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:85
Foam::argList args(argc, argv)
bool found
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:157
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:114