Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
30#include "ListOps.H"
31#include "argList.H"
32#include "Time.H"
33
34// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35
36Foam::timeSelector::timeSelector(const std::string& str)
38 ranges_(str)
39{}
40
41
42// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44bool Foam::timeSelector::contains(const scalar value) const
45{
46 return ranges_.contains(value);
47}
48
50bool 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 =
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
96{
97 return subset(selected(times), times);
98}
99
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 }
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);
314
315 // No timeSelector option specified. Do not change runTime.
316 return instantList(one{}, instant(runTime.value(), runTime.timeName()));
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// ************************************************************************* //
scalar range
Various functions to operate on Lists.
bool found
scalar value() const noexcept
The value (const access).
Definition Instant.H:139
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void push_back(const T &val)
Append an element at the end of the list.
Definition ListI.H:221
instantList times() const
Search the case for valid time directories.
Definition TimePaths.C:230
static label findClosestTimeIndex(const UList< instant > &timeDirs, const scalar t, const word &constantDirName="constant")
Search instant list for the time index closest to the specified time.
Definition TimePaths.C:119
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition Time.C:905
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
T & front()
Access first element of the list, position [0].
Definition UListI.H:239
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
Extract command arguments and options from the supplied argc and argv parameters.
Definition argList.H:119
T get(const label index) const
Get a value from the argument at index.
Definition argListI.H:271
bool found(const word &optName) const
Return true if the named option is found.
Definition argListI.H:171
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition argList.C:389
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:400
static HashTable< string > validOptions
A list of valid options.
Definition argList.H:255
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition instant.H:56
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition one.H:57
Scalar bounds to be used as a unary predicate.
Definition scalarRange.H:65
The timeSelector provides a convenient means of selecting multiple times.
static void addOptions_singleTime()
Add single-time timeSelector options to argList::validOptions().
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
static bool setTimeIfPresent(Time &runTime, const argList &args, const bool forceInitial=false)
Set the runTime based on -constant (if present), -time (value), or -latestTime.
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...
bool selected(const instant &t) const
True if value of the instant is within any of the ranges.
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...
timeSelector() noexcept=default
Default construct.
bool contains(const scalar value) const
True if value is within any of the ranges.
void inplaceSelect(instantList &times) const
Select a list of Time values that are within the ranges.
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
A class for handling words, derived from Foam::string.
Definition word.H:66
engineTime & runTime
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
Different types of constants.
void inplaceSubset(const BoolListType &select, ListType &input, const bool invert=false)
Inplace extract elements of the input list when select is true.
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
List< instant > instantList
List of instants.
Definition instantList.H:41
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299