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-2022 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 the 'constant/' dir in the times list"
119  );
120  }
121  if (withZero)
122  {
124  (
125  "withZero",
126  "Include the '0/' dir in the times list"
127  );
128  }
130  (
131  "noZero",
132  string("Exclude the '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 (
155  const instantList& times,
156  const argList& args,
157  const word& constantName
158 )
159 {
160  if (times.size())
161  {
162  List<bool> selectTimes(times.size(), true);
163 
164  label constantIdx = -1;
165  label zeroIdx = -1;
166  label latestIdx = -1;
167 
168  // Determine locations of constant/ and 0/ directories
169  forAll(times, timei)
170  {
171  if (times[timei].name() == constantName)
172  {
173  constantIdx = timei;
174  }
175  else if (times[timei].value() == 0)
176  {
177  zeroIdx = timei;
178  }
179 
180  if (constantIdx >= 0 && zeroIdx >= 0)
181  {
182  break;
183  }
184  }
185 
186  // Determine latestTime selection (if any)
187  // This must appear before the -time option processing
188  if (args.found("latestTime"))
189  {
190  selectTimes = false;
191  latestIdx = times.size() - 1;
192 
193  // Avoid false match on constant/
194  if (latestIdx == constantIdx)
195  {
196  latestIdx = -1;
197  }
198  }
199 
200  if (args.found("time"))
201  {
202  // Can match 0/, but can never match constant/
203  selectTimes = timeSelector(args["time"]).selected(times);
204  }
205 
206  // Add in latestTime (if selected)
207  if (latestIdx >= 0)
208  {
209  selectTimes[latestIdx] = true;
210  }
211 
212  if (constantIdx >= 0)
213  {
214  // Only add constant/ if specifically requested
215  selectTimes[constantIdx] = args.found("constant");
216  }
217 
218  // Special treatment for 0/
219  if (zeroIdx >= 0)
220  {
221  if (args.found("noZero"))
222  {
223  // Exclude 0/ if specifically requested
224  selectTimes[zeroIdx] = false;
225  }
226  else if (argList::validOptions.found("withZero"))
227  {
228  // With -withZero enabled, drop 0/ unless specifically requested
229  selectTimes[zeroIdx] = args.found("withZero");
230  }
231  }
232 
233  return subset(selectTimes, times);
234  }
235 
236  return times;
237 }
238 
239 
241 (
242  Time& runTime,
243  const argList& args
244 )
245 {
246  instantList times
247  (
249  (
250  runTime.times(),
251  args,
252  runTime.constant()
253  )
254  );
255 
256  if (times.empty())
257  {
259  << "No time specified or available, selecting 'constant'"
260  << endl;
261 
262  times.push_back(instant(0, runTime.constant()));
263  }
264 
265  runTime.setTime(times.front(), 0);
266 
267  return times;
268 }
269 
270 
272 (
273  Time& runTime,
274  const argList& args
275 )
276 {
277  if
278  (
279  args.found("latestTime")
280  || args.found("time")
281  || args.found("constant")
282  || args.found("noZero")
283  || args.found("withZero")
284  )
285  {
286  return select0(runTime, args);
287  }
288 
289  // No timeSelector option specified. Do not change runTime.
290  return instantList(one{}, instant(runTime.value(), runTime.timeName()));
291 }
292 
293 
294 // ************************************************************************* //
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
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:128
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 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 select0) otherwise return just the current ti...
Definition: timeSelector.C:265
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:935
static label findClosestTimeIndex(const instantList &timeDirs, const scalar t, const word &constantDirName="constant")
Search instantList for the time index closest to the specified time.
Definition: TimePaths.C:125
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:234
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
#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