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-2020 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
29#include "timeSelector.H"
30#include "ListOps.H"
31#include "argList.H"
32#include "Time.H"
33
34// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
35
36Foam::timeSelector::timeSelector(const std::string& str)
37:
38 scalarRanges(str)
39{}
40
41
42// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
43
44bool Foam::timeSelector::selected(const instant& value) const
45{
46 return scalarRanges::match(value.value());
47}
48
49
51{
52 List<bool> selectTimes(times.size(), false);
53
54 // Check ranges, avoid false positive on constant/
55 forAll(times, timei)
56 {
57 if (times[timei].name() != "constant" && selected(times[timei]))
58 {
59 selectTimes[timei] = true;
60 }
61 }
62
63 // Check specific values
64 for (const scalarRange& range : *this)
65 {
66 if (range.single())
67 {
68 const scalar target = range.value();
69
70 const label nearestIndex =
72
73 // Note could also test if the index is too far away.
74 // Eg, for times (0 10 20 30 40) selecting 100 will currently
75 // return the closest time (40), but perhaps we should limit that
76 // to the last deltaT?
77
78 if (nearestIndex >= 0)
79 {
80 selectTimes[nearestIndex] = true;
81 }
82 }
83 }
84
85 return selectTimes;
86}
87
88
90{
91 return subset(selected(times), times);
92}
93
94
96{
97 inplaceSubset(selected(times), times);
98}
99
100
102(
103 const bool constant,
104 const bool withZero
105)
106{
107 if (constant)
108 {
110 (
111 "constant",
112 "Include the 'constant/' dir in the times list"
113 );
114 }
115 if (withZero)
116 {
118 (
119 "withZero",
120 "Include the '0/' dir in the times list"
121 );
122 }
124 (
125 "noZero",
126 string("Exclude the '0/' dir from the times list")
127 + (
128 withZero
129 ? ", has precedence over the -withZero option"
130 : ""
131 )
132 );
134 (
135 "latestTime",
136 "Select the latest time"
137 );
139 (
140 "time",
141 "ranges",
142 "List of ranges. Eg, ':10,20 40:70 1000:', 'none', etc"
143 );
144}
145
146
148(
149 const instantList& times,
150 const argList& args,
151 const word& constantName
152)
153{
154 if (times.size())
155 {
156 List<bool> selectTimes(times.size(), true);
157
158 label constantIdx = -1;
159 label zeroIdx = -1;
160 label latestIdx = -1;
161
162 // Determine locations of constant/ and 0/ directories
163 forAll(times, timei)
164 {
165 if (times[timei].name() == constantName)
166 {
167 constantIdx = timei;
168 }
169 else if (times[timei].value() == 0)
170 {
171 zeroIdx = timei;
172 }
173
174 if (constantIdx >= 0 && zeroIdx >= 0)
175 {
176 break;
177 }
178 }
179
180 // Determine latestTime selection (if any)
181 // This must appear before the -time option processing
182 if (args.found("latestTime"))
183 {
184 selectTimes = false;
185 latestIdx = times.size() - 1;
186
187 // Avoid false match on constant/
188 if (latestIdx == constantIdx)
189 {
190 latestIdx = -1;
191 }
192 }
193
194 if (args.found("time"))
195 {
196 // Can match 0/, but can never match constant/
197 selectTimes = timeSelector(args["time"]).selected(times);
198 }
199
200 // Add in latestTime (if selected)
201 if (latestIdx >= 0)
202 {
203 selectTimes[latestIdx] = true;
204 }
205
206 if (constantIdx >= 0)
207 {
208 // Only add constant/ if specifically requested
209 selectTimes[constantIdx] = args.found("constant");
210 }
211
212 // Special treatment for 0/
213 if (zeroIdx >= 0)
214 {
215 if (args.found("noZero"))
216 {
217 // Exclude 0/ if specifically requested
218 selectTimes[zeroIdx] = false;
219 }
220 else if (argList::validOptions.found("withZero"))
221 {
222 // With -withZero enabled, drop 0/ unless specifically requested
223 selectTimes[zeroIdx] = args.found("withZero");
224 }
225 }
226
227 return subset(selectTimes, times);
228 }
229
230 return times;
231}
232
233
235(
236 Time& runTime,
237 const argList& args
238)
239{
240 instantList times
241 (
243 (
244 runTime.times(),
245 args,
247 )
248 );
249
250 if (times.empty())
251 {
253 << "No time specified or available, selecting 'constant'"
254 << endl;
255
256 times.append(instant(0, runTime.constant()));
257 }
258
259 runTime.setTime(times.first(), 0);
260
261 return times;
262}
263
264
266(
267 Time& runTime,
268 const argList& args
269)
270{
271 if
272 (
273 args.found("latestTime")
274 || args.found("time")
275 || args.found("constant")
276 || args.found("noZero")
277 || args.found("withZero")
278 )
279 {
280 return select0(runTime, args);
281 }
282
283 // No timeSelector option specified. Do not change runTime.
285}
286
287
288// ************************************************************************* //
scalar range
Various functions to operate on Lists.
bool found
scalar value() const noexcept
The value (const access)
Definition: Instant.H:118
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:175
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:149
static label findClosestTimeIndex(const instantList &timeDirs, const scalar t, const word &constantName="constant")
Search instantList for the time index closest to the specified time.
Definition: TimePaths.C:156
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:997
T & first()
Return the first element of the list.
Definition: UListI.H:202
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:324
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:335
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:212
const Type & value() const
Return const reference to value.
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:62
constant condensation/saturation model.
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:69
A collection of scalar bounds to be used as a unary predicate.
Definition: scalarRanges.H:58
bool match(const scalar &value) const
Match any condition in the list.
Definition: scalarRangesI.H:38
A List of scalarRange for selecting times.
Definition: timeSelector.H:95
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:102
bool selected(const instant &value) const
Return true if the given instant is within the ranges.
Definition: timeSelector.C:44
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:235
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:266
timeSelector()=default
Default construct.
void inplaceSelect(instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:95
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:89
const wordList & selected() const noexcept
The list of selected files.
A class for handling words, derived from Foam::string.
Definition: word.H:68
engineTime & runTime
#define WarningInFunction
Report a warning using Foam::Warning.
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.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
List< instant > instantList
List of instants.
Definition: instantList.H:47
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::argList args(argc, argv)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333