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-2015 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  "exclude the '0/' dir from the times list, "
139  "has precedence over the -withZero option"
140  );
142  (
143  "latestTime",
144  "select the latest time"
145  );
147  (
148  "newTimes",
149  "select the new times"
150  );
152  (
153  "time",
154  "ranges",
155  "comma-separated time ranges - eg, ':10,20,40:70,1000:'"
156  );
157 }
158 
159 
161 (
162  const instantList& timeDirs,
163  const argList& args,
164  const word& constantName
165 )
166 {
167  if (timeDirs.size())
168  {
169  List<bool> selectTimes(timeDirs.size(), true);
170 
171  // Determine locations of constant/ and 0/ directories
172  label constantIdx = -1;
173  label zeroIdx = -1;
174 
175  forAll(timeDirs, timeI)
176  {
177  if (timeDirs[timeI].name() == constantName)
178  {
179  constantIdx = timeI;
180  }
181  else if (timeDirs[timeI].value() == 0)
182  {
183  zeroIdx = timeI;
184  }
185 
186  if (constantIdx >= 0 && zeroIdx >= 0)
187  {
188  break;
189  }
190  }
191 
192  // Determine latestTime selection (if any)
193  // This must appear before the -time option processing
194  label latestIdx = -1;
195  if (args.optionFound("latestTime"))
196  {
197  selectTimes = false;
198  latestIdx = timeDirs.size() - 1;
199 
200  // Avoid false match on constant/
201  if (latestIdx == constantIdx)
202  {
203  latestIdx = -1;
204  }
205  }
206 
207  if (args.optionFound("time"))
208  {
209  // Can match 0/, but can never match constant/
210  selectTimes = timeSelector
211  (
212  args.optionLookup("time")()
213  ).selected(timeDirs);
214  }
215 
216  // Add in latestTime (if selected)
217  if (latestIdx >= 0)
218  {
219  selectTimes[latestIdx] = true;
220  }
221 
222  if (constantIdx >= 0)
223  {
224  // Only add constant/ if specifically requested
225  selectTimes[constantIdx] = args.optionFound("constant");
226  }
227 
228  // Special treatment for 0/
229  if (zeroIdx >= 0)
230  {
231  if (args.optionFound("noZero"))
232  {
233  // Exclude 0/ if specifically requested
234  selectTimes[zeroIdx] = false;
235  }
236  else if (argList::validOptions.found("withZero"))
237  {
238  // With -withZero enabled, drop 0/ unless specifically requested
239  selectTimes[zeroIdx] = args.optionFound("withZero");
240  }
241  }
242 
243  return subset(selectTimes, timeDirs);
244  }
245  else
246  {
247  return timeDirs;
248  }
249 }
250 
251 
253 (
254  Time& runTime,
255  const argList& args
256 )
257 {
259  (
261  (
262  runTime.times(),
263  args,
264  runTime.constant()
265  )
266  );
267 
268  if (timeDirs.empty())
269  {
271  << "No time specified or available, selecting 'constant'"
272  << endl;
273 
274  timeDirs.append(instant(0, runTime.constant()));
275  }
276 
277  runTime.setTime(timeDirs[0], 0);
278 
279  return timeDirs;
280 }
281 
282 
284 (
285  Time& runTime,
286  const argList& args
287 )
288 {
289  if
290  (
291  args.optionFound("latestTime")
292  || args.optionFound("time")
293  || args.optionFound("constant")
294  || args.optionFound("noZero")
295  || args.optionFound("withZero")
296  )
297  {
298  return select0(runTime, args);
299  }
300  else
301  {
302  // No timeSelector option specified. Do not change runTime.
303  return instantList(1, instant(runTime.value(), runTime.timeName()));
304  }
305 }
306 
307 
309 (
310  Time& runTime,
311  const argList& args,
312  const word& fName
313 )
314 {
316 
317  if (timeDirs.size() && args.optionFound("newTimes"))
318  {
319  List<bool> selectTimes(timeDirs.size(), true);
320 
321  forAll(timeDirs, timeI)
322  {
323  selectTimes[timeI] =
324  !exists(runTime.path()/timeDirs[timeI].name()/fName);
325  }
326 
327  return subset(selectTimes, timeDirs);
328  }
329  else
330  {
331  return timeDirs;
332  }
333 }
334 
335 
336 // ************************************************************************* //
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
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
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
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:76
scalar value() const
Value (const access)
Definition: instant.H:112
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
timeSelector()
Construct Null.
Definition: timeSelector.C:34
const Type & value() const
Return const reference to value.
instantList select(const instantList &) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:100
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:715
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
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
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:97
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:93
const word & constant() const
Return constant name.
Definition: TimePaths.H:124
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0)
Definition: timeSelector.C:284
virtual void setTime(const Time &)
Reset the time and time-index to those of the given time.
Definition: Time.C:924
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options.
Definition: timeSelector.C:253
bool selected(const scalar) const
Return true if the given value is within the ranges.
Definition: scalarRanges.C:59
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:64
bool exists(const fileName &, const bool checkGzip=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: POSIX.C:480
#define WarningInFunction
Report a warning using Foam::Warning.
A List of scalarRange.
Definition: scalarRanges.H:49
void inplaceSelect(instantList &) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:107
IStringStream optionLookup(const word &opt) const
Return an IStringStream from the named option.
Definition: argListI.H:114
ListType subset(const UList< T > &select, const T &value, const ListType &)
Extract elements of List when select is a certain value.
instantList times() const
Search the case for valid time directories.
Definition: Time.C:731
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:83
fileName path() const
Return path.
Definition: Time.H:269
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