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 -------------------------------------------------------------------------------
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 
37 :
38  scalarRanges()
39 {}
40 
41 
42 Foam::timeSelector::timeSelector(const std::string& str)
43 :
44  scalarRanges(str)
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
50 bool Foam::timeSelector::selected(const instant& value) const
51 {
52  return scalarRanges::match(value.value());
53 }
54 
55 
57 {
58  List<bool> selectTimes(times.size(), false);
59 
60  // Check ranges, avoid false positive on constant/
61  forAll(times, timei)
62  {
63  if (times[timei].name() != "constant" && selected(times[timei]))
64  {
65  selectTimes[timei] = true;
66  }
67  }
68 
69  // Check specific values
70  for (const scalarRange& range : *this)
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.append(instant(0, runTime.constant()));
263  }
264 
265  runTime.setTime(times.first(), 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.
291 }
292 
293 
294 // ************************************************************************* //
Foam::TimePaths::findClosestTimeIndex
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
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::scalarRanges
A collection of scalar bounds to be used as a unary predicate.
Definition: scalarRanges.H:55
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::subset
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
Foam::timeSelector::select
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:95
Foam::one
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
Definition: one.H:61
Foam::timeSelector::selected
bool selected(const instant &value) const
Return true if the given instant is within the ranges.
Definition: timeSelector.C:50
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:123
Foam::List::append
void append(const T &val)
Append an element at the end of the list.
Definition: ListI.H:182
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::timeSelector::timeSelector
timeSelector()
Construct null.
Definition: timeSelector.C:36
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:434
Foam::instantList
List< instant > instantList
List of instants.
Definition: instantList.H:44
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::timeSelector::inplaceSelect
void inplaceSelect(instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:101
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::scalarRange
Scalar bounds to be used as a unary predicate.
Definition: scalarRange.H:68
argList.H
Foam::TimePaths::times
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:149
Foam::timeSelector::selectIfPresent
static instantList selectIfPresent(Time &runTime, const argList &args)
Definition: timeSelector.C:272
Foam::argList::validOptions
static HashTable< string > validOptions
A list of valid options.
Definition: argList.H:210
Foam::inplaceSubset
void inplaceSubset(const BoolListType &select, ListType &input, const bool invert=false)
Inplace extract elements of the input list when select is true.
Definition: ListOpsTemplates.C:590
found
bool found
Definition: TABSMDCalcMethod2.H:32
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:338
Time.H
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
Foam::List< bool >
Foam::scalarRanges::match
bool match(const scalar &value) const
Match any condition in the list.
Definition: scalarRangesI.H:38
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1003
Foam::Instant::value
scalar value() const
The value (const access)
Definition: Instant.H:99
timeSelector.H
ListOps.H
Various functions to operate on Lists.
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
Foam::timeSelector::select0
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:241
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:88
Foam::argList::addOption
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:349
args
Foam::argList args(argc, argv)
constant
constant condensation/saturation model.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::timeSelector
A List of scalarRange for selecting times.
Definition: timeSelector.H:92
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:151