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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2024 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "timeSelector.H"
30 #include "ListOps.H"
31 #include "argList.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35 
36 Foam::timeSelector::timeSelector(const std::string& str)
37 :
38  ranges_(str)
39 {}
40 
41 
42 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43 
44 bool Foam::timeSelector::contains(const scalar value) const
45 {
46  return ranges_.contains(value);
47 }
48 
49 
50 bool Foam::timeSelector::contains(const instant& t) const
51 {
52  return ranges_.contains(t.value());
53 }
54 
55 
57 {
58  List<bool> selectTimes(times.size());
59 
60  // Check ranges, avoid false positive on constant/
61  forAll(times, timei)
62  {
63  selectTimes[timei] =
64  (
65  times[timei].name() != "constant" && contains(times[timei])
66  );
67  }
68 
69  // Check specific values
70  for (const scalarRange& range : ranges_)
71  {
72  if (range.single())
73  {
74  const scalar target = range.value();
75 
76  const label nearestIndex =
77  TimePaths::findClosestTimeIndex(times, target);
78 
79  // Note could also test if the index is too far away.
80  // Eg, for times (0 10 20 30 40) selecting 100 will currently
81  // return the closest time (40), but perhaps we should limit that
82  // to the last deltaT?
83 
84  if (nearestIndex >= 0)
85  {
86  selectTimes[nearestIndex] = true;
87  }
88  }
89  }
90 
91  return selectTimes;
92 }
93 
94 
96 {
97  return subset(selected(times), times);
98 }
99 
100 
102 {
103  inplaceSubset(selected(times), times);
104 }
105 
106 
108 (
109  const bool constant,
110  const bool withZero
111 )
112 {
113  if (constant)
114  {
116  (
117  "constant",
118  "Include 'constant/' dir in the times list"
119  );
120  }
121  if (withZero)
122  {
124  (
125  "withZero",
126  "Include '0/' dir in the times list"
127  );
128  }
130  (
131  "noZero",
132  string("Exclude '0/' dir from the times list")
133  + (
134  withZero
135  ? ", has precedence over the -withZero option"
136  : ""
137  )
138  );
140  (
141  "latestTime",
142  "Select the latest time"
143  );
145  (
146  "time",
147  "ranges",
148  "List of ranges. Eg, ':10,20 40:70 1000:', 'none', etc"
149  );
150 }
151 
152 
154 {
156  (
157  "constant",
158  "Include 'constant/' dir in the times"
159  );
161  (
162  "noZero",
163  "Exclude '0/' dir from the times (currently ignored)"
164  );
166  (
167  "latestTime",
168  "Select the latest time"
169  );
171  (
172  "time",
173  "value",
174  "Select the nearest time to the specified value"
175  );
176 }
177 
178 
180 (
181  const instantList& times,
182  const argList& args,
183  const word& constantName
184 )
185 {
186  if (times.size())
187  {
188  List<bool> selectTimes(times.size(), true);
189 
190  label constantIdx = -1;
191  label zeroIdx = -1;
192  label latestIdx = -1;
193 
194  // Determine locations of constant/ and 0/ directories
195  forAll(times, timei)
196  {
197  if (times[timei].name() == constantName)
198  {
199  constantIdx = timei;
200  }
201  else if (times[timei].value() == 0)
202  {
203  zeroIdx = timei;
204  }
205 
206  if (constantIdx >= 0 && zeroIdx >= 0)
207  {
208  break;
209  }
210  }
211 
212  // Determine latestTime selection (if any)
213  // This must appear before the -time option processing
214  if (args.found("latestTime"))
215  {
216  selectTimes = false;
217  latestIdx = times.size() - 1;
218 
219  // Avoid false match on constant/
220  if (latestIdx == constantIdx)
221  {
222  latestIdx = -1;
223  }
224  }
225 
226  if (args.found("time"))
227  {
228  // Can match 0/, but can never match constant/
229  selectTimes = timeSelector(args["time"]).selected(times);
230  }
231 
232  // Add in latestTime (if selected)
233  if (latestIdx >= 0)
234  {
235  selectTimes[latestIdx] = true;
236  }
237 
238  if (constantIdx >= 0)
239  {
240  // Only add constant/ if specifically requested
241  selectTimes[constantIdx] = args.found("constant");
242  }
243 
244  // Special treatment for 0/
245  if (zeroIdx >= 0)
246  {
247  if (args.found("noZero"))
248  {
249  // Exclude 0/ if specifically requested
250  selectTimes[zeroIdx] = false;
251  }
252  else if (argList::validOptions.found("withZero"))
253  {
254  // With -withZero enabled, drop 0/ unless specifically requested
255  selectTimes[zeroIdx] = args.found("withZero");
256  }
257  }
258 
259  return subset(selectTimes, times);
260  }
261 
262  return times;
263 }
264 
265 
267 (
268  Time& runTime,
269  const argList& args
270 )
271 {
272  instantList times
273  (
275  (
276  runTime.times(),
277  args,
278  runTime.constant()
279  )
280  );
281 
282  if (times.empty())
283  {
285  << "No time specified or available, selecting 'constant'"
286  << endl;
287 
288  times.push_back(instant(0, runTime.constant()));
289  }
290 
291  runTime.setTime(times.front(), 0);
292 
293  return times;
294 }
295 
296 
298 (
299  Time& runTime,
300  const argList& args
301 )
302 {
303  if
304  (
305  args.found("latestTime")
306  || args.found("time")
307  || args.found("constant")
308  || args.found("noZero")
309  || args.found("withZero")
310  )
311  {
312  return select0(runTime, args);
313  }
315  // No timeSelector option specified. Do not change runTime.
317 }
318 
319 
321 (
322  Time& runTime,
323  const argList& args,
324  const bool forceInitial
325 )
326 {
327  label timei = -1;
328  instantList times;
329 
330  if
331  (
332  forceInitial
333  || args.found("constant")
334  || args.found("latestTime")
335  || args.found("time")
336  // Currently ignoring -noZero, -withZero
337  )
338  {
339  // Get times list
340  times = runTime.times();
341  }
342 
343  if (times.size())
344  {
345  // Start from first time (eg, for -constant or forced)
346  timei = 0;
347 
348  // Determine latestTime selection (if any)
349  // This must appear before the -time option processing
350  if (args.found("latestTime"))
351  {
352  timei = times.size() - 1;
353  }
354  else if (args.found("time"))
355  {
356  const scalar target = args.get<scalar>("time");
357 
358  timei = TimePaths::findClosestTimeIndex(times, target);
359  }
360 
361 
362  // Avoid "constant" unless specifically requested with -constant,
363  // and the -constant option is actually an expected option
364 
365  if
366  (
367  (timei >= 0 && timei < times.size()-1)
368  && times[timei].name() == "constant"
369  && (argList::validOptions.found("constant") && !args.found("constant"))
370  )
371  {
372  ++timei;
373  }
374  }
375 
376 
377  if (timei >= 0 && timei < times.size())
378  {
379  // Specified a timeSelector option, or forceInitial.
380  // Set the runTime accordingly.
381 
382  runTime.setTime(times[timei], timei);
383  return true;
384  }
385  else
386  {
387  // No timeSelector option specified. Do not change runTime.
388  return false;
389  }
390 }
391 
392 
393 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void inplaceSelect(instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:94
timeSelector() noexcept=default
Default construct.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static bool setTimeIfPresent(Time &runTime, const argList &args, const bool forceInitial=false)
Set the runTime based on -constant (if present), -time (value), or -latestTime.
Definition: timeSelector.C:314
static label findClosestTimeIndex(const UList< instant > &timeDirs, const scalar t, const word &constantDirName="constant")
Search instantList for the time index closest to the specified time.
Definition: TimePaths.C:176
void inplaceSubset(const BoolListType &select, ListType &input, const bool invert=false)
Inplace extract elements of the input list when select is true.
bool selected(const instant &t) const
True if value of the instant is within any of the ranges.
Definition: timeSelector.H:146
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:88
scalar range
Various functions to operate on Lists.
scalar value() const noexcept
The value (const access)
Definition: Instant.H:134
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static void addOptions_singleTime()
Add single-time timeSelector options to argList::validOptions()
Definition: timeSelector.C:146
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:385
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times - as per select0() - otherwise return just the cu...
Definition: timeSelector.C:291
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:902
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
const word & constant() const noexcept
Return constant name.
Definition: TimePathsI.H:112
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options and also set the runTime to the first i...
Definition: timeSelector.C:260
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:118
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:271
#define WarningInFunction
Report a warning using Foam::Warning.
bool contains(const scalar value) const
True if the value is contained by any of the sub-ranges.
Definition: scalarRanges.H:95
List< instant > instantList
List of instants.
Definition: instantList.H:41
Foam::argList args(argc, argv)
bool contains(const scalar value) const
True if value is within any of the ranges.
Definition: timeSelector.C:37
bool found
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:255
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:101
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:56